Skip to content

Commit

Permalink
Working prototype, meets M1
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhsparks committed Oct 29, 2024
1 parent abdc1d4 commit 27d0043
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
^README\.Rmd$
^LICENSE\.md$
^\.github$
^.*\.Rproj$
^\.Rproj\.user$
5 changes: 5 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ Config/testthat/edition: 3
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
Imports:
cli,
data.table,
lubridate,
rlang
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Generated by roxygen2: do not edit by hand

export(get_key)
export(plot)
importFrom(terra,plot)
10 changes: 5 additions & 5 deletions R/get_key.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ get_key <- function() {
#' @keywords internal
#' @noRd
#' @return Called for its side-effects, opens a browser window at the TERN
#' weather data API key request form.
#' accounts page.
.set_tern_key <- function() {
if (interactive()) {
utils::browseURL("https://ternapikeydetails")
utils::browseURL("https://account.tern.org.au/authenticated_user/apikeys")
}

stop(
"You need to set your TERN API key.\n",
"After getting your key set it as 'DPIRD_API_KEY' in .Renviron.\n",
"You need to create and/or set your TERN API key.\n",
"After getting your key set it as 'TERN_API_KEY' in .Renviron.\n",
"TERN_API_KEY='youractualkeynotthisstring'\n",
"For that, use `usethis::edit_r_environ()`"
)

invisible("https://ternapikeydetails")
invisible("https://account.tern.org.au/authenticated_user/apikeys")
}
59 changes: 55 additions & 4 deletions R/get_smips.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,59 @@
#' Get Soil Moisture Integration and Prediction System (SMIPS) Data
#'
#' Soil Moisture Integration and Prediction System (\acronym{SMIPS})
#'
#' Soil Moisture Integration and Prediction System (\acronym{SMIPS}) v1.0.
#'
#' @param collection A character vector of the data collection to be queried,
#' on of:
#' * SMIndex
#' * bucket1
#' * bucket2
#' * deepD
#' * runoff
#' * totalbucket
#' Defaults to \dQuote{totalbucket}.
#' @param dates A date to query, _e.g._, `date = 2017-12-31`, both `Character` or
#' `Date` classes are accepted.
#'
#' @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>
get_smips <- function(start_date, end_date) {
get_smips <- function(collection = "totalbucket",
dates,
api_key = get_key()) {

dates <- lubridate::ymd(dates)
url_dates <- gsub("-", "", dates)
approved_collections <- c("totalbucket",
"SMIndex",
"bucket1",
"bucket2",
"deepD",
"runnoff")
collection <- rlang::arg_match(collection, approved_collections)

collection_url <- data.table::fcase(
collection == "totalbucket",
paste0("smips_totalbucket_mm_", url_dates, ".tif"),
collection == "SMIndex",
paste0("smips_smi_perc_", url_dates, ".tif"),
collection == "bucket1",
paste0("smips_bucket1_mm_", url_dates, ".tif"),
collection == "bucket2",
paste0("smips_bucket2_mm_", url_dates, ".tif"),
collection == "deepD",
paste0("smips_deepD_mm_", url_dates, ".tif"),
collection == "runoff",
paste0("smips_runoff_mm_", url_dates, ".tif")
)

r <- terra::rast(paste0("/vsicurl/https://",
paste0('apikey:', api_key),
"@data.tern.org.au/model-derived/smips/v1_0/",
collection,
"/",
lubridate::year(dates),
"/",
collection_url))
return(r)
}

}
3 changes: 3 additions & 0 deletions R/reexports.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#' @importFrom terra plot
#' @export
terra::plot
23 changes: 21 additions & 2 deletions man/get_smips.Rd

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

16 changes: 16 additions & 0 deletions man/reexports.Rd

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

5 changes: 5 additions & 0 deletions nert.Rproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ SaveWorkspace: No
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: knitr
LaTeX: pdfLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes
LineEndingConversion: Posix
Expand Down

0 comments on commit 27d0043

Please sign in to comment.