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

Bugfixes for model2netcdf.ED2() #2985

Merged
merged 66 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from 64 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
d065aad
remove deprecated `context()`
Aariq Jul 21, 2022
d752058
replace test ED2 output
Aariq Jul 22, 2022
face8ad
attempt to get test working
Aariq Jul 22, 2022
d60fd44
add r-universe to streamline installation
Aariq Jul 22, 2022
bed9564
added some basic tests for mstmipvar
Aariq Jul 25, 2022
d6fee55
fix test to use correct inputs
Aariq Jul 25, 2022
8a271e1
refactor mstmipvar to remove for-loop and deal with #2981
Aariq Jul 25, 2022
59cbb68
update documentation
Aariq Jul 25, 2022
8a08d8f
bump roxygen2 version
Aariq Jul 25, 2022
fbbee18
dots aren't used
Aariq Jul 25, 2022
8bcd163
add test that function runs without ERROR and fix test for filenames
Aariq Jul 25, 2022
000ad00
add a settings object for testing
Aariq Jul 25, 2022
78bac32
existing tests are working again (closes #1329)
Aariq Jul 25, 2022
0b2a122
add check that settings inerits "Settings", replace missing args from…
Aariq Jul 25, 2022
4a55126
whoops, there were still some skipped tests (#1329)
Aariq Jul 25, 2022
8b7d871
use testthat 3e
Aariq Jul 25, 2022
898f5ca
fix test for units (#1329)
Aariq Jul 25, 2022
bf7ac11
only use 3rd edition of testthat in specific tests
Aariq Jul 25, 2022
251cdd7
move logic for extracting PFT names and numbers to helper fun
Aariq Jul 25, 2022
89e67af
replace test ED2 output with one that has an E file
Aariq Jul 25, 2022
cff37de
fix tests for read_E_files
Aariq Jul 25, 2022
6958b0e
fix another test
Aariq Jul 25, 2022
461b637
styling code and comments
Aariq Jul 25, 2022
f9385b4
add test for PFTs
Aariq Jul 25, 2022
7615473
comment styling
Aariq Jul 25, 2022
db5cc47
fix test setup
Aariq Jul 25, 2022
6b12d72
update documentation
Aariq Jul 25, 2022
aae09d1
update changelog
Aariq Jul 25, 2022
86696dd
add self as contributor
Aariq Jul 25, 2022
2b0dee5
fix tests on CI?
Aariq Jul 25, 2022
dfb92bb
modify DESCRIPTION to add r-universe and enable testthat 3e
Aariq Jul 25, 2022
6661a1c
compress ED2 output
Aariq Jul 26, 2022
ab7e600
fix warnings about global variables
Aariq Jul 26, 2022
37ac81c
create .Rbuildignore
Aariq Jul 26, 2022
60bbd46
revert Roxygen
Aariq Jul 26, 2022
76ba245
remove deprecated testthat stuff
Aariq Jul 26, 2022
2432ea4
Merge branch 'develop' into model2netcdf-ed2
Aariq Jul 26, 2022
e0462ef
whoops!
Aariq Jul 26, 2022
3be7ba5
Merged upstream/model2netcdf-ed2 into model2netcdf-ed2
Aariq Jul 26, 2022
cbfa0f9
use usethis::use_raw_data() for pftmapping.csv
Aariq Jul 26, 2022
9ab9df1
fix pfts_nums -> pft_nums
Aariq Jul 26, 2022
1ad17c3
revert to testthat 2nd edition
Aariq Jul 26, 2022
548677d
run generate_dependencies.R
Aariq Jul 26, 2022
dd48996
fix warning: Variables with usage in documentation object 'pftmapping…
Aariq Jul 26, 2022
f12c396
silence warning
Aariq Jul 26, 2022
ad92d74
revert change to roxygen version
Aariq Jul 26, 2022
56b3f09
Merged upstream/develop into model2netcdf-ed2
Aariq Jul 28, 2022
8cf9f1c
cleanup tempdir after test
Aariq Jul 28, 2022
482cf5a
use withr for test fixtures
Aariq Jul 29, 2022
34b2d35
don't track .Rprofile
Aariq Jul 29, 2022
19b829b
Merge branch 'develop' into model2netcdf-ed2
Aariq Aug 1, 2022
7c6f31a
run generate dependencies script
Aariq Aug 1, 2022
8c03709
Merge branch 'develop' into model2netcdf-ed2
Aariq Aug 1, 2022
7cb0e88
Merge branch 'develop' into model2netcdf-ed2
dlebauer Aug 4, 2022
02b5ad1
move internal data to data-raw
Aariq Aug 4, 2022
63e0ba0
script to write internal data to sysdata.rda
Aariq Aug 4, 2022
2f6f9aa
resolve warning about best practices for data()
Aariq Aug 4, 2022
e6a5331
Merge branch 'develop' into move-ed2-data
Aariq Aug 4, 2022
1c1ff4f
Merge branch 'develop' into model2netcdf-ed2
Aariq Aug 4, 2022
e5e8e68
Merge branch 'develop' into move-ed2-data
dlebauer Aug 5, 2022
1903181
Merge branch 'develop' into model2netcdf-ed2
dlebauer Aug 8, 2022
a0db7d0
Merge branch 'develop' into move-ed2-data
dlebauer Aug 8, 2022
926a8fd
Merge branch 'model2netcdf-ed2' into move-ed2-data
Aariq Aug 8, 2022
3018fb3
Merge pull request #3006 from PecanProject/move-ed2-data
dlebauer Aug 8, 2022
e36bad2
Update models/ed/R/model2netcdf.ED2.R
dlebauer Aug 9, 2022
75421b2
update documentation
Aariq Aug 10, 2022
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,6 @@ contrib/modellauncher/modellauncher

# don't checkin python venv used by CodeSpaces
/.venv*

# don't track project level .Rprofile
.Rprofile
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ We are slowly change the license from NCSA opensource to BSD-3 to help with publ
- rstudio was not working behind traefik.
- plots now work in docker containers
- when specifying diferent rstudio user, dev setup would mount pecan folder in wrong path.
- bugs in `model2ncdf.ED2()` that were causing it to both error and also only
convert data for a single PFT fixed (#1329, #2974, #2981)
- Code cleanup in PEcAn.MA to protect against global namespace pollution (#2965, #2973; @nanu1605)
- Fixed vignettes and cleaned up internal warnings in PEcAn.DB (#2966, #2971; @nanu1605).
- Updated unit conversions throughout PEcAn to use the `units` R package
Expand Down
4 changes: 4 additions & 0 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ authors:
- given-names: Yan Zhao
- affiliation: Uber
given-names: Jing Xia
- given-names: Eric R. Scott
affiliation: University of Arizona
orcid: 'https://orcid.org/0000-0002-7430-7879'

preferred-citation:
type: article
title: Facilitating feedbacks between field measurements and ecosystem models
Expand Down
48 changes: 26 additions & 22 deletions base/utils/R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,44 +27,48 @@
##' @param silent logical: suppress log messages about missing variables?
##' @return ncvar based on MstMIP definition
##' @author Rob Kooper
mstmipvar <- function(name, lat = NA, lon = NA, time = NA, nsoil = NA, silent = FALSE) {
mstmipvar <- function(name, lat = NULL, lon = NULL, time = NULL, nsoil = NULL, silent = FALSE) {
nc_var <- PEcAn.utils::standard_vars[PEcAn.utils::standard_vars$Variable.Name == name, ]
dims <- list()

if (nrow(nc_var) == 0) {
if (!silent) {
PEcAn.logger::logger.info("Don't know about variable", name, " in standard_vars in PEcAn.utils")
}
if (is.na(time)) {
time <- ncdf4::ncdim_def(name = "time", units = "days since 1900-01-01 00:00:00",
vals = 1:365, calendar = "standard", unlim = TRUE)
if (is.null(time)) {
time <-
ncdf4::ncdim_def(
name = "time",
units = "days since 1900-01-01 00:00:00",
vals = 1:365,
calendar = "standard",
unlim = TRUE
)
}
return(ncdf4::ncvar_def(name, "", list(time), -999, name))
}
for (i in 1:4) {
vd <- nc_var[[paste0("dim", i)]]
if (vd == "lon" && !is.na(lon)) {
dims[[length(dims) + 1]] <- lon
} else if (vd == "lat" && !is.na(lat)) {
dims[[length(dims) + 1]] <- lat
} else if (vd == "time" && !is.na(time)) {
dims[[length(dims) + 1]] <- time
} else if (vd == "nsoil" && !is.na(nsoil)) {
dims[[length(dims) + 1]] <- nsoil
} else if (is.na(vd)) {
# skip
} else {
if (!silent) {
PEcAn.logger::logger.info("Don't know dimension for", vd, "for variable", name)
}

var_dims <- nc_var[paste0("dim", 1:4)]
pos_dims <- c("lon", "lat", "time", "nsoil")

#check for missing dimensions
no_dims <- pos_dims[!var_dims %in% pos_dims]
if (!silent) {
if (length(no_dims) > 0) {
PEcAn.logger::logger.info("Don't know dimension(s)", no_dims, "for variable", name)
}
}

#replace dim names with values
var_dims <- var_dims[var_dims %in% pos_dims]
var_dims <- lapply(var_dims, function(x) eval(str2lang(x))) #converts character values in `var_dims` to corresponding R objects
dims <- var_dims[!sapply(var_dims, is.null)] #get rid of NULL elements

ncvar <- ncdf4::ncvar_def(name, as.character(nc_var$Units), dims, -999)
if (nc_var$Long.name != "na") {
ncvar$longname <- as.character(nc_var$Long.name)
}
return(ncvar)
} # mstimipvar
}


#--------------------------------------------------------------------------------------------------#
Expand Down
9 changes: 8 additions & 1 deletion base/utils/man/mstmipvar.Rd

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

50 changes: 50 additions & 0 deletions base/utils/tests/testthat/test.utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,53 @@ test_that("tryl returns FALSE if error, else true ", {
expect_true(tryl(1+1))
expect_true(!tryl(log("a")))
})

test_that("mstmipvar works with defaults", {
expect_s3_class(mstmipvar("NPP"), "ncvar4")
})

test_that("mstmipvar works with args specified", {
lat <-
ncdf4::ncdim_def(
name = "lat",
longname = "station_latitude",
units = "degrees_north",
vals = 40,
unlim = FALSE
)
lon <-
ncdf4::ncdim_def(
name = "lon",
longname = "station_longitude",
units = "degrees_east",
vals = -80,
unlim = FALSE
)
time <-
ncdf4::ncdim_def(
name = "time",
units = "days since 1900-01-01 00:00:00",
vals = 30798:44765,
calendar = "standard",
unlim = TRUE
)
nsoil <-
ncdf4::ncdim_def(
name = "SoilLayerMidpoint",
longname = "SoilLayerMidpoint",
units = "meters",
vals = c(-1.75,-1.25,-0.9,-0.7,-0.5,-0.3,-0.15,-0.075, 0),
unlim = FALSE
)

expect_s3_class(mstmipvar("NPP", lat = lat, lon = lon, time = time, nsoil = nsoil),
"ncvar4")
})

# doesn't work because PEcAn.logger doesn't use message()
# test_that("mstmipvar prints message with unknown var", {
# expect_message(
# mstmipvar("banana"),
# "Don't know about variable banana in standard_vars in PEcAn.utils"
# )
# })
4 changes: 4 additions & 0 deletions models/ed/.Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
^Dockerfile$
^model_info\.json$
^scripts$
^data-raw$
Aariq marked this conversation as resolved.
Show resolved Hide resolved
13 changes: 11 additions & 2 deletions models/ed/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ Authors@R: c(person("Mike", "Dietze", role = c("aut", "cre"),
email = "[email protected]"),
person("Alexey", "Shiklomanov", role = c("aut"),
email = "[email protected]"),
person("Eric R.", "Scott", role = c("ctb"),
email = "[email protected]",
comment = c(ORCID = "0000-0002-7430-7879")),
person("University of Illinois, NCSA", role = c("cph")))
Author: David LeBauer, Mike Dietze, Xiaohui Feng, Dan Wang, Carl
Davidson, Rob Kooper, Shawn Serbin, Alexey Shiklomanov
Expand All @@ -28,6 +31,8 @@ Description: The Predictive Ecosystem Carbon Analyzer (PEcAn) is a scientific
streamline the interaction between data and models, and to improve the
efficacy of scientific investigation. This package provides functions to
link the Ecosystem Demography Model, version 2, to PEcAn.
Depends:
R (>= 2.10)
infotroph marked this conversation as resolved.
Show resolved Hide resolved
Imports:
PEcAn.data.atmosphere,
PEcAn.data.land,
Expand All @@ -49,11 +54,15 @@ Imports:
utils,
XML (>= 3.98-1.4)
Suggests:
testthat (>= 1.0.2)
testthat (>= 1.0.2),
withr
Additional_repositories: https://pecanproject.r-universe.dev/
License: BSD_3_clause + file LICENSE
Copyright: Authors
LazyLoad: yes
LazyData: FALSE
LazyData: true
Encoding: UTF-8
RoxygenNote: 7.1.2
Roxygen: list(markdown = TRUE)
Config/testthat/edition: 2

12 changes: 12 additions & 0 deletions models/ed/R/data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#' Mapping of PEcAn PFT names to ED2 PFT numbers
#'
#' A dataset matching PEcAn PFT names to ED PFT numbers.
#'
#' @format A data frame with 73 rows and 2 variables:
#' \describe{
#' \item{PEcAn}{PEcAn PFT names}
#' \item{ED}{ED2 PFT numbers}
#' ...
#' }
#' @source \url{https://github.com/EDmodel/ED2/wiki/Plant-functional-types}
"pftmapping"
Loading