Skip to content

Commit

Permalink
Merge pull request #233 from Burke-Lauenroth-Lab/bugfix_232
Browse files Browse the repository at this point in the history
Update 'get_DayMet_NorthAmerica' to meet external API changes
  • Loading branch information
dschlaep authored Dec 12, 2017
2 parents 4a9095c + 868fbf9 commit 4cf5fb0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: rSFSW2
Title: Simulation Framework for SOILWAT2
Version: 2.4.0
Date: 2017-12-08
Version: 2.4.1
Date: 2017-12-12
Authors@R: c(person("Daniel", "Schlaepfer", email = "daniel.schlaepfer@yale.edu", role = c("aut", "cre")),
person("Caitlin", "Andrews", role = "ctb"),
person("Zach", "Kramer", role = "ctb"),
Expand Down
41 changes: 30 additions & 11 deletions R/WeatherDB.R
Original file line number Diff line number Diff line change
Expand Up @@ -560,16 +560,26 @@ get_DayMet_NorthAmerica <- function(dir_data, cellID, Xdm_WGS84, Ydm_WGS84, star
if (get_from_ornl) {
stopifnot(requireNamespace("daymetr"))

# 'daymetr::download_daymet' stores downloaded file in current working directory
wd_prev <- getwd()
setwd(dir_data)
on.exit(setwd(wd_prev), add = TRUE)
on.exit(if (exists(cellID, envir = globalenv())) {
rm(list = cellID, envir = globalenv())}, add = TRUE)
if (utils::packageVersion("daymetr") < "1.1") {
# 'daymetr::download_daymet' saves downloaded file on disk in current working directory
wd_prev <- getwd()
setwd(dir_data)
on.exit(setwd(wd_prev), add = TRUE)
on.exit(if (exists(cellID, envir = globalenv())) {
rm(list = cellID, envir = globalenv())}, add = TRUE)

dm_temp <- try(daymetr::download_daymet(site = cellID, lat = Ydm_WGS84,
lon = Xdm_WGS84, start = start_year, end = end_year, internal = TRUE,
quiet = TRUE), silent = TRUE)

dm_temp <- try(daymetr::download_daymet(site = cellID, lat = Ydm_WGS84,
lon = Xdm_WGS84, start = start_year, end = end_year, internal = TRUE,
quiet = TRUE), silent = TRUE)
} else {
# 'daymetr::download_daymet' saves downloaded file on disk at `path`
# daymetr returns either list with data.frame OR saves data on specifiable path, but not both
# --> we choose to save on disk because we want to store data for re-use by other projects
dm_temp <- try(daymetr::download_daymet(site = cellID, lat = Ydm_WGS84,
lon = Xdm_WGS84, start = start_year, end = end_year, path = dir_data,
internal = FALSE, quiet = TRUE), silent = TRUE)
}

if (inherits(dm_temp, "try-error")) {
unlink(ftemp)
Expand All @@ -578,12 +588,20 @@ get_DayMet_NorthAmerica <- function(dir_data, cellID, Xdm_WGS84, Ydm_WGS84, star

# Convert to rSOILWAT2 format
if (!inherits(dm_temp, "try-error")) {
temp <- if (exists(cellID, envir = globalenv())) {
temp <- if (inherits(dm_temp, "list") && inherits(dm_temp[["data"]], "data.frame")) {
# 'daymetr' >= v1.2 returns a list with a named element 'data' unless internal = FALSE
dm_temp[["data"]]

} else if (exists(cellID, envir = globalenv())) {
# 'daymetr' < v1.2 created a variable 'cellID' in the global environment
get(cellID, envir = globalenv())$data

} else if (!get_from_ornl && inherits(dm_temp, "data.frame")) {
# already read from file
dm_temp

} else {
# read from file
# not yet read from file
temp <- try(utils::read.table(ftemp, sep = ",", skip = 6, header = TRUE),
silent = TRUE)
if (inherits(temp, "try-error") || !inherits(temp, "data.frame")) {
Expand Down Expand Up @@ -624,6 +642,7 @@ get_DayMet_NorthAmerica <- function(dir_data, cellID, Xdm_WGS84, Ydm_WGS84, star
names(weathDataList) <- as.character(years)

} else {
# Return error object
weathDataList <- dm_temp
}

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ Sys.setenv("R_TESTS" = "")
library("testthat")
library("rSFSW2")

test_check("rSFSW2")
test_check("rSFSW2", reporter = SummaryReporter)
4 changes: 3 additions & 1 deletion tests/testthat/test_WeatherDB_DayMet.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ suppressWarnings(is_online <-
if (!any(do_skip) && is_online) {

#--- Inputs
dm_path <- tempdir()
dm_path <- file.path(tempdir(), "daymet") # avoid https://github.com/khufkens/daymetr/issues/12
dir.create(dm_path, showWarnings = FALSE)
exinfo <- list(GriddedDailyWeatherFromDayMet_NorthAmerica = TRUE)

coords_WGS84 <- data.frame(
Expand Down Expand Up @@ -75,4 +76,5 @@ if (!any(do_skip) && is_online) {

#--- Clean up
unlink(list.files(dm_path, "daymet", full.names = TRUE))
unlink(dm_path)
}

0 comments on commit 4cf5fb0

Please sign in to comment.