Skip to content

Commit

Permalink
change how environment arguments are handeled
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejohnson51 committed Dec 22, 2023
1 parent b970d99 commit e0166c9
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 32 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Package: climateR
Type: Package
Title: climateR
Description: Find, subset, and retrive geospatial data by AOI.
Version: 0.3.2
Version: 0.3.3
Authors@R: c(person("Mike", "Johnson",
role = c("aut", "cre"),
email = "[email protected]"),
Expand Down
52 changes: 26 additions & 26 deletions R/shortcuts.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
call_aoi = function(x, AOI){ if(x$AOI == "."){ x$AOI = eval(parse(text=deparse(AOI)))}; x }

match.call.defaults <- function(...) {
call <- evalq(match.call(expand.dots = FALSE), parent.frame(1))
formals <- evalq(formals(), parent.frame(1))

for(i in setdiff(names(formals), names(call)))
call[i] <- list( formals[[i]] )

match.call(sys.function(sys.parent()), call)
}
# call_aoi = function(x, AOI){ if(x$AOI == "."){ x$AOI = eval(parse(text=deparse(AOI)))}; x }
#
# match.call.defaults <- function(...) {
# call <- evalq(match.call(expand.dots = FALSE), parent.frame(1))
# formals <- evalq(formals(), parent.frame(1))
#
# for(i in setdiff(names(formals), names(call)))
# call[i] <- list( formals[[i]] )
#
# match.call(sys.function(sys.parent()), call)
# }

#' ClimateR dry run
#' @param id The resource name, agency, or catalog identifier
Expand Down Expand Up @@ -52,7 +52,7 @@ getTerraClim = function(AOI, varname = NULL,
startDate = NULL, endDate = NULL,
verbose = FALSE, ID = NULL, dryrun = FALSE){
climater_dap("terraclim",
call_aoi(as.list(match.call.defaults()[-1]), AOI),
as.list(environment()),
verbose, dryrun)
}

Expand Down Expand Up @@ -100,7 +100,7 @@ getDaymet = function(AOI, varname = NULL,
startDate = NULL, endDate = NULL,
verbose = FALSE, ID = NULL, dryrun = FALSE){

climater_dap("daymet4", call_aoi(as.list(match.call.defaults()[-1]), AOI), verbose, dryrun)
climater_dap("daymet4", as.list(environment()), verbose, dryrun)
}

#' @title Get MACA Climate Data for an Area of Interest
Expand All @@ -120,7 +120,7 @@ getMACA = function(AOI, varname, timeRes = 'day',
startDate, endDate = NULL,
verbose = FALSE, ID = NULL, dryrun = FALSE){
if(!timeRes %in% c('day', 'month')){ stop("timeRes must be month or day") }
climater_dap(paste0('maca_', timeRes), call_aoi(as.list(match.call.defaults()[-1]), AOI), verbose, dryrun)
climater_dap(paste0('maca_', timeRes), as.list(environment()), verbose, dryrun)
}

#' @title Get GridMet Climate Data for an Area of Interest
Expand All @@ -136,7 +136,7 @@ getMACA = function(AOI, varname, timeRes = 'day',
getGridMET = function(AOI, varname,
startDate, endDate = NULL,
verbose = FALSE, ID = NULL, dryrun = FALSE){
climater_dap("gridmet", call_aoi(as.list(match.call.defaults()[-1]), AOI), verbose, dryrun)
climater_dap("gridmet", as.list(environment()), verbose, dryrun)
}


Expand All @@ -157,7 +157,7 @@ getLOCA = function(AOI, varname,
startDate, endDate = NULL,
verbose = FALSE, ID = NULL, dryrun = FALSE){
climater_dap('loca',
call_aoi(as.list(match.call.defaults()[-1]), AOI),
as.list(environment()),
verbose, dryrun)
}

Expand All @@ -173,7 +173,7 @@ getVIC = function(AOI, varname,
model = 'CCSM4', scenario = 'rcp45',
startDate, endDate = NULL,
verbose = FALSE, ID = NULL, dryrun = FALSE){
climater_dap('vic', call_aoi(as.list(match.call.defaults()[-1]), AOI), verbose, dryrun)
climater_dap('vic', as.list(environment()), verbose, dryrun)
}

#' @title Get BCCA data
Expand All @@ -188,7 +188,7 @@ getBCCA = function(AOI, varname,
model = 'CCSM4', scenario = 'rcp45', ensemble = NULL,
startDate, endDate = NULL,
verbose = FALSE, ID = NULL, dryrun = FALSE){
climater_dap('bcca', call_aoi(as.list(match.call.defaults()[-1]), AOI), verbose, dryrun)
climater_dap('bcca', as.list(environment()), verbose, dryrun)
}

#' @title Get CHIRPS data
Expand All @@ -212,7 +212,7 @@ getCHIRPS = function(AOI, varname = NULL,
if(!timeRes %in% good_timeRes){ stop("timeRes must be one of: ", paste(good_timeRes, collapse = ", "), call. = FALSE) }

climater_dap(paste0("chirps20Global", timeRes, "P05"),
call_aoi(as.list(match.call.defaults()[-1]), AOI),
as.list(environment()),
verbose,
dryrun)
}
Expand All @@ -233,7 +233,7 @@ getNLDAS = function(AOI, varname = NULL,
stop('netrc file not found. Please run writeNetrc() with earth data credentials.')
} else {
x = writeDodsrc()
data = climater_dap("NLDAS", call_aoi(as.list(match.call.defaults()[-1]), AOI), verbose, dryrun)
data = climater_dap("NLDAS", as.list(environment()), verbose, dryrun)

return(data)
}
Expand All @@ -254,7 +254,7 @@ getGLDAS = function(AOI, varname = NULL, model = NULL,
stop('netrc file not found. Please run writeNetrc() with earth data credentials.')
} else {
x = writeDodsrc()
data = climater_dap("GLDAS", call_aoi(as.list(match.call.defaults()[-1]), AOI), verbose, dryrun)
data = climater_dap("GLDAS", as.list(environment()), verbose, dryrun)
unlink(x)
return(data)
}
Expand All @@ -277,7 +277,7 @@ getMODIS = function(AOI, asset = NULL, varname = NULL,
stop('netrc file not found. Please run writeNetrc() with earth data credentials.')
} else {
x = writeDodsrc()
data = climater_dap("MODIS", call_aoi(as.list(match.call.defaults()[-1]), AOI), verbose, dryrun)
data = climater_dap("MODIS", as.list(environment()), verbose, dryrun)
unlink(x)
return(data)
}
Expand All @@ -297,9 +297,9 @@ getLivneh = function(AOI, varname = NULL,
startDate, endDate = NULL, timeRes = "daily",
verbose = FALSE, ID = NULL, dryrun = FALSE){
if(timeRes == "daily"){
climater_dap("Livneh_daily", call_aoi(as.list(match.call.defaults()[-1]), AOI), verbose, dryrun)
climater_dap("Livneh_daily", as.list(environment()), verbose, dryrun)
} else {
climater_dap("Livneh_monthly", call_aoi(as.list(match.call.defaults()[-1]), AOI), verbose, dryrun)
climater_dap("Livneh_monthly", as.list(environment()), verbose, dryrun)
}
}

Expand All @@ -314,7 +314,7 @@ getLivneh = function(AOI, varname = NULL,
getLivneh_fluxes = function(AOI, varname = NULL,
startDate, endDate = NULL,
verbose = FALSE, ID = NULL, dryrun = FALSE){
climater_dap("Livneh_fluxes", call_aoi(as.list(match.call.defaults()[-1]), AOI), verbose, dryrun)
climater_dap("Livneh_fluxes", as.list(environment()), verbose, dryrun)
}

#' @title Get PRISM data
Expand Down Expand Up @@ -375,7 +375,7 @@ getPRISM = function(AOI, varname = NULL,
return(x)

} else {
climater_dap(id = "prism_monthly", call_aoi(as.list(match.call.defaults()[-1]), AOI), verbose, dryrun)
climater_dap(id = "prism_monthly", as.list(environment()), verbose, dryrun)
}
}

Expand Down
2 changes: 1 addition & 1 deletion man/catalog.Rd

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

16 changes: 12 additions & 4 deletions tests/testthat/test-climateR.R
Original file line number Diff line number Diff line change
Expand Up @@ -449,16 +449,23 @@ test_that("CHIRPS", {

})

test_that("piping AOI", {
test_that("piping", {

xx = bb %>%
xx = bb %>%
getCHIRPS(startDate = "2011-11-29", endDate = "2011-12-03")

expect_true(class(xx) == "list")
expect_true(class(xx[[1]]) == "SpatRaster")
expect_true(names(xx)[1] == "precip")
expect_true(nlyr(xx[[1]]) == 5)

AOI = bb %>%
getNLCD() %>%
get3DEP() %>%
getTerraClimNormals(varname = "ppt")



})

test_that("dap_xyzv", {
Expand All @@ -475,13 +482,14 @@ test_that("dap_xyzv", {

expect_error(dap_xyzv(f, varname = "BLAH"))

URL = "https://gpm1.gesdisc.eosdis.nasa.gov/opendap/hyrax/GPM_L3/GPM_3IMERGHH.06/2021/001/3B-HHR.MS.MRG.3IMERG.20210101-S000000-E002959.0000.V06B.HDF5"
URL = 'http://thredds.northwestknowledge.net:8080/thredds/dodsC/agg_terraclimate_srad_1958_CurrentYear_GLOBE.nc'
#"https://gpm1.gesdisc.eosdis.nasa.gov/opendap/hyrax/GPM_L3/GPM_3IMERGHH.06/2021/001/3B-HHR.MS.MRG.3IMERG.20210101-S000000-E002959.0000.V06B.HDF5"

dap = dap_xyzv(URL)
times = .resource_time(URL, T_name = dap$T_name[1])
xy = .resource_grid(URL, X_name = dap$X_name[1], Y_name = dap$Y_name[1])

expect_true(nrow(dap) == 10)
expect_true(nrow(dap) == 1)
expect_true(length(times) == 3)
expect_true(nrow(xy) == 1)

Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-earthdata.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
library(AOI)
library(terra)
library(climateR)

cities = geocode(c("Fort Collins, CO",
"Durham, NC",
"Raleigh, NC"),
pt = TRUE)

bb = geocode("Fort Collins, CO", bb = TRUE)


test_that("NLDAS", {
# skip on CI - no netrc/dodsrc
Expand Down

0 comments on commit e0166c9

Please sign in to comment.