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..9e12827 100644 --- a/R/definitions.R +++ b/R/definitions.R @@ -3,14 +3,15 @@ #' @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 #' #' @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..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) @@ -31,9 +34,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 +56,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..e34c77a 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.} @@ -12,6 +12,8 @@ definitions(country, station_id, summaries) \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 98fcb50..61c3119 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 ) } @@ -46,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{