From d079c794cab02a1cd1fdc509e14234e412111d4f Mon Sep 17 00:00:00 2001 From: meetagrawal09 Date: Thu, 27 Jul 2023 19:56:16 +0530 Subject: [PATCH 01/12] added tests for land_water_mask --- .../test.integration.download.CRUNCEP.R | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 modules/data.atmosphere/tests/testthat/test.integration.download.CRUNCEP.R diff --git a/modules/data.atmosphere/tests/testthat/test.integration.download.CRUNCEP.R b/modules/data.atmosphere/tests/testthat/test.integration.download.CRUNCEP.R new file mode 100644 index 00000000000..a81cd2387c3 --- /dev/null +++ b/modules/data.atmosphere/tests/testthat/test.integration.download.CRUNCEP.R @@ -0,0 +1,47 @@ + +skip_on_ci() +skip_if_offline() + +tmpdir <- tempfile(pattern = "CRUNCEPdata") +dir.create(tmpdir) +teardown(unlink(tmpdir, recursive = TRUE)) + +outfolder <- tmpdir +start_date <- "2000-01-01" +end_date <- "2000-12-31" +lat.in <- 40 +lon.in <- -88 + +lat.in <- as.numeric(lat.in) +lon.in <- as.numeric(lon.in) + +lat_grid <- floor(2 * (90 - lat.in)) + 1 +lon_grid <- floor(2 * (lon.in + 180)) + 1 + +maskfile <- file.path(outfolder, "cruncep_landwater_mask.nc") +mask_url <- paste0( + "https://thredds.daac.ornl.gov/thredds/ncss/ornldaac/1220/", + "mstmip_driver_global_hd_landwatermask_v1.nc4", + "?var=land_water_mask&disableLLSubset=on&disableProjSubset=on&horizStride=1&", + "accept=netcdf" +) +utils::download.file(mask_url, maskfile) + +mask_nc <- ncdf4::nc_open(maskfile) +on.exit(ncdf4::nc_close(mask_nc), add = TRUE) + +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", { + 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") +}) \ No newline at end of file From 28b056f48c859c15d48a07200037e21fda8b01d0 Mon Sep 17 00:00:00 2001 From: meetagrawal09 Date: Fri, 28 Jul 2023 20:27:43 +0530 Subject: [PATCH 02/12] moved tests --- .../integrationTests/test.download.CRUNCEP.R | 246 ++++++++++++++++++ .../test.integration.download.CRUNCEP.R | 47 ---- 2 files changed, 246 insertions(+), 47 deletions(-) create mode 100644 modules/data.atmosphere/inst/integrationTests/test.download.CRUNCEP.R delete mode 100644 modules/data.atmosphere/tests/testthat/test.integration.download.CRUNCEP.R diff --git a/modules/data.atmosphere/inst/integrationTests/test.download.CRUNCEP.R b/modules/data.atmosphere/inst/integrationTests/test.download.CRUNCEP.R new file mode 100644 index 00000000000..8970f867608 --- /dev/null +++ b/modules/data.atmosphere/inst/integrationTests/test.download.CRUNCEP.R @@ -0,0 +1,246 @@ +library(testthat) +library(ncdf4) +library(lubridate) + +skip_on_ci() +skip_if_offline() + +tmpdir <- tempfile(pattern = "CRUNCEPdata") +dir.create(tmpdir) +on.exit(teardown(unlink(tmpdir, recursive = TRUE))) + +outfolder <- tmpdir +start_date <- "2000-01-01" +end_date <- "2000-12-31" +lat.in <- 40 +lon.in <- -88 +method <- "ncss" +overwrite <- TRUE +verbose <- FALSE +maxErrors <- 10 +sleep <- 2 + +lat.in <- as.numeric(lat.in) +lon.in <- as.numeric(lon.in) + +lat_grid <- floor(2 * (90 - lat.in)) + 1 +lon_grid <- floor(2 * (lon.in + 180)) + 1 + +maskfile <- file.path(outfolder, "cruncep_landwater_mask.nc") +mask_url <- paste0( + "https://thredds.daac.ornl.gov/thredds/ncss/ornldaac/1220/", + "mstmip_driver_global_hd_landwatermask_v1.nc4", + "?var=land_water_mask&disableLLSubset=on&disableProjSubset=on&horizStride=1&", + "accept=netcdf" +) +utils::download.file(mask_url, maskfile) + +mask_nc <- ncdf4::nc_open(maskfile) +on.exit(ncdf4::nc_close(mask_nc), add = TRUE) + +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", { + 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") +}) + + +# Set search radius to up to 2 pixels (1 degree) in any direction +mask_minlat <- lat_grid - 2 +mask_minlon <- lon_grid - 2 +mask_lats <- ncdf4::ncvar_get(mask_nc, "lat", start = mask_minlat, count = 5) +mask_lons <- ncdf4::ncvar_get(mask_nc, "lon", start = mask_minlon, count = 5) +mask_values <- ncdf4::ncvar_get( + mask_nc, + "land_water_mask", + start = c(mask_minlon, mask_minlat), + count = c(5, 5) +) + +# Build a lat-lon grid matrix and calculate distance from target coords +mask_grid <- as.matrix(expand.grid(lon = mask_lons, lat = mask_lats)) +mask_igrid <- as.matrix(expand.grid(lon = seq_along(mask_lons), lats = seq_along(mask_lats))) +mask_dist <- (lon.in - mask_grid[, 1])^2 + (lat.in - mask_grid[, 2])^2 + +# Order by increasing distance (closest first) +mask_order <- order(mask_dist) +mask_igrido <- mask_igrid[mask_order,] +on_land <- as.logical(mask_values[mask_igrido]) +igrid <- which(on_land)[1] +if (igrid > 1) { + lon.in.orig <- lon.in + lat.in.orig <- lat.in + lon.in <- mask_grid[mask_order[igrid], 1] + lat.in <- mask_grid[mask_order[igrid], 2] +} + +if (method == "opendap") { + # Convert lat-lon to grid row and column + lat_grid <- floor(2 * (90 - lat.in)) + 1 + lon_grid <- floor(2 * (lon.in + 180)) + 1 +} else if (method == "ncss") { + # Only downloading point data, so only one point + lat_grid <- 1 + lon_grid <- 1 +} + +start_date <- as.POSIXlt(start_date, tz = "UTC") +end_date <- as.POSIXlt(end_date, tz = "UTC") + +# taking out year from dates +start_year <- lubridate::year(start_date) +end_year <- lubridate::year(end_date) + +# year - 2000 +ylist <- seq(start_year, end_year, by = 1) +rows <- length(ylist) +results <- data.frame(file = character(rows), + host = character(rows), + mimetype = character(rows), + formatname = character(rows), + startdate = character(rows), + enddate = character(rows), + dbfile.name = "CRUNCEP", + stringsAsFactors = FALSE) + +var <- tibble::tribble( + ~DAP.name, ~CF.name, ~units, + "tair", "air_temperature", "Kelvin", + "lwdown", "surface_downwelling_longwave_flux_in_air", "W/m2", + "press", "air_pressure", "Pascal", + "swdown", "surface_downwelling_shortwave_flux_in_air", "W/m2", + "uwind", "eastward_wind", "m/s", + "vwind", "northward_wind", "m/s", + "qair", "specific_humidity", "g/g", + "rain", "precipitation_flux", "kg/m2/s" +) + +for (i in seq_len(rows)) { + year <- ylist[i] + ntime <- 1464 + + loc.file <- file.path(outfolder, paste("CRUNCEP", year, "nc", sep = ".")) + results$file[i] <- loc.file + results$host[i] <- "localhost" + results$startdate[i] <- paste0(year, "-01-01 00:00:00") + results$enddate[i] <- paste0(year, "-12-31 23:59:59") + results$mimetype[i] <- "application/x-netcdf" + results$formatname[i] <- "CF Meteorology" + + ## Create dimensions + lat <- ncdf4::ncdim_def(name = "latitude", units = "degree_north", vals = lat.in, create_dimvar = TRUE) + lon <- ncdf4::ncdim_def(name = "longitude", units = "degree_east", vals = lon.in, create_dimvar = TRUE) + + days_elapsed <- (1:ntime) * 6/24 - 3/24 # data are 6-hourly, with timestamp at center of interval + time <- ncdf4::ncdim_def(name = "time", units = paste0("days since ", year, "-01-01T00:00:00Z"), + vals = as.array(days_elapsed), create_dimvar = TRUE, unlim = TRUE) + + dim <- list(lat, lon, time) + + var.list <- list() + dat.list <- list() + + file_name <- "mstmip_driver_global_hd_climate_%1$s_%2$d_v1.nc4" + ## get data off OpenDAP + + dap_base <- switch( + method, + opendap = paste0("https://thredds.daac.ornl.gov/thredds/dodsC/ornldaac/1220/", file_name), + ncss = paste0("https://thredds.daac.ornl.gov/thredds/ncss/grid/ornldaac/1220/", file_name, "/dataset.html") + ) + + for (j in seq_len(nrow(var))) { + current_var <- var$DAP.name[j] + url <- sprintf(dap_base, current_var, year) + print(paste0("Attempting to access file at: ", url)) + if (method == "opendap") { + dap <- PEcAn.utils::retry.func(ncdf4::nc_open(url, verbose=verbose), maxErrors=maxErrors, sleep=sleep) + } else if (method == "ncss") { + ncss_query <- glue::glue( + url, "?", + "var={current_var}&", + "south={lat.in}&", + "west={lon.in}&", + # Add tiny amount to latitude and longitude to satisfy + # non-point condition, but still be within grid cell. + "north={lat.in + 5e-6}&", + "east={lon.in + 5e-6}&", + # Year starts at 00:00:00 and ends at 21:00:00 + "time_start={year}-01-01T00:00:00Z&", + "time_end={year}-12-31T21:00:00Z&", + "accept=netcdf" + ) + # Cache raw CRUNCEP files so that later workflows don't have to download + # them (even if they do have to do some reprocessing). + raw_file <- file.path( + outfolder, + glue::glue("cruncep-raw-{year}-{lat.in}-{lon.in}-{current_var}.nc") + ) + utils::download.file(ncss_query, raw_file) + dap <- ncdf4::nc_open(raw_file) + } + + # confirm that timestamps match + if (dap$dim$time$len != ntime) { + print(paste0("Expected", ntime, "observations, but", url, "contained", dap$dim$time$len)) + } + dap_time <- PEcAn.utils::ud_convert(dap$dim$time$vals, + dap$dim$time$units, + time$units) + if (!isTRUE(all.equal(dap_time, time$vals))){ + print(paste0("Timestamp mismatch.", + "Expected", min(time$vals), '..', max(time$vals), time$units, + "but got", min(dap_time), "..", max(dap_time))) + } + + dat.list[[j]] <- PEcAn.utils::retry.func( + ncdf4::ncvar_get( + dap, + as.character(var$DAP.name[j]), + c(lon_grid, lat_grid, 1), + c(1, 1, ntime) + ), + maxErrors=maxErrors, sleep=sleep) + + var.list[[j]] <- ncdf4::ncvar_def(name = as.character(var$CF.name[j]), + units = as.character(var$units[j]), + dim = dim, + missval = -999, + verbose = verbose) + ncdf4::nc_close(dap) + } + ## change units of precip to kg/m2/s instead of 6 hour accumulated precip + dat.list[[8]] <- dat.list[[8]] / 21600 + + ## put data in new file + loc <- ncdf4::nc_create(filename = loc.file, vars = var.list, verbose = verbose) + for (j in seq_len(nrow(var))) { + ncdf4::ncvar_put(nc = loc, varid = as.character(var$CF.name[j]), vals = dat.list[[j]]) + } + ncdf4::nc_close(loc) +} + +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"))) +}) \ No newline at end of file diff --git a/modules/data.atmosphere/tests/testthat/test.integration.download.CRUNCEP.R b/modules/data.atmosphere/tests/testthat/test.integration.download.CRUNCEP.R deleted file mode 100644 index a81cd2387c3..00000000000 --- a/modules/data.atmosphere/tests/testthat/test.integration.download.CRUNCEP.R +++ /dev/null @@ -1,47 +0,0 @@ - -skip_on_ci() -skip_if_offline() - -tmpdir <- tempfile(pattern = "CRUNCEPdata") -dir.create(tmpdir) -teardown(unlink(tmpdir, recursive = TRUE)) - -outfolder <- tmpdir -start_date <- "2000-01-01" -end_date <- "2000-12-31" -lat.in <- 40 -lon.in <- -88 - -lat.in <- as.numeric(lat.in) -lon.in <- as.numeric(lon.in) - -lat_grid <- floor(2 * (90 - lat.in)) + 1 -lon_grid <- floor(2 * (lon.in + 180)) + 1 - -maskfile <- file.path(outfolder, "cruncep_landwater_mask.nc") -mask_url <- paste0( - "https://thredds.daac.ornl.gov/thredds/ncss/ornldaac/1220/", - "mstmip_driver_global_hd_landwatermask_v1.nc4", - "?var=land_water_mask&disableLLSubset=on&disableProjSubset=on&horizStride=1&", - "accept=netcdf" -) -utils::download.file(mask_url, maskfile) - -mask_nc <- ncdf4::nc_open(maskfile) -on.exit(ncdf4::nc_close(mask_nc), add = TRUE) - -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", { - 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") -}) \ No newline at end of file From d4721c03ef8d5e3b342c9313bd4854733ab4f201 Mon Sep 17 00:00:00 2001 From: meetagrawal09 Date: Thu, 3 Aug 2023 16:58:02 +0530 Subject: [PATCH 03/12] added checks for variable units --- .../integrationTests/test.download.CRUNCEP.R | 38 +++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/modules/data.atmosphere/inst/integrationTests/test.download.CRUNCEP.R b/modules/data.atmosphere/inst/integrationTests/test.download.CRUNCEP.R index 8970f867608..98310cf6545 100644 --- a/modules/data.atmosphere/inst/integrationTests/test.download.CRUNCEP.R +++ b/modules/data.atmosphere/inst/integrationTests/test.download.CRUNCEP.R @@ -2,9 +2,6 @@ library(testthat) library(ncdf4) library(lubridate) -skip_on_ci() -skip_if_offline() - tmpdir <- tempfile(pattern = "CRUNCEPdata") dir.create(tmpdir) on.exit(teardown(unlink(tmpdir, recursive = TRUE))) @@ -125,6 +122,7 @@ var <- tibble::tribble( "rain", "precipitation_flux", "kg/m2/s" ) +## NCSS for (i in seq_len(rows)) { year <- ylist[i] ntime <- 1464 @@ -243,4 +241,38 @@ test_that("All the required files are downloaded and stored at desired location" # 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) }) \ No newline at end of file From 641297aa6ebf06cea1a1fda89fff4a85dc16a26d Mon Sep 17 00:00:00 2001 From: meetagrawal09 Date: Thu, 3 Aug 2023 19:01:08 +0530 Subject: [PATCH 04/12] clean up of some unwanted code segments --- .../integrationTests/test.download.CRUNCEP.R | 38 ++++++++----------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/modules/data.atmosphere/inst/integrationTests/test.download.CRUNCEP.R b/modules/data.atmosphere/inst/integrationTests/test.download.CRUNCEP.R index 98310cf6545..750fb2f9ec4 100644 --- a/modules/data.atmosphere/inst/integrationTests/test.download.CRUNCEP.R +++ b/modules/data.atmosphere/inst/integrationTests/test.download.CRUNCEP.R @@ -101,14 +101,6 @@ end_year <- lubridate::year(end_date) # year - 2000 ylist <- seq(start_year, end_year, by = 1) rows <- length(ylist) -results <- data.frame(file = character(rows), - host = character(rows), - mimetype = character(rows), - formatname = character(rows), - startdate = character(rows), - enddate = character(rows), - dbfile.name = "CRUNCEP", - stringsAsFactors = FALSE) var <- tibble::tribble( ~DAP.name, ~CF.name, ~units, @@ -122,19 +114,12 @@ var <- tibble::tribble( "rain", "precipitation_flux", "kg/m2/s" ) -## NCSS for (i in seq_len(rows)) { year <- ylist[i] ntime <- 1464 loc.file <- file.path(outfolder, paste("CRUNCEP", year, "nc", sep = ".")) - results$file[i] <- loc.file - results$host[i] <- "localhost" - results$startdate[i] <- paste0(year, "-01-01 00:00:00") - results$enddate[i] <- paste0(year, "-12-31 23:59:59") - results$mimetype[i] <- "application/x-netcdf" - results$formatname[i] <- "CF Meteorology" - + ## Create dimensions lat <- ncdf4::ncdim_def(name = "latitude", units = "degree_north", vals = lat.in, create_dimvar = TRUE) lon <- ncdf4::ncdim_def(name = "longitude", units = "degree_east", vals = lon.in, create_dimvar = TRUE) @@ -149,7 +134,6 @@ for (i in seq_len(rows)) { dat.list <- list() file_name <- "mstmip_driver_global_hd_climate_%1$s_%2$d_v1.nc4" - ## get data off OpenDAP dap_base <- switch( method, @@ -169,17 +153,13 @@ for (i in seq_len(rows)) { "var={current_var}&", "south={lat.in}&", "west={lon.in}&", - # Add tiny amount to latitude and longitude to satisfy - # non-point condition, but still be within grid cell. "north={lat.in + 5e-6}&", "east={lon.in + 5e-6}&", - # Year starts at 00:00:00 and ends at 21:00:00 "time_start={year}-01-01T00:00:00Z&", "time_end={year}-12-31T21:00:00Z&", "accept=netcdf" ) - # Cache raw CRUNCEP files so that later workflows don't have to download - # them (even if they do have to do some reprocessing). + raw_file <- file.path( outfolder, glue::glue("cruncep-raw-{year}-{lat.in}-{lon.in}-{current_var}.nc") @@ -188,7 +168,6 @@ for (i in seq_len(rows)) { dap <- ncdf4::nc_open(raw_file) } - # confirm that timestamps match if (dap$dim$time$len != ntime) { print(paste0("Expected", ntime, "observations, but", url, "contained", dap$dim$time$len)) } @@ -275,4 +254,17 @@ test_that("All cruncep raw data files have the correct variable units", { 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) }) \ No newline at end of file From 7ac83a30bbb5df8388f9b4e7da6557950b0860fc Mon Sep 17 00:00:00 2001 From: meetagrawal09 Date: Fri, 18 Aug 2023 19:05:48 +0530 Subject: [PATCH 05/12] removed duplicate need_packages --- modules/data.atmosphere/R/download.ERA5.R | 2 +- modules/data.atmosphere/R/need_packages.R | 22 ---------------------- 2 files changed, 1 insertion(+), 23 deletions(-) delete mode 100644 modules/data.atmosphere/R/need_packages.R diff --git a/modules/data.atmosphere/R/download.ERA5.R b/modules/data.atmosphere/R/download.ERA5.R index 59d1d134a5f..533aeac96fc 100644 --- a/modules/data.atmosphere/R/download.ERA5.R +++ b/modules/data.atmosphere/R/download.ERA5.R @@ -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) diff --git a/modules/data.atmosphere/R/need_packages.R b/modules/data.atmosphere/R/need_packages.R deleted file mode 100644 index eb895acd6e8..00000000000 --- a/modules/data.atmosphere/R/need_packages.R +++ /dev/null @@ -1,22 +0,0 @@ -#' Check if required packages are installed, and throw an informative -#' error if not. -#' -#' @param ... Package names, as characters. Can be passed as -#' individual arguments, character vectors, or any combination thereof. -#' @return `pkgs`, invisibly -#' @author Alexey Shiklomanov -#' @examples -#' PEcAn.data.atmosphere:::need_packages("stats", "methods") # Always works -#' try(PEcAn.data.atmosphere:::need_packages("notapackage")) -need_packages <- function(...) { - pkgs <- unlist(list(...), recursive = TRUE) - have <- vapply(pkgs, requireNamespace, logical(1), quietly = TRUE) - if (any(!have)) { - msg <- sprintf( - "The following packages are required but not installed: %s", - paste0("`", pkgs, "`", collapse = ", ") - ) - PEcAn.logger::logger.severe(msg) - } - invisible(pkgs) -} From 0dbbdf8560e690ac3c71772c059555d846e8ac21 Mon Sep 17 00:00:00 2001 From: meetagrawal09 Date: Fri, 18 Aug 2023 19:09:47 +0530 Subject: [PATCH 06/12] removed .Rd --- modules/data.atmosphere/man/need_packages.Rd | 27 -------------------- 1 file changed, 27 deletions(-) delete mode 100644 modules/data.atmosphere/man/need_packages.Rd diff --git a/modules/data.atmosphere/man/need_packages.Rd b/modules/data.atmosphere/man/need_packages.Rd deleted file mode 100644 index 6a40181ad96..00000000000 --- a/modules/data.atmosphere/man/need_packages.Rd +++ /dev/null @@ -1,27 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/need_packages.R -\name{need_packages} -\alias{need_packages} -\title{Check if required packages are installed, and throw an informative -error if not.} -\usage{ -need_packages(...) -} -\arguments{ -\item{...}{Package names, as characters. Can be passed as -individual arguments, character vectors, or any combination thereof.} -} -\value{ -`pkgs`, invisibly -} -\description{ -Check if required packages are installed, and throw an informative -error if not. -} -\examples{ -PEcAn.data.atmosphere:::need_packages("stats", "methods") # Always works -try(PEcAn.data.atmosphere:::need_packages("notapackage")) -} -\author{ -Alexey Shiklomanov -} From 08a8793aca74ba3d57f87084ca976c7f281b938b Mon Sep 17 00:00:00 2001 From: meetagrawal09 Date: Mon, 21 Aug 2023 15:01:06 +0530 Subject: [PATCH 07/12] updated integration test --- .../integrationTests/test.download.CRUNCEP.R | 215 +++--------------- 1 file changed, 31 insertions(+), 184 deletions(-) diff --git a/modules/data.atmosphere/inst/integrationTests/test.download.CRUNCEP.R b/modules/data.atmosphere/inst/integrationTests/test.download.CRUNCEP.R index 750fb2f9ec4..dbabb91be5c 100644 --- a/modules/data.atmosphere/inst/integrationTests/test.download.CRUNCEP.R +++ b/modules/data.atmosphere/inst/integrationTests/test.download.CRUNCEP.R @@ -1,39 +1,39 @@ library(testthat) library(ncdf4) -library(lubridate) + +# 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)) tmpdir <- tempfile(pattern = "CRUNCEPdata") dir.create(tmpdir) on.exit(teardown(unlink(tmpdir, recursive = TRUE))) -outfolder <- tmpdir -start_date <- "2000-01-01" -end_date <- "2000-12-31" -lat.in <- 40 -lon.in <- -88 -method <- "ncss" -overwrite <- TRUE -verbose <- FALSE -maxErrors <- 10 -sleep <- 2 - -lat.in <- as.numeric(lat.in) -lon.in <- as.numeric(lon.in) - -lat_grid <- floor(2 * (90 - lat.in)) + 1 -lon_grid <- floor(2 * (lon.in + 180)) + 1 - -maskfile <- file.path(outfolder, "cruncep_landwater_mask.nc") -mask_url <- paste0( - "https://thredds.daac.ornl.gov/thredds/ncss/ornldaac/1220/", - "mstmip_driver_global_hd_landwatermask_v1.nc4", - "?var=land_water_mask&disableLLSubset=on&disableProjSubset=on&horizStride=1&", - "accept=netcdf" +PEcAn.DB::convert_input( + input.id = NA, + outfolder = tmpdir, + formatname = NULL, + mimetype = NULL, + site.id = 1, + start_date = "2000-01-01", + end_date = "2000-12-31", + pkg = 'PEcAn.data.atmosphere', + fcn = 'download.CRUNCEP', + con = NULL, + host = data.frame(name = "localhost"), + browndog = NULL, + write = FALSE, + lat.in = 40, + lon.in = -88, + method = "ncss", + maxErrors = 10, + sleep = 2 ) -utils::download.file(mask_url, maskfile) - -mask_nc <- ncdf4::nc_open(maskfile) -on.exit(ncdf4::nc_close(mask_nc), add = TRUE) test_that("File exists at desired location", { # Set the desired file path @@ -44,6 +44,9 @@ test_that("File exists at desired location", { }) 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 @@ -51,162 +54,6 @@ test_that("NetCDF file contains lat and lon variables", { expect_equal(mask_nc$var$land_water_mask$dim[[2]]$name, "lat") }) - -# Set search radius to up to 2 pixels (1 degree) in any direction -mask_minlat <- lat_grid - 2 -mask_minlon <- lon_grid - 2 -mask_lats <- ncdf4::ncvar_get(mask_nc, "lat", start = mask_minlat, count = 5) -mask_lons <- ncdf4::ncvar_get(mask_nc, "lon", start = mask_minlon, count = 5) -mask_values <- ncdf4::ncvar_get( - mask_nc, - "land_water_mask", - start = c(mask_minlon, mask_minlat), - count = c(5, 5) -) - -# Build a lat-lon grid matrix and calculate distance from target coords -mask_grid <- as.matrix(expand.grid(lon = mask_lons, lat = mask_lats)) -mask_igrid <- as.matrix(expand.grid(lon = seq_along(mask_lons), lats = seq_along(mask_lats))) -mask_dist <- (lon.in - mask_grid[, 1])^2 + (lat.in - mask_grid[, 2])^2 - -# Order by increasing distance (closest first) -mask_order <- order(mask_dist) -mask_igrido <- mask_igrid[mask_order,] -on_land <- as.logical(mask_values[mask_igrido]) -igrid <- which(on_land)[1] -if (igrid > 1) { - lon.in.orig <- lon.in - lat.in.orig <- lat.in - lon.in <- mask_grid[mask_order[igrid], 1] - lat.in <- mask_grid[mask_order[igrid], 2] -} - -if (method == "opendap") { - # Convert lat-lon to grid row and column - lat_grid <- floor(2 * (90 - lat.in)) + 1 - lon_grid <- floor(2 * (lon.in + 180)) + 1 -} else if (method == "ncss") { - # Only downloading point data, so only one point - lat_grid <- 1 - lon_grid <- 1 -} - -start_date <- as.POSIXlt(start_date, tz = "UTC") -end_date <- as.POSIXlt(end_date, tz = "UTC") - -# taking out year from dates -start_year <- lubridate::year(start_date) -end_year <- lubridate::year(end_date) - -# year - 2000 -ylist <- seq(start_year, end_year, by = 1) -rows <- length(ylist) - -var <- tibble::tribble( - ~DAP.name, ~CF.name, ~units, - "tair", "air_temperature", "Kelvin", - "lwdown", "surface_downwelling_longwave_flux_in_air", "W/m2", - "press", "air_pressure", "Pascal", - "swdown", "surface_downwelling_shortwave_flux_in_air", "W/m2", - "uwind", "eastward_wind", "m/s", - "vwind", "northward_wind", "m/s", - "qair", "specific_humidity", "g/g", - "rain", "precipitation_flux", "kg/m2/s" -) - -for (i in seq_len(rows)) { - year <- ylist[i] - ntime <- 1464 - - loc.file <- file.path(outfolder, paste("CRUNCEP", year, "nc", sep = ".")) - - ## Create dimensions - lat <- ncdf4::ncdim_def(name = "latitude", units = "degree_north", vals = lat.in, create_dimvar = TRUE) - lon <- ncdf4::ncdim_def(name = "longitude", units = "degree_east", vals = lon.in, create_dimvar = TRUE) - - days_elapsed <- (1:ntime) * 6/24 - 3/24 # data are 6-hourly, with timestamp at center of interval - time <- ncdf4::ncdim_def(name = "time", units = paste0("days since ", year, "-01-01T00:00:00Z"), - vals = as.array(days_elapsed), create_dimvar = TRUE, unlim = TRUE) - - dim <- list(lat, lon, time) - - var.list <- list() - dat.list <- list() - - file_name <- "mstmip_driver_global_hd_climate_%1$s_%2$d_v1.nc4" - - dap_base <- switch( - method, - opendap = paste0("https://thredds.daac.ornl.gov/thredds/dodsC/ornldaac/1220/", file_name), - ncss = paste0("https://thredds.daac.ornl.gov/thredds/ncss/grid/ornldaac/1220/", file_name, "/dataset.html") - ) - - for (j in seq_len(nrow(var))) { - current_var <- var$DAP.name[j] - url <- sprintf(dap_base, current_var, year) - print(paste0("Attempting to access file at: ", url)) - if (method == "opendap") { - dap <- PEcAn.utils::retry.func(ncdf4::nc_open(url, verbose=verbose), maxErrors=maxErrors, sleep=sleep) - } else if (method == "ncss") { - ncss_query <- glue::glue( - url, "?", - "var={current_var}&", - "south={lat.in}&", - "west={lon.in}&", - "north={lat.in + 5e-6}&", - "east={lon.in + 5e-6}&", - "time_start={year}-01-01T00:00:00Z&", - "time_end={year}-12-31T21:00:00Z&", - "accept=netcdf" - ) - - raw_file <- file.path( - outfolder, - glue::glue("cruncep-raw-{year}-{lat.in}-{lon.in}-{current_var}.nc") - ) - utils::download.file(ncss_query, raw_file) - dap <- ncdf4::nc_open(raw_file) - } - - if (dap$dim$time$len != ntime) { - print(paste0("Expected", ntime, "observations, but", url, "contained", dap$dim$time$len)) - } - dap_time <- PEcAn.utils::ud_convert(dap$dim$time$vals, - dap$dim$time$units, - time$units) - if (!isTRUE(all.equal(dap_time, time$vals))){ - print(paste0("Timestamp mismatch.", - "Expected", min(time$vals), '..', max(time$vals), time$units, - "but got", min(dap_time), "..", max(dap_time))) - } - - dat.list[[j]] <- PEcAn.utils::retry.func( - ncdf4::ncvar_get( - dap, - as.character(var$DAP.name[j]), - c(lon_grid, lat_grid, 1), - c(1, 1, ntime) - ), - maxErrors=maxErrors, sleep=sleep) - - var.list[[j]] <- ncdf4::ncvar_def(name = as.character(var$CF.name[j]), - units = as.character(var$units[j]), - dim = dim, - missval = -999, - verbose = verbose) - ncdf4::nc_close(dap) - } - ## change units of precip to kg/m2/s instead of 6 hour accumulated precip - dat.list[[8]] <- dat.list[[8]] / 21600 - - ## put data in new file - loc <- ncdf4::nc_create(filename = loc.file, vars = var.list, verbose = verbose) - for (j in seq_len(nrow(var))) { - ncdf4::ncvar_put(nc = loc, varid = as.character(var$CF.name[j]), vals = dat.list[[j]]) - } - ncdf4::nc_close(loc) -} - 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"))) From b25e18688815d2397b884cfcd2c9b06d2b7bf187 Mon Sep 17 00:00:00 2001 From: meetagrawal09 Date: Mon, 21 Aug 2023 18:30:05 +0530 Subject: [PATCH 08/12] fixed typos --- modules/data.atmosphere/R/download.US_WCr.R | 2 +- modules/data.atmosphere/R/download.US_Wlef.R | 2 +- modules/data.atmosphere/R/extract_ERA5.R | 2 +- modules/data.atmosphere/man/download.US_WCr.Rd | 2 +- modules/data.atmosphere/man/download.US_Wlef.Rd | 2 +- modules/data.atmosphere/man/extract.nc.ERA5.Rd | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/data.atmosphere/R/download.US_WCr.R b/modules/data.atmosphere/R/download.US_WCr.R index 3924311f526..66615e6ef44 100644 --- a/modules/data.atmosphere/R/download.US_WCr.R +++ b/modules/data.atmosphere/R/download.US_WCr.R @@ -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. ##' diff --git a/modules/data.atmosphere/R/download.US_Wlef.R b/modules/data.atmosphere/R/download.US_Wlef.R index a92e7599e31..4d44c880f1a 100644 --- a/modules/data.atmosphere/R/download.US_Wlef.R +++ b/modules/data.atmosphere/R/download.US_Wlef.R @@ -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. ##' diff --git a/modules/data.atmosphere/R/extract_ERA5.R b/modules/data.atmosphere/R/extract_ERA5.R index bf219b4a509..f85b43cdec8 100644 --- a/modules/data.atmosphere/R/extract_ERA5.R +++ b/modules/data.atmosphere/R/extract_ERA5.R @@ -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} #' diff --git a/modules/data.atmosphere/man/download.US_WCr.Rd b/modules/data.atmosphere/man/download.US_WCr.Rd index 3c3fcfa99d1..c09d4eda31a 100644 --- a/modules/data.atmosphere/man/download.US_WCr.Rd +++ b/modules/data.atmosphere/man/download.US_WCr.Rd @@ -19,7 +19,7 @@ download.US-WCr \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. diff --git a/modules/data.atmosphere/man/download.US_Wlef.Rd b/modules/data.atmosphere/man/download.US_Wlef.Rd index fd461d65299..f20ffdc1fc7 100644 --- a/modules/data.atmosphere/man/download.US_Wlef.Rd +++ b/modules/data.atmosphere/man/download.US_Wlef.Rd @@ -19,7 +19,7 @@ download.US_Wlef \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. } diff --git a/modules/data.atmosphere/man/extract.nc.ERA5.Rd b/modules/data.atmosphere/man/extract.nc.ERA5.Rd index a5c6ce66081..8b82e4490ba 100644 --- a/modules/data.atmosphere/man/extract.nc.ERA5.Rd +++ b/modules/data.atmosphere/man/extract.nc.ERA5.Rd @@ -35,7 +35,7 @@ extract.nc.ERA5( \item{newsite}{site name.} -\item{vars}{variables to be extarcted. If NULL all the variables will be returned.} +\item{vars}{variables to be extracted. If NULL all the variables will be returned.} \item{overwrite}{Logical if files needs to be overwritten.} } From 5484b780ec7088d654d0dd452f627019941d0d45 Mon Sep 17 00:00:00 2001 From: meetagrawal09 Date: Mon, 21 Aug 2023 19:16:57 +0530 Subject: [PATCH 09/12] fixed typos --- modules/data.atmosphere/R/download.AmerifluxLBL.R | 2 +- modules/data.atmosphere/R/extract_local_CMIP5.R | 2 +- modules/data.atmosphere/R/extract_local_NLDAS.R | 2 +- modules/data.atmosphere/R/tdm_predict_subdaily_met.R | 2 +- modules/data.atmosphere/R/tdm_temporal_downscale_functions.R | 2 +- modules/data.atmosphere/man/download.AmerifluxLBL.Rd | 2 +- modules/data.atmosphere/man/extract.local.CMIP5.Rd | 2 +- modules/data.atmosphere/man/extract.local.NLDAS.Rd | 2 +- modules/data.atmosphere/man/predict_subdaily_met.Rd | 2 +- modules/data.atmosphere/man/temporal.downscale.functions.Rd | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/modules/data.atmosphere/R/download.AmerifluxLBL.R b/modules/data.atmosphere/R/download.AmerifluxLBL.R index 2bb5b2b65b1..4fe706a1f33 100644 --- a/modules/data.atmosphere/R/download.AmerifluxLBL.R +++ b/modules/data.atmosphere/R/download.AmerifluxLBL.R @@ -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. diff --git a/modules/data.atmosphere/R/extract_local_CMIP5.R b/modules/data.atmosphere/R/extract_local_CMIP5.R index 45952de9d8a..8cadc9bf16b 100644 --- a/modules/data.atmosphere/R/extract_local_CMIP5.R +++ b/modules/data.atmosphere/R/extract_local_CMIP5.R @@ -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 diff --git a/modules/data.atmosphere/R/extract_local_NLDAS.R b/modules/data.atmosphere/R/extract_local_NLDAS.R index a23c4d74531..2846dafca09 100644 --- a/modules/data.atmosphere/R/extract_local_NLDAS.R +++ b/modules/data.atmosphere/R/extract_local_NLDAS.R @@ -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 diff --git a/modules/data.atmosphere/R/tdm_predict_subdaily_met.R b/modules/data.atmosphere/R/tdm_predict_subdaily_met.R index 58c3d72845e..825bd203672 100644 --- a/modules/data.atmosphere/R/tdm_predict_subdaily_met.R +++ b/modules/data.atmosphere/R/tdm_predict_subdaily_met.R @@ -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? diff --git a/modules/data.atmosphere/R/tdm_temporal_downscale_functions.R b/modules/data.atmosphere/R/tdm_temporal_downscale_functions.R index 3866ad36e99..911b1ba4014 100644 --- a/modules/data.atmosphere/R/tdm_temporal_downscale_functions.R +++ b/modules/data.atmosphere/R/tdm_temporal_downscale_functions.R @@ -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 diff --git a/modules/data.atmosphere/man/download.AmerifluxLBL.Rd b/modules/data.atmosphere/man/download.AmerifluxLBL.Rd index 753bcd5457f..8b30000484f 100644 --- a/modules/data.atmosphere/man/download.AmerifluxLBL.Rd +++ b/modules/data.atmosphere/man/download.AmerifluxLBL.Rd @@ -50,7 +50,7 @@ to the given outfolder. Details about amf_download_base function can be found he https://github.com/chuhousen/amerifluxr/blob/master/R/amf_download_base.R } \details{ -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 } \examples{ \dontrun{ diff --git a/modules/data.atmosphere/man/extract.local.CMIP5.Rd b/modules/data.atmosphere/man/extract.local.CMIP5.Rd index fec601e2925..14eb0142c7e 100644 --- a/modules/data.atmosphere/man/extract.local.CMIP5.Rd +++ b/modules/data.atmosphere/man/extract.local.CMIP5.Rd @@ -45,7 +45,7 @@ extract.local.CMIP5( 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} -\item{adjust.pr}{- adjustment factor fore preciptiation when the extracted values seem off} +\item{adjust.pr}{- adjustment factor fore precipitation when the extracted values seem off} \item{overwrite}{logical. Download a fresh version even if a local file with the same name already exists?} diff --git a/modules/data.atmosphere/man/extract.local.NLDAS.Rd b/modules/data.atmosphere/man/extract.local.NLDAS.Rd index e98ba47e292..b9a9f2e88b8 100644 --- a/modules/data.atmosphere/man/extract.local.NLDAS.Rd +++ b/modules/data.atmosphere/man/extract.local.NLDAS.Rd @@ -41,7 +41,7 @@ to control printing of debug info} 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. } \author{ diff --git a/modules/data.atmosphere/man/predict_subdaily_met.Rd b/modules/data.atmosphere/man/predict_subdaily_met.Rd index 3d4898e3e6f..09a98b8ce0c 100644 --- a/modules/data.atmosphere/man/predict_subdaily_met.Rd +++ b/modules/data.atmosphere/man/predict_subdaily_met.Rd @@ -47,7 +47,7 @@ ensemble rather than overwriting with a default naming scheme} \item{resids}{- logical stating whether to pass on residual data or not} -\item{adjust.pr}{- adjustment factor fore preciptiation when the extracted values seem off} +\item{adjust.pr}{- adjustment factor fore precipitation when the extracted values seem off} \item{force.sanity}{- (logical) do we force the data to meet sanity checks?} diff --git a/modules/data.atmosphere/man/temporal.downscale.functions.Rd b/modules/data.atmosphere/man/temporal.downscale.functions.Rd index 77e86d6c3a7..668fe14e60a 100644 --- a/modules/data.atmosphere/man/temporal.downscale.functions.Rd +++ b/modules/data.atmosphere/man/temporal.downscale.functions.Rd @@ -25,7 +25,7 @@ temporal.downscale.functions( \item{day.window}{- number of days surrounding current day we want to pull statistics from} -\item{resids}{- whether or not to propogate residuals, set to FALSE} +\item{resids}{- whether or not to propagate residuals, set to FALSE} \item{parallel}{- whether or not to run in parallel. this is a feature still being worked on, set to FALSE} From e68f54060a67b29189f6b9289f340f880306c83c Mon Sep 17 00:00:00 2001 From: meetagrawal09 Date: Tue, 22 Aug 2023 14:34:47 +0530 Subject: [PATCH 10/12] fixed typos --- modules/data.atmosphere/R/metgapfill.R | 4 ++-- modules/data.atmosphere/man/metgapfill.Rd | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/data.atmosphere/R/metgapfill.R b/modules/data.atmosphere/R/metgapfill.R index 2d3eba7f096..db53eebe875 100644 --- a/modules/data.atmosphere/R/metgapfill.R +++ b/modules/data.atmosphere/R/metgapfill.R @@ -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, ...) { diff --git a/modules/data.atmosphere/man/metgapfill.Rd b/modules/data.atmosphere/man/metgapfill.Rd index e5a7d37a385..5c96abb1525 100644 --- a/modules/data.atmosphere/man/metgapfill.Rd +++ b/modules/data.atmosphere/man/metgapfill.Rd @@ -21,7 +21,7 @@ metgapfill( ) } \arguments{ -\item{in.path}{location on disk where inputs are stord} +\item{in.path}{location on disk where inputs are stored} \item{in.prefix}{prefix of input and output files} @@ -31,7 +31,7 @@ metgapfill( \item{end_date}{the end date of the data to be downloaded (will only use the year part of the date)} -\item{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} +\item{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} \item{overwrite}{should existing files be overwritten} From 6b1c13a1088e4c41d45d647533d1a61ead220e74 Mon Sep 17 00:00:00 2001 From: meetagrawal09 Date: Tue, 22 Aug 2023 23:07:11 +0530 Subject: [PATCH 11/12] removed teardown and on.exit --- .../integrationTests/test.download.CRUNCEP.R | 208 +++++++++--------- 1 file changed, 103 insertions(+), 105 deletions(-) diff --git a/modules/data.atmosphere/inst/integrationTests/test.download.CRUNCEP.R b/modules/data.atmosphere/inst/integrationTests/test.download.CRUNCEP.R index dbabb91be5c..e6e0a9a6356 100644 --- a/modules/data.atmosphere/inst/integrationTests/test.download.CRUNCEP.R +++ b/modules/data.atmosphere/inst/integrationTests/test.download.CRUNCEP.R @@ -3,115 +3,113 @@ library(ncdf4) # 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)) -tmpdir <- tempfile(pattern = "CRUNCEPdata") -dir.create(tmpdir) -on.exit(teardown(unlink(tmpdir, recursive = TRUE))) - -PEcAn.DB::convert_input( - input.id = NA, - outfolder = tmpdir, - formatname = NULL, - mimetype = NULL, - site.id = 1, - start_date = "2000-01-01", - end_date = "2000-12-31", - pkg = 'PEcAn.data.atmosphere', - fcn = 'download.CRUNCEP', - con = NULL, - host = data.frame(name = "localhost"), - browndog = NULL, - write = FALSE, - lat.in = 40, - lon.in = -88, - method = "ncss", - maxErrors = 10, - sleep = 2 -) - -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) +withr::with_dir(tempdir(), { + tmpdir <- getwd() + PEcAn.DB::convert_input( + input.id = NA, + outfolder = tmpdir, + formatname = NULL, + mimetype = NULL, + site.id = 1, + start_date = "2000-01-01", + end_date = "2000-12-31", + pkg = 'PEcAn.data.atmosphere', + fcn = 'download.CRUNCEP', + con = NULL, + host = data.frame(name = "localhost"), + browndog = NULL, + write = FALSE, + lat.in = 40, + lon.in = -88, + method = "ncss", + maxErrors = 10, + sleep = 2 + ) + + 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) + }) }) \ No newline at end of file From 4090ddc5787f51ff0b0ae9d76ae59ef140379727 Mon Sep 17 00:00:00 2001 From: meetagrawal09 Date: Wed, 23 Aug 2023 16:28:01 +0530 Subject: [PATCH 12/12] moved tests to a function --- .../integrationTests/test.download.CRUNCEP.R | 236 ++++++++++-------- 1 file changed, 125 insertions(+), 111 deletions(-) diff --git a/modules/data.atmosphere/inst/integrationTests/test.download.CRUNCEP.R b/modules/data.atmosphere/inst/integrationTests/test.download.CRUNCEP.R index e6e0a9a6356..8512b49f34d 100644 --- a/modules/data.atmosphere/inst/integrationTests/test.download.CRUNCEP.R +++ b/modules/data.atmosphere/inst/integrationTests/test.download.CRUNCEP.R @@ -1,115 +1,129 @@ library(testthat) library(ncdf4) -# putting logger to debug mode -PEcAn.logger::logger.setUseConsole(TRUE, FALSE) -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 = "2000-01-01", - end_date = "2000-12-31", - pkg = 'PEcAn.data.atmosphere', - fcn = 'download.CRUNCEP', - con = NULL, - host = data.frame(name = "localhost"), - browndog = NULL, - write = FALSE, - lat.in = 40, - lon.in = -88, - method = "ncss", - maxErrors = 10, - sleep = 2 - ) - - 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_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_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) - }) -}) \ No newline at end of file +} + + +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 +) \ No newline at end of file