diff --git a/.github/workflows/deploy-targets-dam.yml b/.github/workflows/deploy-targets-downloads.yml similarity index 92% rename from .github/workflows/deploy-targets-dam.yml rename to .github/workflows/deploy-targets-downloads.yml index c5d7709..bfde6e1 100644 --- a/.github/workflows/deploy-targets-dam.yml +++ b/.github/workflows/deploy-targets-downloads.yml @@ -1,4 +1,4 @@ -name: deploy targets dam +name: deploy targets download on: schedule: @@ -8,7 +8,7 @@ on: - '*' jobs: - deploy-targets-dam: + deploy-targets-download: runs-on: ubuntu-latest container: rocker/tidyverse:4.4.1 steps: @@ -39,6 +39,7 @@ jobs: - name: Run workflow run: | targets::tar_make(dam_level_data_raw_csv) + targets::tar_make(forecasts_download_files) shell: Rscript {0} - name: Commit and push files diff --git a/.github/workflows/test-targets-forecasts.yml b/.github/workflows/test-targets-forecasts.yml new file mode 100644 index 0000000..d6b0b9b --- /dev/null +++ b/.github/workflows/test-targets-forecasts.yml @@ -0,0 +1,38 @@ +name: test targets forecasts + +on: + pull_request: + branches: [main, master] + workflow_dispatch: + branches: + - '*' + +jobs: + test-targets-forecasts: + runs-on: ubuntu-latest + container: rocker/tidyverse:4.4.1 + steps: + - uses: actions/checkout@v4 + + - name: Install system dependencies + run: | + apt-get update && apt-get install -y --no-install-recommends \ + libxt6 libglpk-dev libpoppler-cpp-dev libmagick++-dev \ + libtesseract-dev libleptonica-dev tesseract-ocr-eng + + - name: Install packages from renv.lock (with cache) + if: ${{ !env.ACT }} + uses: r-lib/actions/setup-renv@v2 + with: + cache-version: 2 + + - name: Install packages from renv.lock (local, no cache) + if: ${{ env.ACT }} + run: | + renv::restore() + shell: Rscript {0} + + - name: Run workflow + run: | + targets::tar_make(dplyr::starts_with("forecasts")) + shell: Rscript {0} diff --git a/R/pagasa_forecasts.R b/R/pagasa_forecasts.R new file mode 100644 index 0000000..adf17c3 --- /dev/null +++ b/R/pagasa_forecasts.R @@ -0,0 +1,90 @@ +#' +#' Create PAGASA links to regional weather forecasts PDF +#' +#' @returns A vector of URLs of PAGASA weather forecasts PDFs +#' +#' @examples +#' forecasts_create_urls() +#' +#' @rdname forecasts_create +#' @export +#' + +forecasts_create_urls <- function() { + base_url <- "https://src.panahon.gov.ph/pubfiles/prsd" + + regions <- c( + "mindanao", "national-capital-region", "northern-luzon", + "southern-luzon", "visayas" + ) + + file.path(base_url, regions, "regional_forecast.pdf") +} + +#' +#' Download a PAGASA regional weather forecast PDF +#' +#' @param url A URL to a PAGASA regional weather forecasts PDF. +#' @param directory A direcotry path to download the PAGASA regional weather +#' forecasts PDF to. Default is to current working directory. +#' @param overwrite Logical. Should an existing file with the same file path be +#' overwritten? Default is FALSE. +#' +#' @returns A file path to the downloaded PAGASA regional weather forecast PDF. +#' +#' @examples +#' forecasts_download( +#' url = "https://src.panahon.gov.ph/pubfiles/prsd/mindanao/regional_forecast.pdf" +#' ) +#' +#' @rdname forecasts_download +#' @export +#' + +forecasts_download <- function(url, + directory = "data-raw/forecasts", + overwrite = FALSE) { + ## Quiet down ssl verification ---- + h <- curl::new_handle() + curl::handle_setopt(h, .list = list(ssl_verifypeer = 0L)) + + ## Create file path to download ---- + destfile <- file.path( + directory, Sys.Date(), + stringr::str_remove( + string = url, + pattern = "https://src.panahon.gov.ph/pubfiles/prsd/" + ) + ) + + ## Full directory path ---- + full_directory <- stringr::str_remove( + string = destfile, + pattern = "/regional_forecast.pdf" + ) + + ## Create directories as needed ---- + if (!dir.exists(directory)) dir.create(directory) + if (!dir.exists(file.path(directory, Sys.Date()))) + dir.create(file.path(directory, Sys.Date())) + if (!dir.exists(full_directory)) dir.create(full_directory) + + ## Download PDF ---- + if ( + !destfile %in% list.files(full_directory, full.names = TRUE) | + length(list.files(full_directory, full.names = TRUE)) == 0 + ) { + ## Download file ---- + #download.file(url = url, destfile = destfile) + curl::curl_download(url = url, destfile = destfile, handle = h) + } else { + if (overwrite) { + ## Download file ---- + #download.file(url = url, destfile = destfile) + curl::curl_download(url = url, destfile = destfile, handle = h) + } + } + + ## Return path to downloaded file ---- + destfile +} diff --git a/_targets.R b/_targets.R index 302274b..9730d84 100644 --- a/_targets.R +++ b/_targets.R @@ -27,6 +27,10 @@ source("_targets_dam.R") source("_targets_heat.R") +### Forecasts targets ---- +source("_targets_forecasts.R") + + ### Analysis targets analysis_targets <- tar_plan( diff --git a/_targets_forecasts.R b/_targets_forecasts.R new file mode 100644 index 0000000..0be251c --- /dev/null +++ b/_targets_forecasts.R @@ -0,0 +1,55 @@ +################################################################################ +# +# Targets workflow for forecasts data download, extraction, and processing +# +################################################################################ + +## Download targets ------------------------------------------------------------ + +forecasts_download_targets <- tar_plan( + ### Set PAGASA forecasts pubfiles URL ---- + tar_target( + name = forecasts_pubfiles_urls, + command = forecasts_create_urls() + ), + ### Download PAGASA forecasts PDF ---- + tar_target( + name = forecasts_download_files, + command = forecasts_download( + url = forecasts_pubfiles_urls, + directory = "data-raw/forecasts", + overwrite = FALSE + ), + pattern = map(forecasts_pubfiles_urls), + format = "file" + ) +) + + +## Processing targets ---------------------------------------------------------- +forecasts_processing_targets <- tar_plan( + +) + + +## Analysis targets ------------------------------------------------------------ +forecasts_analysis_targets <- tar_plan( + +) + + +## Output targets -------------------------------------------------------------- +forecasts_output_targets <- tar_plan( +) + + +## Reporting targets ----------------------------------------------------------- +forecasts_report_targets <- tar_plan( + +) + + +## Deploy targets -------------------------------------------------------------- +forecasts_deploy_targets <- tar_plan( + +) diff --git a/data-raw/forecasts/2024-09-08/mindanao/regional_forecast.pdf b/data-raw/forecasts/2024-09-08/mindanao/regional_forecast.pdf new file mode 100644 index 0000000..ee92efe Binary files /dev/null and b/data-raw/forecasts/2024-09-08/mindanao/regional_forecast.pdf differ diff --git a/data-raw/forecasts/2024-09-08/national-capital-region/regional_forecast.pdf b/data-raw/forecasts/2024-09-08/national-capital-region/regional_forecast.pdf new file mode 100644 index 0000000..c131da6 Binary files /dev/null and b/data-raw/forecasts/2024-09-08/national-capital-region/regional_forecast.pdf differ diff --git a/data-raw/forecasts/2024-09-08/northern-luzon/regional_forecast.pdf b/data-raw/forecasts/2024-09-08/northern-luzon/regional_forecast.pdf new file mode 100644 index 0000000..b052a07 Binary files /dev/null and b/data-raw/forecasts/2024-09-08/northern-luzon/regional_forecast.pdf differ diff --git a/data-raw/forecasts/2024-09-08/southern-luzon/regional_forecast.pdf b/data-raw/forecasts/2024-09-08/southern-luzon/regional_forecast.pdf new file mode 100644 index 0000000..8c61be9 Binary files /dev/null and b/data-raw/forecasts/2024-09-08/southern-luzon/regional_forecast.pdf differ diff --git a/data-raw/forecasts/2024-09-08/visayas/regional_forecast.pdf b/data-raw/forecasts/2024-09-08/visayas/regional_forecast.pdf new file mode 100644 index 0000000..3301a95 Binary files /dev/null and b/data-raw/forecasts/2024-09-08/visayas/regional_forecast.pdf differ