generated from ecohealthalliance/container-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove grib_ls dependency, switch to beta version of emcwf because en…
…d of life for previous API is coming this month, and improve AWS storage and retrevial of transformed grib files.
- Loading branch information
Showing
11 changed files
with
509 additions
and
242 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#' Get a tibble of tasks from ECMWF API | ||
#' | ||
#' This function retrieves tasks from the ECMWF (European Centre for Medium-Range Weather Forecasts) API. | ||
#' It sends a GET request using the `httr` package and returns the content of the response as a data frame. | ||
#' | ||
#' @author Nathan Layman | ||
#' | ||
#' @param url A character string specifying the base URL for the ECMWF CDS tasks API. | ||
#' Defaults to "https://cds.climate.copernicus.eu/api/v2/tasks/". | ||
#' | ||
#' @return A data frame with the tasks retrieved from ECMWF. | ||
#' | ||
#' @export | ||
get_ecwmf_tasks <- function(url = "https://cds.climate.copernicus.eu/api/v2/tasks/") { | ||
response <- httr::GET( | ||
url, | ||
httr::authenticate(Sys.getenv("ECMWF_USERID"), Sys.getenv("ECMWF_TOKEN")) | ||
) | ||
|
||
httr::content(response) |> bind_rows() | ||
} | ||
|
||
|
||
#' Clear ECMWF Queued Tasks | ||
#' | ||
#' This function retrieves the currently queued tasks from the ECMWF (European Centre for Medium-Range Weather Forecasts) CDS (Climate Data Store) | ||
#' API and deletes them using their request IDs. | ||
#' | ||
#' @author Nathan Layman | ||
#' | ||
#' @param url A character string specifying the base URL for the ECMWF CDS tasks API. | ||
#' Defaults to "https://cds.climate.copernicus.eu/api/v2/tasks/". | ||
#' | ||
#' @return A data frame with the tasks retrieved from ECMWF. | ||
#' | ||
#' @export | ||
clear_ecwmf_tasks <- function(url = "https://cds.climate.copernicus.eu/api/v2/tasks/") { | ||
|
||
tasks_to_clear <- get_ecwmf_tasks() |> | ||
filter(state == "queued") |> | ||
mutate(request_id = paste0(url, request_id)) |> | ||
pull(request_id) | ||
|
||
request <- walk(tasks_to_clear, ~httr::DELETE(.x, httr::authenticate(Sys.getenv("ECMWF_USERID"), Sys.getenv("ECMWF_TOKEN")))) | ||
|
||
} | ||
|
||
submit_ecwmf_request <- function(request, | ||
url = "https://cds.climate.copernicus.eu/api/v2/tasks/") { | ||
|
||
response <- httr::PUT(jsonlite::toJSON(request), httr::authenticate(Sys.getenv("ECMWF_USERID"), Sys.getenv("ECMWF_TOKEN"))) | ||
|
||
} | ||
response <- POST( | ||
url = paste0(ecmwf_url, "/datasets/data/era5"), # Update endpoint as needed | ||
add_headers( | ||
Authorization = paste("Bearer", api_key), | ||
"Content-Type" = "application/json" | ||
), | ||
body = toJSON(query), # Convert the query to JSON format | ||
encode = "json" # Encode the body as JSON | ||
) | ||
|
||
# Check if the request was successful | ||
if (status_code(response) == 200) { | ||
# Save the response content to the desired file | ||
writeBin(content(response, "raw"), "output.nc") | ||
cat("File saved successfully.\n") | ||
} else { | ||
cat("Failed to fetch data. Status code:", status_code(response), "\n") | ||
} |
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,26 @@ | ||
#' Get and parse gdalinfo metadata from a grib file without relying on grib_ls | ||
#' | ||
#' @author Nathan Layman | ||
#' | ||
#' @param file | ||
#' | ||
#' @return | ||
#' @export | ||
#' | ||
#' @examples | ||
get_grib_metadata <- function(raw_file) { | ||
|
||
gdalinfo_text <- terra::describe(raw_file, options = "json") | ||
|
||
# Remove all text up to first BAND ^GEOGCRS | ||
metadata_start_index <- grep("^Band|^BAND", gdalinfo_text)[1] | ||
metadata_text <- gdalinfo_text[metadata_start_index:length(gdalinfo_text)] | ||
metadata_text <- metadata_text[!str_detect(metadata_text, "^Band|BAND|Metadata|METADATA")] | ||
|
||
metadata <- map_dfr(metadata_text, ~stringr::str_split(.x[1], "=")[[1]] |> | ||
stringr::str_squish() |> setNames(c("name", "value"))) |> | ||
mutate(band = cumsum(name == "Description")) |> | ||
pivot_wider(names_from = "name", values_from = "value") | ||
|
||
metadata | ||
} |
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,12 @@ | ||
#' Check error recorded in meta file for a target | ||
#' | ||
#' @param branch | ||
#' | ||
#' @return | ||
#' @export | ||
#' | ||
#' @examples | ||
tar_get_error <- function(branch) { | ||
branch_name <- deparse(substitute(branch)) | ||
tar_meta() |> filter(name == branch_name) |> pull(error) | ||
} |
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
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
Oops, something went wrong.