From f25b2a0e4ec37475c3456b21028d5242b9d355a3 Mon Sep 17 00:00:00 2001 From: Elio Campitelli Date: Mon, 30 Oct 2023 18:01:10 -0300 Subject: [PATCH 1/9] Adds asymmetric and symmetric SAM --- DESCRIPTION | 2 +- NAMESPACE | 2 + R/download-asymsam.R | 111 ++++++++++++++++++++++++++++++++ man/download_asymsam_monthly.Rd | 44 +++++++++++++ rsoi.Rproj | 1 + tests/testthat/test_download_.R | 3 +- 6 files changed, 160 insertions(+), 3 deletions(-) create mode 100644 R/download-asymsam.R create mode 100644 man/download_asymsam_monthly.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 885dae4..573b5b0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -29,4 +29,4 @@ Suggests: testthat (>= 2.1.0), tibble Encoding: UTF-8 -RoxygenNote: 7.1.2 +RoxygenNote: 7.2.3 diff --git a/NAMESPACE b/NAMESPACE index 48a8846..2ab187d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,6 +2,8 @@ export(download_aao) export(download_ao) +export(download_asymsam_daily) +export(download_asymsam_monthly) export(download_dmi) export(download_enso) export(download_mei) diff --git a/R/download-asymsam.R b/R/download-asymsam.R new file mode 100644 index 0000000..78348b0 --- /dev/null +++ b/R/download-asymsam.R @@ -0,0 +1,111 @@ +#' @export +#' @title Download Asymmetric and Symmetric SAM indices +#' +#' @description The Asymmetric and Symmetric SAM indices are computed as the +#' projection of geopotential height anomalies onto the zonally asymmetric and +#' zonally symmetric parts of the SAM field. +#' +#' @inheritParams download_oni +#' @param level atmospheric levels in hPa to download. +#' +#' @return +#' \itemize{ +#' \item Lev: Atmospheric level in hPa +#' \item Date: Date object that uses the first of the month as a placeholder. Date formatted as date on the first of the month because R only supports one partial of date time +#' \item Index: Type of index. Either "sam", "ssam" or "asam". +#' \item Value: Value of the index +#' \item Value_normalized: Value of the index normalized by the standard deviation of the index +#' \item R.squared: The variance explained by the index (only in the daily version) +#' } +#' +#' @examples +#' \dontrun{ +#' asymsam <- download_asymsam_monthly() +#' } +#' +#' @references \url{https://www.cima.fcen.uba.ar/~elio.campitelli/asymsam/} +download_asymsam_monthly <- function(use_cache = FALSE, file = NULL) { + with_cache(use_cache = use_cache, file = file, + memoised = download_asymsam_monthly_memoised, + unmemoised = download_asymsam_monthly_unmemoised, + read_function = read_asymsam_monthly) +} + +download_asymsam_monthly_unmemoised = function() { + asymsam_monthly_link = "https://www.cima.fcen.uba.ar/~elio.campitelli/asymsam/data/sam_monthly.csv" + + data <- read.csv(asymsam_monthly_link, colClasses = c("integer", "character", "Date", "numeric", "numeric"), + col.names = c("Lev", "Index", "Date", "Value", "Value_normalized")) + data$Index <- factor(data$Index, levels = c("sam", "ssam", "asam")) + + class(data) = c("tbl_df", "tbl", "data.frame") + data +} + +download_asymsam_monthly_memoised <- memoise::memoise(download_asymsam_monthly_unmemoised) + +read_asymsam_monthly <- function(file) { + data <- read.csv(file) + data$Date <- as.Date(data$Date) + data$Index <- factor(data$Index, levels = c("sam", "ssam", "asam")) + class(data) <- c("tbl_df", "tbl", "data.frame") + data +} + + +#' @export +#' @rdname download_asymsam_monthly +download_asymsam_daily <- function(levels = 700, use_cache = FALSE, file = NULL) { + with_cache(use_cache = use_cache, file = file, + memoised = download_asymsam_monthly_memoised, + unmemoised = download_asymsam_daily_unmemoised, + read_function = read_asymsam_monthly, + levels = levels) +} + + +download_asymsam_daily_unmemoised = function(levels = 700) { + available_levels <- c(1L, 2L, 3L, 5L, 7L, 10L, 20L, 30L, 50L, 70L, 100L, 125L, 150L, + 175L, 200L, 225L, 250L, 300L, 350L, 400L, 450L, 500L, 550L, 600L, + 650L, 700L, 750L, 775L, 800L, 825L, 850L, 875L, 900L, 925L, 950L, + 975L, 1000L) + if (levels[1] == "all") { + levels <- available_levels + } + + bad_levels <- !(levels %in% available_levels) + if (any(bad_levels)) { + stop(paste("Invalid levels:", paste(levels[bad_levels], collapse = ", "), + "\nValid levels are:", paste(available_levels, collapse = ", "))) + } + + root_link = "https://www.cima.fcen.uba.ar/~elio.campitelli/asymsam/data/sam_level/" + + all_data <- list() + for (level in levels) { + message("Downloading level: ", level) + link <- paste0(root_link, "sam_", level, "hPa.csv") + data <- read.csv(link, colClasses = c("integer", "character", "Date", "numeric", "numeric", "numeric"), + col.names = c("Lev", "Index", "Date", "Value", "R.squared", "dump")) + data$dump <- NULL + all_data <- append(all_data, list(data)) + } + all_data <- do.call(rbind, all_data) + + all_data$Index <- factor(all_data$Index, levels = c("sam", "ssam", "asam")) + + class(all_data) = c("tbl_df", "tbl", "data.frame") + all_data + } + + +download_asymsam_monthly_memoised <- download_asymsam_monthly_unmemoised + +read_asymsam_daily <- function(file) { + data <- read.csv(file) + data$Date <- as.Date(data$Date) + data$Lev <- as.integer(data$Lev) + data$Index <- factor(data$Index, levels = c("sam", "ssam", "asam")) + class(data) <- c("tbl_df", "tbl", "data.frame") + data +} diff --git a/man/download_asymsam_monthly.Rd b/man/download_asymsam_monthly.Rd new file mode 100644 index 0000000..f6a25f9 --- /dev/null +++ b/man/download_asymsam_monthly.Rd @@ -0,0 +1,44 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/download-asymsam.R +\name{download_asymsam_monthly} +\alias{download_asymsam_monthly} +\alias{download_asymsam_daily} +\title{Download Asymmetric and Symmetric SAM indices} +\usage{ +download_asymsam_monthly(use_cache = FALSE, file = NULL) + +download_asymsam_daily(levels = 700, use_cache = FALSE, file = NULL) +} +\arguments{ +\item{use_cache}{logical option to save and load from cache. If `TRUE`, results will be cached in memory +if `file` is `NULL` or on disk if `file` is not `NULL`.} + +\item{file}{optional character with the full path of a file to save the data. If `cache` is `FALSE` but +`file` is not `NULL`, the results will be downloaded from the internet and saved on disk.} + +\item{level}{atmospheric levels in hPa to download.} +} +\value{ +\itemize{ +\item Lev: Atmospheric level in hPa +\item Date: Date object that uses the first of the month as a placeholder. Date formatted as date on the first of the month because R only supports one partial of date time +\item Index: Type of index. Either "sam", "ssam" or "asam". +\item Value: Value of the index +\item Value_normalized: Value of the index normalized by the standard deviation of the index +\item R.squared: The variance explained by the index (only in the daily version) +} +} +\description{ +The Asymmetric and Symmetric SAM indices are computed as the +projection of geopotential height anomalies onto the zonally asymmetric and +zonally symmetric parts of the SAM field. +} +\examples{ +\dontrun{ +asymsam <- download_asymsam_monthly() +} + +} +\references{ +\url{https://www.cima.fcen.uba.ar/~elio.campitelli/asymsam/} +} diff --git a/rsoi.Rproj b/rsoi.Rproj index 21a4da0..eaa6b81 100644 --- a/rsoi.Rproj +++ b/rsoi.Rproj @@ -15,3 +15,4 @@ LaTeX: pdfLaTeX BuildType: Package PackageUseDevtools: Yes PackageInstallArgs: --no-multiarch --with-keep.source +PackageRoxygenize: rd,collate,namespace diff --git a/tests/testthat/test_download_.R b/tests/testthat/test_download_.R index 60247e8..b261f59 100644 --- a/tests/testthat/test_download_.R +++ b/tests/testthat/test_download_.R @@ -1,5 +1,4 @@ -indexes <- c("oni", "ao", "nao", "soi", "mei", "npgo", "aao", "pdo", "dmi") - +indexes <- c("oni", "ao", "nao", "soi", "mei", "npgo", "aao", "pdo", "dmi", "asymsam_monthly", "asymsam_daily") context("Testing download") From 646fe8fa94f6ba2aa3da90a4338c015ee3092966 Mon Sep 17 00:00:00 2001 From: Elio Campitelli Date: Mon, 30 Oct 2023 18:11:39 -0300 Subject: [PATCH 2/9] Adds citation --- R/download-asymsam.R | 6 ++++-- man/download_asymsam_monthly.Rd | 8 +++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/R/download-asymsam.R b/R/download-asymsam.R index 78348b0..b6915d4 100644 --- a/R/download-asymsam.R +++ b/R/download-asymsam.R @@ -4,9 +4,11 @@ #' @description The Asymmetric and Symmetric SAM indices are computed as the #' projection of geopotential height anomalies onto the zonally asymmetric and #' zonally symmetric parts of the SAM field. +#' The detailed methodology can be found in Campitelli et al. (2022). +#' The source of the data is \url{https://www.cima.fcen.uba.ar/~elio.campitelli/asymsam/} #' #' @inheritParams download_oni -#' @param level atmospheric levels in hPa to download. +#' @param levels atmospheric levels in hPa to download. #' #' @return #' \itemize{ @@ -23,7 +25,7 @@ #' asymsam <- download_asymsam_monthly() #' } #' -#' @references \url{https://www.cima.fcen.uba.ar/~elio.campitelli/asymsam/} +#' @references Campitelli, E., Díaz, L. B., & Vera, C. (2022). Assessment of zonally symmetric and asymmetric components of the Southern Annular Mode using a novel approach. Climate Dynamics, 58(1), 161–178. \url{https://doi.org/10.1007/s00382-021-05896-5} download_asymsam_monthly <- function(use_cache = FALSE, file = NULL) { with_cache(use_cache = use_cache, file = file, memoised = download_asymsam_monthly_memoised, diff --git a/man/download_asymsam_monthly.Rd b/man/download_asymsam_monthly.Rd index f6a25f9..f077f95 100644 --- a/man/download_asymsam_monthly.Rd +++ b/man/download_asymsam_monthly.Rd @@ -16,7 +16,7 @@ if `file` is `NULL` or on disk if `file` is not `NULL`.} \item{file}{optional character with the full path of a file to save the data. If `cache` is `FALSE` but `file` is not `NULL`, the results will be downloaded from the internet and saved on disk.} -\item{level}{atmospheric levels in hPa to download.} +\item{levels}{atmospheric levels in hPa to download.} } \value{ \itemize{ @@ -31,7 +31,9 @@ if `file` is `NULL` or on disk if `file` is not `NULL`.} \description{ The Asymmetric and Symmetric SAM indices are computed as the projection of geopotential height anomalies onto the zonally asymmetric and -zonally symmetric parts of the SAM field. +zonally symmetric parts of the SAM field. +The detailed methodology can be found in Campitelli et al. (2022). +The source of the data is \url{https://www.cima.fcen.uba.ar/~elio.campitelli/asymsam/} } \examples{ \dontrun{ @@ -40,5 +42,5 @@ asymsam <- download_asymsam_monthly() } \references{ -\url{https://www.cima.fcen.uba.ar/~elio.campitelli/asymsam/} +Campitelli, E., Díaz, L. B., & Vera, C. (2022). Assessment of zonally symmetric and asymmetric components of the Southern Annular Mode using a novel approach. Climate Dynamics, 58(1), 161–178. \url{https://doi.org/10.1007/s00382-021-05896-5} } From dc57d99096a75ab0c1cca50d91b593bc9636cb3e Mon Sep 17 00:00:00 2001 From: Elio Campitelli Date: Mon, 30 Oct 2023 18:14:37 -0300 Subject: [PATCH 3/9] Documents available leveles --- R/download-asymsam.R | 4 ++++ man/download_asymsam_monthly.Rd | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/R/download-asymsam.R b/R/download-asymsam.R index b6915d4..270bbad 100644 --- a/R/download-asymsam.R +++ b/R/download-asymsam.R @@ -9,6 +9,10 @@ #' #' @inheritParams download_oni #' @param levels atmospheric levels in hPa to download. +#' If \code{"all"} download all available levels. +#' Available levels are: 1, 2, 3, 5, 7, 10, 20, 30, 50, 70, 100, 125, 150, 175, +#' 200, 225, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750, 775, 800, +#' 825, 850, 875, 900, 925, 950, 975 and 1000. #' #' @return #' \itemize{ diff --git a/man/download_asymsam_monthly.Rd b/man/download_asymsam_monthly.Rd index f077f95..e1728d9 100644 --- a/man/download_asymsam_monthly.Rd +++ b/man/download_asymsam_monthly.Rd @@ -16,7 +16,11 @@ if `file` is `NULL` or on disk if `file` is not `NULL`.} \item{file}{optional character with the full path of a file to save the data. If `cache` is `FALSE` but `file` is not `NULL`, the results will be downloaded from the internet and saved on disk.} -\item{levels}{atmospheric levels in hPa to download.} +\item{levels}{atmospheric levels in hPa to download. +If \verbatim{"all"} download all available levels. +Available levels are: 1, 2, 3, 5, 7, 10, 20, 30, 50, 70, 100, 125, 150, 175, + 200, 225, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750, 775, 800, + 825, 850, 875, 900, 925, 950, 975 and 1000.} } \value{ \itemize{ From 705c18812033b5075d5d316fcf03cfd5f107281a Mon Sep 17 00:00:00 2001 From: Sam Albers Date: Mon, 30 Oct 2023 15:24:15 -0700 Subject: [PATCH 4/9] update actions --- .github/workflows/R-CMD-check.yaml | 19 +++++++++++-------- README.Rmd | 2 +- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 271d466..a3ac618 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -1,4 +1,4 @@ -# Workflow derived from https://github.com/r-lib/actions/tree/master/examples +# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help on: push: @@ -18,7 +18,7 @@ jobs: fail-fast: false matrix: config: - - {os: macOS-latest, r: 'release'} + - {os: macos-latest, r: 'release'} - {os: windows-latest, r: 'release'} - {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'} - {os: ubuntu-latest, r: 'release'} @@ -29,18 +29,21 @@ jobs: R_KEEP_PKG_SOURCE: yes steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - - uses: r-lib/actions/setup-pandoc@v1 + - uses: r-lib/actions/setup-pandoc@v2 - - uses: r-lib/actions/setup-r@v1 + - uses: r-lib/actions/setup-r@v2 with: r-version: ${{ matrix.config.r }} http-user-agent: ${{ matrix.config.http-user-agent }} use-public-rspm: true - - uses: r-lib/actions/setup-r-dependencies@v1 + - uses: r-lib/actions/setup-r-dependencies@v2 with: - extra-packages: rcmdcheck + extra-packages: any::rcmdcheck + needs: check - - uses: r-lib/actions/check-r-package@v1 + - uses: r-lib/actions/check-r-package@v2 + with: + upload-snapshots: true diff --git a/README.Rmd b/README.Rmd index 2e90fda..5d7a499 100644 --- a/README.Rmd +++ b/README.Rmd @@ -15,7 +15,7 @@ knitr::opts_chunk$set( [![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0) -[![R-CMD-check](https://github.com/boshek/rsoi/workflows/R-CMD-check/badge.svg)](https://github.com/boshek/rsoi/actions) +[![R-CMD-check](https://github.com/boshek/rsoi/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/boshek/rsoi/actions/workflows/R-CMD-check.yaml) [![CRAN\_Status\_Badge](https://www.r-pkg.org/badges/version/rsoi)](https://cran.r-project.org/package=rsoi) [![CRAN Downloads](https://cranlogs.r-pkg.org/badges/rsoi?color=brightgreen)](https://CRAN.R-project.org/package=rsoi) [![cran checks](https://cranchecks.info/badges/worst/rsoi)](https://cran.r-project.org/web/checks/check_results_rsoi.html) From 18e975dc6087e45e615ebbf42ea3c28ed8084bbd Mon Sep 17 00:00:00 2001 From: Sam Albers Date: Mon, 30 Oct 2023 16:03:56 -0700 Subject: [PATCH 5/9] accomodate data change --- R/download-nao.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/download-nao.R b/R/download-nao.R index b670b77..ba6c61f 100644 --- a/R/download-nao.R +++ b/R/download-nao.R @@ -39,7 +39,8 @@ download_nao_unmemoised <- function(){ nao = read.fwf(nao_link, widths = c(4, rep(7, 12)), - header = FALSE, + header = FALSE, + skip = 1, col.names = c("Year", month.abb)) reshaped_list <- lapply( From 9f01fb1f2da88c32cd98729432578e4c3e466170 Mon Sep 17 00:00:00 2001 From: Sam Albers Date: Mon, 30 Oct 2023 19:59:53 -0700 Subject: [PATCH 6/9] update pkgdown --- .github/workflows/pkgdown.yaml | 35 +++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/.github/workflows/pkgdown.yaml b/.github/workflows/pkgdown.yaml index 63cbb18..ed7650c 100644 --- a/.github/workflows/pkgdown.yaml +++ b/.github/workflows/pkgdown.yaml @@ -1,8 +1,10 @@ -# Workflow derived from https://github.com/r-lib/actions/tree/master/examples +# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help on: push: branches: [main, master] + pull_request: + branches: [main, master] release: types: [published] workflow_dispatch: @@ -12,24 +14,35 @@ name: pkgdown jobs: pkgdown: runs-on: ubuntu-latest + # Only restrict concurrency for non-PR jobs + concurrency: + group: pkgdown-${{ github.event_name != 'pull_request' || github.run_id }} env: GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + permissions: + contents: write steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - - uses: r-lib/actions/setup-pandoc@v1 + - uses: r-lib/actions/setup-pandoc@v2 - - uses: r-lib/actions/setup-r@v1 + - uses: r-lib/actions/setup-r@v2 with: use-public-rspm: true - - uses: r-lib/actions/setup-r-dependencies@v1 + - uses: r-lib/actions/setup-r-dependencies@v2 with: - extra-packages: pkgdown + extra-packages: any::pkgdown, local::. needs: website - - name: Deploy package - run: | - git config --local user.name "$GITHUB_ACTOR" - git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com" - Rscript -e 'pkgdown::deploy_to_branch(new_process = FALSE)' + - name: Build site + run: pkgdown::build_site_github_pages(new_process = FALSE, install = FALSE) + shell: Rscript {0} + + - name: Deploy to GitHub pages 🚀 + if: github.event_name != 'pull_request' + uses: JamesIves/github-pages-deploy-action@v4.4.1 + with: + clean: false + branch: gh-pages + folder: docs From 004ce1ab8089c1500cee6bc3c6d9508e8f722e27 Mon Sep 17 00:00:00 2001 From: Elio Campitelli Date: Mon, 30 Oct 2023 18:01:10 -0300 Subject: [PATCH 7/9] Adds asymmetric and symmetric SAM --- DESCRIPTION | 2 +- NAMESPACE | 2 + R/download-asymsam.R | 111 ++++++++++++++++++++++++++++++++ man/download_asymsam_monthly.Rd | 44 +++++++++++++ rsoi.Rproj | 1 + tests/testthat/test_download_.R | 3 +- 6 files changed, 160 insertions(+), 3 deletions(-) create mode 100644 R/download-asymsam.R create mode 100644 man/download_asymsam_monthly.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 885dae4..573b5b0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -29,4 +29,4 @@ Suggests: testthat (>= 2.1.0), tibble Encoding: UTF-8 -RoxygenNote: 7.1.2 +RoxygenNote: 7.2.3 diff --git a/NAMESPACE b/NAMESPACE index 48a8846..2ab187d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,6 +2,8 @@ export(download_aao) export(download_ao) +export(download_asymsam_daily) +export(download_asymsam_monthly) export(download_dmi) export(download_enso) export(download_mei) diff --git a/R/download-asymsam.R b/R/download-asymsam.R new file mode 100644 index 0000000..78348b0 --- /dev/null +++ b/R/download-asymsam.R @@ -0,0 +1,111 @@ +#' @export +#' @title Download Asymmetric and Symmetric SAM indices +#' +#' @description The Asymmetric and Symmetric SAM indices are computed as the +#' projection of geopotential height anomalies onto the zonally asymmetric and +#' zonally symmetric parts of the SAM field. +#' +#' @inheritParams download_oni +#' @param level atmospheric levels in hPa to download. +#' +#' @return +#' \itemize{ +#' \item Lev: Atmospheric level in hPa +#' \item Date: Date object that uses the first of the month as a placeholder. Date formatted as date on the first of the month because R only supports one partial of date time +#' \item Index: Type of index. Either "sam", "ssam" or "asam". +#' \item Value: Value of the index +#' \item Value_normalized: Value of the index normalized by the standard deviation of the index +#' \item R.squared: The variance explained by the index (only in the daily version) +#' } +#' +#' @examples +#' \dontrun{ +#' asymsam <- download_asymsam_monthly() +#' } +#' +#' @references \url{https://www.cima.fcen.uba.ar/~elio.campitelli/asymsam/} +download_asymsam_monthly <- function(use_cache = FALSE, file = NULL) { + with_cache(use_cache = use_cache, file = file, + memoised = download_asymsam_monthly_memoised, + unmemoised = download_asymsam_monthly_unmemoised, + read_function = read_asymsam_monthly) +} + +download_asymsam_monthly_unmemoised = function() { + asymsam_monthly_link = "https://www.cima.fcen.uba.ar/~elio.campitelli/asymsam/data/sam_monthly.csv" + + data <- read.csv(asymsam_monthly_link, colClasses = c("integer", "character", "Date", "numeric", "numeric"), + col.names = c("Lev", "Index", "Date", "Value", "Value_normalized")) + data$Index <- factor(data$Index, levels = c("sam", "ssam", "asam")) + + class(data) = c("tbl_df", "tbl", "data.frame") + data +} + +download_asymsam_monthly_memoised <- memoise::memoise(download_asymsam_monthly_unmemoised) + +read_asymsam_monthly <- function(file) { + data <- read.csv(file) + data$Date <- as.Date(data$Date) + data$Index <- factor(data$Index, levels = c("sam", "ssam", "asam")) + class(data) <- c("tbl_df", "tbl", "data.frame") + data +} + + +#' @export +#' @rdname download_asymsam_monthly +download_asymsam_daily <- function(levels = 700, use_cache = FALSE, file = NULL) { + with_cache(use_cache = use_cache, file = file, + memoised = download_asymsam_monthly_memoised, + unmemoised = download_asymsam_daily_unmemoised, + read_function = read_asymsam_monthly, + levels = levels) +} + + +download_asymsam_daily_unmemoised = function(levels = 700) { + available_levels <- c(1L, 2L, 3L, 5L, 7L, 10L, 20L, 30L, 50L, 70L, 100L, 125L, 150L, + 175L, 200L, 225L, 250L, 300L, 350L, 400L, 450L, 500L, 550L, 600L, + 650L, 700L, 750L, 775L, 800L, 825L, 850L, 875L, 900L, 925L, 950L, + 975L, 1000L) + if (levels[1] == "all") { + levels <- available_levels + } + + bad_levels <- !(levels %in% available_levels) + if (any(bad_levels)) { + stop(paste("Invalid levels:", paste(levels[bad_levels], collapse = ", "), + "\nValid levels are:", paste(available_levels, collapse = ", "))) + } + + root_link = "https://www.cima.fcen.uba.ar/~elio.campitelli/asymsam/data/sam_level/" + + all_data <- list() + for (level in levels) { + message("Downloading level: ", level) + link <- paste0(root_link, "sam_", level, "hPa.csv") + data <- read.csv(link, colClasses = c("integer", "character", "Date", "numeric", "numeric", "numeric"), + col.names = c("Lev", "Index", "Date", "Value", "R.squared", "dump")) + data$dump <- NULL + all_data <- append(all_data, list(data)) + } + all_data <- do.call(rbind, all_data) + + all_data$Index <- factor(all_data$Index, levels = c("sam", "ssam", "asam")) + + class(all_data) = c("tbl_df", "tbl", "data.frame") + all_data + } + + +download_asymsam_monthly_memoised <- download_asymsam_monthly_unmemoised + +read_asymsam_daily <- function(file) { + data <- read.csv(file) + data$Date <- as.Date(data$Date) + data$Lev <- as.integer(data$Lev) + data$Index <- factor(data$Index, levels = c("sam", "ssam", "asam")) + class(data) <- c("tbl_df", "tbl", "data.frame") + data +} diff --git a/man/download_asymsam_monthly.Rd b/man/download_asymsam_monthly.Rd new file mode 100644 index 0000000..f6a25f9 --- /dev/null +++ b/man/download_asymsam_monthly.Rd @@ -0,0 +1,44 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/download-asymsam.R +\name{download_asymsam_monthly} +\alias{download_asymsam_monthly} +\alias{download_asymsam_daily} +\title{Download Asymmetric and Symmetric SAM indices} +\usage{ +download_asymsam_monthly(use_cache = FALSE, file = NULL) + +download_asymsam_daily(levels = 700, use_cache = FALSE, file = NULL) +} +\arguments{ +\item{use_cache}{logical option to save and load from cache. If `TRUE`, results will be cached in memory +if `file` is `NULL` or on disk if `file` is not `NULL`.} + +\item{file}{optional character with the full path of a file to save the data. If `cache` is `FALSE` but +`file` is not `NULL`, the results will be downloaded from the internet and saved on disk.} + +\item{level}{atmospheric levels in hPa to download.} +} +\value{ +\itemize{ +\item Lev: Atmospheric level in hPa +\item Date: Date object that uses the first of the month as a placeholder. Date formatted as date on the first of the month because R only supports one partial of date time +\item Index: Type of index. Either "sam", "ssam" or "asam". +\item Value: Value of the index +\item Value_normalized: Value of the index normalized by the standard deviation of the index +\item R.squared: The variance explained by the index (only in the daily version) +} +} +\description{ +The Asymmetric and Symmetric SAM indices are computed as the +projection of geopotential height anomalies onto the zonally asymmetric and +zonally symmetric parts of the SAM field. +} +\examples{ +\dontrun{ +asymsam <- download_asymsam_monthly() +} + +} +\references{ +\url{https://www.cima.fcen.uba.ar/~elio.campitelli/asymsam/} +} diff --git a/rsoi.Rproj b/rsoi.Rproj index 21a4da0..eaa6b81 100644 --- a/rsoi.Rproj +++ b/rsoi.Rproj @@ -15,3 +15,4 @@ LaTeX: pdfLaTeX BuildType: Package PackageUseDevtools: Yes PackageInstallArgs: --no-multiarch --with-keep.source +PackageRoxygenize: rd,collate,namespace diff --git a/tests/testthat/test_download_.R b/tests/testthat/test_download_.R index 60247e8..b261f59 100644 --- a/tests/testthat/test_download_.R +++ b/tests/testthat/test_download_.R @@ -1,5 +1,4 @@ -indexes <- c("oni", "ao", "nao", "soi", "mei", "npgo", "aao", "pdo", "dmi") - +indexes <- c("oni", "ao", "nao", "soi", "mei", "npgo", "aao", "pdo", "dmi", "asymsam_monthly", "asymsam_daily") context("Testing download") From 4f66f3edb3f733f70aaa816cb3d268a6e0ca2b70 Mon Sep 17 00:00:00 2001 From: Elio Campitelli Date: Mon, 30 Oct 2023 18:11:39 -0300 Subject: [PATCH 8/9] Adds citation --- R/download-asymsam.R | 6 ++++-- man/download_asymsam_monthly.Rd | 8 +++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/R/download-asymsam.R b/R/download-asymsam.R index 78348b0..b6915d4 100644 --- a/R/download-asymsam.R +++ b/R/download-asymsam.R @@ -4,9 +4,11 @@ #' @description The Asymmetric and Symmetric SAM indices are computed as the #' projection of geopotential height anomalies onto the zonally asymmetric and #' zonally symmetric parts of the SAM field. +#' The detailed methodology can be found in Campitelli et al. (2022). +#' The source of the data is \url{https://www.cima.fcen.uba.ar/~elio.campitelli/asymsam/} #' #' @inheritParams download_oni -#' @param level atmospheric levels in hPa to download. +#' @param levels atmospheric levels in hPa to download. #' #' @return #' \itemize{ @@ -23,7 +25,7 @@ #' asymsam <- download_asymsam_monthly() #' } #' -#' @references \url{https://www.cima.fcen.uba.ar/~elio.campitelli/asymsam/} +#' @references Campitelli, E., Díaz, L. B., & Vera, C. (2022). Assessment of zonally symmetric and asymmetric components of the Southern Annular Mode using a novel approach. Climate Dynamics, 58(1), 161–178. \url{https://doi.org/10.1007/s00382-021-05896-5} download_asymsam_monthly <- function(use_cache = FALSE, file = NULL) { with_cache(use_cache = use_cache, file = file, memoised = download_asymsam_monthly_memoised, diff --git a/man/download_asymsam_monthly.Rd b/man/download_asymsam_monthly.Rd index f6a25f9..f077f95 100644 --- a/man/download_asymsam_monthly.Rd +++ b/man/download_asymsam_monthly.Rd @@ -16,7 +16,7 @@ if `file` is `NULL` or on disk if `file` is not `NULL`.} \item{file}{optional character with the full path of a file to save the data. If `cache` is `FALSE` but `file` is not `NULL`, the results will be downloaded from the internet and saved on disk.} -\item{level}{atmospheric levels in hPa to download.} +\item{levels}{atmospheric levels in hPa to download.} } \value{ \itemize{ @@ -31,7 +31,9 @@ if `file` is `NULL` or on disk if `file` is not `NULL`.} \description{ The Asymmetric and Symmetric SAM indices are computed as the projection of geopotential height anomalies onto the zonally asymmetric and -zonally symmetric parts of the SAM field. +zonally symmetric parts of the SAM field. +The detailed methodology can be found in Campitelli et al. (2022). +The source of the data is \url{https://www.cima.fcen.uba.ar/~elio.campitelli/asymsam/} } \examples{ \dontrun{ @@ -40,5 +42,5 @@ asymsam <- download_asymsam_monthly() } \references{ -\url{https://www.cima.fcen.uba.ar/~elio.campitelli/asymsam/} +Campitelli, E., Díaz, L. B., & Vera, C. (2022). Assessment of zonally symmetric and asymmetric components of the Southern Annular Mode using a novel approach. Climate Dynamics, 58(1), 161–178. \url{https://doi.org/10.1007/s00382-021-05896-5} } From d67052da14beea46fa7607ddb9addcaf670c936a Mon Sep 17 00:00:00 2001 From: Elio Campitelli Date: Mon, 30 Oct 2023 18:14:37 -0300 Subject: [PATCH 9/9] Documents available leveles --- R/download-asymsam.R | 4 ++++ man/download_asymsam_monthly.Rd | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/R/download-asymsam.R b/R/download-asymsam.R index b6915d4..270bbad 100644 --- a/R/download-asymsam.R +++ b/R/download-asymsam.R @@ -9,6 +9,10 @@ #' #' @inheritParams download_oni #' @param levels atmospheric levels in hPa to download. +#' If \code{"all"} download all available levels. +#' Available levels are: 1, 2, 3, 5, 7, 10, 20, 30, 50, 70, 100, 125, 150, 175, +#' 200, 225, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750, 775, 800, +#' 825, 850, 875, 900, 925, 950, 975 and 1000. #' #' @return #' \itemize{ diff --git a/man/download_asymsam_monthly.Rd b/man/download_asymsam_monthly.Rd index f077f95..e1728d9 100644 --- a/man/download_asymsam_monthly.Rd +++ b/man/download_asymsam_monthly.Rd @@ -16,7 +16,11 @@ if `file` is `NULL` or on disk if `file` is not `NULL`.} \item{file}{optional character with the full path of a file to save the data. If `cache` is `FALSE` but `file` is not `NULL`, the results will be downloaded from the internet and saved on disk.} -\item{levels}{atmospheric levels in hPa to download.} +\item{levels}{atmospheric levels in hPa to download. +If \verbatim{"all"} download all available levels. +Available levels are: 1, 2, 3, 5, 7, 10, 20, 30, 50, 70, 100, 125, 150, 175, + 200, 225, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750, 775, 800, + 825, 850, 875, 900, 925, 950, 975 and 1000.} } \value{ \itemize{