SHARE
TWEET

Untitled




Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- context("Download NARR via THREDDS")
- start_date <- "2012-02-20"
- end_date <- "2012-03-05"
- ntime <- as.numeric(difftime(end_date, start_date) + 1) * 24 / 3 + 1
- lat.in <- 43.3724
- lon.in <- -89.9071
- outfolder <- tempfile()
- dir.create(outfolder)
- # Create a fake NARR file the outfolder against which we will test
- testfile <- file.path(outfolder, "_dl_narr_test.nc")
- time_units <- "hours since 2012-02-20"
- time_vals <- seq_len(difftime(end_date, start_date, units = "hours") * 3) - 1
- timedim <- ncdf4::ncdim_def("time", units = time_units, vals = time_vals)
- reftimedim <- ncdf4::ncdim_def("reftime", units = time_units, vals = time_vals)
- flx_vars <- purrr::pmap(list(
- name = narr_flx_vars[["NARR_name"]],
- units = narr_flx_vars[["units"]]
- ), ncdf4::ncvar_def, dim = timedim)
- sfc_vars <- purrr::pmap(list(
- name = narr_sfc_vars[["NARR_name"]],
- units = narr_sfc_vars[["units"]]
- ), ncdf4::ncvar_def, dim = reftimedim)
- ncdf4::nc_create(testfile, c(flx_vars, sfc_vars))
- nc <- ncdf4::nc_open(testfile, write = TRUE)
- purrr::walk(
- flx_vars,
- ncdf4::ncvar_put,
- nc = nc,
- vals = rexp(length(time_vals))
- )
- purrr::walk(
- sfc_vars,
- ncdf4::ncvar_put,
- nc = nc,
- vals = rexp(length(time_vals))
- )
- teardown({
- ncdf4::nc_close(nc)
- unlink(outfolder, recursive = TRUE, force = TRUE)
- })
- test_that(
- "NARR download works as expected",
- {
- # Instead of reading the target NARR file, we read the testfile.
- # This works because the NARR site code only uses nc_open to read
- # the remote file, never to read anything downloaded.
- r <- with_mock(
- "ncdf4::nc_open" = function(filename) ncdf4::nc_open(testfile),
- download.NARR_site(outfolder, start_date, end_date, lat.in, lon.in,
- progress = FALSE, parallel = FALSE)
- )
- expect_equal(nrow(r), 1)
- expect_true(file.exists(r$file[1]))
- nc <- ncdf4::nc_open(r$file)
- temp <- ncdf4::ncvar_get(nc, "air_temperature")
- precip <- ncdf4::ncvar_get(nc, "precipitation_flux")
- expect_true(all(!is.na(temp)), all(temp > 0), length(temp) == ntime)
- expect_true(all(!is.na(precip)), length(precip) == ntime)
- ncdf4::nc_close(nc)
- }
- )
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.