From 69278b3ae6c5bd4b80944db75d01b167d970ce12 Mon Sep 17 00:00:00 2001 From: vagudets Date: Tue, 4 Jun 2024 15:05:22 +0200 Subject: [PATCH 1/4] Fill array with NA values for time steps before the initial date --- R/Histo2Hindcast.R | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/R/Histo2Hindcast.R b/R/Histo2Hindcast.R index 95112f7..953e633 100644 --- a/R/Histo2Hindcast.R +++ b/R/Histo2Hindcast.R @@ -150,10 +150,15 @@ Histo2Hindcast <- function(data, sdatesin, sdatesout, nleadtimesout, res <- array(dim = c(sdate = length(yrout), ftime = nleadtimesout)) diff_mth <- (yrout - yrin) * 12 + (mthout - mthin) + # diff_mth[which(diff_mth < 0)] <- NA 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]) - res[i, seq_along(ftime_ind)] <- data[1, ftime_ind] + if (diff_mth[i] < 0) { + res[i, seq_along(ftime_ind)] <- rep(NA, length(seq_along(ftime_ind))) + } else { + res[i, seq_along(ftime_ind)] <- data[1, ftime_ind] + } } } -- GitLab From cfc9e3cc6e54708c3578407de3d44db4013f3545 Mon Sep 17 00:00:00 2001 From: vagudets Date: Wed, 5 Jun 2024 11:51:06 +0200 Subject: [PATCH 2/4] Remove commented code; add warning if sdatesout < sdatesin --- R/Histo2Hindcast.R | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/R/Histo2Hindcast.R b/R/Histo2Hindcast.R index 953e633..7791d15 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(paste("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), @@ -150,11 +155,11 @@ Histo2Hindcast <- function(data, sdatesin, sdatesout, nleadtimesout, res <- array(dim = c(sdate = length(yrout), ftime = nleadtimesout)) diff_mth <- (yrout - yrin) * 12 + (mthout - mthin) - # diff_mth[which(diff_mth < 0)] <- NA 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]) 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 { res[i, seq_along(ftime_ind)] <- data[1, ftime_ind] -- GitLab From 824eb19be4b0bf5d680326c07225633dd3182171 Mon Sep 17 00:00:00 2001 From: Victoria Agudetse Roures Date: Wed, 5 Jun 2024 12:14:58 +0200 Subject: [PATCH 3/4] Fix pipeline --- R/Histo2Hindcast.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/Histo2Hindcast.R b/R/Histo2Hindcast.R index 7791d15..ddc8269 100644 --- a/R/Histo2Hindcast.R +++ b/R/Histo2Hindcast.R @@ -131,8 +131,8 @@ Histo2Hindcast <- function(data, sdatesin, sdatesout, nleadtimesout, "format 'YYYYMMDD' or 'YYYYMM'. Found certain month is over 12.") } if (any((yrout - yrin) * 12 + (mthout - mthin) < 0)) { - warning(paste("Some of the start dates requested in 'sdatesout' are" - "earlier than the original start date 'sdatesin'. These" + warning(paste("Some of the start dates requested in 'sdatesout' are", + "earlier than the original start date 'sdatesin'. These", "sdates will be filled with NA values")) } -- GitLab From e5d6b8e2400b999d87f323443e734fb9382dee24 Mon Sep 17 00:00:00 2001 From: Victoria Agudetse Roures Date: Wed, 5 Jun 2024 12:50:47 +0200 Subject: [PATCH 4/4] lintr pointers --- R/Histo2Hindcast.R | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/R/Histo2Hindcast.R b/R/Histo2Hindcast.R index ddc8269..649b75b 100644 --- a/R/Histo2Hindcast.R +++ b/R/Histo2Hindcast.R @@ -131,9 +131,9 @@ Histo2Hindcast <- function(data, sdatesin, sdatesout, nleadtimesout, "format 'YYYYMMDD' or 'YYYYMM'. Found certain month is over 12.") } if (any((yrout - yrin) * 12 + (mthout - mthin) < 0)) { - warning(paste("Some of the start dates requested in 'sdatesout' are", - "earlier than the original start date 'sdatesin'. These", - "sdates will be filled with NA values")) + 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, @@ -156,16 +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]) - 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 { - res[i, seq_along(ftime_ind)] <- data[1, ftime_ind] - } + 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) } -- GitLab