Skip to content

Commit

Permalink
Merge pull request #3081 from PecanProject/history-bug
Browse files Browse the repository at this point in the history
Bug fix for #3080 (write.config.xml.ED2() not using <revision> tag correctly)
  • Loading branch information
mdietze committed Jan 19, 2023
2 parents 3d98b5b + 08342fb commit 1898fc2
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ convert data for a single PFT fixed (#1329, #2974, #2981)
the cdo_setup argument in the template job file. In detail, people will need
to specify cdosetup = "module load cdo/2.0.6" in the host section. More details
are in the Create_Multi_settings.R script. (#3052)
- write.config.xml.ED2() wasn't using the <revision> tag in settings correctly (#3080)
- runModule.get.trait.data() now correctly respects the settings$database$bety$write setting (#2968)
- Fixed a bug in `model2netcdf.ed2()` where .nc file connections were being closed multiple times, printing warnings (#3078)
- Fixed a bug causing the model2netcdf.ED2() step in jobs.sh to be incorrectly written (#3075)
Expand Down
1 change: 1 addition & 0 deletions models/ed/NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Fixed a bug in `model2netcdf.ed2()` where .nc file connections were being closed multiple times, printing warnings (#3078)
* Fixed a bug affecting the generation of job.sh for runs with many PFTs (#3075)
* Added optional `process_partial` argument to `model2netcdf.ED2()` to allow it to process existing output from failed runs.
* write.config.xml.ED2() wasn't using the <revision> tag in settings correctly (#3080)

# PEcAn.ED2 1.7.2.9000

Expand Down
32 changes: 21 additions & 11 deletions models/ed/R/write.configs.ed.R
Original file line number Diff line number Diff line change
Expand Up @@ -443,18 +443,28 @@ remove.config.ED2 <- function(main.outdir = settings$outdir, settings) {
#' @author David LeBauer, Shawn Serbin, Carl Davidson, Alexey Shiklomanov
write.config.xml.ED2 <- function(settings, trait.values, defaults = settings$constants) {

## Find history file TODO this should come from the database
ed2_package_data <- data(package="PEcAn.ED2", envir = environment())
histfile <- paste0("history.r", settings$model$revision) # set history file name to look for in ed2_package_data
if (histfile %in% ed2_package_data$results[, "Item"]) {
PEcAn.logger::logger.debug(paste0("--- Using ED2 History File: ", histfile))
data(list=histfile, package = 'PEcAn.ED2', envir = environment())
edhistory <- get(histfile)
# TODO this should come from the database

# Internal data sets stored in sysdata.RDA are used to override defaults in
# config.xml. This code looks for a history dataset that matches the
# "revision" number for ED2 set in settings (e.g. PEcAn.ED2:::history.r85) and
# if it doesn't find it, it uses a generic file (PEcAn.ED2:::history). To add
# a new history file, add the .csv file to models/ed/data-raw and run the
# sysdata.R script in that folder

if(is.null(settings$model$revision)) {
PEcAn.logger::logger.debug("--- Using Generic ED2 History File")
edhistory <- history
} else {
PEcAn.logger::logger.debug("--- Using Generic ED2 History File: history.csv")
histfile <- "history"
data(list=histfile, package = 'PEcAn.ED2', envir = environment())
edhistory <- get(histfile)
histfile <- paste0("history.r", settings$model$revision)
edhistory <- try(eval(str2lang(histfile)), silent = TRUE)
}

if(inherits(edhistory, "try-error")) {
PEcAn.logger::logger.debug("--- Using Generic ED2 History File")
edhistory <- history
} else {
PEcAn.logger::logger.debug(paste0("--- Using ED2 History File: ", histfile))
}

edtraits <- names(edhistory)
Expand Down
73 changes: 73 additions & 0 deletions models/ed/tests/testthat/test.write.configs.ed.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,49 @@
## # which accompanies this distribution, and is available at
## # http://opensource.ncsa.illinois.edu/license.html
## #-------------------------------------------------------------------------------
# dummy trait values
trait.values <-
list(
SetariaWT = structure(
list(
mort2 = 19.9551305310619,
growth_resp_factor = 0.271418338660799,
leaf_turnover_rate = 4.08633744111248,
leaf_width = 4.16095405673501,
nonlocal_dispersal = 0.208572992916628,
fineroot2leaf = 2.25017242716454,
root_turnover_rate = 0.527523622000746,
seedling_mortality = 0.949939872416094,
stomatal_slope = 4.07086860946459,
quantum_efficiency = 0.0565042189665225,
Vcmax = 22.3047025944851,
r_fract = 0.313812660341759,
cuticular_cond = 12992.9906222683,
root_respiration_rate = 5.48040042748477,
Vm_low_temp = 10.0000004842057,
SLA = 40.1495401375622
),
row.names = "50",
class = "data.frame"
),
env = structure(
list(),
.Names = character(0),
row.names = "NA",
class = "data.frame"
)
)

defaults <-
list(
pft = list(
name = "SetariaWT",
ed2_pft_number = "1",
outdir = "/home/ericrscott/model-vignettes/ED2/testoutput/two_pfts/outdir//pft/SetariaWT",
posteriorid = 9000001416
)
)

test_that("convert.samples.ED works as expected",{
testthat::local_edition(3)
expect_equal(convert.samples.ED(c("Vcmax" = 1))[["Vcmax"]],
Expand Down Expand Up @@ -141,6 +184,36 @@ test_that("New ED2IN tags get added at bottom of file", {
})


test_that("write.config.xml.ED2() uses correct history file", {
#1. read in pecan.xml in data/pecan_checked.xml
settings <- PEcAn.settings::read.settings("data/pecan_checked.xml")
#for debugging:
# settings <- PEcAn.settings::read.settings("models/ed/tests/testthat/data/pecan_checked.xml")

#2. Set rundir to tempdir
rundir <- tempfile()
dir.create(rundir)
on.exit(unlink(rundir, recursive = TRUE))
settings$rundir <- rundir
run.id <- "ENS-00001-76"
dir.create(file.path(rundir, run.id))
#3. set revision to 81
settings$model$revision <- "81"

x <- capture.output(
write.config.xml.ED2(
settings = settings,
trait.values = trait.values,
defaults = defaults
),
type = "message"
)

expect_true(any(stringr::str_detect(x, "history.r81")))

})


## test_that("remove.configs.ED2 works with remote host",{
## settings <- list(outdir = "/tmp/",
## run = list(host = list(name = "ebi-cluster.igb.illinois.edu",
Expand Down

0 comments on commit 1898fc2

Please sign in to comment.