Skip to content

Commit

Permalink
Remove zipR and use utils to zip
Browse files Browse the repository at this point in the history
  • Loading branch information
henrykironde committed Nov 29, 2023
1 parent 67c04e7 commit 957dc6b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 29 deletions.
3 changes: 1 addition & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ Imports:
stats,
viridis,
yaml,
utils,
zipR
utils
Suggests:
pkgdown,
testthat
Expand Down
65 changes: 38 additions & 27 deletions R/zip_unzip_forecasts.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' unZip and zip forecasts by forecast date
#' Zip and unzip forecasts by forecast date
#'
#' @param type either zip or unzip
#' @param forecast_path location of forecast directory
Expand All @@ -15,9 +15,7 @@ zip_unzip <-
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 <- unique(metadata$forecast_date)
unique_dates = sort(unique_dates)
Expand All @@ -32,8 +30,7 @@ zip_unzip <-
}
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,
Expand All @@ -42,32 +39,44 @@ zip_unzip <-
# 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)
yaml_file_path = paste0(proj_path, "forecast_id_", id, yaml_file)
json_file_path = paste0(proj_path, "forecast_id_", id, json_file)
id_date_files <-
c(id_date_files,
csv_file_path,
yaml_file_path,
json_file_path)
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)
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)
if (file.exists(json_file_path)) {
id_date_files <- c(id_date_files, json_file_path)
}
}

if (length(id_date_files)){
zipfile <- normalizePath(zipfile, mustWork = FALSE)
if (file.exists(zipfile)) {
# First remove old zip file if exists
unlink(zipfile)
}
# zip all id_date_files
zip(zipfile, id_date_files)
unlink(id_date_files)
}
zipfile <- normalizePath(zipfile, mustWork = FALSE)
# First remove old zip file if exists
unlink(zipfile)
# zip all id_date_files
zipr(zipfile, id_date_files, compression_level = 9)
unlink(id_date_files)
}

# Zip forecasts_evaluations.csv
id_date_files <-
c(paste0(proj_path, "/forecasts_evaluations.csv"))
id_date_files <- c(paste0(proj_path, "/forecasts_evaluations.csv"))
id_date_files <- normalizePath(id_date_files, mustWork = FALSE)

zipfile <- paste0(proj_path, "/forecasts_evaluations.zip")
zipfile <- normalizePath(zipfile, mustWork = FALSE)

zipr(zipfile, id_date_files, compression_level = 9)
if (file.exists(zipfile)) {
# First remove old zip file if exists
unlink(zipfile)
}
zip(zipfile, id_date_files)
unlink(id_date_files)
}

if (type == "unzip") {
Expand All @@ -77,15 +86,17 @@ zip_unzip <-
zipfile <- paste0(proj_path, "/forecast_id_", forecast_day, ".zip")
zipfile <- normalizePath(zipfile, mustWork = FALSE)
if (file.exists(zipfile)) {
unzip(zipfile, exdir = proj_path)
unzip(zipfile, exdir = proj_path, junkpaths = TRUE)
unlink(zipfile)
}
}

# Unzip forecasts_evaluations.csv
eval_zip <- paste0(proj_path, "/forecasts_evaluations.zip")
eval_zip <- normalizePath(eval_zip, mustWork = FALSE)
utils::unzip(eval_zip, exdir = proj_path)
unlink(eval_zip)
if (file.exists(eval_zip)) {
unzip(eval_zip, exdir = proj_path, junkpaths = TRUE)
unlink(eval_zip)
}
}
}

0 comments on commit 957dc6b

Please sign in to comment.