Skip to content

Commit

Permalink
Merge pull request #369 from henrykironde/resource-forecasts
Browse files Browse the repository at this point in the history
Unzip forecasts files in resources.
  • Loading branch information
henrykironde authored Dec 14, 2023
2 parents 7907852 + 0d42c4c commit 646ab63
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 20 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export(portal_ndvi_href)
export(portal_project_href)
export(portal_weather_href)
export(portalcast)
export(post_process)
export(prefab_datasets)
export(prefab_datasets_controls)
export(prefab_models)
Expand Down
19 changes: 16 additions & 3 deletions R/fill_dir.R
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ fill_resources <- function (main = ".") {

update_directory_configuration(main = main)

# unzip the resource forecast zipped files
resource_forecasts = file.path(main, settings$subdirectories$resources, settings$repository, settings$subdirectories$forecasts)
resource_forecasts = normalizePath(resource_forecasts, mustWork = FALSE)
if (file.exists(resource_forecasts)) {
zip_unzip("unzip", forecast_path = resource_forecasts)
}
invisible( )

}
Expand Down Expand Up @@ -236,15 +242,12 @@ fill_forecasts <- function (main = ".") {

messageq(paste0(" Located ", length(files), " forecast file(s) in resources to be moved to directory ..."), quiet = settings$quiet)
messageq(" ... moving ... ", quiet = settings$quiet)

copied <- file.copy(from = files,
to = forecasts_path(main = main),
recursive = TRUE)

messageq(paste0(" ... ", sum(copied), " files moved. "), quiet = settings$quiet)

# unzip the forecast zipped files
zip_unzip("unzip", forecast_path = forecasts_path(main = main))
invisible( )

}
Expand Down Expand Up @@ -310,3 +313,13 @@ fill_models <- function (main = ".",
}


#' After portalcast and evaluate_forecast run post_process to zip files
#'
#' @param main location of main directory with a forecast dir
#'
#' @export
#'
post_process <- function(main= ".") {
settings <- read_directory_settings(main = main)
zip_unzip("zip", forecasts_path(main = main))
}
39 changes: 22 additions & 17 deletions R/zip_unzip_forecasts.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,68 +20,73 @@ zip_unzip <- function(type = NULL,
utils::zip(zipfile, files)
unlink(files)
}

messageq("Preparing forecasts files")
proj_path <- forecast_path
forecasts_metadata <- paste0(proj_path, "/forecasts_metadata.csv")

proj_path <- normalizePath(proj_path, mustWork = FALSE)
forecasts_metadata <- normalizePath(forecasts_metadata, mustWork = FALSE)
forecasts_metadata <-
normalizePath(forecasts_metadata, mustWork = FALSE)
metadata <- read.csv(forecasts_metadata)
unique_dates <- sort(unique(metadata$"forecast_date"))

if (type == "zip") {
csv_file <- "_forecast_table.csv"
yaml_file <- "_metadata.yaml"
json_file <- "_model_forecast.json"

if (!is.null(date)) {
unique_dates <- c(date)
messageq(paste0("Zipping forecasts files for ", date))
}

for (forecast_day in unique_dates) {
id_date_files <- c()
zipfile <- paste0(proj_path, "/forecast_id_", forecast_day, ".zip")

zipfile <-
paste0(proj_path, "/forecast_id_", forecast_day, ".zip")

# Get all the values of that particular day in a data frame
newdata <- subset(metadata,
newdata <- subset(
metadata,
forecast_date == forecast_day,
select = c("forecast_id", "forecast_date")
)

# For each forecast_id get 3 files
All_ids <- newdata$"forecast_id"
for (id in All_ids) {
csv_file_path <- paste0(proj_path, "/forecast_id_", id, csv_file)
if (file.exists(csv_file_path)) {
id_date_files <- c(id_date_files, csv_file_path)
}
yaml_file_path <- paste0(proj_path, "/forecast_id_", id, yaml_file)
yaml_file_path <-
paste0(proj_path, "/forecast_id_", id, yaml_file)
if (file.exists(yaml_file_path)) {
id_date_files <- c(id_date_files, yaml_file_path)
}
json_file_path <- paste0(proj_path, "/forecast_id_", id, json_file)
json_file_path <-
paste0(proj_path, "/forecast_id_", id, json_file)
if (file.exists(json_file_path)) {
id_date_files <- c(id_date_files, json_file_path)
}
}

if (length(id_date_files)) {
# Zip forecast files for the current date
zip_files(zipfile, id_date_files)
}
}

# Zip forecasts_evaluations.csv
eval_file <- paste0(proj_path, "/forecasts_evaluations.csv")
zipfile <- paste0(proj_path, "/forecasts_evaluations.zip")
zip_files(zipfile, eval_file)
}

if (type == "unzip") {
messageq("Unzipping forecasts files")

# Unzip files based on unique_dates
for (forecast_day in unique_dates) {
zipfile <- paste0(proj_path, "/forecast_id_", forecast_day, ".zip")
Expand All @@ -91,7 +96,7 @@ zip_unzip <- function(type = NULL,
unlink(zipfile)
}
}

# Unzip forecasts_evaluations.csv
eval_zip <- paste0(proj_path, "/forecasts_evaluations.zip")
eval_zip <- normalizePath(eval_zip, mustWork = FALSE)
Expand Down
14 changes: 14 additions & 0 deletions man/post_process.Rd

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

0 comments on commit 646ab63

Please sign in to comment.