write n2o and ch4 flux variables to SIPNET netCDF#3816
Conversation
| outdir <- withr::local_tempdir(pattern = "sipnet_out_") | ||
| rundir <- withr::local_tempdir(pattern = "sipnet_run_") | ||
|
|
||
| # minimal sipnet.param — model2netcdf only reads leafCSpWt for LAI |
There was a problem hiding this comment.
Not for this PR, but: Given we go to the trouble of reading leafCSpWt from the param file, why does it get combined with a constant leafC <- 0.48 instead of reading cFracLeaf from the very next param line?
| c("plantWoodInit\t30000\t0\t6600\t14000\t200", | ||
| "leafCSpWt\t32\t0\t13\t500\t0"), |
There was a problem hiding this comment.
Sipnet v2 drops the extra numeric columns and I would hope it's safe to do that here too:
| c("plantWoodInit\t30000\t0\t6600\t14000\t200", | |
| "leafCSpWt\t32\t0\t13\t500\t0"), | |
| c("plantWoodInit\t30000", | |
| "leafCSpWt\t32"), |
There was a problem hiding this comment.
Also: is the plantWoodInit line needed for this test to work?
There was a problem hiding this comment.
no, plantWoodInit is not needed. model2netcdf.SIPNET only reads leafCSpWt from the param file. I will trim sipnet.param down to just that one line
| nc_file <- file.path(real_outdir, "2002.nc") | ||
| expect_true(file.exists(nc_file)) | ||
|
|
||
| nc <- ncdf4::nc_open(nc_file) | ||
| on.exit(ncdf4::nc_close(nc), add = TRUE) | ||
| vars <- names(nc$var) |
There was a problem hiding this comment.
Tests like this seem like a great use case for the netcdf2df() I proposed in #3788 (which isn't merged yet, so no foul for not using it here!) -- one line to slurp the test file into a df instead of needing to remember these incantations every time.
| n2o <- as.numeric(ncdf4::ncvar_get(nc, "N2O_flux")) | ||
| ch4 <- as.numeric(ncdf4::ncvar_get(nc, "CH4_flux")) | ||
| gpp <- as.numeric(ncdf4::ncvar_get(nc, "GPP")) |
There was a problem hiding this comment.
All of these should be stored as numeric already, and if they aren't then we want to know (as the expect_equal below will do if we skip coercion)
| n2o <- as.numeric(ncdf4::ncvar_get(nc, "N2O_flux")) | |
| ch4 <- as.numeric(ncdf4::ncvar_get(nc, "CH4_flux")) | |
| gpp <- as.numeric(ncdf4::ncvar_get(nc, "GPP")) | |
| n2o <- ncdf4::ncvar_get(nc, "N2O_flux") | |
| ch4 <- ncdf4::ncvar_get(nc, "CH4_flux") | |
| gpp <- ncdf4::ncvar_get(nc, "GPP") |
There was a problem hiding this comment.
right! that these should already be numeric , as.numeric() was masking the type check. But ncvar_get() returns a 1-D array (carries dim attribute), so bare comparison with rep() fails on attributes. Switched to as.vector(), which strips the array dimension without coercing type
c00644b
Description
Motivation and Context
Review Time Estimate
Types of changes
Checklist: