Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/DongchenZ/pecan into dev…
Browse files Browse the repository at this point in the history
…elop
  • Loading branch information
Dongchen Zhang committed Aug 25, 2023
2 parents 59eeadf + 892b379 commit 957d72c
Show file tree
Hide file tree
Showing 22 changed files with 150 additions and 70 deletions.
2 changes: 1 addition & 1 deletion modules/data.atmosphere/R/download.AmerifluxLBL.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
##' to the given outfolder. Details about amf_download_base function can be found here:
##' https://github.com/chuhousen/amerifluxr/blob/master/R/amf_download_base.R
##'
##' Uses Ameirflux LBL JSON API to download met data from Ameriflux towers in CSV format
##' Uses Ameriflux LBL JSON API to download met data from Ameriflux towers in CSV format
##'
##' @export
##' @param sitename the Ameriflux ID of the site to be downloaded, used as file name prefix.
Expand Down
2 changes: 1 addition & 1 deletion modules/data.atmosphere/R/download.ERA5.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ download.ERA5.old <- function(outfolder, start_date, end_date, lat.in, lon.in,
"This function is an incomplete prototype! Use with caution!"
)

need_packages("reticulate")
PEcAn.utils::need_packages("reticulate")

if (!is.null(reticulate_python)) {
reticulate::use_python(reticulate_python)
Expand Down
2 changes: 1 addition & 1 deletion modules/data.atmosphere/R/download.US_WCr.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
##'
##' @section General Description:
##' Obtains data from Ankur Desai's Willow Creek flux tower, and selects certain variables (NEE and LE) to return
##' Data is retruned at the given timestep in the given range.
##' Data is returned at the given timestep in the given range.
##'
##' This data includes information on a number of flux variables.
##'
Expand Down
2 changes: 1 addition & 1 deletion modules/data.atmosphere/R/download.US_Wlef.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
##'
##' @section General Description:
##' Obtains data from Ankur Desai's WLEF/ Parks Fall flux tower, and selects certain variables (NEE and LE) to return
##' Data is retruned at the given timestep in the given range.
##' Data is returned at the given timestep in the given range.
##'
##' This data includes information on a number of flux variables.
##'
Expand Down
2 changes: 1 addition & 1 deletion modules/data.atmosphere/R/extract_ERA5.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#' @param outfolder Path to directory where nc files need to be saved.
#' @param in.prefix initial portion of the filename that does not vary by date. Does not include directory; specify that as part of in.path.
#' @param newsite site name.
#' @param vars variables to be extarcted. If NULL all the variables will be returned.
#' @param vars variables to be extracted. If NULL all the variables will be returned.
#' @param overwrite Logical if files needs to be overwritten.
#' @details For the list of variables check out the documentation at \link{https://confluence.ecmwf.int/display/CKB/ERA5+data+documentation#ERA5datadocumentation-Spatialgrid}
#'
Expand Down
2 changes: 1 addition & 1 deletion modules/data.atmosphere/R/extract_local_CMIP5.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
##' @param date.origin (optional) specify the date of origin for timestamps in the files being read.
##' If NULL defaults to 1850 for historical simulations (except MPI-ESM-P) and
##' 850 for p1000 simulations (plus MPI-ESM-P historical). Format: YYYY-MM-DD
##' @param adjust.pr - adjustment factor fore preciptiation when the extracted values seem off
##' @param adjust.pr - adjustment factor fore precipitation when the extracted values seem off
##' @param overwrite logical. Download a fresh version even if a local file with the same name already exists?
##' @param verbose logical. to control printing of debug info
##' @param ... Other arguments, currently ignored
Expand Down
2 changes: 1 addition & 1 deletion modules/data.atmosphere/R/extract_local_NLDAS.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
##' @description This function extracts NLDAS data from grids that have been downloaded and stored locally.
##' Once upon a time, you could query these files directly from the internet, but now they're
##' behind a tricky authentication wall. Files are saved as a netCDF file in CF conventions.
##' These files are ready to be used in the general PEcAn workflow or fed into the downscalign
##' These files are ready to be used in the general PEcAn workflow or fed into the downscaling
##' workflow.
# -----------------------------------
# Parameters
Expand Down
4 changes: 2 additions & 2 deletions modules/data.atmosphere/R/metgapfill.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
##' Future version will first downscale and fill with NARR, then REddyProc
##'
##' @export
##' @param in.path location on disk where inputs are stord
##' @param in.path location on disk where inputs are stored
##' @param in.prefix prefix of input and output files
##' @param outfolder location on disk where outputs will be stored
##' @param start_date the start date of the data to be downloaded (will only use the year part of the date)
##' @param end_date the end date of the data to be downloaded (will only use the year part of the date)
##' @param overwrite should existing files be overwritten
##' @param verbose should the function be very verbose
##' @param lst is timezone offset from UTC, if timezone is available in time:units atribute in file, it will use that, default is to assume UTC
##' @param lst is timezone offset from UTC, if timezone is available in time:units attribute in file, it will use that, default is to assume UTC
##' @author Ankur Desai
metgapfill <- function(in.path, in.prefix, outfolder, start_date, end_date, lst = 0,
overwrite = FALSE, verbose = FALSE, ...) {
Expand Down
22 changes: 0 additions & 22 deletions modules/data.atmosphere/R/need_packages.R

This file was deleted.

2 changes: 1 addition & 1 deletion modules/data.atmosphere/R/tdm_predict_subdaily_met.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
##' @param ens.labs - vector containing the labels (suffixes) for each ensemble member; this allows you to add to your
##' ensemble rather than overwriting with a default naming scheme
##' @param resids - logical stating whether to pass on residual data or not
##' @param adjust.pr - adjustment factor fore preciptiation when the extracted values seem off
##' @param adjust.pr - adjustment factor fore precipitation when the extracted values seem off
##' @param force.sanity - (logical) do we force the data to meet sanity checks?
##' @param sanity.tries - how many time should we try to predict a reasonable value before giving up? We don't want to end up in an infinite loop
##' @param overwrite logical: replace output file if it already exists?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# -----------------------------------
##' @param dat.train - training data generated by tdm_nc2dat.train.R
##' @param n.beta - number of betas to generate
##' @param resids - whether or not to propogate residuals, set to FALSE
##' @param resids - whether or not to propagate residuals, set to FALSE
##' @param parallel - whether or not to run in parallel. this is a feature
##' still being worked on, set to FALSE
##' @param n.cores - number of cores to use parallel processing on, set to NULL
Expand Down
129 changes: 129 additions & 0 deletions modules/data.atmosphere/inst/integrationTests/test.download.CRUNCEP.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
library(testthat)
library(ncdf4)

test_download_CRUNCEP <- function(start_date, end_date, lat.in, lon.in, method, maxErrors, sleep) {
# putting logger to debug mode
PEcAn.logger::logger.setUseConsole(TRUE, FALSE)
on.exit(PEcAn.logger::logger.setUseConsole(TRUE, TRUE), add = TRUE)
PEcAn.logger::logger.setLevel("DEBUG")

# mocking functions
mockery::stub(PEcAn.DB::convert_input, 'dbfile.input.check', data.frame())
mockery::stub(PEcAn.DB::convert_input, 'db.query', data.frame(id = 1))

withr::with_dir(tempdir(), {
tmpdir <- getwd()
PEcAn.DB::convert_input(
input.id = NA,
outfolder = tmpdir,
formatname = NULL,
mimetype = NULL,
site.id = 1,
start_date = start_date,
end_date = end_date,
pkg = 'PEcAn.data.atmosphere',
fcn = 'download.CRUNCEP',
con = NULL,
host = data.frame(name = "localhost"),
browndog = NULL,
write = FALSE,
lat.in = lat.in,
lon.in = lon.in,
method = method,
maxErrors = maxErrors,
sleep = sleep
)

test_that("File exists at desired location", {
# Set the desired file path
file_path <- paste0(tmpdir, "/cruncep_landwater_mask.nc")

# Check if file exists at desired location
expect_true(file.exists(file_path))
})

test_that("NetCDF file contains lat and lon variables", {

mask_nc <- ncdf4::nc_open(paste0(tmpdir, "/cruncep_landwater_mask.nc"))
on.exit(ncdf4::nc_close(mask_nc), add = TRUE)
expect_true("land_water_mask" %in% names(mask_nc$var))

# Check the dimensions of "land_water_mask" variable
expect_equal(mask_nc$var$land_water_mask$dim[[1]]$name, "lon")
expect_equal(mask_nc$var$land_water_mask$dim[[2]]$name, "lat")
})

test_that("All the required files are downloaded and stored at desired location", {
# Cached raw CRUNCEP files
expect_true(file.exists(paste0(tmpdir, "/cruncep-raw-2000-40--88-tair.nc")))
expect_true(file.exists(paste0(tmpdir, "/cruncep-raw-2000-40--88-lwdown.nc")))
expect_true(file.exists(paste0(tmpdir, "/cruncep-raw-2000-40--88-press.nc")))
expect_true(file.exists(paste0(tmpdir, "/cruncep-raw-2000-40--88-swdown.nc")))
expect_true(file.exists(paste0(tmpdir, "/cruncep-raw-2000-40--88-uwind.nc")))
expect_true(file.exists(paste0(tmpdir, "/cruncep-raw-2000-40--88-vwind.nc")))
expect_true(file.exists(paste0(tmpdir, "/cruncep-raw-2000-40--88-qair.nc")))
expect_true(file.exists(paste0(tmpdir, "/cruncep-raw-2000-40--88-rain.nc")))

# CRUNCEP file
expect_true(file.exists(paste0(tmpdir, "/CRUNCEP.2000.nc")))
})

test_that("All cruncep raw data files have the correct variable units", {
nc <- nc_open(paste0(tmpdir, "/cruncep-raw-2000-40--88-tair.nc"))
expect_equal(nc$var$tair$units, "K")
nc_close(nc)

nc <- nc_open(paste0(tmpdir, "/cruncep-raw-2000-40--88-lwdown.nc"))
expect_equal(nc$var$lwdown$units, "W/m2")
nc_close(nc)

nc <- nc_open(paste0(tmpdir, "/cruncep-raw-2000-40--88-press.nc"))
expect_equal(nc$var$press$units, "Pa")
nc_close(nc)

nc <- nc_open(paste0(tmpdir, "/cruncep-raw-2000-40--88-swdown.nc"))
expect_equal(nc$var$swdown$units, "W/m2")
nc_close(nc)

nc <- nc_open(paste0(tmpdir, "/cruncep-raw-2000-40--88-uwind.nc"))
expect_equal(nc$var$uwind$units, "m/s")
nc_close(nc)

nc <- nc_open(paste0(tmpdir, "/cruncep-raw-2000-40--88-vwind.nc"))
expect_equal(nc$var$vwind$units, "m/s")
nc_close(nc)

nc <- nc_open(paste0(tmpdir, "/cruncep-raw-2000-40--88-qair.nc"))
expect_equal(nc$var$qair$units, "g/g")
nc_close(nc)

nc <- nc_open(paste0(tmpdir, "/cruncep-raw-2000-40--88-rain.nc"))
expect_equal(nc$var$rain$units, "mm/6h")
nc_close(nc)
})

test_that("Combined data file has the correct variable units", {
nc <- nc_open(paste0(tmpdir, "/CRUNCEP.2000.nc"))
expect_equal(nc$var$air_temperature$units, "Kelvin")
expect_equal(nc$var$surface_downwelling_longwave_flux_in_air$units, "W/m2")
expect_equal(nc$var$air_pressure$units, "Pascal")
expect_equal(nc$var$surface_downwelling_shortwave_flux_in_air$units, "W/m2")
expect_equal(nc$var$eastward_wind$units, "m/s")
expect_equal(nc$var$northward_wind$units, "m/s")
expect_equal(nc$var$specific_humidity$units, "g/g")
expect_equal(nc$var$precipitation_flux$units, "kg/m2/s")
nc_close(nc)
})
})
}


test_download_CRUNCEP(
start_date = "2000-01-01",
end_date = "2000-12-31",
lat.in = 40,
lon.in = -88,
method = "ncss",
maxErrors = 10,
sleep = 2
)
2 changes: 1 addition & 1 deletion modules/data.atmosphere/man/download.AmerifluxLBL.Rd

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

2 changes: 1 addition & 1 deletion modules/data.atmosphere/man/download.US_WCr.Rd

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

2 changes: 1 addition & 1 deletion modules/data.atmosphere/man/download.US_Wlef.Rd

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

2 changes: 1 addition & 1 deletion modules/data.atmosphere/man/extract.local.CMIP5.Rd

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

2 changes: 1 addition & 1 deletion modules/data.atmosphere/man/extract.local.NLDAS.Rd

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

2 changes: 1 addition & 1 deletion modules/data.atmosphere/man/extract.nc.ERA5.Rd

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

4 changes: 2 additions & 2 deletions modules/data.atmosphere/man/metgapfill.Rd

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

27 changes: 0 additions & 27 deletions modules/data.atmosphere/man/need_packages.Rd

This file was deleted.

2 changes: 1 addition & 1 deletion modules/data.atmosphere/man/predict_subdaily_met.Rd

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

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

0 comments on commit 957d72c

Please sign in to comment.