Skip to content

Commit

Permalink
Merge pull request #57 from panukatan/dev
Browse files Browse the repository at this point in the history
add targets for forecasts; fix add workflow for weather forecasts #55
  • Loading branch information
ernestguevarra authored Sep 8, 2024
2 parents 0aaae0c + cd6cafd commit 498db92
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: deploy targets dam
name: deploy targets download

on:
schedule:
Expand All @@ -8,7 +8,7 @@ on:
- '*'

jobs:
deploy-targets-dam:
deploy-targets-download:
runs-on: ubuntu-latest
container: rocker/tidyverse:4.4.1
steps:
Expand Down Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions .github/workflows/test-targets-forecasts.yml
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}
90 changes: 90 additions & 0 deletions R/pagasa_forecasts.R
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
}
4 changes: 4 additions & 0 deletions _targets.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ source("_targets_dam.R")
source("_targets_heat.R")


### Forecasts targets ----
source("_targets_forecasts.R")


### Analysis targets
analysis_targets <- tar_plan(

Expand Down
55 changes: 55 additions & 0 deletions _targets_forecasts.R
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 not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 498db92

Please sign in to comment.