Skip to content

Commit b22fa0a

Browse files
authored
Merge pull request #227 from jread-usgs/master
adding logical vectors to parse and write nml functions #226
2 parents bca248c + 306ca70 commit b22fa0a

File tree

6 files changed

+146
-17
lines changed

6 files changed

+146
-17
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Package: glmtools
22
Type: Package
33
Title: glmtools
4-
Version: 0.14.5
5-
Date: 2017-01-11
4+
Version: 0.14.6
5+
Date: 2017-01-31
66
Authors@R: c( person("Jordan", "Read", role = c("aut","cre"),
77
email = "jread@usgs.gov"),
88
person("Luke", "Winslow", role = "aut",

R/nml_helpers.R

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@ buildVal <- function(textLine, lineNum, blckName){
2626
}
2727
if (any(grep("'",parVl))){
2828

29-
parVl <- gsub("'","",parVl)#c(as.character(unlist(strsplit(parVl,","))))
29+
parVl <- gsub("'","",parVl)
3030
}else if (any(grep("\"",parVl))){
3131
parVl <- gsub("\"","",parVl)
32-
}else if (any(grep(".true.",parVl))){
33-
parVl <- TRUE
34-
}else if (any(grep(".false.",parVl))){
35-
parVl <- FALSE
32+
}else if (isTRUE(grepl(".true.",parVl) || grepl(".false.",parVl))){
33+
logicals <- unlist(strsplit(parVl,","))
34+
parVl <- from.glm_boolean(logicals)
3635
}else if (any(grep(",",parVl))){ # comma-sep-nums
3736
parVl <- c(as.numeric(unlist(strsplit(parVl,","))))
3837
}else { # test for number
@@ -43,6 +42,29 @@ buildVal <- function(textLine, lineNum, blckName){
4342
return(lineVal)
4443
}
4544

45+
#' go from glm2.nml logical vectors to R logicals
46+
#'
47+
#' @param values a vector of strings containing either .false. or .true.
48+
#' @return a logical vector
49+
#' @keywords internal
50+
from.glm_boolean <- function(values){
51+
52+
logicals <- sapply(values, FUN = function(x){
53+
if (!isTRUE(grepl(".true.", x) || grepl(".false.", x))){
54+
stop(x, ' is not a .true. or .false.; conversion to TRUE or FALSE failed.',
55+
call. = FALSE)
56+
}
57+
return(ifelse(isTRUE(grepl(".true.", x)), TRUE, FALSE))
58+
})
59+
return(as.logical(logicals))
60+
}
61+
62+
to.glm_boolean <- function(values){
63+
val.logical <- values
64+
values[val.logical] <- '.true.'
65+
values[!val.logical] <- '.false.'
66+
return(values)
67+
}
4668
# private function
4769
findBlck <- function(nml,argName){
4870

R/print.nml.R

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,19 @@ print.nml <- function(x, ...){
1212
cat(names(blckList)[j])
1313
cat(' = ')
1414
if (length(blckList[[j]])>1){
15-
writer <- paste(c(blckList[[j]]),collapse=', ')
15+
if (is.logical(blckList[[j]])){
16+
charText <- to.glm_boolean(blckList[[j]])
17+
} else {
18+
charText <- c(blckList[[j]])
19+
}
20+
writer <- paste(charText,collapse=', ')
1621
} else if (is.character(blckList[[j]])) {
17-
charText <- strsplit(blckList[[j]],',')
18-
writer <- paste(c("'",paste(c(charText[[1]]),collapse="','"),"'"),collapse='')
19-
} else if (is.logical(blckList[[j]]) & blckList[[j]]){
20-
writer <- ".true."
21-
} else if (is.logical(blckList[[j]]) & !blckList[[j]]){
22-
writer <- ".false."
22+
charText <- strsplit(blckList[[j]],',')
23+
writer <- paste(c("'",paste(c(charText[[1]]),collapse="','"),"'"),collapse='')
24+
} else if (is.logical(blckList[[j]])){
25+
writer <- to.glm_boolean(blckList[[j]])
2326
} else {
24-
writer <- blckList[[j]]
27+
writer <- blckList[[j]]
2528
}
2629
cat(writer)
2730
cat('\n')

R/read_nml.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ read_nml <- function(nml_file = 'template'){
5151
carryover = ''
5252

5353
for (j in (blckOpen[i]+1):(blckClse[i]-1)){
54+
5455
textLine <- paste(carryover, gsub("\t","",gsub(" ","",fileLines[j])), sep='')
55-
#cat(textLine,'\n')
56-
#browser()
5756
if(substr(textLine,1,1)!='!'){
5857
# Add a check here, sometimes, if there is a hanging comma,
5958
#and only sometimes that means add next row

inst/extdata/multiple_booleans.nml

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
&glm_setup
2+
sim_name = 'GLMSimulation'
3+
max_layers = 1000
4+
min_layer_vol = 0.025
5+
min_layer_thick = 0.25
6+
max_layer_thick = 0.5
7+
Kw = 0.7687144
8+
coef_mix_conv = 0.1
9+
coef_wind_stir = 0.2827018
10+
coef_mix_shear = 0.2644648
11+
coef_mix_turb = 0.4
12+
coef_mix_KH = 0.204202
13+
coef_mix_hyp = 0.5982979
14+
/
15+
&morphometry
16+
lake_name = 'Cannonsville'
17+
latitude = 42.078
18+
longitude = -75.3344
19+
bsn_len = 12000
20+
bsn_wid = 800
21+
bsn_vals = 36
22+
H = 301.22, 301.83, 303.35, 304.88, 306.4, 307.93, 309.45, 310.98, 312.5, 314.02, 315.55, 317.07, 318.6, 320.12, 321.65, 323.17, 324.7, 326.22, 327.74, 329.27, 330.79, 332.32, 333.84, 335.37, 336.89, 338.42, 339.94, 341.46, 342.99, 344.51, 346.04, 347.56, 349.09, 350.61, 352.13, 353.66
23+
A = 1630, 1630, 42600, 79400, 140000, 220000, 335000, 655000, 1090000, 1260000, 1470000, 2020000, 2850000, 3270000, 3720000, 4780000, 5760000, 6270000, 6820000, 8120000, 9240000, 9950000, 10800000, 11900000, 12800000, 13400000, 13900000, 14600000, 15400000, 15900000, 16500000, 17400000, 18300000, 19300000, 20300000, 21300000
24+
/
25+
&time
26+
timefmt = 2
27+
start = '2000-01-01 00:00:00'
28+
stop = '2002-12-08 23:00:00'
29+
dt = 3600
30+
timezone = -4
31+
/
32+
&output
33+
out_dir = '.'
34+
out_fn = 'output'
35+
nsave = 24
36+
csv_lake_fname = 'Physics'
37+
csv_point_nlevs = 1
38+
csv_point_fname = 'WQ'
39+
csv_point_at = 20
40+
csv_point_nvars = 5
41+
csv_point_vars = 'temp','OXY_oxy','OGM_doc','PHY_TCHLA','TOT_tp'
42+
csv_outlet_fname = 'outlet_'
43+
csv_outlet_nvars = 3
44+
csv_outlet_vars = 'flow','temp','OXY_oxy'
45+
csv_ovrflw_fname = 'overflow'
46+
/
47+
&init_profiles
48+
num_depths = 3
49+
the_depths = 0, 3, 25.5
50+
the_temps = 4, 4, 4
51+
the_sals = 0.76, 0.76, 0.76
52+
lake_depth = 40
53+
num_wq_vars = 0
54+
/
55+
&meteorology
56+
met_sw = .true.
57+
lw_type = 'LW_IN'
58+
rain_sw = .false.
59+
atm_stab = .false.
60+
catchrain = .false.
61+
rad_mode = 2
62+
albedo_mode = 1
63+
cloud_mode = 4
64+
meteo_fl = 'NLDAS2_Cannonsville_C6.csv'
65+
subdaily = .true.
66+
wind_factor = 1
67+
sw_factor = 0.6212019
68+
cd = 0.001216207
69+
ce = 0.001336918
70+
ch = 0.001268856
71+
rain_threshold = 0.01
72+
runoff_coef = 0.3
73+
/
74+
&inflow
75+
num_inflows = 3
76+
names_of_strms = 'Trout','WBDR','ungaged'
77+
strm_hf_angle = 10, 10, 10
78+
strmbd_slope = 47, 47, 47
79+
strmbd_drag = 0.01326389, 0.01326389, 0.01326389
80+
inflow_factor = 1, 1, 1
81+
inflow_fl = 'Cannonsville_inflow_Trout.csv','Cannonsville_inflow_WBDR.csv','Cannonsville_inflow_ungaged.csv'
82+
inflow_varnum = 2
83+
inflow_vars = 'FLOW','TEMP'
84+
/
85+
&outflow
86+
num_outlet = 3
87+
flt_off_sw = .false.,.false.,.false.
88+
outl_elvs = 330.86, 311, 350.52
89+
bsn_len_outl = 996, 837, 4600
90+
bsn_wid_outl = 433, 363, 2000
91+
outflow_fl = 'Cannonsville_outflow_withdraw.csv','Cannonsville_outflow_damrelease.csv','Cannonsville_outflow_spill.csv'
92+
outflow_factor = 1, 1, 1
93+
/

tests/testthat/test-nml.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,19 @@ test_that("set_nml() with different datatypes", {
1414

1515
})
1616

17+
context("can use vectors for logicals")
18+
test_that("set_nml() can use a vector for logicals", {
19+
nml <- set_nml(nml, 'flt_off_sw', c(T, F, F))
20+
expect_true(get_nml_value(nml, 'flt_off_sw')[1])
21+
expect_false(get_nml_value(nml, 'flt_off_sw')[2])
22+
expect_error(set_nml(nml, 'flt_off_sw', c(T, F, '.false.')))
23+
})
1724

25+
test_that("can read in nml with vector for logicals", {
26+
nml <- read_nml(system.file(package='glmtools','extdata','multiple_booleans.nml'))
27+
expect_true(length(get_nml_value(nml, 'flt_off_sw')) > 1)
28+
expect_is(get_nml_value(nml, 'flt_off_sw'), 'logical')
29+
})
1830

1931
context("reading a bad nml file")
2032
test_that("file errors out",{

0 commit comments

Comments
 (0)