diff --git a/NAMESPACE b/NAMESPACE index e69ff53..55cee95 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -47,4 +47,7 @@ importFrom(lazyeval,lazy_eval) importFrom(ncdf4,nc_close) importFrom(ncdf4,nc_open) importFrom(ncdf4,ncatt_get) +importFrom(ncdf4,ncvar_add) +importFrom(ncdf4,ncvar_def) importFrom(ncdf4,ncvar_get) +importFrom(ncdf4,ncvar_put) diff --git a/R/convert_sim_var.R b/R/convert_sim_var.R index 5724fd9..b37c1d8 100644 --- a/R/convert_sim_var.R +++ b/R/convert_sim_var.R @@ -10,6 +10,7 @@ #' @param longname the longname for the new variable #' @export #' @importFrom lazyeval lazy_dots lazy_eval +#' @importFrom ncdf4 ncvar_def ncvar_put ncvar_add #' @examples #' \dontrun{ #'sim_folder <- run_example_sim(verbose = FALSE) @@ -17,12 +18,13 @@ #'convert_sim_var(nc_file, tempF = temp/5*9+32, unit='degF',longname='temperature degrees Farenheit') #'plot_var(nc_file, 'tempF') #'convert_sim_var(nc_file, crazy_var = temp-u_mean*1000) -#'plot_var(nc_file, crazy_var) +#'plot_var(nc_file, 'crazy_var') #' #' } #' convert_sim_var <- function(nc_file='output.nc', ..., unit='', longname=''){ + sim.vars <- sim_vars(nc_file)$name message('convert_sim_var is untested and in development') # // here, vals would be defined by the function passed in by `...`. Probably captured w/ lazyeval? @@ -32,10 +34,16 @@ convert_sim_var <- function(nc_file='output.nc', ..., unit='', longname=''){ stop('not yet ready to handle multi-var expressions') var.name <- names(convert) + if (var.name %in% sim.vars) + stop(var.name, ' cannot be added, it already exists.', call. = FALSE) fun.string <- deparse(convert[[1]]$expr) variables <- strsplit(fun.string,"[^a-zA-Z_]")[[1]] variables <- variables[variables != ''] + vars.not.in <- variables[!variables %in% sim.vars] + if (length(vars.not.in) > 0) + stop(paste(vars.not.in, collapse=', '), ' do not exist in simulation vars', call. = FALSE) + data <- lapply(variables, function(v) get_raw(nc_file, v)) names(data) <- variables vals <- lazyeval::lazy_eval(convert, data=data)[[1]] diff --git a/man/convert_sim_var.Rd b/man/convert_sim_var.Rd index 994f88b..c9bc3ad 100644 --- a/man/convert_sim_var.Rd +++ b/man/convert_sim_var.Rd @@ -26,7 +26,7 @@ nc_file <- file.path(sim_folder, 'output.nc') convert_sim_var(nc_file, tempF = temp/5*9+32, unit='degF',longname='temperature degrees Farenheit') plot_var(nc_file, 'tempF') convert_sim_var(nc_file, crazy_var = temp-u_mean*1000) -plot_var(nc_file, crazy_var) +plot_var(nc_file, 'crazy_var') } diff --git a/tests/testthat/test-convert_sim_var.R b/tests/testthat/test-convert_sim_var.R new file mode 100644 index 0000000..250d4f8 --- /dev/null +++ b/tests/testthat/test-convert_sim_var.R @@ -0,0 +1,24 @@ +context("convert variables in netcdf output") + +test_that("errors with poor matches", { + sim_folder <- run_example_sim(verbose = FALSE) + nc_file <<- file.path(sim_folder, 'output.nc') + expect_error(convert_sim_var(nc_file, temp = temp*2), "temp cannot be added, it already exists.") + expect_error(convert_sim_var(nc_file, temp.new = garbage), "garbage do not exist in simulation vars") +}) + +test_that("can add variable", { + convert_sim_var(nc_file, temp.new = temp*2) + expect_true('temp.new' %in% sim_vars(nc_file)$name) + expect_is(get_var(nc_file, var_name = 'temp.new'), 'data.frame') +}) + +test_that("errors when you try to add it again", { + expect_error(convert_sim_var(nc_file, temp.new = temp*2)) +}) + +test_that("can modify variable with more than one variable function", { + + convert_sim_var(nc_file, crazy_var = temp-u_mean*1000) + expect_true('crazy_var' %in% sim_vars(nc_file)$name) +}) \ No newline at end of file