Skip to content

Commit

Permalink
Merge pull request #43 from eliocamp/dmi
Browse files Browse the repository at this point in the history
Adds download_dmi to download Dipole Mode Index (#42)
  • Loading branch information
boshek authored Mar 23, 2022
2 parents ea1cce3 + 71d4a86 commit 899fa0e
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export(download_aao)
export(download_ao)
export(download_dmi)
export(download_enso)
export(download_mei)
export(download_nao)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Skip some tests on CRAN and use CI services to catch errors.
* Removed links causing this failures:
> Check process probably crashed or hung up for 20 minutes ... killed
* Adds Dipole Mode Index. #42

## rsoi v0.5.3
* Remove Extended PDO because data is no longer available.
Expand Down
81 changes: 81 additions & 0 deletions R/download-dmi.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#' @export
#' @title Download Dipole Mode Index (DMI)
#'
#' @description Intensity of the IOD is represented by anomalous SST gradient
#' between the western equatorial Indian Ocean (50E-70E and 10S-10N) and the
#' south eastern equatorial Indian Ocean (90E-110E and 10S-0N).
#' This gradient is named as Dipole Mode Index (DMI).
#' When the DMI is positive then, the phenomenon is refereed as the positive
#' IOD and when it is negative, it is refereed as negative IOD.
#'
#'
#' @inheritParams download_oni
#'
#' @return
#' \itemize{
#' \item Year: Year of record
#' \item Month: Month of record
#' \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 DMI: Dipole Mode Index
#' }
#'
#' @examples
#' \dontrun{
#' dmi <- download_dmi()
#' }
#'
#' @references \url{https://psl.noaa.gov/gcos_wgsp/Timeseries/DMI/}
download_dmi <- function(use_cache = FALSE, file = NULL) {
with_cache(use_cache = use_cache, file = file,
memoised = download_dmi_memoised,
unmemoised = download_dmi_unmemoised,
read_function = read_dmi)
}

download_dmi_unmemoised = function() {
dmi_link = "https://psl.noaa.gov/gcos_wgsp/Timeseries/Data/dmi.had.long.data"

res = tryCatch(
check_response(dmi_link),
error = function(e) {
message(e)
return(invisible(NULL))
}
)

years = strsplit(trimws(readLines(res, n = 1)), " ")[[1]]
rows = diff(as.numeric(years)) + 1
months = month.abb

dmi = read.csv(check_response(dmi_link), # reset connection
header = FALSE,
col.names = c("Year", months),
nrows = rows,
skip = 1,
na.strings = "-9999.000",
sep = "",
stringsAsFactors = FALSE)

grid = expand.grid(Year = dmi$Year, Month = months)
grid$DMI = c(as.matrix(dmi[, months]))
grid$Month = factor(grid$Month, levels = months)
grid$Date = as.Date(paste0(grid$Year, "-", as.numeric(grid$Month) ,"-01"), "%Y-%m-%d")

grid <- grid[, c("Year", "Month", "Date", "DMI")]
class(grid) = c("tbl_df", "tbl", "data.frame")

return(grid)
}

download_dmi_memoised <- memoise::memoise(download_dmi_unmemoised)


read_dmi <- function(file) {
data <- read.csv(file)
data$Date <- as.Date(data$Date)
data$Month <- factor(data$Month,
levels = month.abb)

class(data) <- c("tbl_df", "tbl", "data.frame")
data
}
40 changes: 40 additions & 0 deletions man/download_dmi.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/testthat/test_download_.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
indexes <- c("oni", "ao", "nao", "soi", "mei", "npgo", "aao", "pdo")
indexes <- c("oni", "ao", "nao", "soi", "mei", "npgo", "aao", "pdo", "dmi")

context("Testing download")

Expand Down

0 comments on commit 899fa0e

Please sign in to comment.