diff --git a/.Rbuildignore b/.Rbuildignore index b3da98e..fa32e75 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -2,3 +2,5 @@ ^\.Rproj\.user$ .travis.yml appveyor.yml +^README\.Rmd$ +^README-.*\.png$ diff --git a/DESCRIPTION b/DESCRIPTION index 0adf3b9..17cb2b4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: glmtools Type: Package Title: glmtools -Version: 0.14.7 +Version: 0.14.8 Authors@R: c( person("Jordan", "Read", role = c("aut","cre"), email = "jread@usgs.gov"), person("Luke", "Winslow", role = "aut", diff --git a/NAMESPACE b/NAMESPACE index ed572c5..706e7c9 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -39,9 +39,10 @@ export(validate_sim) export(water.temperature) export(whole.lake.temperature) export(write_nml) -import(GLMr) import(rLakeAnalyzer) import(tools) +importFrom(GLMr,nml_template_path) +importFrom(GLMr,run_glm) importFrom(akima,interp) importFrom(grDevices,colorRampPalette) importFrom(grDevices,dev.off) diff --git a/R/get_nml_value.R b/R/get_nml_value.R index 5bd5bbb..8e5a7d5 100644 --- a/R/get_nml_value.R +++ b/R/get_nml_value.R @@ -3,19 +3,28 @@ #'@description This function returns an nml value according to the arg_name nml list for GLM. #'@param glm_nml a nml (a list) for GLM config #'@param arg_name a string representing a valid field in glm_nml -#' @param \dots additional arguments passed to \code{get_block}, such as warn=TRUE +#'@param \dots additional arguments passed to \code{get_block}, such as warn=TRUE +#'@param nml_file a string with the path to the GLM glm2.nml file or +#'\code{'template'} for loading the GLM template nml file with GLMr (default) #'@return arg_val value for the valid field in glm_nml specified by \code{arg_name} #'@author #'Jordan S. Read #'@seealso \link{read_nml}, \link{set_nml} -#'@import GLMr #'@examples #'# read in default nml template from GLMr #'glm_nml <- read_nml() #'get_nml_value(glm_nml,arg_name = 'Kw') #'@export -get_nml_value <- function(glm_nml, arg_name, ...){ +get_nml_value <- function(glm_nml = NA, arg_name, nml_file = 'template', ...){ + if(!all(is.na(glm_nml)) & nml_file != 'template'){ + stop("Must specify either an nml object via 'glm_nml' or + an nml file path via 'nml_file'") + } + + if(all(is.na(glm_nml))){ + glm_nml <- read_nml(nml_file) + } blck = get_block(glm_nml, arg_name, ...) arg_name = get_arg_name(arg_name) diff --git a/R/nml_helpers.R b/R/nml_helpers.R index 58d8ec8..e881ee8 100644 --- a/R/nml_helpers.R +++ b/R/nml_helpers.R @@ -6,12 +6,12 @@ buildVal <- function(textLine, lineNum, blckName){ # remove all text after comment string textLine <- strsplit(textLine,'!')[[1]][1] - if (!any(grep("=",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] + parNm <- params[[1]][1] + parVl <- params[[1]][2] # figure out what parval is...if string, remove quotes and keep as string # ***for boolean text, use "indentical" so that 0!= FALSE # can be: string, number, comma-sep-numbers, or boolean diff --git a/R/read_nml.R b/R/read_nml.R index b1ce17a..59d13de 100644 --- a/R/read_nml.R +++ b/R/read_nml.R @@ -13,12 +13,11 @@ #'@examples #'glm_nml <- read_nml() #'print(glm_nml) -#'@import GLMr #'@export read_nml <- function(nml_file = 'template'){ + nml_file <- nml_path_norm(nml_file) - if (!ascii_only(nml_file)){ stop('non-ASCII characters found in nml file on line ', what_ascii(nml_file)) } @@ -34,30 +33,33 @@ read_nml <- function(nml_file = 'template'){ fileLines <- fileLines[!ignoreLn] # find all lines which start with "&" * requires FIRST char to be value - lineIdx <- seq(1,length(lineStart)) blckOpen <- lineIdx[lineStart=="&"] blckClse <- lineIdx[lineStart=="/"] - nml <- list() - for (i in 1:length(blckOpen)){ - blckName <- substr(fileLines[blckOpen[i]],2,nchar(fileLines[blckOpen[i]])) - blckName <- gsub("\\s", "", blckName) - oldNms <- names(nml) - nml[[i]] <- list() - names(nml) <- c(oldNms,blckName) + for (i in seq_len(length(blckOpen))){ + blckName <- substr(fileLines[blckOpen[i]], + 2, nchar(fileLines[blckOpen[i]])) + blckName <- gsub("\\s", "", blckName) + oldNms <- names(nml) + nml[[i]] <- list() + names(nml) <- c(oldNms,blckName) - carryover = '' + carryover <- '' for (j in (blckOpen[i]+1):(blckClse[i]-1)){ - textLine <- paste(carryover, gsub("\t","",gsub(" ","",fileLines[j])), sep='') - if(substr(textLine,1,1)!='!'){ + textLine <- paste(carryover, + gsub("\t", "", gsub(" ", "", fileLines[j])), sep = '') + + if(substr(textLine, 1, 1) != '!'){ # Add a check here, sometimes, if there is a hanging comma, #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]))){ + if(substr(textLine, nchar(textLine), nchar(textLine)) == ',' && + j+1 <= length(fileLines) && + !any(grep("=", fileLines[j + 1])) && + !any(grep("/", fileLines[j + 1]))){ carryover = textLine next @@ -65,8 +67,8 @@ read_nml <- function(nml_file = 'template'){ carryover = '' } # else, line is commented out - lineVal <- buildVal(textLine, lineNum=j, blckName) - nml[[i]] <- c(nml[[i]],lineVal) + lineVal <- buildVal(textLine, lineNum = j, blckName) + nml[[i]] <- c(nml[[i]], lineVal) } } } @@ -74,10 +76,10 @@ read_nml <- function(nml_file = 'template'){ return(nml) } - +#' @importFrom GLMr nml_template_path nml_path_norm <- function(nml_file){ if (nml_file == "template"){ - nml_file <- nml_template_path() + nml_file <- GLMr::nml_template_path() } if (!is_nml_file(nml_file)){ stop(nml_file, ' is not of file type *.nml') diff --git a/R/run_example_sim.R b/R/run_example_sim.R index 9a8c15b..56c6504 100644 --- a/R/run_example_sim.R +++ b/R/run_example_sim.R @@ -4,6 +4,7 @@ #'@param verbose should operations and output of GLM be shown #'@keywords methods #'@seealso \code{\link[GLMr]{run_glm}} +#'@importFrom GLMr run_glm #'@author #'Jordan S. Read, Luke A. Winslow #'@examples @@ -73,7 +74,7 @@ run_example_sim = function(sim_folder, verbose = TRUE){ if(verbose){cat('writing nml file to ', nml_file,'\n')} write_nml(glm_nml = nml, file = nml_file) - run_glm(sim_folder = sim_folder, verbose = verbose) + GLMr::run_glm(sim_folder = sim_folder, verbose = verbose) if(verbose){cat('simulation complete. \n*.nc output located in ', nc_file,'\n')} diff --git a/man/get_nml_value.Rd b/man/get_nml_value.Rd index 13064c0..dbdf450 100644 --- a/man/get_nml_value.Rd +++ b/man/get_nml_value.Rd @@ -4,13 +4,16 @@ \alias{get_nml_value} \title{gets a nml value according to an arg_name} \usage{ -get_nml_value(glm_nml, arg_name, ...) +get_nml_value(glm_nml = NA, arg_name, nml_file = "template", ...) } \arguments{ \item{glm_nml}{a nml (a list) for GLM config} \item{arg_name}{a string representing a valid field in glm_nml} +\item{nml_file}{a string with the path to the GLM glm2.nml file or +\code{'template'} for loading the GLM template nml file with GLMr (default)} + \item{\dots}{additional arguments passed to \code{get_block}, such as warn=TRUE} } \value{ diff --git a/tests/testthat/test-nml.R b/tests/testthat/test-nml.R index 9533ad4..4d1a9a9 100644 --- a/tests/testthat/test-nml.R +++ b/tests/testthat/test-nml.R @@ -28,6 +28,21 @@ test_that("can read in nml with vector for logicals", { expect_is(get_nml_value(nml, 'flt_off_sw'), 'logical') }) +test_that("can read values from an nml file", { + nml <- read_nml() + nml <- set_nml(nml, "sim_name", "test") + temp_nml <- tempfile(fileext = ".nml") + write_nml(nml, temp_nml) + + expect_true(get_nml_value(arg_name = "sim_name") == "GLMSimulation") + expect_true( + get_nml_value(arg_name = "sim_name", nml_file = temp_nml) == "test") + + nml <- read_nml() + expect_error( + get_nml_value(nml, arg_name = "sim_name", nml_file = temp_nml)) +}) + context("reading a bad nml file") test_that("file errors out",{ expect_error(read_nml(system.file('extdata','bad_glm2.nml',package='glmtools')))