Commit a2ff73d5 authored by Carlos Delgado Torres's avatar Carlos Delgado Torres
Browse files

updated the rest of the indices functions (removed member_dim, added na.rm,...

updated the rest of the indices functions (removed member_dim, added na.rm, now they use multiApply, updated documentation)
parent 8b21a132
Pipeline #5156 passed with stage
in 3 minutes and 13 seconds
......@@ -4,22 +4,20 @@
#'weighted-averaged surface air temperature anomalies over land and sea surface
#'temperature anomalies over the ocean.
#'
#'@param data_tas A numerical array indicating the surface air temperature data
#' to be used for the index computation with the dimensions: 1) latitude,
#' longitude, start date, forecast month, and member (in case of decadal
#' predictions), 2) latitude, longitude, year, month and member (in case of
#' historical simulations), or 3) latitude, longitude, year and month (in case
#' of observations or reanalyses). This data has to be provided, at least,
#' over the whole region needed to compute the index. The dimensions must be
#' identical to those of data_tos.
#'@param data_tos A numerical array indicating the sea surface temperature data
#' to be used for the index computation with the dimensions: 1) latitude,
#' longitude, start date, forecast month, and member (in case of decadal
#' predictions), 2) latitude, longitude, year, month and member (in case of
#' historical simulations), or 3) latitude, longitude, year and month (in case
#' of observations or reanalyses). This data has to be provided, at least,
#' over the whole region needed to compute the index. The dimensions must be
#' identical to those of data_tas.
#'@param data_tas A numerical array with the surface air temperature data
#' to be used for the index computation with, at least, the
#' dimensions: 1) latitude, longitude, start date and forecast month
#' (in case of decadal predictions), 2) latitude, longitude, year and month
#' (in case of historical simulations or observations). This data has to be
#' provided, at least, over the whole region needed to compute the index.
#' The dimensions must be identical to thos of data_tos.
#' #'@param data_tos A numerical array with the sea surface temperature data
#' to be used for the index computation with, at least, the
#' dimensions: 1) latitude, longitude, start date and forecast month
#' (in case of decadal predictions), 2) latitude, longitude, year and month
#' (in case of historical simulations or observations). This data has to be
#' provided, at least, over the whole region needed to compute the index.
#' The dimensions must be identical to thos of data_tas.
#'@param data_lats A numeric vector indicating the latitudes of the data.
#'@param data_lons A numeric vector indicating the longitudes of the data.
#'@param mask_sea_land An array with dimensions [lat_dim = data_lats, lon_dim =
......@@ -55,23 +53,22 @@
#' anomalies, set it to FALSE. The default value is NULL.\cr
#' In case of parameter 'type' is 'dcpp', 'indices_for_clim' must be relative
#' to the first forecast year, and the climatology is automatically computed
#' over the actual common period for the different forecast years.
#' over the common calendar period for the different forecast years.
#'@param year_dim A character string indicating the name of the year dimension
#' The default value is 'year'. Only used if parameter 'type' is 'hist' or
#' 'obs'.
#'@param month_dim A character string indicating the name of the month
#' dimension. The default value is 'month'. Only used if parameter 'type' is
#' 'hist' or 'obs'.
#'@param member_dim A character string indicating the name of the member
#' dimension. The default value is 'member'. Only used if parameter 'type' is
#' 'dcpp' or 'hist'.
#'@param na.rm A logical value indicanting whether to remove NA values. The default
#' value is TRUE.
#'@param ncores An integer indicating the number of cores to use for parallel
#' computation. The default value is NULL.
#'
#'@return A numerical array of the GMST anomalies with the dimensions of:
#' 1) sdate, forecast year, and member (in case of decadal predictions);
#' 2) year and member (in case of historical simulations); or
#' 3) year (in case of observations or reanalyses).
#'@return A numerical array with the GMST anomalies with the same dimensions as data_tas except
#' the lat_dim, lon_dim and fmonth_dim (month_dim) in case of decadal predictions
#' (historical simulations or observations). In case of decadal predictions, a new dimension
#' 'fyear' is added.
#'
#'@examples
#' ## Observations or reanalyses
......@@ -113,7 +110,7 @@
GMST <- function(data_tas, data_tos, data_lats, data_lons, mask_sea_land, sea_value,
type, mask = NULL, lat_dim = 'lat', lon_dim = 'lon', monini = 11,
fmonth_dim = 'fmonth', sdate_dim = 'sdate', indices_for_clim = NULL,
year_dim = 'year', month_dim = 'month', member_dim = 'member', ncores = NULL) {
year_dim = 'year', month_dim = 'month', na.rm = TRUE, ncores = NULL) {
## Input Checks
# data_tas and data_tos
......@@ -236,14 +233,9 @@ GMST <- function(data_tas, data_tos, data_lats, data_lons, mask_sea_land, sea_va
stop("Parameter 'month_dim' is not found in 'data_tas' or 'data_tos' dimension.")
}
}
# member_dim
if (type == 'hist' | type == 'dcpp') {
if (!(is.character(member_dim) & length(member_dim) == 1)) {
stop("Parameter 'member_dim' must be a character string.")
}
if (!member_dim %in% names(dim(data_tas)) | !member_dim %in% names(dim(data_tos))) {
stop("Parameter 'member_dim' is not found in 'data_tas' or 'data_tos' dimension.")
}
# na.rm
if (!na.rm %in% c(TRUE,FALSE)) {
stop("Parameter 'na.rm' must be TRUE or FALSE")
}
# ncores
if (!is.null(ncores)) {
......@@ -253,7 +245,6 @@ GMST <- function(data_tas, data_tos, data_lats, data_lons, mask_sea_land, sea_va
}
}
## combination of tas and tos (data)
mask_tas_tos <- function(data_tas, data_tos, mask_sea_land, sea_value) {
data <- data_tas
......@@ -284,9 +275,17 @@ GMST <- function(data_tas, data_tos, data_lats, data_lons, mask_sea_land, sea_va
londim = which(names(dim(data)) == lon_dim),
latdim = which(names(dim(data)) == lat_dim))
INDEX <- .Indices(data = data, type = type, monini = monini,
indices_for_clim = indices_for_clim, fmonth_dim = fmonth_dim,
sdate_dim = sdate_dim, year_dim = year_dim,
month_dim = month_dim, member_dim = member_dim)
if (type == 'dcpp'){
target_dims <- c(sdate_dim, fmonth_dim)
} else if (type %in% c('hist','obs')){
target_dims <- c(year_dim, month_dim)
}
source('/esarchive/scratch/cdelgado/gitlab/s2dv/R/Utils.R')
INDEX <- multiApply::Apply(data = data, target_dims = target_dims, fun = .Indices,
type = type, monini = monini, indices_for_clim = indices_for_clim,
fmonth_dim = fmonth_dim, sdate_dim = sdate_dim,
year_dim = year_dim, month_dim = month_dim,
na.rm = na.rm, ncores = ncores)$output1
return(INDEX)
}
......@@ -3,11 +3,10 @@
#'The Global Surface Air Temperature (GSAT) anomalies are computed as the
#'weighted-averaged surface air temperature anomalies over the global region.
#'
#'@param data A numerical array to be used for the index computation with the
#' dimensions: 1) latitude, longitude, start date, forecast month, and member
#' (in case of decadal predictions), 2) latitude, longitude, year, month and
#' member (in case of historical simulations), or 3) latitude, longitude, year
#' and month (in case of observations or reanalyses). This data has to be
#'@param data A numerical array to be used for the index computation with, at least, the
#' dimensions: 1) latitude, longitude, start date and forecast month
#' (in case of decadal predictions), 2) latitude, longitude, year and month
#' (in case of historical simulations or observations). This data has to be
#' provided, at least, over the whole region needed to compute the index.
#'@param data_lats A numeric vector indicating the latitudes of the data.
#'@param data_lons A numeric vector indicating the longitudes of the data.
......@@ -40,23 +39,22 @@
#' anomalies, set it to FALSE. The default value is NULL.\cr
#' In case of parameter 'type' is 'dcpp', 'indices_for_clim' must be relative
#' to the first forecast year, and the climatology is automatically computed
#' over the actual common period for the different forecast years.
#' over the common calendar period for the different forecast years.
#'@param year_dim A character string indicating the name of the year dimension
#' The default value is 'year'. Only used if parameter 'type' is 'hist' or
#' 'obs'.
#'@param month_dim A character string indicating the name of the month
#' dimension. The default value is 'month'. Only used if parameter 'type' is
#' 'hist' or 'obs'.
#'@param member_dim A character string indicating the name of the member
#' dimension. The default value is 'member'. Only used if parameter 'type' is
#' 'dcpp' or 'hist'.
#'@param na.rm A logical value indicanting whether to remove NA values. The default
#' value is TRUE.
#'@param ncores An integer indicating the number of cores to use for parallel
#' computation. The default value is NULL.
#'
#'@return A numerical array of the GSAT anomalies with the dimensions of:
#' 1) sdate, forecast year, and member (in case of decadal predictions);
#' 2) year and member (in case of historical simulations); or
#' 3) year (in case of observations or reanalyses).
#'@return A numerical array with the GSAT anomalies with the same dimensions as data except
#' the lat_dim, lon_dim and fmonth_dim (month_dim) in case of decadal predictions
#' (historical simulations or observations). In case of decadal predictions, a new dimension
#' 'fyear' is added.
#'
#'@examples
#' ## Observations or reanalyses
......@@ -83,7 +81,7 @@
GSAT <- function(data, data_lats, data_lons, type, lat_dim = 'lat', lon_dim = 'lon',
mask = NULL, monini = 11, fmonth_dim = 'fmonth', sdate_dim = 'sdate',
indices_for_clim = NULL, year_dim = 'year', month_dim = 'month',
member_dim = 'member', ncores = NULL) {
na.rm = TRUE, ncores = NULL) {
## Input Checks
# data
......@@ -204,14 +202,9 @@ GSAT <- function(data, data_lats, data_lons, type, lat_dim = 'lat', lon_dim = 'l
stop("Parameter 'month_dim' is not found in 'data' dimension.")
}
}
# member_dim
if (type == 'hist' | type == 'dcpp') {
if (!(is.character(member_dim) & length(member_dim) == 1)) {
stop("Parameter 'member_dim' must be a character string.")
}
if (!member_dim %in% names(dim(data))) {
stop("Parameter 'member_dim' is not found in 'data' dimension.")
}
# na.rm
if (!na.rm %in% c(TRUE,FALSE)) {
stop("Parameter 'na.rm' must be TRUE or FALSE")
}
data <- ClimProjDiags::WeightedMean(data = data, lon = data_lons, lat = data_lats,
......@@ -219,9 +212,17 @@ GSAT <- function(data, data_lats, data_lons, type, lat_dim = 'lat', lon_dim = 'l
londim = which(names(dim(data)) == lon_dim),
latdim = which(names(dim(data)) == lat_dim))
INDEX <- .Indices(data = data, type = type, monini = monini,
indices_for_clim = indices_for_clim, fmonth_dim = fmonth_dim,
sdate_dim = sdate_dim, year_dim = year_dim,
month_dim = month_dim, member_dim = member_dim)
if (type == 'dcpp'){
target_dims <- c(sdate_dim, fmonth_dim)
} else if (type %in% c('hist','obs')){
target_dims <- c(year_dim, month_dim)
}
source('/esarchive/scratch/cdelgado/gitlab/s2dv/R/Utils.R')
INDEX <- multiApply::Apply(data = data, target_dims = target_dims, fun = .Indices,
type = type, monini = monini, indices_for_clim = indices_for_clim,
fmonth_dim = fmonth_dim, sdate_dim = sdate_dim,
year_dim = year_dim, month_dim = month_dim,
na.rm = na.rm, ncores = ncores)$output1
return(INDEX)
}
......@@ -6,11 +6,10 @@
#'anomalies over 20ºS-48ºS, 165ºE-190ºE (NW pole) and the weighted-averaged SST
#' anomalies over 44ºS-65ºS, 220ºE-260ºE (SE pole) (Saurral et al., 2020).
#'
#'@param data A numerical array to be used for the index computation with the
#' dimensions: 1) latitude, longitude, start date, forecast month, and member
#' (in case of decadal predictions), 2) latitude, longitude, year, month and
#' member (in case of historical simulations), or 3) latitude, longitude, year
#' and month (in case of observations or reanalyses). This data has to be
#'@param data A numerical array to be used for the index computation with, at least, the
#' dimensions: 1) latitude, longitude, start date and forecast month
#' (in case of decadal predictions), 2) latitude, longitude, year and month
#' (in case of historical simulations or observations). This data has to be
#' provided, at least, over the whole region needed to compute the index.
#'@param data_lats A numeric vector indicating the latitudes of the data.
#'@param data_lons A numeric vector indicating the longitudes of the data.
......@@ -43,23 +42,22 @@
#' anomalies, set it to FALSE. The default value is NULL.\cr
#' In case of parameter 'type' is 'dcpp', 'indices_for_clim' must be relative
#' to the first forecast year, and the climatology is automatically computed
#' over the actual common period for the different forecast years.
#' over the common calendar period for the different forecast years.
#'@param year_dim A character string indicating the name of the year dimension
#' The default value is 'year'. Only used if parameter 'type' is 'hist' or
#' 'obs'.
#'@param month_dim A character string indicating the name of the month
#' dimension. The default value is 'month'. Only used if parameter 'type' is
#' 'hist' or 'obs'.
#'@param member_dim A character string indicating the name of the member
#' dimension. The default value is 'member'. Only used if parameter 'type' is
#' 'dcpp' or 'hist'.
#'@param na.rm A logical value indicanting whether to remove NA values. The default
#' value is TRUE.
#'@param ncores An integer indicating the number of cores to use for parallel
#' computation. The default value is NULL.
#'
#'@return A numerical array of the SPOD index with the dimensions of:
#' 1) sdate, forecast year, and member (in case of decadal predictions);
#' 2) year and member (in case of historical simulations); or
#' 3) year (in case of observations or reanalyses).
#'@return A numerical array with the SPOD index with the same dimensions as data except
#' the lat_dim, lon_dim and fmonth_dim (month_dim) in case of decadal predictions
#' (historical simulations or observations). In case of decadal predictions, a new dimension
#' 'fyear' is added.
#'
#'@examples
#' ## Observations or reanalyses
......@@ -86,7 +84,7 @@
SPOD <- function(data, data_lats, data_lons, type, lat_dim = 'lat', lon_dim = 'lon',
mask = NULL, monini = 11, fmonth_dim = 'fmonth', sdate_dim = 'sdate',
indices_for_clim = NULL, year_dim = 'year', month_dim = 'month',
member_dim = 'member', ncores = NULL) {
na.rm = TRUE, ncores = NULL) {
## Input Checks
# data
......@@ -207,14 +205,9 @@ SPOD <- function(data, data_lats, data_lons, type, lat_dim = 'lat', lon_dim = 'l
stop("Parameter 'month_dim' is not found in 'data' dimension.")
}
}
# member_dim
if (type == 'hist' | type == 'dcpp') {
if (!(is.character(member_dim) & length(member_dim) == 1)) {
stop("Parameter 'member_dim' must be a character string.")
}
if (!member_dim %in% names(dim(data))) {
stop("Parameter 'member_dim' is not found in 'data' dimension.")
}
# na.rm
if (!na.rm %in% c(TRUE,FALSE)) {
stop("Parameter 'na.rm' must be TRUE or FALSE")
}
## Regions for IPO_SPOD (Saurral et al., 2020)
......@@ -238,9 +231,17 @@ SPOD <- function(data, data_lats, data_lons, type, lat_dim = 'lat', lon_dim = 'l
data <- ClimProjDiags::CombineIndices(indices = list(mean_1,mean_2),
weights = NULL, operation = 'subtract') # (mean_1 - mean_2)
INDEX <- .Indices(data = data, type = type, monini = monini,
indices_for_clim = indices_for_clim, fmonth_dim = fmonth_dim,
sdate_dim = sdate_dim, year_dim = year_dim,
month_dim = month_dim, member_dim = member_dim)
if (type == 'dcpp'){
target_dims <- c(sdate_dim, fmonth_dim)
} else if (type %in% c('hist','obs')){
target_dims <- c(year_dim, month_dim)
}
source('/esarchive/scratch/cdelgado/gitlab/s2dv/R/Utils.R')
INDEX <- multiApply::Apply(data = data, target_dims = target_dims, fun = .Indices,
type = type, monini = monini, indices_for_clim = indices_for_clim,
fmonth_dim = fmonth_dim, sdate_dim = sdate_dim,
year_dim = year_dim, month_dim = month_dim,
na.rm = na.rm, ncores = ncores)$output1
return(INDEX)
}
......@@ -5,11 +5,10 @@
#'170ºE-270ºE minus the mean of the weighted-averaged SST anomalies over
#'25ºN-45ºN, 140ºE-215ºE and 50ºS-15ºS, 150ºE-200ºE (Henley et al., 2015).
#'
#'@param data A numerical array to be used for the index computation with the
#' dimensions: 1) latitude, longitude, start date, forecast month, and member
#' (in case of decadal predictions), 2) latitude, longitude, year, month and
#' member (in case of historical simulations), or 3) latitude, longitude, year
#' and month (in case of observations or reanalyses). This data has to be
#'@param data A numerical array to be used for the index computation with, at least, the
#' dimensions: 1) latitude, longitude, start date and forecast month
#' (in case of decadal predictions), 2) latitude, longitude, year and month
#' (in case of historical simulations or observations). This data has to be
#' provided, at least, over the whole region needed to compute the index.
#'@param data_lats A numeric vector indicating the latitudes of the data.
#'@param data_lons A numeric vector indicating the longitudes of the data.
......@@ -42,23 +41,22 @@
#' anomalies, set it to FALSE. The default value is NULL.\cr
#' In case of parameter 'type' is 'dcpp', 'indices_for_clim' must be relative
#' to the first forecast year, and the climatology is automatically computed
#' over the actual common period for the different forecast years.
#' over the common calendar period for the different forecast years.
#'@param year_dim A character string indicating the name of the year dimension
#' The default value is 'year'. Only used if parameter 'type' is 'hist' or
#' 'obs'.
#'@param month_dim A character string indicating the name of the month
#' dimension. The default value is 'month'. Only used if parameter 'type' is
#' 'hist' or 'obs'.
#'@param member_dim A character string indicating the name of the member
#' dimension. The default value is 'member'. Only used if parameter 'type' is
#' 'dcpp' or 'hist'.
#'@param na.rm A logical value indicanting whether to remove NA values. The default
#' value is TRUE.
#'@param ncores An integer indicating the number of cores to use for parallel
#' computation. The default value is NULL.
#'
#'@return A numerical array of the TPI index with the dimensions of:
#' 1) sdate, forecast year, and member (in case of decadal predictions);
#' 2) year and member (in case of historical simulations); or
#' 3) year (in case of observations or reanalyses).
#'@return A numerical array with the TPI index with the same dimensions as data except
#' the lat_dim, lon_dim and fmonth_dim (month_dim) in case of decadal predictions
#' (historical simulations or observations). In case of decadal predictions, a new dimension
#' 'fyear' is added.
#'
#'@examples
#' ## Observations or reanalyses
......@@ -85,7 +83,7 @@
TPI <- function(data, data_lats, data_lons, type, lat_dim = 'lat', lon_dim = 'lon',
mask = NULL, monini = 11, fmonth_dim = 'fmonth', sdate_dim = 'sdate',
indices_for_clim = NULL, year_dim = 'year', month_dim = 'month',
member_dim = 'member', ncores = NULL) {
na.rm = TRUE, ncores = NULL) {
## Input Checks
# data
......@@ -206,14 +204,9 @@ TPI <- function(data, data_lats, data_lons, type, lat_dim = 'lat', lon_dim = 'lo
stop("Parameter 'month_dim' is not found in 'data' dimension.")
}
}
# member_dim
if (type == 'hist' | type == 'dcpp') {
if (!(is.character(member_dim) & length(member_dim) == 1)) {
stop("Parameter 'member_dim' must be a character string.")
}
if (!member_dim %in% names(dim(data))) {
stop("Parameter 'member_dim' is not found in 'data' dimension.")
}
# na.rm
if (!na.rm %in% c(TRUE,FALSE)) {
stop("Parameter 'na.rm' must be TRUE or FALSE")
}
# Regions for IPO_TPI (psl.noaa.gov/data/timeseries/IPOTPI)
......@@ -247,9 +240,17 @@ TPI <- function(data, data_lats, data_lons, type, lat_dim = 'lat', lon_dim = 'lo
data <- ClimProjDiags::CombineIndices(indices = list(mean_2, mean_1_3),
weights = NULL, operation = 'subtract') # mean_2 - ((mean_1 + mean_3)/2)
INDEX <- .Indices(data = data, type = type, monini = monini,
indices_for_clim = indices_for_clim, fmonth_dim = fmonth_dim,
sdate_dim = sdate_dim, year_dim = year_dim,
month_dim = month_dim, member_dim = member_dim)
if (type == 'dcpp'){
target_dims <- c(sdate_dim, fmonth_dim)
} else if (type %in% c('hist','obs')){
target_dims <- c(year_dim, month_dim)
}
source('/esarchive/scratch/cdelgado/gitlab/s2dv/R/Utils.R')
INDEX <- multiApply::Apply(data = data, target_dims = target_dims, fun = .Indices,
type = type, monini = monini, indices_for_clim = indices_for_clim,
fmonth_dim = fmonth_dim, sdate_dim = sdate_dim,
year_dim = year_dim, month_dim = month_dim,
na.rm = na.rm, ncores = ncores)$output1
return(INDEX)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment