From 48bdcbecc91f25e1eb95677fb8ad6c77255ed878 Mon Sep 17 00:00:00 2001 From: dschlaep Date: Mon, 11 Dec 2017 14:44:31 -0500 Subject: [PATCH 1/3] Update 'get_DayMet_NorthAmerica' to meet external API changes - close #232 - 'get_DayMet_NorthAmerica' has now code to handle 'daymetr' versions < v1.2 and >= v1.2 --- R/WeatherDB.R | 41 +++++++++++++++++++------- tests/testthat/test_WeatherDB_DayMet.R | 4 ++- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/R/WeatherDB.R b/R/WeatherDB.R index 904b70aa..416ff03f 100644 --- a/R/WeatherDB.R +++ b/R/WeatherDB.R @@ -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 (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) @@ -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")) { @@ -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 } diff --git a/tests/testthat/test_WeatherDB_DayMet.R b/tests/testthat/test_WeatherDB_DayMet.R index 8b75a0e3..6eaf775b 100644 --- a/tests/testthat/test_WeatherDB_DayMet.R +++ b/tests/testthat/test_WeatherDB_DayMet.R @@ -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( @@ -75,4 +76,5 @@ if (!any(do_skip) && is_online) { #--- Clean up unlink(list.files(dm_path, "daymet", full.names = TRUE)) + unlink(dm_path) } From 3164722604617cf5259c79b895195f8c540aa9ba Mon Sep 17 00:00:00 2001 From: dschlaep Date: Tue, 12 Dec 2017 11:22:03 -0500 Subject: [PATCH 2/3] Function 'packageVersion' is from package 'utils' --- R/WeatherDB.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/WeatherDB.R b/R/WeatherDB.R index 416ff03f..d0099845 100644 --- a/R/WeatherDB.R +++ b/R/WeatherDB.R @@ -560,7 +560,7 @@ get_DayMet_NorthAmerica <- function(dir_data, cellID, Xdm_WGS84, Ydm_WGS84, star if (get_from_ornl) { stopifnot(requireNamespace("daymetr")) - if (packageVersion("daymetr") < "1.1") { + if (utils::packageVersion("daymetr") < "1.1") { # 'daymetr::download_daymet' saves downloaded file on disk in current working directory wd_prev <- getwd() setwd(dir_data) From 868fbf9e3104192bf6f466c33f2f9793d7527dee Mon Sep 17 00:00:00 2001 From: dschlaep Date: Tue, 12 Dec 2017 11:27:50 -0500 Subject: [PATCH 3/3] Update patch number and more meaningful check output of tests --- DESCRIPTION | 4 ++-- tests/testthat.R | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 58ea4d37..72d617b2 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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"), diff --git a/tests/testthat.R b/tests/testthat.R index 4847010e..4f9e3b47 100644 --- a/tests/testthat.R +++ b/tests/testthat.R @@ -7,4 +7,4 @@ Sys.setenv("R_TESTS" = "") library("testthat") library("rSFSW2") -test_check("rSFSW2") +test_check("rSFSW2", reporter = SummaryReporter)