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

Set nml with vectors #245

Merged
merged 5 commits into from Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions DESCRIPTION
@@ -1,12 +1,14 @@
Package: glmtools
Type: Package
Title: glmtools
Version: 0.14.8
Version: 0.15.0
Date: 2017-01-31
Authors@R: c( person("Jordan", "Read", role = c("aut","cre"),
email = "jread@usgs.gov"),
person("Luke", "Winslow", role = "aut",
email = "lwinslow@usgs.gov"))
email = "lwinslow@usgs.gov"),
person("Joseph", "Stachelek", role = "ctb",
email = "stachel2@msu.edu"))
Description: Tools for working with the GLM lake model.
URL: https://github.com/USGS-R/glmtools
BugReports: https://github.com/USGS-R/glmtools/issues
Expand Down Expand Up @@ -35,4 +37,4 @@ VignetteBuilder: knitr
BuildVignettes: true
LazyLoad: yes
LazyData: yes
RoxygenNote: 6.0.1
RoxygenNote: 6.1.1
5 changes: 4 additions & 1 deletion R/set_nml.R
Expand Up @@ -17,7 +17,7 @@
#'print(glm_nml)
#'@seealso \link{get_nml_value}, \link{read_nml}
#'@export
set_nml <- function(glm_nml,arg_name,arg_val,arg_list=NULL){
set_nml <- function(glm_nml, arg_name, arg_val, arg_list = NULL){

if (missing(arg_name) & missing(arg_val)){
return(setnmlList(glm_nml,arg_list))
Expand Down Expand Up @@ -46,6 +46,9 @@ set_nml <- function(glm_nml,arg_name,arg_val,arg_list=NULL){
# get appropriate block to place val within ** assumes no duplicate param names in other blocks **
blck <- get_block(glm_nml,arg_name)
arg_name <- get_arg_name(arg_name)
if(length(arg_val) > 1 & is.character(arg_val)){
arg_val <- paste0(arg_val, collapse = ",")
}
glm_nml[[blck]][[arg_name]] <- arg_val
return(glm_nml)
}
38 changes: 38 additions & 0 deletions tests/testthat/test-nml.R
Expand Up @@ -28,6 +28,44 @@ test_that("can read in nml with vector for logicals", {
expect_is(get_nml_value(nml, 'flt_off_sw'), 'logical')
})

test_that("can read and write nml with vectors", {
# read character vectors
glm_nml <- read_nml()
glm_nml <- set_nml(glm_nml, arg_list = list(
'inflow_fl' = c("yahara.csv", "yahara2.csv")))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a small test to verify that setting it the old way has an identical result? So that we stay backwards compatible with this change. I expect it is the same, but better to have a test.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I think the new test I've added checks for this.

expect_equal(
length(get_nml_value(glm_nml, arg_name = "inflow_fl")),
1)

# writing character vectors is backwards compatible
glm_nml <- read_nml()
glm_nml <- set_nml(glm_nml, arg_name = "inflow_fl",
arg_val = c("yahara.csv", "yahara2.csv"))
expect_equal(
length(get_nml_value(glm_nml, arg_name = "inflow_fl")),
1)

# read numeric vectors
glm_nml <- read_nml()
glm_nml <- set_nml(glm_nml, arg_list = list(
'A' = c(1, 2, 3)))
expect_true(
length(get_nml_value(glm_nml, arg_name = "A")) > 1)

# write character vectors
write_path <- paste0(tempdir(), 'glm2.nml')
write_nml(glm_nml, file = write_path)
expect_equal(
length(get_nml_value(read_nml(write_path), arg_name = "inflow_fl")),
1)

# write numeric vectors
write_path <- paste0(tempdir(), 'glm2.nml')
write_nml(glm_nml, file = write_path)
expect_true(
length(get_nml_value(read_nml(write_path), arg_name = "A")) > 1)
})

test_that("can read values from an nml file", {
nml <- read_nml()
nml <- set_nml(nml, "sim_name", "test")
Expand Down