diff --git a/DESCRIPTION b/DESCRIPTION index ef0bcd1..ef000f5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: glmtools Type: Package Title: glmtools -Version: 0.6.2 +Version: 0.7.0 Date: 2013-09-18 Author: Jordan S Read, Luke A Winslow Maintainer: Jordan S Read diff --git a/NAMESPACE b/NAMESPACE index 7d90865..324e4bf 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -26,6 +26,8 @@ export(resample_to_field) export(run_example_sim) export(set_nml) export(sim_metrics) +export(sim_var_longname) +export(sim_var_units) export(sim_vars) export(summarize_sim) export(validate_sim) diff --git a/R/nml_helpers.R b/R/nml_helpers.R index a95930b..9f7197f 100644 --- a/R/nml_helpers.R +++ b/R/nml_helpers.R @@ -1,12 +1,14 @@ # private function -buildVal <- function(textLine){ +buildVal <- function(textLine, lineNum, blckName){ #-----function appends nml list with new values----- # remove all text after comment string textLine <- strsplit(textLine,'!')[[1]][1] - if (!any(grep("=",textLine))){stop(c("no hanging lines allowed in .nml, used ",textLine))} + if (!any(grep("=",textLine))){ + stop(c("no hanging lines allowed in .nml, used ",textLine,'.\nSee line number:',lineNum,' in "&',blckName,'" section.')) + } params <- strsplit(textLine,"=") # break text at "=" parNm <- params[[1]][1] parVl <- params[[1]][2] @@ -101,3 +103,14 @@ ascii_only <- function(file){ } } + +.validate_nml <- function(nml){ + # test for required blocks: + required_blks <- c('outflow', 'inflow', 'meteorology', 'init_profiles', 'output', 'time', 'morphometry', 'glm_setup') + blk_match <- required_blks %in% names(nml) + if (!all(blk_match)){ + stop('parsing error in nml file.',required_blks[blk_match],'missing.') + } + return(TRUE) + +} diff --git a/R/plot_var.R b/R/plot_var.R index 147e291..548a110 100644 --- a/R/plot_var.R +++ b/R/plot_var.R @@ -6,9 +6,9 @@ #'@param num_cells number of vertical cells to use for heatmap #'@param fig_path F if plot to screen, string path if save plot as .png #'@param add F if create new figure, T if add to existing -#'@param bar_title NULL if use var_name, or specify as a string to name plot variable +#'@param bar_title NULL if use \code{\link{sim_var_longname}}, or specify as a string to name plot variable #'@keywords methods -#'@seealso \link{get_temp} +#'@seealso \code{\link{get_temp}}, \code{\link{sim_var_longname}} #'@author #'Jordan S. Read, Luke A. Winslow #'@examples @@ -29,13 +29,35 @@ plot_var <- function(file, var_name, col_lim, reference = 'surface', num_cells = min_depth <- 0 z_out <- seq(min_depth, max_depth,length.out = num_cells) variable_df <- get_var(file, reference = reference, z_out, var_name=var_name) + + if (is.null(bar_title)){ + bar_title <- sim_var_longname(file, var_name) + } + + if (is.character(fig_path)){ + gen_default_fig(file_name = fig_path) + } + if (ncol(variable_df) == 2){ - stop('plot_var() not yet supported for 1D output variables') + .plot_timeseries(variable_df, add, bar_title) + } else { + .plot_heatmap(variable_df, col_lim, reference, add, bar_title, z_out) } + + if (is.character(fig_path)){ + dev.off() + } else { + #layout(as.matrix(1)) + } + +} + +.plot_heatmap <- function(variable_df, col_lim, reference, add, bar_title, z_out){ palette <- colorRampPalette(c("violet","blue","cyan", "green3", "yellow", "orange", "red"), bias = 1, space = "rgb") + + if (missing(col_lim)) col_lim <- range(variable_df[, -1], na.rm = TRUE) levels <- seq(col_lim[1], col_lim[2], by = diff(col_lim)/15) - #col_subs <- unique(floor(seq(col_lim[1], col_lim[2]-1, length.out = 15))) col_subs <- levels colors <- palette(n = length(levels)-1) dates <- variable_df[, 1] @@ -43,10 +65,7 @@ plot_var <- function(file, var_name, col_lim, reference = 'surface', num_cells = xaxis <- get_xaxis(dates) yaxis <- get_yaxis_2D(z_out, reference) - if (is.character(fig_path)){ - gen_default_fig(file_name = fig_path) - } - + plot_layout(xaxis, yaxis, add) .filled.contour(x = dates, y = z_out, z =matrix_var, @@ -55,14 +74,11 @@ plot_var <- function(file, var_name, col_lim, reference = 'surface', num_cells = axis_layout(xaxis, yaxis) #doing this after heatmap so the axis are on top - if (is.null(bar_title)){ - bar_title = var_name - } color_key(levels, colors, subs=col_subs, col_label = bar_title) - if (is.character(fig_path)){ - dev.off() - } else { - #layout(as.matrix(1)) - } + } +.plot_timeseries <- function(variable_df, add, bar_title){ + if (add) par(new=TRUE) + plot(variable_df, ylab = bar_title) +} diff --git a/R/read_nml.R b/R/read_nml.R index d712e79..0c19612 100644 --- a/R/read_nml.R +++ b/R/read_nml.R @@ -56,7 +56,7 @@ read_nml <- function(nml_file = 'template'){ #browser() if(substr(textLine,1,1)!='!'){ # Add a check here, sometimes, if there is a hanging comma, - #and only sumtimes that means add next row + #and only sometimes that means add next row if(substr(textLine,nchar(textLine), nchar(textLine)) == ',' && j+1 <= length(fileLines) && !any(grep("=",fileLines[j+1])) && !any(grep("/",fileLines[j+1]))){ @@ -66,14 +66,17 @@ read_nml <- function(nml_file = 'template'){ carryover = '' } # else, line is commented out - lineVal <- buildVal(textLine) + lineVal <- buildVal(textLine, lineNum=j, blckName) nml[[i]] <- c(nml[[i]],lineVal) } } } - return(.nml(nml)) + nml <- .nml(nml) + .validate_nml(nml) + return(nml) } + nml_path_norm <- function(nml_file){ if (nml_file == "template"){ nml_file <- nml_template_path() diff --git a/R/sim_var_longname.R b/R/sim_var_longname.R new file mode 100644 index 0000000..92b44a0 --- /dev/null +++ b/R/sim_var_longname.R @@ -0,0 +1,25 @@ +#'@title get long name of variable from a GLM simulation +#'@param file a string with the path to the netcdf output from GLM +#'@param var_name name of a valid variable from a simulation (see \code{\link{sim_vars}}) +#'@keywords methods +#'@seealso \code{\link{sim_vars}}, \code{\link{sim_var_units}} +#'@author +#'Jordan S. Read +#'@examples +#'sim_folder <- run_example_sim(verbose = FALSE) +#'nc_file <- file.path(sim_folder, 'output.nc') +#'vars <- sim_vars(file = nc_file) +#'sim_var_units(nc_file, vars[1]) +#'sim_var_units(nc_file, 'u_mean') +#'@export +sim_var_longname <- function(file, var_name){ + + glm_nc <- get_glm_nc(file) + longname <- glm_nc$var[[var_name]]$longname + close_glm_nc(glm_nc) + if (is.null(longname)){stop(var_name,' not found. Check variable name')} + return(longname) +} + + + diff --git a/R/sim_var_units.R b/R/sim_var_units.R new file mode 100644 index 0000000..f3c811a --- /dev/null +++ b/R/sim_var_units.R @@ -0,0 +1,24 @@ +#'@title get units of variable from a GLM simulation +#'@param file a string with the path to the netcdf output from GLM +#'@param var_name name of a valid variable from a simulation (see \code{\link{sim_vars}}) +#'@keywords methods +#'@seealso \code{\link{sim_vars}}, \code{\link{sim_var_longname}} +#'@author +#'Jordan S. Read +#'@examples +#'sim_folder <- run_example_sim(verbose = FALSE) +#'nc_file <- file.path(sim_folder, 'output.nc') +#'vars <- sim_vars(file = nc_file) +#'sim_var_longname(nc_file, vars[1]) +#'sim_var_longname(nc_file, 'u_mean') +#'@export +sim_var_units <- function(file, var_name){ + + glm_nc <- get_glm_nc(file) + units <- glm_nc$var[[var_name]]$units + close_glm_nc(glm_nc) + if (is.null(units)){stop(var_name,' not found. Check variable name')} + return(units) +} + + diff --git a/inst/extdata/bad_glm2.nml b/inst/extdata/bad_glm2.nml new file mode 100755 index 0000000..6673984 --- /dev/null +++ b/inst/extdata/bad_glm2.nml @@ -0,0 +1,352 @@ +!------------------------------------------------------------------------------- +! general model setup +!------------------------------------------------------------------------------- +! +! sim_name [string] title of simulation +! max_layers [integer] maximum number of layers +! min_layer_vol [real] minimum layer volume (m3 * 1000) +! min_layer_thick [real] minimum layer thickness (m) +! max_layer_thick [real] maximum layer thickness (m) +! Kw [real] background light attenuation (m**-1) +! coef_mix_conv [real] mixing efficiency - convective overturn +! coef_wind_stir [real] mixing efficiency - wind stirring +! coef_mix_turb [real] mixing efficiency - unsteady turbulence effects +! coef_mix_shear [real] mixing efficiency - shear production +! coef_mix_KH [real] mixing efficiency - hypolimnetic Kelvin-Helmholtz turbulent billows +! coef_mix_hyp [real] mixing efficiency - hypolimnetic turbulence +! deep_mixing [bool] flag to disable deep-mixing +! +!------------------------------------------------------------------------------- +&glm_setup + sim_name = 'GLM Simulation' + max_layers = 200 + min_layer_vol = 0.025 + min_layer_thick = 0.250 + !min_layer_thick = 0.350 + max_layer_thick = 0.500 + !max_layer_thick = 0.900 + !-- Light Parameters + Kw = 0.5 + !-- Mixing Parameters + coef_mix_conv = 0.125 + coef_wind_stir = 0.23 + coef_mix_shear = 0.20 + coef_mix_turb = 0.51 + coef_mix_KH = 0.30 + coef_mix_hyp = 0.5 +! non_avg = .true. +! deep_mixing = .true. +/ + +!------------------------------------------------------------------------------- +! wq setup +! if this block is read, water quality functionality will be enabled +!------------------------------------------------------------------------------- +! wq_lib [string] +! Select which WQ library to use; +! valid options are 'aed2' or 'fabm' [default is 'aed2'] +! ode_method [integer] +! ODE numerical scheme for source and sink dynamics +! 1: first-order explicit (not positive) +! 2: second-order explicit Runge-Kutta (not positive) +! 3: fourth-order explicit Runge-Kutta (not positive) +! 4: Patankar (first-order, not conservative) +! 5: Patankar-RK (second-order, not conservative) +! 6: Patankar-RK (does not work, not conservative) +! 7: Modified Patankar (1st-order, conservat., posit.) +! 8: Modified Patankar-RK (2nd-order, conservat., posit.) +! 9: Modified Patankar-RK (does not work, conservat., +! posit.) +! 10: Extended Modified Patankar (1st-order, conservat., +! posit.) +! 11: Extended Modified Patankar-RK (2nd-order, conservat., +! posit.) +! This variable is used only if bio_calc = True +! split_factor [integer, minimum = 1] +! number of biogeochemical time steps per physical time step +! bioshade_feedback [bool] +! feedback of bio-turbidity to temperature equation +! repair_state [bool] +! FABM option to repeair state variables that have -ve's +! wq_nml_file [string] +! name of .nml file to be passed to WQ library; +! the default is {wq_lib}.nml (eg aed2.nml) +! mobility_off [bool] +! flag to turn off settling/rising +! multi_ben [bool] +! GLM specific option for FABM to do benthic fluxes only +! in bottom layer, or on flanks of all layers (.true.) +!------------------------------------------------------------------------------- +&wq_setup + wq_lib = 'aed2' + wq_nml_file = 'aed2.nml' + ode_method = 1 + split_factor = 1 + bioshade_feedback = .true. + !bioshade_feedback = .false. + repair_state = .true. +! mobility_off = .false. + multi_ben = .false. +/ + +!------------------------------------------------------------------------------- +! lake details +!------------------------------------------------------------------------------- +! +! name [string] +! name of the lake +! latitude [float, minimum = -90, maximum = 90, unit = deg North] +! latitude +! longitude [float, minimum = -360, maximum = 360, unit = deg East] +! longitude +! base_elev [float] +! base elevation (m) +! crest_elev [float] +! crest elevation (m) +! bsn_len [float] +! basin length at crest (m) +! bsn_wid [float] +! basin width at crest (m) +! bsn_vals [integer] +! number of depth points on height-area relationship +! H [float] +! elevations (m) (comma separated list, len=bsn_vals) +! A [float] +! area (m2) (comma separated list, len=bsn_vals) +! +!------------------------------------------------------------------------------- +&morphometry + lake_name = 'Mendota' + latitude = 43 + longitude = -89 + bsn_len = 7000 + bsn_wid = 6500 + bsn_vals = 15 + ! H(m) A(m2 * 1000) V(m3 * 1000) + H = 375.00640,376.79166,378.57691,380.36217,382.14743,383.93269, 385.71794,387.50320,389.28846,391.07371,392.85897,394.64423, 396.42949,398.21474,400.00000 + A = 0.00000,2827226.39,5654452.79,8481679.18,11308905.58,14136131.97, 16963358.37,19790584.76,22617811.16,25445037.55,28272263.95,31099490.34, 33926716.74,36753943.13,39581169.52 +/ + +!------------------------------------------------------------------------------- +! duration of run +!------------------------------------------------------------------------------- +! +! timefmt [integer] +! method to specify start and duration of model run +! 1: duration computed from number of time steps, MaxN (bogus start +! date used) [no longer implemented!!] +! 2: duration computed from given start and stop dates (number of time +! steps MaxN computed) +! 3: duration computed from number of time steps, MaxN (start date as +! specified, stop date computed) +! start [string, format = "yyyy-mm-dd hh:mm:ss"] +! nominal start date +! This variable is used only if timefmt != 1 +! stop [string, format = "yyyy-mm-dd hh:mm:ss"] +! nominal stop date +! This variable is used only if timefmt = 2 +! dt [float, minimum = 0.001, maximum = 86400, unit = s] +! Time step for integration +! numb_days [number of days to run the simulation ] +! This variable is used only if timefmt != 2 +! +!------------------------------------------------------------------------------- +&time + timefmt = 3 + start = '2009-04-15 00:00:00' + !stop = '2009-12-29 23:00:00' + dt = 3600.0 + num_days=250 + timezone = -5.0 +/ + +!------------------------------------------------------------------------------- +! format for output and filename(s) +!------------------------------------------------------------------------------- +! +! out_dir [string] +! path to output directory (set permissions) +! out_fn [string] +! name of output netcdf file +! nsave [integer, minimum = 1, maximum = 86400] +! save results every 'nsave' timesteps +! csv_lake_fname [string] +! name of lake.csv lake simulation daily summary information +! csv_point_nlevs [integer] +! number of depths at which to dump a csv time-series file +! csv_point_at [real] +! height from bottom for point csv file(s) (comma separated list) +! csv_point_fname [string] +! name of csv output file(s) (comma separated list) +! csv_point_nvars [integer] +! number of variables to output into csv +! csv_point_vars [string] +! list of names of variables to output, - order IS important +! csv_outlet_allinone [bool] +! put all outflow data into the same csv file +! csv_outlet_fname [string] +! name of csv output file(s) (comma separated list) +! csv_outlet_nvars [integer] +! number of variables to output into outlet csv +! csv_outlet_vars [string] +! list of names of variables to output +! csv_ovrflw_fname [string] +! name of csv file to record amount and quality of overflow +! +!-------------------------------------------------------------------------------! simulation number 1 +&output + out_dir = '.' + out_fn = 'output' + nsave = 8 + csv_point_nlevs = 2 + csv_point_fname = 'Results/WQ_' + csv_point_at = 20, 22 + csv_point_nvars = 6 + csv_lake_fname = 'Results/Physics' + csv_point_vars = 'temp','OXY_oxy','PHY_CYANOPCH1','PHY_CYANONPCH2','PHY_CHLOROPCH3','PHY_DIATOMPCH4' +/ +!------------------------------------------------------------------------------- +! initial condition profiles +!------------------------------------------------------------------------------- +! +! lake_depth [float] initial lake depth (m) +! num_depths [integer] number of depths provided for initial profiles +! the_depths [float] the depths of the initial profile points (m) +! the_temps [float] the temperature of the initial profile points (C) +! the_sals [float] the salinity of the initial profile points (psu) +! num_wq_vars [integer] number of non GLM (ie FABM) vars to be initialised +! wq_names [string] names of non GLM (ie FABM) vars to be initialised +! wq_init_vals [float] array of FABM vars (rows = vars; cols = depths) +! +!------------------------------------------------------------------------------- +&init_profiles + num_depths = 6 + the_depths = 0.0, 4.0, 8.0, 12.0, 16.0, 20.0 + the_temps = 5.1, 5.0, 4.9, 4.9, 4.8, 4.8 + the_sals = 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 + lake_depth = 25 + num_wq_vars = 6 + wq_names = 'OGM_don', + 'OGM_pon', + 'OGM_dop', + 'OGM_pop', + 'OGM_doc', + 'OGM_poc' +! 'aed_silica_rsi' + wq_init_vals = 1.1, 1.2, 1.3,1.3,1.3,1.3 + 2.1, 2.2, 2.3,2.3,2.3,2.3 + 3.1, 3.2, 3.3,3.3,3.3,3.3 + 4.1, 4.2, 4.3,4.3,4.3,4.3 + 5.1, 5.2, 5.3,5.3,5.3,5.3 + 6.1, 6.2, 6.3, 6.3, 6.3, 6.3 +/ +!------------------------------------------------------------------------------- +! meteorology +!------------------------------------------------------------------------------- +! +! met_sw [bool] switch to include surface meteorological forcing +! lw_type [string] type of longwave data supplied (LW_IN/LW_CC/LW_NET) +! rain_sw [bool] include rainfall nutrient composition +! snow_sw [bool] include snowfall (m/d) +! atm_stab [bool] account for non-neutral atmospheric stability +! catchrain [bool] flag that enables runoff from exposed banks of lake area +! rad_mode [integer] short and long wave radation model configuration (see manual) +! albedo_mode [integer] shortwave albedo calculation method +! cloud_mode [integer] atmospheric emmisivity calculation method +! +! meteo_fl [string] name of file with meteorology input data +! wind_factor [float] wind multiplication factor (-) +! wind_factor [float] wind multiplication factor (-) +! rain_factor [float] wind multiplication factor (-) +! sw_factor [float] wind multiplication factor (-) +! lw_factor [float] wind multiplication factor (-) +! at_factor [float] wind multiplication factor (-) +! rh_factor [float] wind multiplication factor (-) +! +! ce [float] bulk aerodynamic coefficient for latent heat transfer +! ch [float] bulk aerodynamic coefficient for sensible heat transfer +! cd [float] bulk aerodynamic coefficient for transfer of momentum +! rain_threshold [float] rainfall amount (m) required before runoff from exposed banks +! runoff_coef [float] conversion of rainfall to runoff in exposed lake banks +! +!------------------------------------------------------------------------------- +&meteorology + met_sw = .true. + lw_type = 'LW_IN' + rain_sw = .false. + snow_sw = .true. + atm_stab = .false. + catchrain = .false. + rad_mode = 1 + albedo_mode = 1 + cloud_mode = 4 + !-- BC file details + meteo_fl = 'Mendota_hourly.csv' + subdaily = .true. + wind_factor = 1.0 +! sw_factor = 1.0 +! lw_factor = 1.0 +! at_factor = 1.0 +! rh_factor = 1.0 +! rain_factor = 1.0 + cd = 0.0013 + ce = 0.0013 + ch = 0.0013 + rain_threshold = 0.01 + runoff_coef = 0.3 +! time_fmt = 'YYYY-MM-DD hh:mm:ss' +/ +!------------------------------------------------------------------------------- +! inflows +!------------------------------------------------------------------------------- +! +! num_inflows [integer] number of inflowing streams (0+) +! names_of_strms [string] names of streams (comma separated list) +! strm_hf_angle [float] stream half angle (degrees) +! strmbd_slope [float] streambed slope (degrees) +! strmbd_drag [float] streambed drag coefficient (-) +! inflow_factor [float] inflow flow rate multiplier (-) +! inflow_fl [string] inflow data filename(s) (comma separated list) +! inflow_varnum [integer] number of columns (excluding date) to be read +! inflow_vars [string] variable names of inflow file columns +! This should be a comma separated list, and must +! include FLOW, SALT & TEMP (for GLM), and +! optionally can include FABM var names. +! coef_inf_entrain [real] entrainment coefficient for inflows +! +!------------------------------------------------------------------------------- +&inflow + num_inflows = 3 + names_of_strms = 'Yahara','Pheasant','SpringHarbor' + strm_hf_angle = 65.0,65.0,65.0 + strmbd_slope = 3.0,3.0,3.0 + strmbd_drag = 0.0160,0.0160,0.0160 + inflow_factor = 1.0,1.0,1.0 + inflow_fl = 'Mendota_yahara.csv','Mendota_pheasant.csv','Mendota_springharbor.csv' + inflow_varnum = 4 + inflow_vars = 'FLOW', 'TEMP','NIT_nit','PHS_frp' +/ +!------------------------------------------------------------------------------- +! outflows +!------------------------------------------------------------------------------- +! +! num_outlet [integer] no. of outlets +! flt_off_sw [bool] floating offtake switches +! outl_elvs [float] outlet elevations (comma separated list) +! bsn_len_outl [float] basin length at outlets (m) +! bsn_wid_outl [float] basin width at outlets (m) +! outflow_fl [string] outflow data file +! outflow_factor [float] outflow flow rate multiplier (-) +! seepage [bool] do seepage processing [default is off - ie no seepage] +! seepage_rate [float] seepage rate of water (m/day) from bottom layer +! +!------------------------------------------------------------------------------- + &outflow + num_outlet = 1 + flt_off_sw = .false. + outl_elvs = 396 + bsn_len_outl = 799 + bsn_wid_outl = 398 + outflow_fl = 'outflow.csv' + outflow_factor = 1.0 +/ diff --git a/man/plot_var.Rd b/man/plot_var.Rd index 7b3b68a..a90de76 100644 --- a/man/plot_var.Rd +++ b/man/plot_var.Rd @@ -22,7 +22,7 @@ plot_var(file, var_name, col_lim, reference = "surface", num_cells = 100, \item{add}{F if create new figure, T if add to existing} -\item{bar_title}{NULL if use var_name, or specify as a string to name plot variable} +\item{bar_title}{NULL if use \code{\link{sim_var_longname}}, or specify as a string to name plot variable} } \description{ plot variable from a GLM simulation @@ -41,7 +41,7 @@ fig_path = 'aed_out.png') Jordan S. Read, Luke A. Winslow } \seealso{ -\link{get_temp} +\code{\link{get_temp}}, \code{\link{sim_var_longname}} } \keyword{methods} diff --git a/man/sim_var_longname.Rd b/man/sim_var_longname.Rd new file mode 100644 index 0000000..84b0753 --- /dev/null +++ b/man/sim_var_longname.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2 (4.1.0): do not edit by hand +% Please edit documentation in R/sim_var_longname.R +\name{sim_var_longname} +\alias{sim_var_longname} +\title{get long name of variable from a GLM simulation} +\usage{ +sim_var_longname(file, var_name) +} +\arguments{ +\item{file}{a string with the path to the netcdf output from GLM} + +\item{var_name}{name of a valid variable from a simulation (see \code{\link{sim_vars}})} +} +\description{ +get long name of variable from a GLM simulation +} +\examples{ +sim_folder <- run_example_sim(verbose = FALSE) +nc_file <- file.path(sim_folder, 'output.nc') +vars <- sim_vars(file = nc_file) +sim_var_units(nc_file, vars[1]) +sim_var_units(nc_file, 'u_mean') +} +\author{ +Jordan S. Read +} +\seealso{ +\code{\link{sim_vars}}, \code{\link{sim_var_units}} +} +\keyword{methods} + diff --git a/man/sim_var_units.Rd b/man/sim_var_units.Rd new file mode 100644 index 0000000..d1d442f --- /dev/null +++ b/man/sim_var_units.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2 (4.1.0): do not edit by hand +% Please edit documentation in R/sim_var_units.R +\name{sim_var_units} +\alias{sim_var_units} +\title{get units of variable from a GLM simulation} +\usage{ +sim_var_units(file, var_name) +} +\arguments{ +\item{file}{a string with the path to the netcdf output from GLM} + +\item{var_name}{name of a valid variable from a simulation (see \code{\link{sim_vars}})} +} +\description{ +get units of variable from a GLM simulation +} +\examples{ +sim_folder <- run_example_sim(verbose = FALSE) +nc_file <- file.path(sim_folder, 'output.nc') +vars <- sim_vars(file = nc_file) +sim_var_longname(nc_file, vars[1]) +sim_var_longname(nc_file, 'u_mean') +} +\author{ +Jordan S. Read +} +\seealso{ +\code{\link{sim_vars}}, \code{\link{sim_var_longname}} +} +\keyword{methods} + diff --git a/tests/testthat/test-get_outputNC.R b/tests/testthat/test-get_outputNC.R index 8b30528..da97295 100644 --- a/tests/testthat/test-get_outputNC.R +++ b/tests/testthat/test-get_outputNC.R @@ -40,4 +40,15 @@ test_that("get_ice",{ t_out <- as.POSIXct(c("1900-01-01","1900-01-02")) expect_warning(get_ice(nc_file, t_out = t_out, method = 'interp')) expect_warning(get_ice(nc_file, t_out = t_out, method = 'interp', snow.rm = FALSE)) +}) + +context("sim_vars") + +test_that("get variables from sim",{ + sim_folder <- run_example_sim(verbose = FALSE) + nc_file <- file.path(sim_folder, 'output.nc') + expect_is(sim_vars(file = nc_file), 'character') + expect_is(sim_var_longname(nc_file, 'u_mean'), 'character') + expect_is(sim_var_units(nc_file, 'u_mean'), 'character') + expect_error(sim_var_units(nc_file, 'u_meanBADNAME')) }) \ No newline at end of file diff --git a/tests/testthat/test-nml.R b/tests/testthat/test-nml.R index cf8beb1..e7c40c2 100644 --- a/tests/testthat/test-nml.R +++ b/tests/testthat/test-nml.R @@ -14,6 +14,21 @@ test_that("set_nml() with different datatypes", { }) +context("testing validate nml") +test_that(".validate nml works",{ + nml_bad <- nml + nml_bad$time <- NULL # remove a required block + expect_error(glmtools:::.validate_nml(nml_bad)) + expect_true(glmtools:::.validate_nml(nml)) + + +}) + +context("reading a bad nml file") +test_that("file errors out",{ + expect_error(read_nml(system.file('extdata','bad_glm2.nml',package='glmtools'))) +}) + context("set_nml() and get_nml_value() with lists and arguments") test_that("reading and setting nml works as expected", { # set string to a string