Skip to content

Commit

Permalink
add functions and targets for downloading agriculture forecasts; fix …
Browse files Browse the repository at this point in the history
…add farm weather forecasts download targets #63
  • Loading branch information
ernestguevarra committed Sep 8, 2024
1 parent 19b6c14 commit 96cb577
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 13 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/deploy-targets-downloads.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ on:
schedule:
- cron: "0 7 * * *"
workflow_dispatch:
branches:
- '*'

jobs:
deploy-targets-download:
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/deploy-targets-releases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ on:
schedule:
- cron: "0 17 * * SUN"
workflow_dispatch:
branches:
- '*'

jobs:
deploy-targets-releases:
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/test-targets-climate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ on:
pull_request:
branches: [main, master]
workflow_dispatch:
branches:
- '*'

jobs:
test-targets-climate:
Expand Down
36 changes: 36 additions & 0 deletions .github/workflows/test-targets-forecasts-agriculture.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: test targets forecasts agriculture

on:
pull_request:
branches: [main, master]
workflow_dispatch:

jobs:
test-targets-forecasts-agriculture:
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_agriculture"))
shell: Rscript {0}
2 changes: 0 additions & 2 deletions .github/workflows/test-targets-forecasts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ on:
pull_request:
branches: [main, master]
workflow_dispatch:
branches:
- '*'

jobs:
test-targets-forecasts:
Expand Down
10 changes: 5 additions & 5 deletions R/pagasa_forecasts.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ forecasts_create_urls <- function() {
#'
#' Download a PAGASA regional weather forecast PDF
#'
#' @param url A URL to a PAGASA regional weather forecasts 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
Expand All @@ -41,7 +41,7 @@ forecasts_create_urls <- function() {
#' @export
#'

forecasts_download <- function(url,
forecasts_download <- function(.url,
directory = "data-raw/forecasts",
overwrite = FALSE) {
## Quiet down ssl verification ----
Expand All @@ -52,7 +52,7 @@ forecasts_download <- function(url,
destfile <- file.path(
directory, Sys.Date(),
stringr::str_remove(
string = url,
string = .url,
pattern = "https://src.panahon.gov.ph/pubfiles/prsd/"
)
)
Expand All @@ -76,12 +76,12 @@ forecasts_download <- function(url,
) {
## Download file ----
#download.file(url = url, destfile = destfile)
curl::curl_download(url = url, destfile = destfile, handle = h)
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)
curl::curl_download(url = .url, destfile = destfile, handle = h)
}
}

Expand Down
54 changes: 54 additions & 0 deletions R/pagasa_forecasts_agriculture.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#'
#' Download PAGASA daily agriculture forecasts
#'
#' @param directory Path to directory to download the daily agriculture
#' forecast PDF to.
#' @param overwrite Logical. Should an existing file with the same file path be
#' overwritten? Default is FALSE.
#'
#' @returns The path to the downloaded daily agriculture forecast PDF.
#'
#' @examples
#' forecasts_agriculture_download()
#'
#' @rdname forecasts_agriculture
#' @export
#'

forecasts_agriculture_download <- function(directory = "data-raw/forecasts_agriculture",
overwrite = FALSE) {
if (weekdays(Sys.Date()) %in% c("Saturday", "Sunday")) {
.url <- "https://pubfiles.pagasa.dost.gov.ph/pagasaweb/files/agriculture/weekend_special_farm_forecast/weekend_special_farm_weather_forecast.pdf"
} else {
.url = "https://pubfiles.pagasa.dost.gov.ph/pagasaweb/files/agriculture/farm_weather_forecast/farm_weather_forecast.pdf"
}

## 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, paste0(Sys.Date(), ".pdf"))

## Create directories as needed ----
if (!dir.exists(directory)) dir.create(directory)

## Download PDF ----
if (
!destfile %in% list.files(directory, full.names = TRUE) |
length(list.files(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
}
14 changes: 14 additions & 0 deletions _targets_forecasts.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ forecasts_download_targets <- tar_plan(
),
pattern = map(forecasts_pubfiles_urls),
format = "file"
),
### Download PAGASA agriculture forecasts PDF ----
tar_target(
name = forecasts_agriculture_download_files,
command = forecasts_agriculture_download(),
format = "file"
)
)

Expand All @@ -39,6 +45,14 @@ forecasts_data_targets <- tar_plan(
name = forecasts_data_raw,
command = forecasts_get_data(forecasts_archive_pdfs),
pattern = map(forecasts_archive_pdfs)
),
### List PAGASA agriculture forecasts data files ----
tar_target(
name = forecasts_agriculture_archive_pdfs,
command = list.files(
path = "data-raw/forecasts_agriculture",
full.names = TRUE, recursive = TRUE
)
)
)

Expand Down
Binary file added data-raw/forecasts_agriculture/2024-09-08.pdf
Binary file not shown.

0 comments on commit 96cb577

Please sign in to comment.