generated from OxfordIHTM/ihtm-targets-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from panukatan/dev
add targets for forecasts; fix add workflow for weather forecasts #55
- Loading branch information
Showing
10 changed files
with
190 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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( | ||
|
||
) |
Binary file not shown.
Binary file added
BIN
+194 KB
data-raw/forecasts/2024-09-08/national-capital-region/regional_forecast.pdf
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.