From e5b0132638045642bd3cbcef074c11cdebc85bc4 Mon Sep 17 00:00:00 2001 From: aho Date: Wed, 27 Sep 2023 18:24:58 +0200 Subject: [PATCH 1/4] Bugfix for no fcst case --- modules/Units/Units.R | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/modules/Units/Units.R b/modules/Units/Units.R index 07e17543..bd8d7650 100644 --- a/modules/Units/Units.R +++ b/modules/Units/Units.R @@ -22,9 +22,9 @@ Units <- function(recipe, data) { } else { flux <- recipe$Analysis$Variable$flux } - orig_units <- list() + orig_units <- vector('list', length = length(data)) + names(orig_units) <- names(var_names) for (element in names(var_names)) { - orig_units[[element]] <- c() for (x in var_names[[element]]) { orig_units[[element]] <- c(orig_units[[element]], data[[element]]$attrs$Variable$metadata[[x]]$units) @@ -48,11 +48,13 @@ Units <- function(recipe, data) { user_units <- gsub("\\**", "", user_units) user_units <- gsub("\\*", "", user_units) orig_units <- lapply(orig_units, function(x) { - x <- tolower(x) - x <- gsub(" ", "", x) - x <- gsub("\\**", "", x) - x <- gsub("\\*", "", x) - x <- unlist(ifelse(rep(length(x)==0, length(user_units)), user_units, x)) #when fcst is NULL + if (!is.null(x)) { + x <- tolower(x) + x <- gsub(" ", "", x) + x <- gsub("\\**", "", x) + x <- gsub("\\*", "", x) +# x <- unlist(ifelse(rep(length(x)==0, length(user_units)), user_units, x)) #when fcst is NULL + } }) ## TODO: ## if "/" appears substitute by -1 in at the end of next unit. How to know? @@ -62,12 +64,13 @@ Units <- function(recipe, data) { res <- data } else { if (recipe$Run$filesystem == 'esarchive' && - !(length(unique(orig_units)) == 1)) { - warn(recipe$Run$logger, + (sum(!sapply(unique(orig_units), is.null)) != 1)) { + warn(recipe$Run$logger, paste("The units in", names(orig_units), "are not all equal.", "If this is not expected, please contact the ES data team.")) } res <- sapply(1:length(data), function(x) { + if (!is.null(data[[x]])) { if (!all(orig_units[x] == user_units)) { result <- units_transform(data[x], orig_units = orig_units[[x]], @@ -78,6 +81,9 @@ Units <- function(recipe, data) { } else { result <- data[x] } + } else { + result <- data[x] + } return(result) }, simplify = TRUE) # instead of lapply to get the named list directly info(recipe$Run$logger, "##### UNIT CONVERSION COMPLETE #####") -- GitLab From 259e57476a084b955b1e0570826f4c3ee80f61e5 Mon Sep 17 00:00:00 2001 From: aho Date: Wed, 27 Sep 2023 18:34:47 +0200 Subject: [PATCH 2/4] Paste dataset names as one string in the warning --- modules/Units/Units.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/Units/Units.R b/modules/Units/Units.R index bd8d7650..9a6da965 100644 --- a/modules/Units/Units.R +++ b/modules/Units/Units.R @@ -66,7 +66,8 @@ Units <- function(recipe, data) { if (recipe$Run$filesystem == 'esarchive' && (sum(!sapply(unique(orig_units), is.null)) != 1)) { warn(recipe$Run$logger, - paste("The units in", names(orig_units), "are not all equal.", + paste("The units in", paste(names(orig_units), collapse = ', '), + "are not all equal.", "If this is not expected, please contact the ES data team.")) } res <- sapply(1:length(data), function(x) { -- GitLab From fcc9d195dec6b994f800bb1f3ba73c9c8e0469e0 Mon Sep 17 00:00:00 2001 From: aho Date: Thu, 28 Sep 2023 09:31:07 +0200 Subject: [PATCH 3/4] Improve warning message; tidy code --- modules/Units/Units.R | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/modules/Units/Units.R b/modules/Units/Units.R index 9a6da965..a93d22e8 100644 --- a/modules/Units/Units.R +++ b/modules/Units/Units.R @@ -53,7 +53,6 @@ Units <- function(recipe, data) { x <- gsub(" ", "", x) x <- gsub("\\**", "", x) x <- gsub("\\*", "", x) -# x <- unlist(ifelse(rep(length(x)==0, length(user_units)), user_units, x)) #when fcst is NULL } }) ## TODO: @@ -67,21 +66,21 @@ Units <- function(recipe, data) { (sum(!sapply(unique(orig_units), is.null)) != 1)) { warn(recipe$Run$logger, paste("The units in", paste(names(orig_units), collapse = ', '), - "are not all equal.", + "were not all equal and will be uniformed.", "If this is not expected, please contact the ES data team.")) } res <- sapply(1:length(data), function(x) { if (!is.null(data[[x]])) { - if (!all(orig_units[x] == user_units)) { - result <- units_transform(data[x], - orig_units = orig_units[[x]], - user_units = user_units, - var_names[[x]], freq = freq, - flux = flux, - ncores = ncores) - } else { - result <- data[x] - } + if (!all(orig_units[x] == user_units)) { + result <- units_transform(data[x], + orig_units = orig_units[[x]], + user_units = user_units, + var_names[[x]], freq = freq, + flux = flux, + ncores = ncores) + } else { + result <- data[x] + } } else { result <- data[x] } -- GitLab From 4c5d7b987d256808b76cfc34875a844115a58804 Mon Sep 17 00:00:00 2001 From: Victoria Agudetse Roures Date: Fri, 29 Sep 2023 15:25:39 +0200 Subject: [PATCH 4/4] Typo --- modules/Units/Units.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/Units/Units.R b/modules/Units/Units.R index a93d22e8..a143c0a2 100644 --- a/modules/Units/Units.R +++ b/modules/Units/Units.R @@ -66,7 +66,7 @@ Units <- function(recipe, data) { (sum(!sapply(unique(orig_units), is.null)) != 1)) { warn(recipe$Run$logger, paste("The units in", paste(names(orig_units), collapse = ', '), - "were not all equal and will be uniformed.", + "were not all equal and will be uniformized.", "If this is not expected, please contact the ES data team.")) } res <- sapply(1:length(data), function(x) { -- GitLab