-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from AAGI-AUS/devel
- Loading branch information
Showing
13 changed files
with
412 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
export(download_gtiff) | ||
export(get_key) | ||
export(get_smips) | ||
export(plot) | ||
export(read_cog) | ||
export(read_cog_dt) | ||
importFrom(terra,plot) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
|
||
|
||
|
||
.make_smips_url <- function(.collection, .day) { | ||
url_date <- gsub("-", "", .day) | ||
|
||
approved_collections <- c("totalbucket", | ||
"SMindex", | ||
"bucket1", | ||
"bucket2", | ||
"deepD", | ||
"runnoff") | ||
collection <- rlang::arg_match(.collection, approved_collections) | ||
|
||
.check_collection_agreement(.collection = .collection, .day = .day) | ||
|
||
collection_url <- data.table::fcase( | ||
collection == "totalbucket", | ||
paste0("smips_totalbucket_mm_", url_date, ".tif"), | ||
collection == "SMindex", | ||
paste0("smips_smi_perc_", url_date, ".tif"), | ||
collection == "bucket1", | ||
paste0("smips_bucket1_mm_", url_date, ".tif"), | ||
collection == "bucket2", | ||
paste0("smips_bucket2_mm_", url_date, ".tif"), | ||
collection == "deepD", | ||
paste0("smips_deepD_mm_", url_date, ".tif"), | ||
collection == "runoff", | ||
paste0("smips_runoff_mm_", url_date, ".tif") | ||
) | ||
} | ||
|
||
|
||
.check_collection_agreement <- function(.collection, .day) { | ||
.this_year <- lubridate::year(lubridate::today()) | ||
.last_week <- lubridate::today() - 7 | ||
.url_year <- lubridate::year(.day) | ||
|
||
if (.collection == "totalbucket" && | ||
.url_year < 2005 || | ||
.day > .last_week) { | ||
cli::cli_abort("The data are not available before 2005 and past {.last_week}") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#' Read COGs from TERN | ||
#' | ||
#' Read Cloud Optimised Geotiff (\acronym{COG}) files from \acronym{TERN} in | ||
#' your active \R session. | ||
#' | ||
#' @note | ||
#' Currently only Soil Moisture Integration and Prediction System | ||
#' (\acronym{SMIPS}) v1.0 is supported. | ||
#' | ||
#' @param data A character vector of the data source to be queried, currently | ||
#' only \dQuote{smips}. | ||
#' @param collection A character vector of the data collection to be queried, | ||
#' currenly only \dQuote{smips} is supported with the following collections: | ||
#' * SMindex | ||
#' * bucket1 | ||
#' * bucket2 | ||
#' * deepD | ||
#' * runoff | ||
#' * totalbucket | ||
#' Defaults to \dQuote{totalbucket}. | ||
#' @param day A single day's date to query, _e.g._, `day = "2017-12-31"`, both | ||
#' `Character` and `Date` classes are accepted. | ||
#' @param api_key A `character` string containing your \acronym{API} key, | ||
#' a random string provided to you by \acronym{TERN}, for the request. | ||
#' Defaults to automatically detecting your key from your local .Renviron, | ||
#' .Rprofile or similar. Alternatively, you may directly provide your key as | ||
#' a string here or use functionality like that from \CRANpkg{keyring}. If | ||
#' nothing is provided, you will be prompted on how to set up your \R session | ||
#' so that it is auto-detected and a browser window will open at the | ||
#' \acronym{TERN} website for you to request a key. | ||
#' | ||
#' @family COGs | ||
#' | ||
#' @examplesIf interactive() | ||
#' | ||
#' r <- read_cog(day = "2024-01-01") | ||
#' | ||
#' # terra::plot() is re-exported for convenience | ||
#' plot(r) | ||
#' | ||
#' @return A [terra::rast] object | ||
#' @references <https://portal.tern.org.au/metadata/TERN/d1995ee8-53f0-4a7d-91c2-ad5e4a23e5e0https://geonetwork.tern.org.au/geonetwork/srv/eng/catalog.search#/metadata/d1995ee8-53f0-4a7d-91c2-ad5e4a23e5e0> | ||
#' @export | ||
|
||
read_cog <- function(data = "smips", | ||
collection = "totalbucket", | ||
day, | ||
api_key = get_key()) { | ||
day <- lubridate::ymd(day) | ||
url_year <- lubridate::year(day) | ||
|
||
if (data == "smips") { | ||
collection_url <- .make_smips_url(.collection = collection, .day = day) | ||
return(terra::rast( | ||
paste0( | ||
"/vsicurl/https://", | ||
paste0("apikey:", api_key), | ||
"@data.tern.org.au/model-derived/smips/v1_0/", | ||
collection, | ||
"/", | ||
url_year, | ||
"/", | ||
.make_smips_url(.collection = collection, .day = day) | ||
) | ||
) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#' Read COGs from TERN | ||
#' | ||
#' Read Cloud Optimised Geotiff (\acronym{COG}) files from \acronym{TERN} in | ||
#' your active \R session as a \CRANpkg{data.table} object. | ||
#' | ||
#' @note | ||
#' Currently only Soil Moisture Integration and Prediction System | ||
#' (\acronym{SMIPS}) v1.0 is supported. | ||
#' | ||
#' @inherit read_cog | ||
#' | ||
#' @family COGs | ||
#' | ||
#' @examplesIf interactive() | ||
#' | ||
#' r <- read_cog_dt(day = "2024-01-01") | ||
#' | ||
#' r | ||
#' | ||
#' @return A [data.table::data.table] object | ||
#' @references <https://portal.tern.org.au/metadata/TERN/d1995ee8-53f0-4a7d-91c2-ad5e4a23e5e0https://geonetwork.tern.org.au/geonetwork/srv/eng/catalog.search#/metadata/d1995ee8-53f0-4a7d-91c2-ad5e4a23e5e0> | ||
#' @export | ||
|
||
read_cog_dt <- function(data = "smips", | ||
collection = "totalbucket", | ||
day, | ||
api_key = get_key()) { | ||
r <- read_cog( | ||
data = data, | ||
collection = collection, | ||
day = day, | ||
api_key = api_key | ||
) | ||
r <- data.table::setDT(terra::as.data.frame(r, xy = TRUE)) | ||
data.table::setnames(r, old = c("x", "y"), new = c("lon", "lat")) | ||
return(r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Oops, something went wrong.