diff --git a/R/Histo2Hindcast.R b/R/Histo2Hindcast.R index 95112f78b740682332ef1a9a1a89b20488e85639..649b75bb6349497bbe4247ef72dbc746d33aa803 100644 --- a/R/Histo2Hindcast.R +++ b/R/Histo2Hindcast.R @@ -130,6 +130,11 @@ Histo2Hindcast <- function(data, sdatesin, sdatesout, nleadtimesout, stop("Parameter 'sdatesout' must be a vector of character in the ", "format 'YYYYMMDD' or 'YYYYMM'. Found certain month is over 12.") } + if (any((yrout - yrin) * 12 + (mthout - mthin) < 0)) { + warning("Some of the start dates requested in 'sdatesout' are ", + "earlier than the original start date 'sdatesin'. These ", + "sdates will be filled with NA values") + } res <- Apply(data, target_dims = c(sdate_dim, ftime_dim), @@ -151,11 +156,13 @@ Histo2Hindcast <- function(data, sdatesin, sdatesout, nleadtimesout, diff_mth <- (yrout - yrin) * 12 + (mthout - mthin) for (i in seq_along(diff_mth)) { - if (diff_mth[i] < dim(data)[2]) { - ftime_ind <- max(1 + diff_mth[i], 1):min(nleadtimesout + diff_mth[i], dim(data)[2]) + ftime_ind <- max(1 + diff_mth[i], 1):min(nleadtimesout + diff_mth[i], dim(data)[2]) + if (diff_mth[i] < 0) { + # Fill with NA values if the requested date is earlier than available data + res[i, seq_along(ftime_ind)] <- rep(NA, length(seq_along(ftime_ind))) + } else if (diff_mth[i] < dim(data)[2]) { res[i, seq_along(ftime_ind)] <- data[1, ftime_ind] } } - return(res) }