From 78cad7f1cbcff89a0bd68567acc9a0ae744a2541 Mon Sep 17 00:00:00 2001 From: CeresBarros Date: Tue, 12 Dec 2023 14:45:33 -0800 Subject: [PATCH 01/22] remove deprecated `normal_inputs` --- R/normal.R | 42 ------------------------------------------ 1 file changed, 42 deletions(-) diff --git a/R/normal.R b/R/normal.R index 5af8d674..fd056510 100644 --- a/R/normal.R +++ b/R/normal.R @@ -1,45 +1,3 @@ -#' Create normal baseline input for `downscale` -#' @template normal -#' @template dem -#' @param ... Other arguments passed [lapse_rate()] for lapse rates calculation. -#' -#' @return A normal baseline to use with `downscale`. -#' A `SpatRaster` containing normals, lapse rates and digital elevation model -#' layers. -#' @importFrom terra rast writeRaster -#' @export -normal_input <- function(normal = list_normal()[1], dem = NULL, ...) { - # Check if we have data, if not download some. - data_check() - - # When using label - if (!inherits(normal, "SpatRaster")) { - # Where are normal files for this normal - dir_normal <- file.path( - data_path(), - getOption("climr.normal.path", default = "inputs_pkg/normal"), - normal - ) - file_tif <- list.files(dir_normal, full.names = TRUE, pattern = "\\.tif") - res <- rast(file_tif) - attr(res, "builder") <- "climr" - # Return preprocessed raster - return(res) - } - - # Compute lapse rates - lr <- lapse_rate(normal, dem, ...) - - # Actual writing - f <- tempfile(fileext = "tif") - writeRaster(c(normal, lr, dem), f, overwrite = TRUE, gdal = "COMPRESS=NONE") - - res <- rast(f) - attr(res, "builder") <- "climr" - - return(res) -} - #' Create normal baseline input for `downscale` from postgis database. #' @template dbCon #' @template bbox From ff8003dad3befb91becb8706d98901551c398b33 Mon Sep 17 00:00:00 2001 From: CeresBarros Date: Tue, 12 Dec 2023 14:46:24 -0800 Subject: [PATCH 02/22] rename `normal_inputs_post_gis` -> `normal_inputs` --- Example_Use.R | 2 +- NAMESPACE | 1 - R/downscale.R | 10 +++++----- R/normal.R | 2 +- man/normal_input_postgis.Rd | 6 +++--- 5 files changed, 10 insertions(+), 11 deletions(-) diff --git a/Example_Use.R b/Example_Use.R index 2fbc171b..4609a711 100644 --- a/Example_Use.R +++ b/Example_Use.R @@ -84,7 +84,7 @@ ggplot(res2[RUN != "ensembleMean" & GCM != "Observed",],aes(x = PERIOD, y = PPT, thebb <- get_bb(in_xyz) ##get bounding box based on input points dbCon <- data_connect() ##get normal -normalbc <- normal_input_postgis(dbCon = dbCon, normal = "normal_bc", bbox = thebb, cache = TRUE) +normalbc <- normal_input(dbCon = dbCon, normal = "normal_bc", bbox = thebb, cache = TRUE) plot(normalbc[[13]]) ##gcm annomalies diff --git a/NAMESPACE b/NAMESPACE index 0453c0bf..842e71d3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -24,7 +24,6 @@ export(list_run) export(list_ssp) export(list_variables) export(normal_input) -export(normal_input_postgis) export(pgGetTerra) import(data.table) import(uuid) diff --git a/R/downscale.R b/R/downscale.R index 958f4474..804ad41e 100644 --- a/R/downscale.R +++ b/R/downscale.R @@ -58,9 +58,9 @@ climr_downscale <- function(xyz, which_normal = c("auto", "BC", "NorAm"), histor message("Getting normals...") if (which_normal == "NorAm") { - normal <- normal_input_postgis(dbCon = dbCon, normal = "normal_na", bbox = thebb, cache = cache) + normal <- normal_input(dbCon = dbCon, normal = "normal_na", bbox = thebb, cache = cache) } else if (which_normal == "BC") { - normal <- normal_input_postgis(dbCon = dbCon, normal = "normal_bc", bbox = thebb, cache = cache) + normal <- normal_input(dbCon = dbCon, normal = "normal_bc", bbox = thebb, cache = cache) } else { if (ncol(xyz) == 3) xyz$ID <- 1:nrow(xyz) bc_outline <- rast(system.file("extdata", "bc_outline.tif", package = "climr")) @@ -71,9 +71,9 @@ climr_downscale <- function(xyz, which_normal = c("auto", "BC", "NorAm"), histor xyz <- xyz[!is.na(pnts$PPT01),] thebb_bc <- get_bb(xyz) message("for BC...") - normal <- normal_input_postgis(dbCon = dbCon, normal = "normal_bc", bbox = thebb_bc, cache = cache) + normal <- normal_input(dbCon = dbCon, normal = "normal_bc", bbox = thebb_bc, cache = cache) } else { - normal <- normal_input_postgis(dbCon = dbCon, normal = "normal_na", bbox = thebb, cache = cache) + normal <- normal_input(dbCon = dbCon, normal = "normal_na", bbox = thebb, cache = cache) } } @@ -145,7 +145,7 @@ climr_downscale <- function(xyz, which_normal = c("auto", "BC", "NorAm"), histor na_xyz <- xyz_save[!xyz_save[, 4] %in% bc_ids,] thebb <- get_bb(na_xyz) message("Now North America...") - normal <- normal_input_postgis(dbCon = dbCon, normal = "normal_na", bbox = thebb, cache = cache) + normal <- normal_input(dbCon = dbCon, normal = "normal_na", bbox = thebb, cache = cache) results_na <- downscale( xyz = na_xyz, diff --git a/R/normal.R b/R/normal.R index fd056510..f9bd6e40 100644 --- a/R/normal.R +++ b/R/normal.R @@ -11,7 +11,7 @@ #' @importFrom data.table fread fwrite data.table #' @importFrom uuid UUIDgenerate #' @export -normal_input_postgis <- function(dbCon, bbox = NULL, normal = "normal_na", cache = TRUE) { +normal_input <- function(dbCon, bbox = NULL, normal = "normal_na", cache = TRUE) { ## check cached if (dir.exists(paste0(cache_path(), "/normal/", normal))) { bnds <- fread(paste0(cache_path(), "/normal/", normal, "/meta_data.csv")) diff --git a/man/normal_input_postgis.Rd b/man/normal_input_postgis.Rd index d4576cb3..0a3db21f 100644 --- a/man/normal_input_postgis.Rd +++ b/man/normal_input_postgis.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/normal.R -\name{normal_input_postgis} -\alias{normal_input_postgis} +\name{normal_inputs} +\alias{normal_inputs} \title{Create normal baseline input for \code{downscale} from postgis database.} \usage{ -normal_input_postgis(dbCon, bbox = NULL, normal = "normal_na", cache = TRUE) +normal_inputs(dbCon, bbox = NULL, normal = "normal_na", cache = TRUE) } \arguments{ \item{dbCon}{A db connection object created by \code{data_connect}.} From 032179f26450595b5e917d8c29731b3a025eff73 Mon Sep 17 00:00:00 2001 From: CeresBarros Date: Tue, 12 Dec 2023 23:00:13 -0800 Subject: [PATCH 03/22] re-doc --- man/calc_bFFP.Rd | 10 ++++++---- man/calc_eFFP.Rd | 8 +++++--- man/climr_downscale.Rd | 2 +- man/normal_input.Rd | 20 ++++++++++---------- man/normal_input_postgis.Rd | 26 -------------------------- 5 files changed, 22 insertions(+), 44 deletions(-) delete mode 100644 man/normal_input_postgis.Rd diff --git a/man/calc_bFFP.Rd b/man/calc_bFFP.Rd index 1ed5f016..70ebf861 100644 --- a/man/calc_bFFP.Rd +++ b/man/calc_bFFP.Rd @@ -21,10 +21,12 @@ bFFP : Day of the year on which the Frost-Free Period begins } \examples{ \dontrun{ -t_min_list <- list("1" = -35, "2" = -32, "3" = -25, "4" = -10, -"5" = -5, "6" = 3, "7" = 15, "8" = 17, "9" = 10, "10" = -5, -"11" = -20, "12" = -30) +t_min_list <- list( + "1" = -35, "2" = -32, "3" = -25, "4" = -10, + "5" = -5, "6" = 3, "7" = 15, "8" = 17, "9" = 10, "10" = -5, + "11" = -20, "12" = -30 +) -climr:::calc_bFFP(td = 30, NFFD= 10, t_min_list = t_min_list) +climr:::calc_bFFP(td = 30, NFFD = 10, t_min_list = t_min_list) } } diff --git a/man/calc_eFFP.Rd b/man/calc_eFFP.Rd index 0b2c2855..6d1360dc 100644 --- a/man/calc_eFFP.Rd +++ b/man/calc_eFFP.Rd @@ -19,9 +19,11 @@ bFFP : Day of the year on which the Frost-Free Period ends } \examples{ \dontrun{ -t_min_list <- list("1" = -35, "2" = -32, "3" = -25, "4" = -10, -"5" = -5, "6" = 3, "7" = 15, "8" = 17, "9" = 10, "10" = -5, -"11" = -20, "12" = -30) +t_min_list <- list( + "1" = -35, "2" = -32, "3" = -25, "4" = -10, + "5" = -5, "6" = 3, "7" = 15, "8" = 17, "9" = 10, "10" = -5, + "11" = -20, "12" = -30 +) climr:::calc_eFFP(NFFD = 10, t_min_list = t_min_list) } diff --git a/man/climr_downscale.Rd b/man/climr_downscale.Rd index 560260e0..b87563f0 100644 --- a/man/climr_downscale.Rd +++ b/man/climr_downscale.Rd @@ -59,7 +59,7 @@ Downscale and Calculate climate variables for points of interest } \examples{ { -##TODO. + ## TODO. } } diff --git a/man/normal_input.Rd b/man/normal_input.Rd index 29e4dbd4..67e8c9dd 100644 --- a/man/normal_input.Rd +++ b/man/normal_input.Rd @@ -2,25 +2,25 @@ % Please edit documentation in R/normal.R \name{normal_input} \alias{normal_input} -\title{Create normal baseline input for \code{downscale}} +\title{Create normal baseline input for \code{downscale} from postgis database.} \usage{ -normal_input(normal = list_normal()[1], dem = NULL, ...) +normal_input(dbCon, bbox = NULL, normal = "normal_na", cache = TRUE) } \arguments{ +\item{dbCon}{A db connection object created by \code{data_connect}.} + +\item{bbox}{Numeric vector of length 4 giving bounding box of study region, created by \code{get_bb()}.} + \item{normal}{A character or a SpatRaster. For character, label of the normal baseline to use. Can be obtained from \code{list_normal()}. For SpatRaster, 36 layers normal climate variables with names PPT01:PPT12, Tmax01:Tmax12 and Tmin01:Tmin12. Default to \code{list_normal()[1]}.} -\item{dem}{A digital elevation model SpatRaster. Only needed if normal is a -SpatRaster. Defaults to \code{NULL}.} - -\item{...}{Other arguments passed \code{\link[=lapse_rate]{lapse_rate()}} for lapse rates calculation.} +\item{cache}{Logical specifying whether to cache new data locally or no. Defaults to \code{TRUE}.} } \value{ -A normal baseline to use with \code{downscale}. -A \code{SpatRaster} containing normals, lapse rates and digital elevation model -layers. +A normal baseline to use with \code{downscale}. A \code{SpatRaster} containing normals, lapse rates +and digital elevation model layers. } \description{ -Create normal baseline input for \code{downscale} +Create normal baseline input for \code{downscale} from postgis database. } diff --git a/man/normal_input_postgis.Rd b/man/normal_input_postgis.Rd deleted file mode 100644 index 0a3db21f..00000000 --- a/man/normal_input_postgis.Rd +++ /dev/null @@ -1,26 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/normal.R -\name{normal_inputs} -\alias{normal_inputs} -\title{Create normal baseline input for \code{downscale} from postgis database.} -\usage{ -normal_inputs(dbCon, bbox = NULL, normal = "normal_na", cache = TRUE) -} -\arguments{ -\item{dbCon}{A db connection object created by \code{data_connect}.} - -\item{bbox}{Numeric vector of length 4 giving bounding box of study region, created by \code{get_bb()}.} - -\item{normal}{A character or a SpatRaster. For character, label of the normal baseline to use. -Can be obtained from \code{list_normal()}. For SpatRaster, 36 layers normal climate variables with -names PPT01:PPT12, Tmax01:Tmax12 and Tmin01:Tmin12. Default to \code{list_normal()[1]}.} - -\item{cache}{Logical specifying whether to cache new data locally or no. Defaults to \code{TRUE}.} -} -\value{ -A normal baseline to use with \code{downscale}. A \code{SpatRaster} containing normals, lapse rates -and digital elevation model layers. -} -\description{ -Create normal baseline input for \code{downscale} from postgis database. -} From b3903fe0004aae91c26ffef2713d1a5721dd4895 Mon Sep 17 00:00:00 2001 From: CeresBarros Date: Tue, 12 Dec 2023 23:07:13 -0800 Subject: [PATCH 04/22] attempts to fix example - not working yet --- R/downscale.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/R/downscale.R b/R/downscale.R index 804ad41e..bdc14212 100644 --- a/R/downscale.R +++ b/R/downscale.R @@ -191,8 +191,10 @@ climr_downscale <- function(xyz, which_normal = c("auto", "BC", "NorAm"), histor #' @export #' @examples #' \dontrun{ +#' dbCon <- data_connect() #' xyz <- data.frame(lon = runif(10, -140, -106), lat = runif(10, 37, 61), elev = runif(10)) -#' normal <- normal_input() +#' thebb <- get_bb(xyz) ##get bounding box based on input points +#' normal <- normal_input(dbCon = dbCon, bbox = thebb, cache = TRUE) #' gcm_input <- gcm_input(list_gcm()[3], list_ssp()[1], list_period()[2]) #' downscale(xyz, normal, gcm) #' historic <- historic_input() From a18bf27a31146d74238d31a764d784c54b9f18ef Mon Sep 17 00:00:00 2001 From: CeresBarros Date: Wed, 13 Dec 2023 11:03:34 -0800 Subject: [PATCH 05/22] minor: indentations --- R/dbGetRaster.R | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/R/dbGetRaster.R b/R/dbGetRaster.R index 2cf9cf0f..b4458c47 100644 --- a/R/dbGetRaster.R +++ b/R/dbGetRaster.R @@ -21,14 +21,14 @@ pgGetTerra <- function(conn, name, tile, rast = "rast", bands = 37:73, name1 <- name nameque <- paste(name1, collapse = ".") namechar <- gsub("'", "''", paste(gsub('^"|"$', "", name1), collapse = ".")) - + ## rast query name rastque <- dbQuoteIdentifier(conn, rast) - + projID <- dbGetQuery(conn, paste0("select ST_SRID(", rastque, ") as srid from ", nameque, " where rid = 1;"))$srid[1] - - - + + + if (length(bands) > 1664) { ## maximum number of columns info <- dbGetQuery(conn, paste0( "select @@ -52,7 +52,7 @@ pgGetTerra <- function(conn, name, tile, rast = "rast", bands = 37:73, bands_temp <- bands[brks[i]:(brks[i + 1] - 1)] bandqs1 <- paste0("UNNEST(ST_Dumpvalues(rast, ", bands_temp, ")) as vals_", bands_temp) bandqs2 <- paste0("ST_Union(rast", rastque, ",", bands_temp, ") rast_", bands_temp) - + rast_vals_temp <- dbGetQuery(conn, paste0( "SELECT ", paste(bandqs1, collapse = ","), " from (SELECT ST_Union(rast) rast FROM ", nameque, " WHERE ST_Intersects(", @@ -84,7 +84,7 @@ pgGetTerra <- function(conn, name, tile, rast = "rast", bands = 37:73, # if(boundary[1] - boundary[2] > max_dist | boundary[3] - boundary[4] > max_dist) { x_seq <- unique(c(seq(boundary[2], boundary[1], by = max_dist), boundary[1])) y_seq <- unique(c(seq(boundary[4], boundary[3], by = max_dist), boundary[3])) - + boundary_ls <- list() if (length(x_seq) < 2 | length(y_seq) < 2) { boundary_ls[["11"]] <- boundary @@ -95,8 +95,8 @@ pgGetTerra <- function(conn, name, tile, rast = "rast", bands = 37:73, } } } - - + + r_list <- lapply(boundary_ls, FUN = make_raster) r_list <- r_list[!sapply(r_list, is.null)] if (length(r_list) > 1) { @@ -194,15 +194,15 @@ make_raster <- function(boundary) { cat(".") info <- dbGetQuery(conn, paste0( "select - st_xmax(st_envelope(rast)) as xmx, - st_xmin(st_envelope(rast)) as xmn, - st_ymax(st_envelope(rast)) as ymx, - st_ymin(st_envelope(rast)) as ymn, - st_width(rast) as cols, - st_height(rast) as rows - from - (select st_union(", rastque, ",", 1, ") rast from ", nameque, "\n - WHERE ST_Intersects(", + st_xmax(st_envelope(rast)) as xmx, + st_xmin(st_envelope(rast)) as xmn, + st_ymax(st_envelope(rast)) as ymx, + st_ymin(st_envelope(rast)) as ymn, + st_width(rast) as cols, + st_height(rast) as rows + from + (select st_union(", rastque, ",", 1, ") rast from ", nameque, "\n + WHERE ST_Intersects(", rastque, ",ST_SetSRID(ST_GeomFromText('POLYGON((", boundary[4], " ", boundary[1], ",", boundary[4], " ", boundary[2], ",\n ", boundary[3], " ", boundary[2], ",", boundary[3], From 78e8368a6fc1e47e9ce96ff536ba2c28fb3eb50f Mon Sep 17 00:00:00 2001 From: CeresBarros Date: Wed, 13 Dec 2023 11:04:01 -0800 Subject: [PATCH 06/22] make_raster: add missing args --- R/dbGetRaster.R | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/R/dbGetRaster.R b/R/dbGetRaster.R index b4458c47..5fc16696 100644 --- a/R/dbGetRaster.R +++ b/R/dbGetRaster.R @@ -77,7 +77,7 @@ pgGetTerra <- function(conn, name, tile, rast = "rast", bands = 37:73, ) } else { if (!tile) { - rout <- make_raster(boundary) + rout <- make_raster(boundary, conn, rastque, nameque, projID, bands) return(rout) } max_dist <- 5 @@ -97,7 +97,10 @@ pgGetTerra <- function(conn, name, tile, rast = "rast", bands = 37:73, } - r_list <- lapply(boundary_ls, FUN = make_raster) + r_list <- lapply(boundary_ls, FUN = make_raster, + conn = conn, rastque = rastque, + nameque = nameque, projID = projID, + bands = bands) r_list <- r_list[!sapply(r_list, is.null)] if (length(r_list) > 1) { rout <- merge(sprc(r_list)) @@ -178,19 +181,24 @@ get_bb <- function(in_xyz) { } + #' Make raster from a boundary #' #' Used internally to access the PostGRS database and #' create a SpatRaster using a given spatial boundary #' -#' @template boundary +#' @param boundary +#' @param conn +#' @param rastque +#' @param nameque +#' @param projID +#' @param bands #' #' @return a SpatRaster #' #' @importFrom DBI dbGetQuery #' @importFrom terra rast -#' -make_raster <- function(boundary) { +make_raster <- function(boundary, conn, rastque, nameque, projID, bands) { cat(".") info <- dbGetQuery(conn, paste0( "select From c525583f5752e3a70dd863a37f1ed6e73bc8a11a Mon Sep 17 00:00:00 2001 From: CeresBarros Date: Wed, 13 Dec 2023 11:06:34 -0800 Subject: [PATCH 07/22] fix missing import --- R/dbGetRaster.R | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/R/dbGetRaster.R b/R/dbGetRaster.R index 5fc16696..6843bf3b 100644 --- a/R/dbGetRaster.R +++ b/R/dbGetRaster.R @@ -10,11 +10,10 @@ #' @return A SpatRaster #' #' @importFrom data.table setDT copy -#' @importFrom terra rast merge +#' @importFrom terra rast merge sprc #' @importFrom RPostgres dbQuoteIdentifier dbGetQuery #' @importFrom DBI dbQuoteIdentifier dbGetQuery #' @export - pgGetTerra <- function(conn, name, tile, rast = "rast", bands = 37:73, boundary = NULL) { ## Check and prepare the schema.name From 93f62b7f0288847e9952302d6740d311b43bd37c Mon Sep 17 00:00:00 2001 From: CeresBarros Date: Wed, 13 Dec 2023 11:07:07 -0800 Subject: [PATCH 08/22] cull global vars (they are now fun args) --- R/globalVars.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/globalVars.R b/R/globalVars.R index da63632f..a9bf5c40 100644 --- a/R/globalVars.R +++ b/R/globalVars.R @@ -1,9 +1,9 @@ utils::globalVariables(c( "Period", "Period1", "Period2", "Run", - "Scenario", "Year", "bands", "conn", + "Scenario", "Year", "fullnm", "laynum", "list_gcm_ts", - "list_period", "list_years_ts", "mod", "nameque", - "numlay", "period", "projID", "rastque", "run", "sprc", "var" + "list_period", "list_years_ts", "mod", + "numlay", "period", "run", "var" )) # From 01a6556809b34bbe6a0ecf8dd0673d0777460119 Mon Sep 17 00:00:00 2001 From: CeresBarros Date: Wed, 13 Dec 2023 11:13:06 -0800 Subject: [PATCH 09/22] replace old function name --- R/gcm.R | 5 ++++- R/globalVars.R | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/R/gcm.R b/R/gcm.R index fb169e03..9fd5186d 100644 --- a/R/gcm.R +++ b/R/gcm.R @@ -205,6 +205,7 @@ gcm_hist_input <- function(dbCon, bbox = NULL, gcm = list_gcm(), years = 1901:19 # period <- 2020:2050 #' Create gcm timeseries input for `downscale` using data on Postgis database. +#' #' @template dbCon #' @template bbox #' @template gcm @@ -212,14 +213,16 @@ gcm_hist_input <- function(dbCon, bbox = NULL, gcm = list_gcm(), years = 1901:19 #' @param years Numeric or character vector in in `2020:2100` #' @template max_run #' @param cache Logical specifying whether to cache new data locally or no. Default `TRUE` +#' #' @return An object to use with `downscale`. A list of `SpatRaster` with, possibly, multiple layers. +#' #' @importFrom terra rast writeRaster ext nlyr #' @importFrom utils head #' @importFrom RPostgres dbGetQuery #' @import uuid #' @import data.table #' @export -gcm_ts_input <- function(dbCon, bbox = NULL, gcm = list_gcm_ts(), ssp = list_ssp(), years = list_years_ts(), max_run = 0L, cache = TRUE) { +gcm_ts_input <- function(dbCon, bbox = NULL, gcm = list_gcm(), ssp = list_ssp(), years = list_years_ts(), max_run = 0L, cache = TRUE) { period <- years dbnames <- structure(list( GCM = c( diff --git a/R/globalVars.R b/R/globalVars.R index a9bf5c40..df3be400 100644 --- a/R/globalVars.R +++ b/R/globalVars.R @@ -1,7 +1,7 @@ utils::globalVariables(c( "Period", "Period1", "Period2", "Run", "Scenario", "Year", - "fullnm", "laynum", "list_gcm_ts", + "fullnm", "laynum", "list_period", "list_years_ts", "mod", "numlay", "period", "run", "var" )) From 425fd05b2a50d6c4263517de623c859da7778218 Mon Sep 17 00:00:00 2001 From: CeresBarros Date: Wed, 13 Dec 2023 11:15:38 -0800 Subject: [PATCH 10/22] re-doc --- NAMESPACE | 1 + R/downscale.R | 11 +++++++++-- man/downscale.Rd | 7 ++++++- man/gcm_ts_input.Rd | 2 +- man/make_raster.Rd | 14 ++++++++++++-- 5 files changed, 29 insertions(+), 6 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 842e71d3..b0a91ab9 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -68,6 +68,7 @@ importFrom(terra,nlyr) importFrom(terra,rast) importFrom(terra,resample) importFrom(terra,sources) +importFrom(terra,sprc) importFrom(terra,writeRaster) importFrom(terra,xres) importFrom(terra,yres) diff --git a/R/downscale.R b/R/downscale.R index bdc14212..aa5afd99 100644 --- a/R/downscale.R +++ b/R/downscale.R @@ -169,6 +169,7 @@ climr_downscale <- function(xyz, which_normal = c("auto", "BC", "NorAm"), histor #' Downscale target rasters to points of interest +#' #' @param xyz A 3-column matrix or data.frame (x, y, z) or (lon, lat, elev). #' @param normal Reference normal baseline input from `normal_input`. #' @param gcm Global Circulation Models input from `gcm_input`. Default to NULL. @@ -182,19 +183,25 @@ climr_downscale <- function(xyz, which_normal = c("auto", "BC", "NorAm"), histor #' `variables` dataset. Default to monthly PPT, Tmax, Tmin. #' @param ppt_lr A boolean. Apply lapse rate adjustment to precipitations. Default to FALSE. #' @param nthread An integer. Number of parallel threads to use to do computations. Default to 1L. +#' #' @import data.table #' @importFrom terra extract rast sources ext xres yres crop #' @importFrom parallel makeForkCluster makePSOCKcluster stopCluster splitIndices parLapply +#' #' @return A downscaled dataset. If `gcm` is NULL, this is just the downscaled `normal` #' at point locations. If `gcm` is provided, this returns a downscaled dataset for each #' point location, general circulation model, shared socioeconomic pathway, run and period. +#' #' @export #' @examples #' \dontrun{ #' dbCon <- data_connect() #' xyz <- data.frame(lon = runif(10, -140, -106), lat = runif(10, 37, 61), elev = runif(10)) -#' thebb <- get_bb(xyz) ##get bounding box based on input points -#' normal <- normal_input(dbCon = dbCon, bbox = thebb, cache = TRUE) +#' ## get bounding box based on input points +#' thebb <- get_bb(xyz) +#' normal <- normal_input(dbCon = dbCon, bbox = thebb, cache = TRUE) +#' +#' ## pick one GCM, one SSP and one period from the list of available options #' gcm_input <- gcm_input(list_gcm()[3], list_ssp()[1], list_period()[2]) #' downscale(xyz, normal, gcm) #' historic <- historic_input() diff --git a/man/downscale.Rd b/man/downscale.Rd index 3ac46c58..1f761267 100644 --- a/man/downscale.Rd +++ b/man/downscale.Rd @@ -53,8 +53,13 @@ Downscale target rasters to points of interest } \examples{ \dontrun{ +dbCon <- data_connect() xyz <- data.frame(lon = runif(10, -140, -106), lat = runif(10, 37, 61), elev = runif(10)) -normal <- normal_input() +## get bounding box based on input points +thebb <- get_bb(xyz) +normal <- normal_input(dbCon = dbCon, bbox = thebb, cache = TRUE) + +## pick one GCM, one SSP and one period from the list of available options gcm_input <- gcm_input(list_gcm()[3], list_ssp()[1], list_period()[2]) downscale(xyz, normal, gcm) historic <- historic_input() diff --git a/man/gcm_ts_input.Rd b/man/gcm_ts_input.Rd index 9d9fd5ed..c7808d3f 100644 --- a/man/gcm_ts_input.Rd +++ b/man/gcm_ts_input.Rd @@ -7,7 +7,7 @@ gcm_ts_input( dbCon, bbox = NULL, - gcm = list_gcm_ts(), + gcm = list_gcm(), ssp = list_ssp(), years = list_years_ts(), max_run = 0L, diff --git a/man/make_raster.Rd b/man/make_raster.Rd index 8c93fc78..9e792cd6 100644 --- a/man/make_raster.Rd +++ b/man/make_raster.Rd @@ -4,10 +4,20 @@ \alias{make_raster} \title{Make raster from a boundary} \usage{ -make_raster(boundary) +make_raster(boundary, conn, rastque, nameque, projID, bands) } \arguments{ -\item{boundary}{Numeric vector of length 4 (ymax,ymin,xmax,xmin). Defaults to \code{NULL}.} +\item{boundary}{} + +\item{conn}{} + +\item{rastque}{} + +\item{nameque}{} + +\item{projID}{} + +\item{bands}{} } \value{ a SpatRaster From 5f8e23c591837ff144707af21b5f77b589ee41e4 Mon Sep 17 00:00:00 2001 From: CeresBarros Date: Wed, 13 Dec 2023 11:28:29 -0800 Subject: [PATCH 11/22] `gcm_input_postgis` renamed to `gcm_input` --- Example_Use.R | 2 +- NAMESPACE | 2 +- R/downscale.R | 2 +- R/gcm.R | 2 +- README.md | 2 +- man/downscale.Rd | 3 +++ man/{gcm_input_postgis.Rd => gcm_input.Rd} | 6 +++--- 7 files changed, 11 insertions(+), 8 deletions(-) rename man/{gcm_input_postgis.Rd => gcm_input.Rd} (95%) diff --git a/Example_Use.R b/Example_Use.R index 4609a711..baf12131 100644 --- a/Example_Use.R +++ b/Example_Use.R @@ -88,7 +88,7 @@ normalbc <- normal_input(dbCon = dbCon, normal = "normal_bc", bbox = thebb, cach plot(normalbc[[13]]) ##gcm annomalies -gcm <- gcm_input_postgis(dbCon, bbox = thebb, gcm = c("ACCESS-ESM1-5", "EC-Earth3"), +gcm <- gcm_input(dbCon, bbox = thebb, gcm = c("ACCESS-ESM1-5", "EC-Earth3"), ssp = c("ssp370"), period = c("2021_2040","2041_2060","2061_2080"), max_run = 0, diff --git a/NAMESPACE b/NAMESPACE index b0a91ab9..c9781580 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -9,7 +9,7 @@ export(data_prepare) export(data_update) export(downscale) export(gcm_hist_input) -export(gcm_input_postgis) +export(gcm_input) export(gcm_ts_input) export(get_bb) export(historic_input) diff --git a/R/downscale.R b/R/downscale.R index aa5afd99..dca17737 100644 --- a/R/downscale.R +++ b/R/downscale.R @@ -87,7 +87,7 @@ climr_downscale <- function(xyz, which_normal = c("auto", "BC", "NorAm"), histor if (!is.null(gcm_models)) { if (!is.null(gcm_period)) { message("Getting GCMs...") - gcm <- gcm_input_postgis(dbCon, + gcm <- gcm_input(dbCon, bbox = thebb, gcm = gcm_models, ssp = ssp, period = gcm_period, diff --git a/R/gcm.R b/R/gcm.R index 9fd5186d..ce841ada 100644 --- a/R/gcm.R +++ b/R/gcm.R @@ -13,7 +13,7 @@ #' @importFrom uuid UUIDgenerate #' @import data.table #' @export -gcm_input_postgis <- function(dbCon, bbox = NULL, gcm = list_gcm(), ssp = list_ssp(), period = list_period(), max_run = 0L, cache = TRUE) { +gcm_input <- function(dbCon, bbox = NULL, gcm = list_gcm(), ssp = list_ssp(), period = list_period(), max_run = 0L, cache = TRUE) { dbnames <- structure(list( GCM = c( "ACCESS-ESM1-5", "BCC-CSM2-MR", "CanESM5", diff --git a/README.md b/README.md index 2a496510..f06b9419 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ normal <- normal_input(dbCon = dbCon, bbox = thebb, cache = TRUE) ##get normal d plot(normal[[1]]) ##get GCM anomolies (20 yr periods) -gcm <- gcm_input_postgis(dbCon, bbox = thebb, gcm = c("ACCESS-ESM1-5", "EC-Earth3"), +gcm <- gcm_input(dbCon, bbox = thebb, gcm = c("ACCESS-ESM1-5", "EC-Earth3"), ssp = c("ssp370"), period = c("2021_2040","2041_2060","2061_2080"), max_run = 0, diff --git a/man/downscale.Rd b/man/downscale.Rd index 1f761267..6785e2ec 100644 --- a/man/downscale.Rd +++ b/man/downscale.Rd @@ -51,6 +51,9 @@ point location, general circulation model, shared socioeconomic pathway, run and \description{ Downscale target rasters to points of interest } +\details{ +Additional details... TODO. +} \examples{ \dontrun{ dbCon <- data_connect() diff --git a/man/gcm_input_postgis.Rd b/man/gcm_input.Rd similarity index 95% rename from man/gcm_input_postgis.Rd rename to man/gcm_input.Rd index 197d6d0b..70b903d3 100644 --- a/man/gcm_input_postgis.Rd +++ b/man/gcm_input.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/gcm.R -\name{gcm_input_postgis} -\alias{gcm_input_postgis} +\name{gcm_input} +\alias{gcm_input} \title{Create gcm input for \code{downscale} using data on Postgis database.} \usage{ -gcm_input_postgis( +gcm_input( dbCon, bbox = NULL, gcm = list_gcm(), From 87d9592c92f365fcad15d01a26c66aef01ac4cc1 Mon Sep 17 00:00:00 2001 From: CeresBarros Date: Wed, 13 Dec 2023 11:28:50 -0800 Subject: [PATCH 12/22] need more detail in `downscale` doc --- R/downscale.R | 3 +++ 1 file changed, 3 insertions(+) diff --git a/R/downscale.R b/R/downscale.R index dca17737..efa30b43 100644 --- a/R/downscale.R +++ b/R/downscale.R @@ -170,6 +170,9 @@ climr_downscale <- function(xyz, which_normal = c("auto", "BC", "NorAm"), histor #' Downscale target rasters to points of interest #' +#' @details +#' Additional details... TODO. +#' #' @param xyz A 3-column matrix or data.frame (x, y, z) or (lon, lat, elev). #' @param normal Reference normal baseline input from `normal_input`. #' @param gcm Global Circulation Models input from `gcm_input`. Default to NULL. From 95b6440c3b395c33789a25df2c61df52d7480bf9 Mon Sep 17 00:00:00 2001 From: CeresBarros Date: Wed, 13 Dec 2023 11:32:03 -0800 Subject: [PATCH 13/22] `list_period` and `list_years_ts` are now `list_gcm_period` --- R/downscale.R | 4 ++-- R/gcm.R | 6 +++--- R/globalVars.R | 6 ++---- man-roxygen/period.R | 2 +- man/climr_downscale.Rd | 2 +- man/downscale.Rd | 2 +- man/gcm_hist_input.Rd | 2 +- man/gcm_input.Rd | 4 ++-- man/gcm_ts_input.Rd | 2 +- man/historic_input.Rd | 2 +- 10 files changed, 15 insertions(+), 17 deletions(-) diff --git a/R/downscale.R b/R/downscale.R index efa30b43..1be96d45 100644 --- a/R/downscale.R +++ b/R/downscale.R @@ -6,7 +6,7 @@ #' @param historic_ts Historic years requested. Must be in `1902:2015`. Default `NULL` #' @param gcm_models Vector of GCM names. Options are `list_gcm()`. Used for gcm periods, gcm timeseries, and historic timeseries. Default `NULL` #' @template ssp -#' @param gcm_period Requested future periods. Options are `list_period()` +#' @param gcm_period Requested future periods. Options are `list_gcm_period()` #' @param gcm_ts_years Requested future timeseries years. Must be in `2015:2100` #' @param gcm_hist_years Requested historic modelled years. Must be in `1851:2010` #' @template max_run @@ -205,7 +205,7 @@ climr_downscale <- function(xyz, which_normal = c("auto", "BC", "NorAm"), histor #' normal <- normal_input(dbCon = dbCon, bbox = thebb, cache = TRUE) #' #' ## pick one GCM, one SSP and one period from the list of available options -#' gcm_input <- gcm_input(list_gcm()[3], list_ssp()[1], list_period()[2]) +#' gcm_input <- gcm_input(list_gcm()[3], list_ssp()[1], list_gcm_period()[2]) #' downscale(xyz, normal, gcm) #' historic <- historic_input() #' out <- downscale(xyz, normal, gcm = NULL, historic = historic, ppt_lr = FALSE) diff --git a/R/gcm.R b/R/gcm.R index ce841ada..a9f3138d 100644 --- a/R/gcm.R +++ b/R/gcm.R @@ -13,7 +13,7 @@ #' @importFrom uuid UUIDgenerate #' @import data.table #' @export -gcm_input <- function(dbCon, bbox = NULL, gcm = list_gcm(), ssp = list_ssp(), period = list_period(), max_run = 0L, cache = TRUE) { +gcm_input <- function(dbCon, bbox = NULL, gcm = list_gcm(), ssp = list_ssp(), period = list_gcm_period(), max_run = 0L, cache = TRUE) { dbnames <- structure(list( GCM = c( "ACCESS-ESM1-5", "BCC-CSM2-MR", "CanESM5", @@ -107,7 +107,7 @@ gcm_input <- function(dbCon, bbox = NULL, gcm = list_gcm(), ssp = list_ssp(), pe #' @template bbox #' @template gcm #' @param years Numeric vector of desired years. Must be in `1851:2015`. -#' Can be obtained from `list_period()`. Default to `list_period()`. +#' Can be obtained from `list_gcm_period()`. Default to `list_gcm_period()`. #' @template max_run #' @param cache Logical specifying whether to cache new data locally or no. Default `TRUE` #' @return An object to use with `downscale`. A list of `SpatRaster` with, possibly, multiple layers. @@ -222,7 +222,7 @@ gcm_hist_input <- function(dbCon, bbox = NULL, gcm = list_gcm(), years = 1901:19 #' @import uuid #' @import data.table #' @export -gcm_ts_input <- function(dbCon, bbox = NULL, gcm = list_gcm(), ssp = list_ssp(), years = list_years_ts(), max_run = 0L, cache = TRUE) { +gcm_ts_input <- function(dbCon, bbox = NULL, gcm = list_gcm(), ssp = list_ssp(), years = list_gcm_period(), max_run = 0L, cache = TRUE) { period <- years dbnames <- structure(list( GCM = c( diff --git a/R/globalVars.R b/R/globalVars.R index df3be400..df6e9c67 100644 --- a/R/globalVars.R +++ b/R/globalVars.R @@ -1,9 +1,7 @@ utils::globalVariables(c( "Period", "Period1", "Period2", "Run", - "Scenario", "Year", - "fullnm", "laynum", - "list_period", "list_years_ts", "mod", - "numlay", "period", "run", "var" + "Scenario", "Year", "fullnm", "laynum", + "mod", "numlay", "period", "run", "var" )) # diff --git a/man-roxygen/period.R b/man-roxygen/period.R index 5df77d8d..b1fbd238 100644 --- a/man-roxygen/period.R +++ b/man-roxygen/period.R @@ -1,2 +1,2 @@ #' @param period A character vector. Label of the period to use. -#' Can be obtained from `list_period()`. Default to `list_period()`. +#' Can be obtained from `list_gcm_period()`. Default to `list_gcm_period()`. diff --git a/man/climr_downscale.Rd b/man/climr_downscale.Rd index b87563f0..cca3ecf0 100644 --- a/man/climr_downscale.Rd +++ b/man/climr_downscale.Rd @@ -34,7 +34,7 @@ climr_downscale( \item{ssp}{A character vector. Label of the shared socioeconomic pathways to use. See available options with \code{list_ssp()}. Defaults to \code{list_ssp()}.} -\item{gcm_period}{Requested future periods. Options are \code{list_period()}} +\item{gcm_period}{Requested future periods. Options are \code{list_gcm_period()}} \item{gcm_ts_years}{Requested future timeseries years. Must be in \code{2015:2100}} diff --git a/man/downscale.Rd b/man/downscale.Rd index 6785e2ec..6a59411a 100644 --- a/man/downscale.Rd +++ b/man/downscale.Rd @@ -63,7 +63,7 @@ thebb <- get_bb(xyz) normal <- normal_input(dbCon = dbCon, bbox = thebb, cache = TRUE) ## pick one GCM, one SSP and one period from the list of available options -gcm_input <- gcm_input(list_gcm()[3], list_ssp()[1], list_period()[2]) +gcm_input <- gcm_input(list_gcm()[3], list_ssp()[1], list_gcm_period()[2]) downscale(xyz, normal, gcm) historic <- historic_input() out <- downscale(xyz, normal, gcm = NULL, historic = historic, ppt_lr = FALSE) diff --git a/man/gcm_hist_input.Rd b/man/gcm_hist_input.Rd index 2660194b..bd340fe0 100644 --- a/man/gcm_hist_input.Rd +++ b/man/gcm_hist_input.Rd @@ -22,7 +22,7 @@ gcm_hist_input( Can be obtained from \code{list_gcm()}. Default to \code{list_gcm()}.} \item{years}{Numeric vector of desired years. Must be in \code{1851:2015}. -Can be obtained from \code{list_period()}. Default to \code{list_period()}.} +Can be obtained from \code{list_gcm_period()}. Default to \code{list_gcm_period()}.} \item{max_run}{integer. Maximum number of model runs to include. A value of 0 returns the \code{ensembleMean} only. Runs are included in the order they diff --git a/man/gcm_input.Rd b/man/gcm_input.Rd index 70b903d3..996a9667 100644 --- a/man/gcm_input.Rd +++ b/man/gcm_input.Rd @@ -9,7 +9,7 @@ gcm_input( bbox = NULL, gcm = list_gcm(), ssp = list_ssp(), - period = list_period(), + period = list_gcm_period(), max_run = 0L, cache = TRUE ) @@ -26,7 +26,7 @@ Can be obtained from \code{list_gcm()}. Default to \code{list_gcm()}.} See available options with \code{list_ssp()}. Defaults to \code{list_ssp()}.} \item{period}{A character vector. Label of the period to use. -Can be obtained from \code{list_period()}. Default to \code{list_period()}.} +Can be obtained from \code{list_gcm_period()}. Default to \code{list_gcm_period()}.} \item{max_run}{integer. Maximum number of model runs to include. A value of 0 returns the \code{ensembleMean} only. Runs are included in the order they diff --git a/man/gcm_ts_input.Rd b/man/gcm_ts_input.Rd index c7808d3f..b27fc1f8 100644 --- a/man/gcm_ts_input.Rd +++ b/man/gcm_ts_input.Rd @@ -9,7 +9,7 @@ gcm_ts_input( bbox = NULL, gcm = list_gcm(), ssp = list_ssp(), - years = list_years_ts(), + years = list_gcm_period(), max_run = 0L, cache = TRUE ) diff --git a/man/historic_input.Rd b/man/historic_input.Rd index e1ab9e77..9e06465e 100644 --- a/man/historic_input.Rd +++ b/man/historic_input.Rd @@ -12,7 +12,7 @@ historic_input(dbCon, bbox = NULL, period = list_historic(), cache = TRUE) \item{bbox}{Numeric vector of length 4 giving bounding box of study region, created by \code{get_bb()}.} \item{period}{A character vector. Label of the period to use. -Can be obtained from \code{list_period()}. Default to \code{list_period()}.} +Can be obtained from \code{list_gcm_period()}. Default to \code{list_gcm_period()}.} \item{cache}{Logical specifying whether to cache new data locally or no. Defaults to \code{TRUE}.} } From e3a57a26d1157150c549adc0d12815a7d8f4e5e2 Mon Sep 17 00:00:00 2001 From: CeresBarros Date: Wed, 13 Dec 2023 11:40:05 -0800 Subject: [PATCH 14/22] fix missing args in example --- R/downscale.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/downscale.R b/R/downscale.R index 1be96d45..1035d215 100644 --- a/R/downscale.R +++ b/R/downscale.R @@ -205,7 +205,7 @@ climr_downscale <- function(xyz, which_normal = c("auto", "BC", "NorAm"), histor #' normal <- normal_input(dbCon = dbCon, bbox = thebb, cache = TRUE) #' #' ## pick one GCM, one SSP and one period from the list of available options -#' gcm_input <- gcm_input(list_gcm()[3], list_ssp()[1], list_gcm_period()[2]) +#' gcm_input <- gcm_input(dbCon, thebb, gcm = list_gcm()[3], list_ssp()[1], list_gcm_period()[2]) #' downscale(xyz, normal, gcm) #' historic <- historic_input() #' out <- downscale(xyz, normal, gcm = NULL, historic = historic, ppt_lr = FALSE) From e27cc218c4975019d44f6dda89d66b7783ee8ff6 Mon Sep 17 00:00:00 2001 From: CeresBarros Date: Wed, 13 Dec 2023 11:40:23 -0800 Subject: [PATCH 15/22] need additional detail in `climr_downscale` doc --- R/downscale.R | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/R/downscale.R b/R/downscale.R index 1035d215..c20df9fa 100644 --- a/R/downscale.R +++ b/R/downscale.R @@ -1,5 +1,8 @@ #' Downscale and Calculate climate variables for points of interest - +#' +#' @details +#' Additional details... TODO. +#' #' @param xyz three or four column data.frame: long, lat, elev, (id) #' @param which_normal Select which normal layer to use. Default is "auto", which selects the highest resolution normal for each point #' @param historic_period Which historic period. Default `NULL` From 587359c63b3d17c150774c2233b4e9a05aec31c8 Mon Sep 17 00:00:00 2001 From: CeresBarros Date: Wed, 13 Dec 2023 11:51:26 -0800 Subject: [PATCH 16/22] missing args in example --- R/downscale.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/downscale.R b/R/downscale.R index c20df9fa..b32a424b 100644 --- a/R/downscale.R +++ b/R/downscale.R @@ -210,7 +210,7 @@ climr_downscale <- function(xyz, which_normal = c("auto", "BC", "NorAm"), histor #' ## pick one GCM, one SSP and one period from the list of available options #' gcm_input <- gcm_input(dbCon, thebb, gcm = list_gcm()[3], list_ssp()[1], list_gcm_period()[2]) #' downscale(xyz, normal, gcm) -#' historic <- historic_input() +#' historic <- historic_input(dbCon, thebb) #' out <- downscale(xyz, normal, gcm = NULL, historic = historic, ppt_lr = FALSE) #' } downscale <- function(xyz, normal, gcm = NULL, historic = NULL, gcm_ts = NULL, gcm_hist = NULL, historic_ts = NULL, return_normal = FALSE, From 9fdd2cf32d915f95fa507a154e524dabb34df86f Mon Sep 17 00:00:00 2001 From: CeresBarros Date: Wed, 13 Dec 2023 11:51:40 -0800 Subject: [PATCH 17/22] minor improvements in example --- R/downscale.R | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/R/downscale.R b/R/downscale.R index b32a424b..41f68b85 100644 --- a/R/downscale.R +++ b/R/downscale.R @@ -203,15 +203,21 @@ climr_downscale <- function(xyz, which_normal = c("auto", "BC", "NorAm"), histor #' \dontrun{ #' dbCon <- data_connect() #' xyz <- data.frame(lon = runif(10, -140, -106), lat = runif(10, 37, 61), elev = runif(10)) +#' #' ## get bounding box based on input points #' thebb <- get_bb(xyz) #' normal <- normal_input(dbCon = dbCon, bbox = thebb, cache = TRUE) #' #' ## pick one GCM, one SSP and one period from the list of available options -#' gcm_input <- gcm_input(dbCon, thebb, gcm = list_gcm()[3], list_ssp()[1], list_gcm_period()[2]) +#' gcm <- gcm_input(dbCon, thebb, gcm = list_gcm()[3], list_ssp()[1], list_gcm_period()[2]) +#' +#' ## notice coarseness of the data +#' terra::plot(gcm[[1]]) +#' #' downscale(xyz, normal, gcm) #' historic <- historic_input(dbCon, thebb) -#' out <- downscale(xyz, normal, gcm = NULL, historic = historic, ppt_lr = FALSE) +#' +#' downscale(xyz, normal, gcm = NULL, historic = historic, ppt_lr = FALSE) #' } downscale <- function(xyz, normal, gcm = NULL, historic = NULL, gcm_ts = NULL, gcm_hist = NULL, historic_ts = NULL, return_normal = FALSE, vars = sort(sprintf(c("PPT%02d", "Tmax%02d", "Tmin%02d"), sort(rep(1:12, 3)))), From 26f151a23ea6cc976b437c85f8bdf9d551cdba62 Mon Sep 17 00:00:00 2001 From: CeresBarros Date: Wed, 13 Dec 2023 12:12:45 -0800 Subject: [PATCH 18/22] re-doc --- man/climr_downscale.Rd | 3 +++ man/downscale.Rd | 12 +++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/man/climr_downscale.Rd b/man/climr_downscale.Rd index cca3ecf0..6fe74a91 100644 --- a/man/climr_downscale.Rd +++ b/man/climr_downscale.Rd @@ -57,6 +57,9 @@ Historic, normal, and future periods are all returned in one table. \description{ Downscale and Calculate climate variables for points of interest } +\details{ +Additional details... TODO. +} \examples{ { ## TODO. diff --git a/man/downscale.Rd b/man/downscale.Rd index 6a59411a..dd278e5d 100644 --- a/man/downscale.Rd +++ b/man/downscale.Rd @@ -58,14 +58,20 @@ Additional details... TODO. \dontrun{ dbCon <- data_connect() xyz <- data.frame(lon = runif(10, -140, -106), lat = runif(10, 37, 61), elev = runif(10)) + ## get bounding box based on input points thebb <- get_bb(xyz) normal <- normal_input(dbCon = dbCon, bbox = thebb, cache = TRUE) ## pick one GCM, one SSP and one period from the list of available options -gcm_input <- gcm_input(list_gcm()[3], list_ssp()[1], list_gcm_period()[2]) +gcm <- gcm_input(dbCon, thebb, gcm = list_gcm()[3], list_ssp()[1], list_gcm_period()[2]) + +## notice coarseness of the data +terra::plot(gcm[[1]]) + downscale(xyz, normal, gcm) -historic <- historic_input() -out <- downscale(xyz, normal, gcm = NULL, historic = historic, ppt_lr = FALSE) +historic <- historic_input(dbCon, thebb) + +downscale(xyz, normal, gcm = NULL, historic = historic, ppt_lr = FALSE) } } From 3b93669d3d92be48438f9412c29e8fc27a166fdf Mon Sep 17 00:00:00 2001 From: CeresBarros Date: Wed, 13 Dec 2023 12:53:58 -0800 Subject: [PATCH 19/22] gcm_ts_input takes a vector of years, not a period --- R/gcm.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/gcm.R b/R/gcm.R index a9f3138d..030c41e5 100644 --- a/R/gcm.R +++ b/R/gcm.R @@ -210,7 +210,7 @@ gcm_hist_input <- function(dbCon, bbox = NULL, gcm = list_gcm(), years = 1901:19 #' @template bbox #' @template gcm #' @template ssp -#' @param years Numeric or character vector in in `2020:2100` +#' @param years Numeric or character vector in `2020:2100`. Defaults to `2020:2030`. #' @template max_run #' @param cache Logical specifying whether to cache new data locally or no. Default `TRUE` #' @@ -222,7 +222,7 @@ gcm_hist_input <- function(dbCon, bbox = NULL, gcm = list_gcm(), years = 1901:19 #' @import uuid #' @import data.table #' @export -gcm_ts_input <- function(dbCon, bbox = NULL, gcm = list_gcm(), ssp = list_ssp(), years = list_gcm_period(), max_run = 0L, cache = TRUE) { +gcm_ts_input <- function(dbCon, bbox = NULL, gcm = list_gcm(), ssp = list_ssp(), years = 2020:2030, max_run = 0L, cache = TRUE) { period <- years dbnames <- structure(list( GCM = c( From ee3ded114c064b93b5fb67fabed4b25630cd7816 Mon Sep 17 00:00:00 2001 From: CeresBarros Date: Wed, 13 Dec 2023 12:54:26 -0800 Subject: [PATCH 20/22] add missing doc - avoid cran check failures --- R/data.R | 1 + R/dbGetRaster.R | 16 ++++++++-------- man-roxygen/bands.R | 1 + man-roxygen/conn.R | 1 + 4 files changed, 11 insertions(+), 8 deletions(-) create mode 100644 man-roxygen/bands.R create mode 100644 man-roxygen/conn.R diff --git a/R/data.R b/R/data.R index 51d25940..e343ff29 100644 --- a/R/data.R +++ b/R/data.R @@ -4,6 +4,7 @@ #' Update external package data #' @param gcm A character. Relative path from the source root to global circulation models files folder. #' Default to option value "climr.gcm.path" if set, or "inputs_pkg/gcmData". +#' @param historic blah blah #' @param normal A character. Relative path from the source root to base normal files folder. #' @param quiet A logical. If `TRUE`, suppress status messages (if any), and the progress bar. #' @param ... Others parameters such as `source` or `repo` for content getting functions. diff --git a/R/dbGetRaster.R b/R/dbGetRaster.R index 6843bf3b..a1f669c9 100644 --- a/R/dbGetRaster.R +++ b/R/dbGetRaster.R @@ -1,10 +1,10 @@ #' Download raster with bounding box from PostGIS -#' @param conn a DBI or RPostgres connection object +#' @template conn #' @param name character. Table name in database #' @param tile TODO #' @param rast character. Name of column which stores raster data. #' Defaults to "rast" -#' @param bands Which raster bands to return. Default 37:73. +#' @template bands #' @template boundary #' @return A SpatRaster @@ -186,12 +186,12 @@ get_bb <- function(in_xyz) { #' Used internally to access the PostGRS database and #' create a SpatRaster using a given spatial boundary #' -#' @param boundary -#' @param conn -#' @param rastque -#' @param nameque -#' @param projID -#' @param bands +#' @template boundary +#' @template conn +#' @param rastque rast query name obtained with e.g. `dbQuoteIdentifier(conn, "rast")` +#' @param nameque schema.name +#' @param projID projID in data.base +#' @template bands #' #' @return a SpatRaster #' diff --git a/man-roxygen/bands.R b/man-roxygen/bands.R new file mode 100644 index 00000000..52d7d9a8 --- /dev/null +++ b/man-roxygen/bands.R @@ -0,0 +1 @@ +#' @param bands Which raster bands to return. Default 37:73. \ No newline at end of file diff --git a/man-roxygen/conn.R b/man-roxygen/conn.R new file mode 100644 index 00000000..a5cfc35d --- /dev/null +++ b/man-roxygen/conn.R @@ -0,0 +1 @@ +#' @param conn a DBI or RPostgres connection object \ No newline at end of file From 589acc50da55cd9a29831456e06d3b07778fc940 Mon Sep 17 00:00:00 2001 From: CeresBarros Date: Wed, 13 Dec 2023 12:54:31 -0800 Subject: [PATCH 21/22] minor --- R/dbGetRaster.R | 2 -- 1 file changed, 2 deletions(-) diff --git a/R/dbGetRaster.R b/R/dbGetRaster.R index a1f669c9..c1448009 100644 --- a/R/dbGetRaster.R +++ b/R/dbGetRaster.R @@ -26,8 +26,6 @@ pgGetTerra <- function(conn, name, tile, rast = "rast", bands = 37:73, projID <- dbGetQuery(conn, paste0("select ST_SRID(", rastque, ") as srid from ", nameque, " where rid = 1;"))$srid[1] - - if (length(bands) > 1664) { ## maximum number of columns info <- dbGetQuery(conn, paste0( "select From 7e50eb7835bbda70f4e36198ffa4401fb2b59dd3 Mon Sep 17 00:00:00 2001 From: CeresBarros Date: Wed, 13 Dec 2023 12:54:38 -0800 Subject: [PATCH 22/22] re-doc; checks passing --- man/data_update.Rd | 2 ++ man/gcm_ts_input.Rd | 4 ++-- man/make_raster.Rd | 12 ++++++------ 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/man/data_update.Rd b/man/data_update.Rd index c396292f..dda21cbc 100644 --- a/man/data_update.Rd +++ b/man/data_update.Rd @@ -18,6 +18,8 @@ data_update( \item{gcm}{A character. Relative path from the source root to global circulation models files folder. Default to option value "climr.gcm.path" if set, or "inputs_pkg/gcmData".} +\item{historic}{blah blah} + \item{normal}{A character. Relative path from the source root to base normal files folder.} \item{quiet}{A logical. If \code{TRUE}, suppress status messages (if any), and the progress bar.} diff --git a/man/gcm_ts_input.Rd b/man/gcm_ts_input.Rd index b27fc1f8..1a88b3de 100644 --- a/man/gcm_ts_input.Rd +++ b/man/gcm_ts_input.Rd @@ -9,7 +9,7 @@ gcm_ts_input( bbox = NULL, gcm = list_gcm(), ssp = list_ssp(), - years = list_gcm_period(), + years = 2020:2030, max_run = 0L, cache = TRUE ) @@ -25,7 +25,7 @@ Can be obtained from \code{list_gcm()}. Default to \code{list_gcm()}.} \item{ssp}{A character vector. Label of the shared socioeconomic pathways to use. See available options with \code{list_ssp()}. Defaults to \code{list_ssp()}.} -\item{years}{Numeric or character vector in in \code{2020:2100}} +\item{years}{Numeric or character vector in \code{2020:2100}. Defaults to \code{2020:2030}.} \item{max_run}{integer. Maximum number of model runs to include. A value of 0 returns the \code{ensembleMean} only. Runs are included in the order they diff --git a/man/make_raster.Rd b/man/make_raster.Rd index 9e792cd6..8af9d170 100644 --- a/man/make_raster.Rd +++ b/man/make_raster.Rd @@ -7,17 +7,17 @@ make_raster(boundary, conn, rastque, nameque, projID, bands) } \arguments{ -\item{boundary}{} +\item{boundary}{Numeric vector of length 4 (ymax,ymin,xmax,xmin). Defaults to \code{NULL}.} -\item{conn}{} +\item{conn}{a DBI or RPostgres connection object} -\item{rastque}{} +\item{rastque}{rast query name obtained with e.g. \code{dbQuoteIdentifier(conn, "rast")}} -\item{nameque}{} +\item{nameque}{schema.name} -\item{projID}{} +\item{projID}{projID in data.base} -\item{bands}{} +\item{bands}{Which raster bands to return. Default 37:73.} } \value{ a SpatRaster