From a15961974e6c9a34f0306de6eaaac2d062b81713 Mon Sep 17 00:00:00 2001 From: Nicolau Manubens Date: Thu, 23 Mar 2017 13:06:24 +0100 Subject: [PATCH 1/4] Enhancement in PlotEquiMap. --- R/PlotEquiMap.R | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/R/PlotEquiMap.R b/R/PlotEquiMap.R index b8b7bd60..51599dc7 100644 --- a/R/PlotEquiMap.R +++ b/R/PlotEquiMap.R @@ -83,16 +83,28 @@ PlotEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, dims <- dim(var) # Transpose the input matrices because the base plot functions work directly # with dimensions c(lon, lat). + transpose <- FALSE + if (!is.null(names(dims))) { + if (any(names(dims) %in% .KnownLonNames()) && + any(names(dims) %in% .KnownLatNames())) { + if (which(names(dims) %in% .KnownLonNames()) != 1) { + transpose <- TRUE + } + } + } if (dims[1] != length(lon) || dims[2] != length(lat)) { if (dims[1] == length(lat) && dims[2] == length(lon)) { - var <- t(var) - if (!is.null(varu)) varu <- t(varu) - if (!is.null(varv)) varv <- t(varv) - if (!is.null(contours)) contours <- t(contours) - if (!is.null(dots)) dots <- aperm(dots, c(1, 3, 2)) - dims <- dim(var) + transpose <- TRUE } } + if (transpose) { + var <- t(var) + if (!is.null(varu)) varu <- t(varu) + if (!is.null(varv)) varv <- t(varv) + if (!is.null(contours)) contours <- t(contours) + if (!is.null(dots)) dots <- aperm(dots, c(1, 3, 2)) + dims <- dim(var) + } # Check lon if (length(lon) != dims[1]) { -- GitLab From 17fa84d9e9f9de200345490f91c1a3e04dff1693 Mon Sep 17 00:00:00 2001 From: Nicolau Manubens Date: Mon, 27 Mar 2017 19:36:13 +0200 Subject: [PATCH 2/4] Small fix in ArrayToNetCDF. --- R/ArrayToNetCDF.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/ArrayToNetCDF.R b/R/ArrayToNetCDF.R index 0d8a87b8..64b85eda 100644 --- a/R/ArrayToNetCDF.R +++ b/R/ArrayToNetCDF.R @@ -74,7 +74,7 @@ ArrayToNetCDF <- function(arrays, file_path) { if (!is.numeric(dim_info[['len']])) { stop("The provided 'len' for the ", k, "th dimension in the ", i, "th array must be a numeric value.") } - dim_info[['len']] <- round(dim_info[['len']][1]) + dim_info[['len']] <- as.integer(round(dim_info[['len']][1])) if (dim_info[['len']] != dim(arrays[[i]])[k]) { stop("The provided 'len' for the ", k, "th dimension in the ", i, "th array does not match the actual length of the provided array.") } -- GitLab From 523afdf58c7d35a0932d07b43250033b5ac111ac Mon Sep 17 00:00:00 2001 From: Nicolau Manubens Date: Mon, 27 Mar 2017 23:06:40 +0200 Subject: [PATCH 3/4] Enhancements for ArrayToNetCDF. --- R/ArrayToNetCDF.R | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/R/ArrayToNetCDF.R b/R/ArrayToNetCDF.R index 64b85eda..20b2227e 100644 --- a/R/ArrayToNetCDF.R +++ b/R/ArrayToNetCDF.R @@ -40,7 +40,7 @@ ArrayToNetCDF <- function(arrays, file_path) { } dim_names <- names(dim(arrays[[i]])) if (!is.null(dim_names)) { - if (any(is.na(dim_names) || (sapply(dim_names, nchar) == 0))) { + if (any(is.na(dim_names) | (sapply(dim_names, nchar) == 0))) { stop("The provided arrays must have all named dimensions or ", "all unnamed dimensions.") } @@ -292,10 +292,14 @@ ArrayToNetCDF <- function(arrays, file_path) { if (!is.character(var_info[['coordinates']])) { stop("The attribute 'coordinates' must be a character string.") } - if (!(all(strsplit(var_info[['coordinates']], ' ')[[1]] %in% sapply(defined_vars, '[[', 'name')))) { - stop("All the dimensions appearing in 'coordinates' must point to defined variables.") + coords <- strsplit(var_info[['coordinates']], ' ')[[1]] + if (!(all(coords %in% sapply(defined_vars, '[[', 'name') | + coords %in% sapply(defined_dims[which(sapply(defined_dims, '[[', 'create_dimvar'))], '[[', 'name')))) { + coords <- coords[which(coords %in% sapply(defined_vars, '[[', 'name') | + coords %in% sapply(defined_dims[which(sapply(defined_dims, '[[', 'create_dimvar'))], '[[', 'name'))] + .warning("Some of the dimensions appearing in 'coordinates' have been removed because they point to undefined variables.") } - ncatt_put(ncdf_object, defined_vars[[var_counter]]\$name, 'coordinates', var_info[['coordinates']]) + ncatt_put(ncdf_object, defined_vars[[var_counter]]\$name, 'coordinates', paste(coords, collapse = ' ')) } attrs_to_skip <- which(names(var_info) %in% c('addOffset', 'scaleFact', 'coordinates')) attrs_to_add <- names(var_info) @@ -303,7 +307,10 @@ ArrayToNetCDF <- function(arrays, file_path) { attrs_to_add <- attrs_to_add[-attrs_to_skip] } for (attribute_name in attrs_to_add) { - ncatt_put(ncdf_object, defined_vars[[var_counter]]\$name, attribute_name, var_info[[attribute_name]]) + if (is.numeric(var_info[[attribute_name]]) || + is.character(var_info[[attribute_name]])) { + ncatt_put(ncdf_object, defined_vars[[var_counter]]\$name, attribute_name, var_info[[attribute_name]]) + } } var_counter <- var_counter + 1 } -- GitLab From 6107224ccb90c221b6a030126a8d81438ece90d3 Mon Sep 17 00:00:00 2001 From: Nicolau Manubens Date: Wed, 29 Mar 2017 21:12:03 +0200 Subject: [PATCH 4/4] Fixes in ArrayToNetCDF. --- R/ArrayToNetCDF.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/ArrayToNetCDF.R b/R/ArrayToNetCDF.R index 20b2227e..47fe13ef 100644 --- a/R/ArrayToNetCDF.R +++ b/R/ArrayToNetCDF.R @@ -98,7 +98,7 @@ ArrayToNetCDF <- function(arrays, file_path) { if (!('vals' %in% names(dim_info))) { dim_info[['vals']] <- 1:dim_info[['len']] } else { - if (!is.numeric(dim_info[['vals']])) { + if (!(is.numeric(dim_info[['vals']]))) { stop("The provided 'vals' for the ", k, "th dimension in the ", i, "th array must be a numeric vector.") } if (dim_info[['units']] == '') { @@ -301,7 +301,7 @@ ArrayToNetCDF <- function(arrays, file_path) { } ncatt_put(ncdf_object, defined_vars[[var_counter]]\$name, 'coordinates', paste(coords, collapse = ' ')) } - attrs_to_skip <- which(names(var_info) %in% c('addOffset', 'scaleFact', 'coordinates')) + attrs_to_skip <- which(names(var_info) %in% c('addOffset', 'scaleFact', 'coordinates', 'dim')) attrs_to_add <- names(var_info) if (length(attrs_to_skip) > 0) { attrs_to_add <- attrs_to_add[-attrs_to_skip] -- GitLab