Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add file argument to get_nml_value #236

Merged
merged 8 commits into from
Aug 19, 2017
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
^\.Rproj\.user$
.travis.yml
appveyor.yml
^README\.Rmd$
^README-.*\.png$
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 12 additions & 3 deletions R/get_nml_value.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions R/nml_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 21 additions & 19 deletions R/read_nml.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -34,50 +33,53 @@ 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
}else{
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)
}
}
}
nml <- .nml(nml)
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')
Expand Down
3 changes: 2 additions & 1 deletion R/run_example_sim.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')}

Expand Down
5 changes: 4 additions & 1 deletion man/get_nml_value.Rd

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

15 changes: 15 additions & 0 deletions tests/testthat/test-nml.R
Original file line number Diff line number Diff line change
Expand Up @@ -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')))
Expand Down