From 3b8b87ccbf725b6c2cf145d9693230dab84b38f9 Mon Sep 17 00:00:00 2001 From: lilyclements Date: Wed, 27 Mar 2024 21:49:49 +0000 Subject: [PATCH 1/2] adding functionality for definitions when summaries are called form bucket --- R/annual_rainfall_summaries.R | 12 +++++++++--- R/definitions.R | 4 ++-- R/export_r_instat_to_bucket.R | 15 ++++++++++++--- man/definitions.Rd | 2 +- man/export_r_instat_to_bucket.Rd | 2 ++ 5 files changed, 26 insertions(+), 9 deletions(-) diff --git a/R/annual_rainfall_summaries.R b/R/annual_rainfall_summaries.R index 91c39fa..9c6525f 100644 --- a/R/annual_rainfall_summaries.R +++ b/R/annual_rainfall_summaries.R @@ -20,12 +20,18 @@ annual_rainfall_summaries <- function(country, station_id, summaries = c("annual list_return <- NULL # do the summaries exist already? - summary_data <- epicsadata::get_summaries_data(country, station_id, summary = "annual_rainfall_summaries") - + get_summaries <- get_summaries_data(country, station_id, summary = "annual_rainfall_summaries") + summary_data <- get_summaries[[1]] # what if the definitions is different? Have an override option. # if the summary data exists, and if you do not want to override it then: if (nrow(summary_data) > 0 & override == FALSE) { - list_return[[1]] <- "definition from summary" + files <- epicsadata::get_objects_in_bucket(country, station_id, get_summaries) + if (nrow(files) == 0) { + list_return[[1]] <- (definitions(country, station_id, summaries = summaries)) + } else { + list_return[[1]] <- (definitions(country, station_id, summaries = summaries, file_name)) + } + } else { # Get data definitions and summary definitions definitions <- definitions(country = country, station_id = station_id, summaries = summaries) diff --git a/R/definitions.R b/R/definitions.R index 3bce0ae..5981e87 100644 --- a/R/definitions.R +++ b/R/definitions.R @@ -9,8 +9,8 @@ #' #' @examples # e.g. definitions("zm", "16", "annual_rain") #' # error: definitions("zm", "1", c("annual_rain", "hi", "end_season")) -definitions <- function(country, station_id, summaries){ - definition_data <- epicsadata::get_definitions_data(country = country, station_id = station_id) +definitions <- function(country, station_id, summaries, file = NULL){ + definition_data <- epicsadata::get_definitions_data(country = country, station_id = station_id, file = file) definition_data <- purrr::map(.x = summaries, .f = ~ definition_data[[.x]]) names(definition_data) <- summaries # are any NULL 1 = NULL diff --git a/R/export_r_instat_to_bucket.R b/R/export_r_instat_to_bucket.R index fdff1d8..0dbe76f 100644 --- a/R/export_r_instat_to_bucket.R +++ b/R/export_r_instat_to_bucket.R @@ -31,9 +31,12 @@ export_r_instat_to_bucket <- function(data, data_by_year, data_by_year_month = N rain = NULL, tmin = NULL, tmax = NULL, year = NULL, month = NULL, summaries = c("annual_rainfall", "annual_temperature", "monthly_temperature", "extremes", "crop_success", "start_season"), file_path, file_name, + station_id, country, include_summary_data = FALSE){ - definitions_data <- collate_definitions_data(data = data, + timestamp <- format(Sys.time(), format = "%Y%m%d%H%M%S") + + definitions_data <- epicsadata::collate_definitions_data(data = data, data_by_year = data_by_year, data_by_year_month = data_by_year_month, crop_data = crop_data, @@ -50,10 +53,16 @@ export_r_instat_to_bucket <- function(data, data_by_year, data_by_year_month = N auto_unbox = TRUE, pretty = TRUE) # Read from computer to bucket - epicsadata::add_definitions_to_bucket(country = country, station_id = station_id, new_definitions = file_name) - + add_definitions_to_bucket(country = country, station_id = station_id, + new_definitions = paste0(file_path, file_name), + timestamp = timestamp) if (include_summary_data){ # function to read summary data from R-Instat into summaries in buckets + if ("annual_rainfall" %in% summaries){ + add_summaries_to_bucket(country = country, station_id = station_id, data = data_book$get_data_frame(data_by_year), + summary = "annual_rainfall_summaries", timestamp = timestamp) + } } + return("Uploaded to Bucket") } diff --git a/man/definitions.Rd b/man/definitions.Rd index 33cda88..e484ead 100644 --- a/man/definitions.Rd +++ b/man/definitions.Rd @@ -4,7 +4,7 @@ \alias{definitions} \title{Definitions} \usage{ -definitions(country, station_id, summaries) +definitions(country, station_id, summaries, file = NULL) } \arguments{ \item{country}{\code{character(1)} The country code of the data.} diff --git a/man/export_r_instat_to_bucket.Rd b/man/export_r_instat_to_bucket.Rd index 98fcb50..9594cc6 100644 --- a/man/export_r_instat_to_bucket.Rd +++ b/man/export_r_instat_to_bucket.Rd @@ -18,6 +18,8 @@ export_r_instat_to_bucket( "extremes", "crop_success", "start_season"), file_path, file_name, + station_id, + country, include_summary_data = FALSE ) } From 27854a23ba3c750137e8cc3346542f7134ca58d4 Mon Sep 17 00:00:00 2001 From: lilyclements Date: Wed, 27 Mar 2024 21:55:08 +0000 Subject: [PATCH 2/2] documenting objects --- R/definitions.R | 1 + R/export_r_instat_to_bucket.R | 3 +++ man/definitions.Rd | 2 ++ man/export_r_instat_to_bucket.Rd | 5 +++++ 4 files changed, 11 insertions(+) diff --git a/R/definitions.R b/R/definitions.R index 5981e87..9e12827 100644 --- a/R/definitions.R +++ b/R/definitions.R @@ -3,6 +3,7 @@ #' @param country `character(1)` The country code of the data. #' @param station_id `character(1)` The station code in the data. #' @param summaries `character` Vector of summaries to display +#' @param file Default `NULL` meaning that the most recent definitions file will be found and imported. Otherwise specify as a string the file to import. In format: "STATIONNAME.TIMESTAMP" e.g. "1.20240311152831" #' #' @return TODO #' @export diff --git a/R/export_r_instat_to_bucket.R b/R/export_r_instat_to_bucket.R index 0dbe76f..171f61b 100644 --- a/R/export_r_instat_to_bucket.R +++ b/R/export_r_instat_to_bucket.R @@ -16,6 +16,9 @@ #' @param summaries A character vector specifying the types of summaries to include. #' @param file_path The path to the directory where the JSON file will be saved locally. #' @param file_name The name of the JSON file (without the ".json" extension). +#' @param country `character(1)` The country code of the data. +#' @param station_id `character` The id's of the stations to analyse. Either a +#' single value or a vector. #' @param include_summary_data Logical indicating whether to include summary data in the export. #' #' @return NULL (invisibly) diff --git a/man/definitions.Rd b/man/definitions.Rd index e484ead..e34c77a 100644 --- a/man/definitions.Rd +++ b/man/definitions.Rd @@ -12,6 +12,8 @@ definitions(country, station_id, summaries, file = NULL) \item{station_id}{\code{character(1)} The station code in the data.} \item{summaries}{\code{character} Vector of summaries to display} + +\item{file}{Default \code{NULL} meaning that the most recent definitions file will be found and imported. Otherwise specify as a string the file to import. In format: "STATIONNAME.TIMESTAMP" e.g. "1.20240311152831"} } \value{ TODO diff --git a/man/export_r_instat_to_bucket.Rd b/man/export_r_instat_to_bucket.Rd index 9594cc6..61c3119 100644 --- a/man/export_r_instat_to_bucket.Rd +++ b/man/export_r_instat_to_bucket.Rd @@ -48,6 +48,11 @@ export_r_instat_to_bucket( \item{file_name}{The name of the JSON file (without the ".json" extension).} +\item{station_id}{\code{character} The id's of the stations to analyse. Either a +single value or a vector.} + +\item{country}{\code{character(1)} The country code of the data.} + \item{include_summary_data}{Logical indicating whether to include summary data in the export.} } \value{