Skip to content

Commit

Permalink
Version update, ID unit declaration, piping, and general speed ups
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejohnson51 committed Dec 9, 2023
1 parent af55c77 commit 0fbaac5
Show file tree
Hide file tree
Showing 256 changed files with 2,084 additions and 770 deletions.
Binary file modified .DS_Store
Binary file not shown.
19 changes: 9 additions & 10 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
^.*\.Rproj$
^\.Rproj\.user$
^\.travis\.yml$
inProgress
sampleFiles
data_backup
help
^inProgress$
^sampleFiles$
^data_backup$
^help$
^README\.Rmd$
^data-raw$
.github
^codecov\.yml$
^\.github$
.lazytest
img
^\.lazytest$
^img$
^_pkgdown\.yml$
^docs$
^pkgdown$
^vignettes/articles$
.dodsrc
.urs_cookies
stac_testing
^\.dodsrc$
\.urs_cookies$
^stac_testing$
9 changes: 4 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
Package: climateR
Type: Package
Title: climateR
Description: Find, subset and retrive climate and geospatial data by AOI.
Version: 0.3.1.4
Description: Find, subset, and retrive geospatial data by AOI.
Version: 0.3.2
Authors@R: c(person("Mike", "Johnson",
role = c("aut", "cre"),
email = "mikecp11@gmail.com"),
email = "jjohnson@lynker.com"),
person("Justin", "Singh", role = "ctb"),
person("Angus", "Watters", role = "ctb"),
person("ESIP", role = "fnd"),
person("NOAA OWP", role = "fnd"))
Maintainer: Mike Johnson <mikecp11@gmail.com>
Maintainer: Mike Johnson <jjohnson@lynker.com>
BugReports: https://github.com/mikejohnson51/climateR/issues
URL: https://github.com/mikejohnson51/climateR
Depends:
Expand Down
7 changes: 7 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export("%>%")
export(.resource_grid)
export(.resource_time)
export(animation)
Expand All @@ -21,6 +22,7 @@ export(getDaymet)
export(getDodsrcPath)
export(getGLDAS)
export(getGridMET)
export(getISRIC_soils)
export(getLCMAP)
export(getLOCA)
export(getLOCA_hydro)
Expand All @@ -44,6 +46,7 @@ export(make_ext)
export(make_vect)
export(merge_across_time)
export(parse_date)
export(plot)
export(read_dap_file)
export(read_ftp)
export(read_live_catalog)
Expand Down Expand Up @@ -73,6 +76,7 @@ importFrom(dplyr,mutate_all)
importFrom(dplyr,rename)
importFrom(dplyr,select)
importFrom(dplyr,slice)
importFrom(dplyr,slice_sample)
importFrom(dplyr,ungroup)
importFrom(future.apply,future_lapply)
importFrom(gifski,save_gif)
Expand All @@ -97,7 +101,9 @@ importFrom(terra,crs)
importFrom(terra,ext)
importFrom(terra,extract)
importFrom(terra,flip)
importFrom(terra,geomtype)
importFrom(terra,intersect)
importFrom(terra,is.points)
importFrom(terra,is.related)
importFrom(terra,merge)
importFrom(terra,minmax)
Expand All @@ -110,6 +116,7 @@ importFrom(terra,setGDALconfig)
importFrom(terra,sprc)
importFrom(terra,time)
importFrom(terra,union)
importFrom(terra,unique)
importFrom(terra,units)
importFrom(terra,vect)
importFrom(terra,xmax)
Expand Down
16 changes: 13 additions & 3 deletions R/catalog.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

"catalog"

NULL

#' @importFrom arrow read_parquet
#' @importFrom dplyr filter mutate select distinct `%>%` everything mutate_all bind_rows left_join rename group_by ungroup slice
#' @importFrom dplyr filter mutate select distinct `%>%` everything mutate_all bind_rows left_join rename group_by ungroup slice slice_sample
#' @importFrom glue glue
#' @importFrom terra intersect project vect crs ext relate rast crop flip `ext<-` `crs<-` `units<-` `time<-` union sprc merge units nlyr as.polygons plot extract time align ymax ymin xmax xmin plot minmax setGDALconfig is.related
#' @importFrom terra intersect project vect crs ext relate rast crop flip `ext<-` `crs<-` `units<-` `time<-` union sprc merge units nlyr as.polygons extract time align ymax ymin xmax xmin plot minmax setGDALconfig is.related is.points geomtype unique
#' @importFrom RNetCDF open.nc close.nc var.get.nc dim.inq.nc var.inq.nc utcal.nc att.get.nc
#' @importFrom future.apply future_lapply
#' @importFrom ncmeta nc_coord_var nc_grid_mapping_atts nc_gm_to_prj nc_vars nc_var nc_dims
Expand All @@ -16,4 +18,12 @@
#' @importFrom stats complete.cases
#' @importFrom grDevices blues9

NULL

#' @export
terra::plot

#' @export
dplyr::`%>%`



150 changes: 84 additions & 66 deletions R/climater_filter.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ climater_filter <- function(id = NULL,
if (nrow(catalog) == 0) {
stop("no data to filter.", call. = FALSE)
}



if(inherits(AOI, "list")){
AOI = AOI[[1]]
}

### 1 ---- varname filter
if(!is.null(varname)){
Expand All @@ -62,6 +68,17 @@ climater_filter <- function(id = NULL,
)
}
}

if(!is.null(scenario)){
if ("historical" %in% catalog$scenario) {
scenario <- c("historical", scenario)
}

if(!is.null(scenario)){
catalog <- filter(catalog, scenario %in% !!scenario)
}
}


### 2 ---- model filter
if (!is.null(model)) {
Expand Down Expand Up @@ -90,9 +107,34 @@ climater_filter <- function(id = NULL,
)
}
}


### ---- AOI filter
if(!is.null(AOI)){

gid = sapply(1:nrow(catalog), function(x) {
suppressWarnings({
tryCatch({
sum(terra::is.related(terra::project(terra::ext(AOI), crs(AOI), catalog$crs[x]),
make_vect(catalog[x,]),
"intersects")) > 0
}, error = function(e) {
FALSE
})

})
})

catalog = catalog[gid, ]

if(nrow(catalog) == 0){
stop("No data found in provided AOI.", call. = FALSE)
}

}

### 3 ---- date & scenario filter

if(!is.null(startDate)){

endDate = ifelse(is.null(endDate), as.character(startDate), as.character(endDate))
Expand All @@ -115,79 +157,55 @@ climater_filter <- function(id = NULL,
}
}

if(!is.null(scenario)){
if ("historical" %in% catalog$scenario) {
scenario <- c("historical", scenario)
}

if(!is.null(scenario)){
catalog <- filter(catalog, scenario %in% !!scenario)
}
}


### 3 ---- ensemble filter
###
if(all(is.na(ensemble)) & !is.null(model)){
catalog = filter(catalog, model %in% !!model)
} else {

if(length(ensemble) != length(model)){
catalog = catalog %>%
group_by(model, ensemble) %>%
slice(1) %>%
ungroup()
} else {
### 2 ---- ensemble filter
# If ensemble is NULL set to 1
if(is.null(ensemble)){ ensemble = 1 }
# If data has ensembles set to TRUE
eflag = any(!is.na(catalog$ensemble))

u <- unique(catalog$ensemble)

if (length(u) > 1 & is.null(ensemble)) {
warning("There are ", length(u), " ensembles available. Since ensemble was left NULL, we default to ", u[1] , call. = FALSE)
catalog <- filter(catalog, ensemble %in% u[1])
} else if(is.null(ensemble)){
catalog = catalog
} else {
if (all(ensemble %in% catalog$ensemble)) {
catalog <- filter(catalog, ensemble %in% !!ensemble)
} else {
bad <- ensemble[!ensemble %in% u]

m <- distinct(select(catalog, model, ensemble))

stop("'", bad, "' not availiable ensemble for '", catalog$id[1], "'. Try: \n\t",
paste(">", m$ensemble, collapse = "\n\t"),
call. = FALSE
if(eflag) {
if (all(ensemble %in% catalog$ensemble) | !is.numeric(ensemble)) {
catalog <- filter(catalog, ensemble %in% !!ensemble)
} else if (is.numeric(ensemble)) {

cond = any(table(catalog$model, catalog$ensemble) > ensemble)

catalog = slice_sample(catalog,
by = c('id', 'variable', 'model', "scenario"),
n = ensemble)

if (ensemble == 1 & cond) {
message(
"Multiple ensembles available per model. Since `ensemble = NULL`, we default to:\n\t> ",
paste0(catalog$model, " [", catalog$scenario, "] [", catalog$ensemble, "]",
collapse = "\n\t> ")
)

}
} else {
bad <- ensemble[!ensemble %in% catalog$ensemble]

m <- distinct(select(catalog, model, ensemble))

stop(
"'",
bad,
"' not availiable ensemble for '",
catalog$id[1],
"'. Try: \n\t",
paste(">", m$ensemble, collapse = "\n\t"),
call. = FALSE
)
}
}
}


if(nrow(catalog) == 0 ){
stop("Configuration not found.")
}
}

### ---- AOI filter
if(!is.null(AOI)){


gid = sapply(1:nrow(catalog), function(x) {
suppressWarnings({
tryCatch({
sum(terra::is.related(make_vect(catalog[x,]),
terra::project(terra::ext(AOI), crs(AOI), catalog$crs[x]),
"intersects")) > 0
}, error = function(e) {
FALSE
})

})
})

catalog = catalog[gid, ]

if(nrow(catalog) == 0){
stop("No data found in provided AOI.", call. = FALSE)
}

}

catalog[!duplicated(select(catalog, -URL)), ]
}
Loading

0 comments on commit 0fbaac5

Please sign in to comment.