From e65feff06e51f7e32e396b3058366daaedf02aa9 Mon Sep 17 00:00:00 2001 From: nperez Date: Tue, 4 May 2021 10:28:11 +0200 Subject: [PATCH] Temporal substtingfeatue added --- R/WindCapacityFactor.R | 51 ++++++++++++++++++++++++++++++++--- R/WindPowerDensity.R | 50 +++++++++++++++++++++++++++++++--- man/CST_WindCapacityFactor.Rd | 17 +++++++++++- man/CST_WindPowerDensity.Rd | 17 +++++++++++- man/WindCapacityFactor.Rd | 20 +++++++++++++- man/WindPowerDensity.Rd | 20 +++++++++++++- 6 files changed, 165 insertions(+), 10 deletions(-) diff --git a/R/WindCapacityFactor.R b/R/WindCapacityFactor.R index 2b60ae4..4b9bffb 100644 --- a/R/WindCapacityFactor.R +++ b/R/WindCapacityFactor.R @@ -8,6 +8,10 @@ #' #'@param wind a s2dv_cube object with instantaneous wind speeds expressed in m/s. #'@param IEC_class a string indicating the IEC wind class (see IEC 61400-1) of the turbine to be selected. Classes \code{'I'}, \code{'II'} and \code{'III'} are suitable for sites with an annual mean wind speed of 10, 8.5 and 7.5 m/s respectively. Classes \code{'I/II'} and \code{'II/III'} indicate intermediate turbines that fit both classes. More details of the five turbines and a plot of its power curves can be found in Lledó et al. (2019). +#'@param start an optional parameter to defined the initial date of the period to select from the data by providing a list of two elements: the initial date of the period and the initial month of the period. By default it is set to NULL and the indicator is computed using all the data provided in \code{data}. +#'@param end an optional parameter to defined the final date of the period to select from the data by providing a list of two elements: the final day of the period and the final month of the period. By default it is set to NULL and the indicator is computed using all the data provided in \code{data}. +#'@param time_dim a character string indicating the name of the function to compute the indicator. By default, it is set to 'ftime'. More than one dimension name matching the dimensions provided in the object \code{data$data} can be specified. +#'@param ncores an integer indicating the number of cores to use in parallel computation for temporal subsetting. #'@return A s2dv_cube object containing the Wind Capacity Factor (unitless). #' #'@examples @@ -20,17 +24,39 @@ #'WCF <- CST_WindCapacityFactor(wind, IEC_class = "III") #' #'@export -CST_WindCapacityFactor <- function(wind, IEC_class = c("I", "I/II", "II", "II/III", "III")) { +CST_WindCapacityFactor <- function(wind, IEC_class = c("I", "I/II", "II", "II/III", "III"), + start = NULL, end = NULL, time_dim = 'ftime', + ncores = NULL) { if (!inherits(wind, 's2dv_cube')) { stop("Parameter 'wind' must be of the class 's2dv_cube', ", "as output by CSTools::CST_Load.") } - wind$data <- WindCapacityFactor(wind$data, IEC_class = IEC_class) + # when subsetting is needed, dimensions are also needed: + if (!is.null(start) && !is.null(end)) { + if (is.null(dim(wind$Dates$start))) { + if (length(wind$Dates$start) != dim(wind$data)[time_dim]) { + if (length(wind$Dates$start) == + prod(dim(wind$data)[time_dim] * dim(wind$data)['sdate'])) { + dim(wind$Dates$start) <- c(dim(wind$data)[time_dim], + dim(wind$data)['sdate']) + } + } else { + warning("Dimensions in 'data' element 'Dates$start' are missed/unmatched. All data would be used.") + } + } + } + wind$data <- WindCapacityFactor(wind$data, IEC_class = IEC_class, dates = wind$Dates[[1]], + start = start, end = end, ncores = ncores) if ('Variable' %in% names(wind)) { if ('varName' %in% names(wind$Variable)) { wind$Variable$varName <- 'WindCapacityFactor' } } + if (!is.null(start) && !is.null(end)) { + wind$Dates <- SelectPeriodOnDates(dates = wind$Dates[[1]], + start = start, end = end, + time_dim = time_dim, ncores = ncores) + } return(wind) } #'Wind capacity factor @@ -43,6 +69,12 @@ CST_WindCapacityFactor <- function(wind, IEC_class = c("I", "I/II", "II", "II/II #' #'@param wind a multidimensional array, vector or scalar with instantaneous wind speeds expressed in m/s. #'@param IEC_class a string indicating the IEC wind class (see IEC 61400-1) of the turbine to be selected. Classes \code{'I'}, \code{'II'} and \code{'III'} are suitable for sites with an annual mean wind speed of 10, 8.5 and 7.5 m/s respectively. Classes \code{'I/II'} and \code{'II/III'} indicate intermediate turbines that fit both classes. More details of the five turbines and a plot of its power curves can be found in Lledó et al. (2019). +#'@param dates a vector of dates or a multidimensional array of dates with named dimensions matching the dimensions on parameter 'data'. By default it is NULL, to select a period this parameter must be provided. +#'@param start an optional parameter to defined the initial date of the period to select from the data by providing a list of two elements: the initial date of the period and the initial month of the period. By default it is set to NULL and the indicator is computed using all the data provided in \code{data}. +#'@param end an optional parameter to defined the final date of the period to select from the data by providing a list of two elements: the final day of the period and the final month of the period. By default it is set to NULL and the indicator is computed using all the data provided in \code{data}. +#'@param time_dim a character string indicating the name of the function to compute the indicator. By default, it is set to 'ftime'. More than one dimension name matching the dimensions provided in the object \code{data$data} can be specified. +#'@param ncores an integer indicating the number of cores to use in parallel computation for temporal subsetting. +#' #'@return An array with the same dimensions as wind, containing the Wind Capacity Factor (unitless). #' #'@importFrom stats approxfun @@ -52,7 +84,9 @@ CST_WindCapacityFactor <- function(wind, IEC_class = c("I", "I/II", "II", "II/II #'WCF <- WindCapacityFactor(wind, IEC_class = "III") #' #'@export -WindCapacityFactor <- function(wind, IEC_class = c("I", "I/II", "II", "II/III", "III")) { +WindCapacityFactor <- function(wind, IEC_class = c("I", "I/II", "II", "II/III", "III"), + dates = NULL, start = NULL, end = NULL, + time_dim = 'time', ncores = NULL) { IEC_class <- match.arg(IEC_class) pc_files <- c( "I" = "Enercon_E70_2.3MW.txt", @@ -63,6 +97,17 @@ WindCapacityFactor <- function(wind, IEC_class = c("I", "I/II", "II", "II/III", ) pc_file <- system.file("power_curves", pc_files[IEC_class], package = "CSIndicators", mustWork = T) pc <- read_pc(pc_file) + if (!is.null(dates)) { + if (!is.null(start) && !is.null(end)) { + if (!any(c(is.list(start), is.list(end)))) { + stop("Parameter 'start' and 'end' must be lists indicating the ", + "day and the month of the period start and end.") + } + wind <- SelectPeriodOnData(wind, dates, start, end, + time_dim = time_dim, ncores = ncores) + } + } + cf <- wind2CF(wind, pc) return(cf) } diff --git a/R/WindPowerDensity.R b/R/WindPowerDensity.R index a231e45..1ecdc30 100644 --- a/R/WindPowerDensity.R +++ b/R/WindPowerDensity.R @@ -6,6 +6,11 @@ #' #'@param wind a s2dv_cube object with instantaneous wind speeds expressed in m/s obtained from CST_Load or s2dv_cube functions from CSTools pacakge #'@param ro a scalar, or alternatively a multidimensional array with the same dimensions as wind, with the air density expressed in kg/m^3. By default it takes the value 1.225, the standard density of air at 15ºC and 1013.25 hPa. +#'@param start an optional parameter to defined the initial date of the period to select from the data by providing a list of two elements: the initial date of the period and the initial month of the period. By default it is set to NULL and the indicator is computed using all the data provided in \code{data}. +#'@param end an optional parameter to defined the final date of the period to select from the data by providing a list of two elements: the final day of the period and the final month of the period. By default it is set to NULL and the indicator is computed using all the data provided in \code{data}. +#'@param time_dim a character string indicating the name of the function to compute the indicator. By default, it is set to 'ftime'. More than one dimension name matching the dimensions provided in the object \code{data$data} can be specified. +#'@param ncores an integer indicating the number of cores to use in parallel computation for temporal subsetting. +#' #'@return A s2dv_cube object containing Wind Power Density expressed in W/m^2. #' #'@examples @@ -18,17 +23,38 @@ #'WPD <- CST_WindPowerDensity(wind) #' #'@export -CST_WindPowerDensity <- function(wind, ro = 1.225) { +CST_WindPowerDensity <- function(wind, ro = 1.225, start = NULL, end = NULL, + time_dim = 'ftime', ncores = NULL) { if (!inherits(wind, 's2dv_cube')) { stop("Parameter 'wind' must be of the class 's2dv_cube', ", "as output by CSTools::CST_Load.") } - wind$data <- WindPowerDensity(wind$data, ro = ro) + # when subsetting is needed, dimensions are also needed: + if (!is.null(start) && !is.null(end)) { + if (is.null(dim(wind$Dates$start))) { + if (length(wind$Dates$start) != dim(wind$data)[time_dim]) { + if (length(wind$Dates$start) == + prod(dim(wind$data)[time_dim] * dim(wind$data)['sdate'])) { + dim(wind$Dates$start) <- c(dim(wind$data)[time_dim], + dim(wind$data)['sdate']) + } + } else { + warning("Dimensions in 'wind' element 'Dates$start' are missed/unmatched. All data would be used.") + } + } + } + wind$data <- WindPowerDensity(wind$data, ro = ro, dates = wind$Dates[[1]], + start = start, end = end, ncores = ncores) if ('Variable' %in% names(wind)) { if ('varName' %in% names(wind$Variable)) { wind$Variable$varName <- 'WindPowerDensity' } } + if (!is.null(start) && !is.null(end)) { + wind$Dates <- SelectPeriodOnDates(dates = wind$Dates[[1]], + start = start, end = end, + time_dim = time_dim, ncores = ncores) + } return(wind) } @@ -40,6 +66,12 @@ CST_WindPowerDensity <- function(wind, ro = 1.225) { #' #'@param wind a multidimensional array, vector or scalar with instantaneous wind speeds expressed in m/s. #'@param ro a scalar, or alternatively a multidimensional array with the same dimensions as wind, with the air density expressed in kg/m^3. By default it takes the value 1.225, the standard density of air at 15ºC and 1013.25 hPa. +#'@param dates a vector of dates or a multidimensional array of dates with named dimensions matching the dimensions on parameter 'data'. By default it is NULL, to select a period this parameter must be provided. +#'@param start an optional parameter to defined the initial date of the period to select from the data by providing a list of two elements: the initial date of the period and the initial month of the period. By default it is set to NULL and the indicator is computed using all the data provided in \code{data}. +#'@param end an optional parameter to defined the final date of the period to select from the data by providing a list of two elements: the final day of the period and the final month of the period. By default it is set to NULL and the indicator is computed using all the data provided in \code{data}. +#'@param time_dim a character string indicating the name of the function to compute the indicator. By default, it is set to 'ftime'. More than one dimension name matching the dimensions provided in the object \code{data$data} can be specified. +#'@param ncores an integer indicating the number of cores to use in parallel computation for temporal subsetting. +#' #'@return An array with the same dimensions as wind, containing Wind Power Density expressed in W/m^2. #' #'@examples @@ -47,6 +79,18 @@ CST_WindPowerDensity <- function(wind, ro = 1.225) { #'WPD <- WindPowerDensity(wind) #' #'@export -WindPowerDensity <- function(wind, ro = 1.225) { +WindPowerDensity <- function(wind, ro = 1.225, dates = NULL, start = NULL, end = NULL, + time_dim = 'time', ncores = NULL) { + if (!is.null(dates)) { + if (!is.null(start) && !is.null(end)) { + if (!any(c(is.list(start), is.list(end)))) { + stop("Parameter 'start' and 'end' must be lists indicating the ", + "day and the month of the period start and end.") + } + wind <- SelectPeriodOnData(wind, dates, start, end, + time_dim = time_dim, ncores = ncores) + } + } + return(0.5 * ro * wind^3) } diff --git a/man/CST_WindCapacityFactor.Rd b/man/CST_WindCapacityFactor.Rd index 0c75557..9e0a46c 100644 --- a/man/CST_WindCapacityFactor.Rd +++ b/man/CST_WindCapacityFactor.Rd @@ -4,12 +4,27 @@ \alias{CST_WindCapacityFactor} \title{Wind capacity factor on s2dv_cube objects} \usage{ -CST_WindCapacityFactor(wind, IEC_class = c("I", "I/II", "II", "II/III", "III")) +CST_WindCapacityFactor( + wind, + IEC_class = c("I", "I/II", "II", "II/III", "III"), + start = NULL, + end = NULL, + time_dim = "ftime", + ncores = NULL +) } \arguments{ \item{wind}{a s2dv_cube object with instantaneous wind speeds expressed in m/s.} \item{IEC_class}{a string indicating the IEC wind class (see IEC 61400-1) of the turbine to be selected. Classes \code{'I'}, \code{'II'} and \code{'III'} are suitable for sites with an annual mean wind speed of 10, 8.5 and 7.5 m/s respectively. Classes \code{'I/II'} and \code{'II/III'} indicate intermediate turbines that fit both classes. More details of the five turbines and a plot of its power curves can be found in Lledó et al. (2019).} + +\item{start}{an optional parameter to defined the initial date of the period to select from the data by providing a list of two elements: the initial date of the period and the initial month of the period. By default it is set to NULL and the indicator is computed using all the data provided in \code{data}.} + +\item{end}{an optional parameter to defined the final date of the period to select from the data by providing a list of two elements: the final day of the period and the final month of the period. By default it is set to NULL and the indicator is computed using all the data provided in \code{data}.} + +\item{time_dim}{a character string indicating the name of the function to compute the indicator. By default, it is set to 'ftime'. More than one dimension name matching the dimensions provided in the object \code{data$data} can be specified.} + +\item{ncores}{an integer indicating the number of cores to use in parallel computation for temporal subsetting.} } \value{ A s2dv_cube object containing the Wind Capacity Factor (unitless). diff --git a/man/CST_WindPowerDensity.Rd b/man/CST_WindPowerDensity.Rd index a0d552c..8033bfe 100644 --- a/man/CST_WindPowerDensity.Rd +++ b/man/CST_WindPowerDensity.Rd @@ -4,12 +4,27 @@ \alias{CST_WindPowerDensity} \title{Wind power density on s2dv_cube objects} \usage{ -CST_WindPowerDensity(wind, ro = 1.225) +CST_WindPowerDensity( + wind, + ro = 1.225, + start = NULL, + end = NULL, + time_dim = "ftime", + ncores = NULL +) } \arguments{ \item{wind}{a s2dv_cube object with instantaneous wind speeds expressed in m/s obtained from CST_Load or s2dv_cube functions from CSTools pacakge} \item{ro}{a scalar, or alternatively a multidimensional array with the same dimensions as wind, with the air density expressed in kg/m^3. By default it takes the value 1.225, the standard density of air at 15ºC and 1013.25 hPa.} + +\item{start}{an optional parameter to defined the initial date of the period to select from the data by providing a list of two elements: the initial date of the period and the initial month of the period. By default it is set to NULL and the indicator is computed using all the data provided in \code{data}.} + +\item{end}{an optional parameter to defined the final date of the period to select from the data by providing a list of two elements: the final day of the period and the final month of the period. By default it is set to NULL and the indicator is computed using all the data provided in \code{data}.} + +\item{time_dim}{a character string indicating the name of the function to compute the indicator. By default, it is set to 'ftime'. More than one dimension name matching the dimensions provided in the object \code{data$data} can be specified.} + +\item{ncores}{an integer indicating the number of cores to use in parallel computation for temporal subsetting.} } \value{ A s2dv_cube object containing Wind Power Density expressed in W/m^2. diff --git a/man/WindCapacityFactor.Rd b/man/WindCapacityFactor.Rd index 789a901..935a3e3 100644 --- a/man/WindCapacityFactor.Rd +++ b/man/WindCapacityFactor.Rd @@ -4,12 +4,30 @@ \alias{WindCapacityFactor} \title{Wind capacity factor} \usage{ -WindCapacityFactor(wind, IEC_class = c("I", "I/II", "II", "II/III", "III")) +WindCapacityFactor( + wind, + IEC_class = c("I", "I/II", "II", "II/III", "III"), + dates = NULL, + start = NULL, + end = NULL, + time_dim = "time", + ncores = NULL +) } \arguments{ \item{wind}{a multidimensional array, vector or scalar with instantaneous wind speeds expressed in m/s.} \item{IEC_class}{a string indicating the IEC wind class (see IEC 61400-1) of the turbine to be selected. Classes \code{'I'}, \code{'II'} and \code{'III'} are suitable for sites with an annual mean wind speed of 10, 8.5 and 7.5 m/s respectively. Classes \code{'I/II'} and \code{'II/III'} indicate intermediate turbines that fit both classes. More details of the five turbines and a plot of its power curves can be found in Lledó et al. (2019).} + +\item{dates}{a vector of dates or a multidimensional array of dates with named dimensions matching the dimensions on parameter 'data'. By default it is NULL, to select a period this parameter must be provided.} + +\item{start}{an optional parameter to defined the initial date of the period to select from the data by providing a list of two elements: the initial date of the period and the initial month of the period. By default it is set to NULL and the indicator is computed using all the data provided in \code{data}.} + +\item{end}{an optional parameter to defined the final date of the period to select from the data by providing a list of two elements: the final day of the period and the final month of the period. By default it is set to NULL and the indicator is computed using all the data provided in \code{data}.} + +\item{time_dim}{a character string indicating the name of the function to compute the indicator. By default, it is set to 'ftime'. More than one dimension name matching the dimensions provided in the object \code{data$data} can be specified.} + +\item{ncores}{an integer indicating the number of cores to use in parallel computation for temporal subsetting.} } \value{ An array with the same dimensions as wind, containing the Wind Capacity Factor (unitless). diff --git a/man/WindPowerDensity.Rd b/man/WindPowerDensity.Rd index b0dfd77..5642edb 100644 --- a/man/WindPowerDensity.Rd +++ b/man/WindPowerDensity.Rd @@ -4,12 +4,30 @@ \alias{WindPowerDensity} \title{Wind power density on multidimensional array objects} \usage{ -WindPowerDensity(wind, ro = 1.225) +WindPowerDensity( + wind, + ro = 1.225, + dates = NULL, + start = NULL, + end = NULL, + time_dim = "time", + ncores = NULL +) } \arguments{ \item{wind}{a multidimensional array, vector or scalar with instantaneous wind speeds expressed in m/s.} \item{ro}{a scalar, or alternatively a multidimensional array with the same dimensions as wind, with the air density expressed in kg/m^3. By default it takes the value 1.225, the standard density of air at 15ºC and 1013.25 hPa.} + +\item{dates}{a vector of dates or a multidimensional array of dates with named dimensions matching the dimensions on parameter 'data'. By default it is NULL, to select a period this parameter must be provided.} + +\item{start}{an optional parameter to defined the initial date of the period to select from the data by providing a list of two elements: the initial date of the period and the initial month of the period. By default it is set to NULL and the indicator is computed using all the data provided in \code{data}.} + +\item{end}{an optional parameter to defined the final date of the period to select from the data by providing a list of two elements: the final day of the period and the final month of the period. By default it is set to NULL and the indicator is computed using all the data provided in \code{data}.} + +\item{time_dim}{a character string indicating the name of the function to compute the indicator. By default, it is set to 'ftime'. More than one dimension name matching the dimensions provided in the object \code{data$data} can be specified.} + +\item{ncores}{an integer indicating the number of cores to use in parallel computation for temporal subsetting.} } \value{ An array with the same dimensions as wind, containing Wind Power Density expressed in W/m^2. -- GitLab