diff --git a/NAMESPACE b/NAMESPACE index 75f0312a3bbe88db64373121b81caa3b350305ac..012f76cfb679e9a7bf310fc913581d7b07bd4caa 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -36,6 +36,7 @@ export(CST_RainFARM) export(CST_RegimesAssign) export(CST_SaveExp) export(CST_SplitDim) +export(CST_Start) export(CST_Subset) export(CST_WeatherRegimes) export(Calibration) @@ -76,6 +77,7 @@ import(qmap) import(rainfarmr) import(s2dv) import(scales) +import(startR) import(stats) importFrom(ClimProjDiags,SelBox) importFrom(ClimProjDiags,Subset) diff --git a/R/CST_Analogs.R b/R/CST_Analogs.R index b6cbfa4e3fb5f4764985a6ae0ec13db84069274b..53706d176a89a70b8119fdce6d76786558f08763 100644 --- a/R/CST_Analogs.R +++ b/R/CST_Analogs.R @@ -237,11 +237,15 @@ CST_Analogs <- function(expL, obsL, expVar = NULL, obsVar = NULL, region = NULL, expL$coords[[lon_name]] <- SelBox(obsL$data, lon = as.vector(obsL$coords[[lon_name]]), lat = as.vector(obsL$coords[[lat_name]]), - region = region)$lon + region = region, + londim = lon_name, + latdim = lat_name)$lon expL$coords[[lat_name]] <- SelBox(obsL$data, lon = as.vector(obsL$coords[[lon_name]]), lat = as.vector(obsL$coords[[lat_name]]), - region = region)$lat + region = region, + londim = lon_name, + latdim = lat_name)$lat } } @@ -933,8 +937,10 @@ FindAnalog <- function(expL, obsL, time_obsL, expVar, obsVar, criteria, dim(Analogs_dates) <- dim(best) if (all(!is.null(region), !is.null(lonVar), !is.null(latVar))) { if (is.null(obsVar)) { - obsVar <- SelBox(obsL, lon = lonL, lat = latL, region = region)$data - expVar <- SelBox(expL, lon = lonL, lat = latL, region = region)$data + obsVar <- SelBox(obsL, lon = lonL, lat = latL, region = region, + londim = lon_name, latdim = lat_name)$data + expVar <- SelBox(expL, lon = lonL, lat = latL, region = region, + londim = lon_name, latdim = lat_name)$data Analogs_fields <- Subset(obsVar, along = which(names(dim(obsVar)) == 'time'), indices = best) @@ -943,7 +949,7 @@ FindAnalog <- function(expL, obsL, time_obsL, expVar, obsVar, criteria, } else { obslocal <- SelBox(obsVar, lon = lonVar, lat = latVar, - region = region)$data + region = region, londim = lon_name, latdim = lat_name)$data Analogs_fields <- Subset(obslocal, along = which(names(dim(obslocal)) == 'time'), indices = best) @@ -1102,8 +1108,10 @@ Select <- function(expL, obsL, expVar = NULL, obsVar = NULL, position = pos1)) } if (criteria == "Local_dist" | criteria == "Local_cor") { - obs <- SelBox(obsL, lon = lonL, lat = latL, region = region)$data - exp <- SelBox(expL, lon = lonL, lat = latL, region = region)$data + obs <- SelBox(obsL, lon = lonL, lat = latL, region = region, + londim = lon_name, latdim = lat_name)$data + exp <- SelBox(expL, lon = lonL, lat = latL, region = region, + londim = lon_name, latdim = lat_name)$data metric2 <- Apply(list(obs), target_dims = list(c(lat_name, lon_name)), fun = .select, exp, metric = "dist", lon_name = lon_name, lat_name = lat_name)$output1 @@ -1128,8 +1136,10 @@ Select <- function(expL, obsL, expVar = NULL, obsVar = NULL, } } if (criteria == "Local_cor") { - obs <- SelBox(obsVar, lon = lonVar, lat = latVar, region = region)$data - exp <- SelBox(expVar, lon = lonVar, lat = latVar, region = region)$data + obs <- SelBox(obsVar, lon = lonVar, lat = latVar, region = region, + londim = lon_name, latdim = lat_name)$data + exp <- SelBox(expVar, lon = lonVar, lat = latVar, region = region, + londim = lon_name, latdim = lat_name)$data metric3 <- Apply(list(obs), target_dims = list(c(lat_name, lon_name)), fun = .select, exp, metric = "cor", lon_name = lon_name, lat_name = lat_name)$output1 diff --git a/R/CST_Start.R b/R/CST_Start.R new file mode 100644 index 0000000000000000000000000000000000000000..722568ccbcdb75e2a027a63c3572e9ccf6b753fb --- /dev/null +++ b/R/CST_Start.R @@ -0,0 +1,47 @@ +#'CSTools data retrieval function using Start +#' +#'This function aggregates, subsets and retrieves sub-seasonal, seasonal, +#'decadal or climate projection data from NetCDF files in a local file system +#'and arranges it for easy application of the CSTools functions. It calls the +#'function \code{Start} from startR, which is an R package started at BSC with +#'the aim to develop a tool that allows the user to automatically process large +#'multidimensional distributed data sets. Then, the output is transformed into +#'`s2dv_cube` object. +#' +#'It receives any number of parameters (`...`) that are automatically forwarded +#'to the `startR::Start` function. See details in `?startR::Start`. +#' +#'@param ... Parameters that are automatically forwarded to the `startR::Start` +#' function. See details in `?startR::Start`. +#'@examples +#'\dontrun{ +#' sdates <- c('20101101', '20111101', '20121101') +#' latmin <- 44 +#' latmax <- 47 +#' lonmin <- 6 +#' lonmax <- 9 +#' data <- CST_Start(dat = path, +#' var = 'prlr', +#' ensemble = indices(1:6), +#' sdate = sdates, +#' time = 121:151, +#' latitude = values(list(latmin, latmax)), +#' longitude = values(list(lonmin, lonmax)), +#' synonims = list(longitude = c('lon', 'longitude'), +#' latitude = c('lat', 'latitude')), +#' return_vars = list(time = 'sdate', +#' longitude = NULL, latitude = NULL), +#' retrieve = FALSE) +#'} +#'\dontshow{ +#' exp <- CSTools::lonlat_temp_st$exp +#' obs <- CSTools::lonlat_temp_st$obs +#' data <- CSTools::lonlat_prec +#'} +#'@import startR +#'@export +CST_Start <- function(...) { + res <- Start(...) + res <- as.s2dv_cube(res) + return(res) +} \ No newline at end of file diff --git a/R/sample_data_st.R b/R/sample_data_st.R new file mode 100644 index 0000000000000000000000000000000000000000..e276a8c9fce9d12f246a7399b2c98b07131ce015 --- /dev/null +++ b/R/sample_data_st.R @@ -0,0 +1,129 @@ +#'Sample Of Experimental And Observational Climate Data In Function Of Longitudes And Latitudes with Start +#' +#'This sample data set contains gridded seasonal forecast and corresponding +#'observational data from the Copernicus Climate Change ECMWF-System 5 forecast +#'system, and from the Copernicus Climate Change ERA-5 reconstruction. +#'Specifically, for the 'tas' (2-meter temperature) variable, for the 15 first +#'forecast ensemble members, monthly averaged, for the 3 first forecast time +#'steps (lead months 1 to 4) of the November start dates of 2000 to 2005, for +#'the Mediterranean region (27N-48N, 12W-40E). The data was generated on (or +#'interpolated onto, for the reconstruction) a rectangular regular grid of size +#'360 by 181. +#' +#'The `CST_Start` call used to generate the data set in the infrastructure of +#'the Earth Sciences Department of the Barcelona Supercomputing Center is shown +#'next. Note that `CST_Start` internally calls `startR::Start` and then uses +#'`as.s2dv_cube` that converts the `startR_array` into `s2dv_cube`. +#'\preformatted{ +#' lonlat_temp_st <- NULL +#' repos_exp <- paste0('/esarchive/exp/ecmwf/system5c3s/monthly_mean/', +#' '$var$_f6h/$var$_$sdate$.nc') +#' sdates <- sapply(2000:2005, function(x) paste0(x, '1101')) +#' lonmax <- 40 +#' lonmin <- -12 +#' latmax <- 48 +#' latmin <- 27 +#' lonlat_temp_st$exp <- CST_Start(dat = repos_exp, +#' var = 'tas', +#' member = indices(1:15), +#' sdate = sdates, +#' ftime = indices(1:3), +#' lat = values(list(latmin, latmax)), +#' lat_reorder = Sort(decreasing = TRUE), +#' lon = values(list(lonmin, lonmax)), +#' lon_reorder = CircularSort(0, 360), +#' synonims = list(lon = c('lon', 'longitude'), +#' lat = c('lat', 'latitude'), +#' member = c('member', 'ensemble'), +#' ftime = c('ftime', 'time')), +#' return_vars = list(lat = NULL, +#' lon = NULL, ftime = 'sdate'), +#' retrieve = TRUE) +#' dates <- c(paste0(2000, c(11, 12)), paste0(2001, c('01', 11, 12)), +#' paste0(2002, c('01', 11, 12)), paste0(2003, c('01', 11, 12)), +#' paste0(2004, c('01', 11, 12)), paste0(2005, c('01', 11, 12)), 200601) +#' dates <- sapply(dates, function(x) {paste0(x, '01')}) +#' dates <- as.POSIXct(dates, format = '%Y%m%d', 'UTC') +#' dim(dates) <- c(ftime = 3, sdate = 6) +#' +#' path.obs <- '/esarchive/recon/ecmwf/era5/monthly_mean/$var$_f1h-r1440x721cds/$var$_$date$.nc' +#' lonlat_temp_st$obs <- CST_Start(dat = path.obs, +#' var = 'tas', +#' date = unique(format(dates, '%Y%m')), +#' ftime = values(dates), +#' ftime_across = 'date', +#' ftime_var = 'ftime', +#' merge_across_dims = TRUE, +#' split_multiselected_dims = TRUE, +#' lat = values(list(latmin, latmax)), +#' lat_reorder = Sort(decreasing = TRUE), +#' lon = values(list(lonmin, lonmax)), +#' lon_reorder = CircularSort(0, 360), +#' synonims = list(lon = c('lon', 'longitude'), +#' lat = c('lat', 'latitude'), +#' ftime = c('ftime', 'time')), +#' transform = CDORemapper, +#' transform_extra_cells = 2, +#' transform_params = list(grid = 'r360x181', +#' method = 'conservative'), +#' transform_vars = c('lat', 'lon'), +#' return_vars = list(lon = NULL, +#' lat = NULL, +#' ftime = 'date'), +#' retrieve = TRUE) +#'} +#' +#'@name lonlat_temp_st +#'@docType data +#'@author Nicolau Manubens \email{nicolau.manubens@bsc.es} +#'@keywords data +NULL + +#'Sample Of Experimental Precipitation Data In Function Of Longitudes And Latitudes with Start +#' +#'This sample data set contains a small cutout of gridded seasonal precipitation +#'forecast data from the Copernicus Climate Change ECMWF-System 5 forecast +#'system, to be used to demonstrate downscaling. Specifically, for the 'pr' +#'(precipitation) variable, for the first 6 forecast ensemble members, daily +#'values, for all 31 days in March following the forecast starting dates in +#'November of years 2010 to 2012, for a small 4x4 pixel cutout in a region in +#'the North-Western Italian Alps (44N-47N, 6E-9E). The data resolution is 1 +#'degree. +#' +#'The `CST_Start` call used to generate the data set in the infrastructure of +#'the Marconi machine at CINECA is shown next, working on files which were +#'extracted from forecast data available in the MEDSCOPE internal archive. +#' +#'\preformatted{ +#' path <- paste0('/esarchive/exp/ecmwf/system5c3s/daily_mean/', +#' '$var$_s0-24h/$var$_$sdate$.nc') +#' sdates = c('20101101', '20111101', '20121101') +#' latmin <- 44 +#' latmax <- 47 +#' lonmin <- 6 +#' lonmax <- 9 +#' +#' lonlat_prec_st <- CST_Start(dat = path, +#' var = 'prlr', +#' member = indices(1:6), +#' sdate = sdates, +#' ftime = 121:151, +#' lat = values(list(latmin, latmax)), +#' lat_reorder = Sort(decreasing = TRUE), +#' lon = values(list(lonmin, lonmax)), +#' lon_reorder = CircularSort(0, 360), +#' synonims = list(lon = c('lon', 'longitude'), +#' lat = c('lat', 'latitude'), +#' ftime = c('time', 'ftime'), +#' member = c('member', 'ensemble')), +#' return_vars = list(ftime = 'sdate', +#' lon = NULL, lat = NULL), +#' retrieve = TRUE) +#'} +#' +#'@name lonlat_prec_st +#'@docType data +#'@author Jost von Hardenberg \email{j.vonhardenberg@isac.cnr.it} +#'@author An-Chi Ho \email{an.ho@bsc.es} +#'@keywords data +NULL diff --git a/data/lonlat_prec_st.rda b/data/lonlat_prec_st.rda new file mode 100644 index 0000000000000000000000000000000000000000..405bd2297ddfac596eb65b96458b2f8f14df1975 Binary files /dev/null and b/data/lonlat_prec_st.rda differ diff --git a/data/lonlat_temp_st.rda b/data/lonlat_temp_st.rda new file mode 100644 index 0000000000000000000000000000000000000000..b9f8c2bf1116adfc67fc7bef518250b7d6575b82 Binary files /dev/null and b/data/lonlat_temp_st.rda differ diff --git a/man/CST_Start.Rd b/man/CST_Start.Rd new file mode 100644 index 0000000000000000000000000000000000000000..6069f0c8607414610c60adf491ee3c3202c6da3c --- /dev/null +++ b/man/CST_Start.Rd @@ -0,0 +1,51 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/CST_Start.R +\name{CST_Start} +\alias{CST_Start} +\title{CSTools data retrieval function using Start} +\usage{ +CST_Start(...) +} +\arguments{ +\item{...}{Parameters that are automatically forwarded to the `startR::Start` +function. See details in `?startR::Start`.} +} +\description{ +This function aggregates, subsets and retrieves sub-seasonal, seasonal, +decadal or climate projection data from NetCDF files in a local file system +and arranges it for easy application of the CSTools functions. It calls the +function \code{Start} from startR, which is an R package started at BSC with +the aim to develop a tool that allows the user to automatically process large +multidimensional distributed data sets. Then, the output is transformed into +`s2dv_cube` object. +} +\details{ +It receives any number of parameters (`...`) that are automatically forwarded +to the `startR::Start` function. See details in `?startR::Start`. +} +\examples{ +\dontrun{ + sdates <- c('20101101', '20111101', '20121101') + latmin <- 44 + latmax <- 47 + lonmin <- 6 + lonmax <- 9 + data <- CST_Start(dat = path, + var = 'prlr', + ensemble = indices(1:6), + sdate = sdates, + time = 121:151, + latitude = values(list(latmin, latmax)), + longitude = values(list(lonmin, lonmax)), + synonims = list(longitude = c('lon', 'longitude'), + latitude = c('lat', 'latitude')), + return_vars = list(time = 'sdate', + longitude = NULL, latitude = NULL), + retrieve = FALSE) +} +\dontshow{ +exp <- CSTools::lonlat_temp_st$exp +obs <- CSTools::lonlat_temp_st$obs +data <- CSTools::lonlat_prec +} +} diff --git a/man/lonlat_prec_st.Rd b/man/lonlat_prec_st.Rd new file mode 100644 index 0000000000000000000000000000000000000000..3f0de74af5d14b385170e6b6ce788236840d984e --- /dev/null +++ b/man/lonlat_prec_st.Rd @@ -0,0 +1,54 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/sample_data_st.R +\docType{data} +\name{lonlat_prec_st} +\alias{lonlat_prec_st} +\title{Sample Of Experimental Precipitation Data In Function Of Longitudes And Latitudes with Start} +\description{ +This sample data set contains a small cutout of gridded seasonal precipitation +forecast data from the Copernicus Climate Change ECMWF-System 5 forecast +system, to be used to demonstrate downscaling. Specifically, for the 'pr' +(precipitation) variable, for the first 6 forecast ensemble members, daily +values, for all 31 days in March following the forecast starting dates in +November of years 2010 to 2012, for a small 4x4 pixel cutout in a region in +the North-Western Italian Alps (44N-47N, 6E-9E). The data resolution is 1 +degree. +} +\details{ +The `CST_Start` call used to generate the data set in the infrastructure of +the Marconi machine at CINECA is shown next, working on files which were +extracted from forecast data available in the MEDSCOPE internal archive. + +\preformatted{ + path <- paste0('/esarchive/exp/ecmwf/system5c3s/daily_mean/', + '$var$_s0-24h/$var$_$sdate$.nc') + sdates = c('20101101', '20111101', '20121101') + latmin <- 44 + latmax <- 47 + lonmin <- 6 + lonmax <- 9 + + lonlat_prec_st <- CST_Start(dat = path, + var = 'prlr', + member = indices(1:6), + sdate = sdates, + ftime = 121:151, + lat = values(list(latmin, latmax)), + lat_reorder = Sort(decreasing = TRUE), + lon = values(list(lonmin, lonmax)), + lon_reorder = CircularSort(0, 360), + synonims = list(lon = c('lon', 'longitude'), + lat = c('lat', 'latitude'), + ftime = c('time', 'ftime'), + member = c('member', 'ensemble')), + return_vars = list(ftime = 'sdate', + lon = NULL, lat = NULL), + retrieve = TRUE) +} +} +\author{ +Jost von Hardenberg \email{j.vonhardenberg@isac.cnr.it} + +An-Chi Ho \email{an.ho@bsc.es} +} +\keyword{data} diff --git a/man/lonlat_temp_st.Rd b/man/lonlat_temp_st.Rd new file mode 100644 index 0000000000000000000000000000000000000000..3ac3e048eda8e1c251eaea29c7d28a4989ff316e --- /dev/null +++ b/man/lonlat_temp_st.Rd @@ -0,0 +1,85 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/sample_data_st.R +\docType{data} +\name{lonlat_temp_st} +\alias{lonlat_temp_st} +\title{Sample Of Experimental And Observational Climate Data In Function Of Longitudes And Latitudes with Start} +\description{ +This sample data set contains gridded seasonal forecast and corresponding +observational data from the Copernicus Climate Change ECMWF-System 5 forecast +system, and from the Copernicus Climate Change ERA-5 reconstruction. +Specifically, for the 'tas' (2-meter temperature) variable, for the 15 first +forecast ensemble members, monthly averaged, for the 3 first forecast time +steps (lead months 1 to 4) of the November start dates of 2000 to 2005, for +the Mediterranean region (27N-48N, 12W-40E). The data was generated on (or +interpolated onto, for the reconstruction) a rectangular regular grid of size +360 by 181. +} +\details{ +The `CST_Start` call used to generate the data set in the infrastructure of +the Earth Sciences Department of the Barcelona Supercomputing Center is shown +next. Note that `CST_Start` internally calls `startR::Start` and then uses +`as.s2dv_cube` that converts the `startR_array` into `s2dv_cube`. +\preformatted{ + lonlat_temp_st <- NULL + repos_exp <- paste0('/esarchive/exp/ecmwf/system5c3s/monthly_mean/', + '$var$_f6h/$var$_$sdate$.nc') + sdates <- sapply(2000:2005, function(x) paste0(x, '1101')) + lonmax <- 40 + lonmin <- -12 + latmax <- 48 + latmin <- 27 + lonlat_temp_st$exp <- CST_Start(dat = repos_exp, + var = 'tas', + member = indices(1:15), + sdate = sdates, + ftime = indices(1:3), + lat = values(list(latmin, latmax)), + lat_reorder = Sort(decreasing = TRUE), + lon = values(list(lonmin, lonmax)), + lon_reorder = CircularSort(0, 360), + synonims = list(lon = c('lon', 'longitude'), + lat = c('lat', 'latitude'), + member = c('member', 'ensemble'), + ftime = c('ftime', 'time')), + return_vars = list(lat = NULL, + lon = NULL, ftime = 'sdate'), + retrieve = TRUE) + dates <- c(paste0(2000, c(11, 12)), paste0(2001, c('01', 11, 12)), + paste0(2002, c('01', 11, 12)), paste0(2003, c('01', 11, 12)), + paste0(2004, c('01', 11, 12)), paste0(2005, c('01', 11, 12)), 200601) + dates <- sapply(dates, function(x) {paste0(x, '01')}) + dates <- as.POSIXct(dates, format = '%Y%m%d', 'UTC') + dim(dates) <- c(ftime = 3, sdate = 6) + + path.obs <- '/esarchive/recon/ecmwf/era5/monthly_mean/$var$_f1h-r1440x721cds/$var$_$date$.nc' + lonlat_temp_st$obs <- CST_Start(dat = path.obs, + var = 'tas', + date = unique(format(dates, '%Y%m')), + ftime = values(dates), + ftime_across = 'date', + ftime_var = 'ftime', + merge_across_dims = TRUE, + split_multiselected_dims = TRUE, + lat = values(list(latmin, latmax)), + lat_reorder = Sort(decreasing = TRUE), + lon = values(list(lonmin, lonmax)), + lon_reorder = CircularSort(0, 360), + synonims = list(lon = c('lon', 'longitude'), + lat = c('lat', 'latitude'), + ftime = c('ftime', 'time')), + transform = CDORemapper, + transform_extra_cells = 2, + transform_params = list(grid = 'r360x181', + method = 'conservative'), + transform_vars = c('lat', 'lon'), + return_vars = list(lon = NULL, + lat = NULL, + ftime = 'date'), + retrieve = TRUE) +} +} +\author{ +Nicolau Manubens \email{nicolau.manubens@bsc.es} +} +\keyword{data}