diff --git a/modules/Units/Units.R b/modules/Units/Units.R index 07e175436a66563fb81ec2df9cd2ffd0447c8b75..a143c0a2037684594cd7642149fa039240e4784c 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,12 @@ 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) + } }) ## TODO: ## if "/" appears substitute by -1 in at the end of next unit. How to know? @@ -62,19 +63,24 @@ Units <- function(recipe, data) { res <- data } else { if (recipe$Run$filesystem == 'esarchive' && - !(length(unique(orig_units)) == 1)) { - warn(recipe$Run$logger, - paste("The units in", names(orig_units), "are not all equal.", + (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 uniformized.", "If this is not expected, please contact the ES data team.")) } res <- sapply(1:length(data), function(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) + 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] + } } else { result <- data[x] }