Skip to content

Commit

Permalink
tests for #202
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan S Read committed Mar 27, 2016
1 parent 88fcb20 commit 660eeaf
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Expand Up @@ -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)
10 changes: 9 additions & 1 deletion R/convert_sim_var.R
Expand Up @@ -10,19 +10,21 @@
#' @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)
#'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')
#'
#' }
#'
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?
Expand All @@ -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]]
Expand Down
2 changes: 1 addition & 1 deletion man/convert_sim_var.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions 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)
})

0 comments on commit 660eeaf

Please sign in to comment.