Skip to content

Commit

Permalink
Merge pull request #7 from AAGI-AUS/devel
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhsparks authored Nov 7, 2024
2 parents 7c136cd + 8824202 commit c2a05b4
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 164 deletions.
20 changes: 11 additions & 9 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,27 @@ Version: 0.0.0.9000
Authors@R: c(
person("Adam H.", "Sparks", , "[email protected]", role = "cre",
comment = c(ORCID = "0000-0002-0061-8359")),
person("Russell", "Edson", , "[email protected]", role = "aut")
person("Russell", "Edson", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0002-4607-5396")),
person("Max", "Moldovan", , "[email protected]", role = "ctb")
)
Description: Provides access to Australia's TERN (Terrestrial Ecosystem
Research Network) data through the API, <https://tern.org.au>.
License: MIT + file LICENSE
Suggests:
roxyglobals,
testthat (>= 3.0.0)
Config/testthat/edition: 3
Encoding: UTF-8
Roxygen: list(markdown = TRUE, roclets = c("collate", "namespace", "rd",
"roxyglobals::global_roclet"))
RoxygenNote: 7.3.2
Imports:
cli,
data.table,
lubridate,
rlang,
terra,
utils
Suggests:
roxyglobals,
testthat (>= 3.0.0)
Config/roxyglobals/filename: globals.R
Config/roxyglobals/unique: FALSE
Config/testthat/edition: 3
Encoding: UTF-8
Roxygen: list(markdown = TRUE, roclets = c("collate", "namespace", "rd",
"roxyglobals::global_roclet"))
RoxygenNote: 7.3.2
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
export(get_key)
export(plot)
export(read_cog)
export(read_cog_dt)
importFrom(terra,plot)
15 changes: 9 additions & 6 deletions R/read_cog.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' 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
#' @param dataset 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:
Expand Down Expand Up @@ -51,7 +51,7 @@
#' @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",
read_cog <- function(dataset = "smips",
collection = "totalbucket",
day,
api_key = get_key(),
Expand All @@ -66,7 +66,7 @@ read_cog <- function(data = "smips",
day <- .check_date(day)
url_year <- lubridate::year(day)

if (data == "smips") {
if (dataset == "smips") {
collection_url <- .make_smips_url(.collection = collection, .day = day)

while (attempt <= max_tries && !success)
Expand All @@ -90,7 +90,8 @@ read_cog <- function(data = "smips",
}, error = function(e) {
if (attempt < max_tries) {
delay <- initial_delay * 2 ^ (attempt - 1)
cli::cli_alert("Download failed on attempt { attempt }. Retrying in { delay } seconds...")
cli::cli_alert("Download failed on attempt { attempt }.
Retrying in { delay } seconds...")
Sys.sleep(delay)
attempt <- attempt + 1
} else {
Expand Down Expand Up @@ -120,7 +121,8 @@ read_cog <- function(data = "smips",
"Ymd", "dmY", "mdY", "BdY", "Bdy", "bdY", "bdy"
), tz = Sys.timezone()),
warning = function(c) {
cli::cli_abort("{ x } is not in a valid date format. Please enter a valid date format.")
cli::cli_abort("{ x } is not in a valid date format.
Please enter a valid date format.")
}
)
return(x)
Expand Down Expand Up @@ -168,7 +170,8 @@ read_cog <- function(data = "smips",
if (.collection == "totalbucket" &&
.url_year < 2005 ||
.day > .last_week) {
cli::cli_abort("The data are not available before 2005 and past { .last_week }")
cli::cli_abort("The data are not available before 2005 and roughly
much past { .last_week }")
}
}

Expand Down
40 changes: 0 additions & 40 deletions R/read_cog_dt.R

This file was deleted.

21 changes: 15 additions & 6 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,22 @@ r <- read_cog(day = "2024-01-01")
plot(r)
```

## Example reading a COG as a data.table
## Extract Values Given Lat/Lon Values

This is a basic example which shows you how you can fetch one day's data from the SMIPS data as a data.table:
Extract Soil Moisture for Corrigin and Merriden, WA and Tamworth, NSW given latitude and longitude values for each.

```{r example_dt}
library(nert)
r <- read_cog_dt(day = "2024-01-01")
```{r}
df <- structure(list(
location = c("Corrigin", "Merredin", "Tamworth"),
x = c(117.87, 118.28, 150.84),
y = c(-32.33, -31.48, -31.07)
),
row.names = c(NA, -3L),
class = "data.frame")
cog_df <- terra::extract(x = r, y = df[, c("x", "y")], xy = TRUE)
r
cog_df <- cbind(df$location, cog_df)
names(cog_df) <- c("location", "ID", "smips_totalbucket_mm_20240101", "x", "y")
cog_df
```
40 changes: 20 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,27 @@ plot(r)

<img src="man/figures/README-example_cog-1.png" width="100%" />

## Example reading a COG as a data.table
## Extract Values Given Lat/Lon Values

This is a basic example which shows you how you can fetch one day’s data
from the SMIPS data as a data.table:
Extract Soil Moisture for Corrigin and Merriden, WA and Tamworth, NSW
given latitude and longitude values for each.

``` r
library(nert)
r <- read_cog_dt(day = "2024-01-01")

r
#> lon lat smips_totalbucket_mm_20240101
#> <num> <num> <num>
#> 1: 142.5328 -10.69951 38.68500
#> 2: 142.5228 -10.70951 43.40917
#> 3: 142.5328 -10.70951 41.99442
#> 4: 142.4428 -10.71951 38.94186
#> 5: 142.4928 -10.71951 43.28778
#> ---
#> 6873516: 146.8517 -43.62003 40.96997
#> 6873517: 146.8617 -43.62003 38.49732
#> 6873518: 146.8717 -43.62003 48.08897
#> 6873519: 146.8517 -43.63003 33.92958
#> 6873520: 146.8617 -43.63003 32.85794
df <- structure(list(
location = c("Corrigin", "Merredin", "Tamworth"),
x = c(117.87, 118.28, 150.84),
y = c(-32.33, -31.48, -31.07)
),
row.names = c(NA, -3L),
class = "data.frame")

cog_df <- terra::extract(x = r, y = df[, c("x", "y")], xy = TRUE)

cog_df <- cbind(df$location, cog_df)
names(cog_df) <- c("location", "ID", "smips_totalbucket_mm_20240101", "x", "y")
cog_df
#> location ID smips_totalbucket_mm_20240101 x y
#> 1 Corrigin 1 0.06715473 117.8688 -32.33328
#> 2 Merredin 2 0.22716530 118.2787 -31.48353
#> 3 Tamworth 3 93.44989014 150.8408 -31.07365
```
13 changes: 11 additions & 2 deletions codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@
"@type": "Person",
"givenName": "Russell",
"familyName": "Edson",
"email": "[email protected]"
"email": "[email protected]",
"@id": "https://orcid.org/0000-0002-4607-5396"
}
],
"contributor": [
{
"@type": "Person",
"givenName": "Max",
"familyName": "Moldovan",
"email": "[email protected]"
}
],
"maintainer": [
Expand Down Expand Up @@ -124,7 +133,7 @@
},
"SystemRequirements": null
},
"fileSize": "174.876KB",
"fileSize": "171.536KB",
"codeRepository": "https://github.com/AAGI-AUS/nert",
"readme": "https://github.com/AAGI-AUS/nert/blob/main/README.md",
"contIntegration": ["https://github.com/AAGI-AUS/nert/actions/workflows/R-CMD-check.yaml", "https://app.codecov.io/gh/AAGI-AUS/nert"]
Expand Down
21 changes: 21 additions & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
CMD
COGs
Codecov
Corrigin
Geotiff
Merriden
NSW
ORCID
Optimised
Renviron
Rprofile
SMIPS
SMindex
bashrc
config
currenly
deepD
smips
totalbucket
visualise
zshrc
7 changes: 6 additions & 1 deletion man/nert-package.Rd

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

8 changes: 2 additions & 6 deletions man/read_cog.Rd

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

73 changes: 0 additions & 73 deletions man/read_cog_dt.Rd

This file was deleted.

0 comments on commit c2a05b4

Please sign in to comment.