diff --git a/DESCRIPTION b/DESCRIPTION index ae13f8c9e95e8d3eacb745ebae86dd3c7919525f..c3c0b6be1b1be81562c1d99fef98efaf4b547a5d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: easyNCDF Title: Tools to Easily Read/Write NetCDF Files into/from Multidimensional R Arrays -Version: 0.0.6 +Version: 0.0.7 Authors@R: c( person("BSC-CNS", role = c("aut", "cph")), person("Nicolau", "Manubens", , "nicolau.manubens@bsc.es", role = c("aut", "cre"))) diff --git a/R/ArrayToNc.R b/R/ArrayToNc.R index a716232d5be35e5f8f373c56ebdd0b170e40cfc9..de3367e7cdbfbeec39b22bbea765dbc45ddca546 100644 --- a/R/ArrayToNc.R +++ b/R/ArrayToNc.R @@ -23,7 +23,10 @@ ArrayToNc <- function(arrays, file_path) { } else { vars_info <- NULL } - global_attrs[names(array_attrs)] <- array_attrs + #global_attrs[names(array_attrs)] <- array_attrs + if ('global_attrs' %in% names(array_attrs)) { + global_attrs <- c(global_attrs, array_attrs[['global_attrs']]) + } var_dim <- which(names(dim(arrays[[i]])) %in% c('var', 'variable')) if (length(var_dim) > 0) { var_dim <- var_dim[1] @@ -323,6 +326,12 @@ ArrayToNc <- function(arrays, file_path) { var_counter <- var_counter + 1 } } + # Adding global attributes + if (length(global_attrs) > 0) { + for (attr_name in names(global_attrs)) { + ncatt_put(ncdf_object, 0, attr_name, global_attrs[[attr_name]]) + } + } nc_close(ncdf_object) invisible(NULL) } diff --git a/R/NcReadDims.R b/R/NcReadDims.R index 660b4efe20e64d9194a22100846d7bb6831bfe5c..5d2d87cd395b7c13a6c569ca3b2567064bb75e82 100644 --- a/R/NcReadDims.R +++ b/R/NcReadDims.R @@ -52,8 +52,7 @@ NcReadDims <- function(file_to_read, var_names = NULL) { new_dim <- c(var = 1) found_dims <- c(new_dim, found_dims) if (!is.null(dims)) { - dims <- .MergeArrayDims(dims, found_dims) - dims <- pmax(dims[[1]], dims[[2]]) + dims <- .MergeArrayDims(dims, found_dims)[[3]] dims['var'] <- dims['var'] + 1 } else { dims <- found_dims diff --git a/R/NcReadVarNames.R b/R/NcReadVarNames.R index 95dfe2b054a15c3bbddb78c4f61c3948570bc5a7..ccab8bc9d758a8814d00b67c83832b79a4b82d27 100644 --- a/R/NcReadVarNames.R +++ b/R/NcReadVarNames.R @@ -25,5 +25,9 @@ NcReadVarNames <- function(file_to_read) { var_names <- c(var_names, extra_dimvars) } + if (close) { + file_closer(file_object) + } + var_names } diff --git a/R/Subset.R b/R/Subset.R index a084a1c2ff3526c460bdcc87e8741cfa355d0386..d10629c59dea260cc4d5b9e37dcb14706aae6cc3 100644 --- a/R/Subset.R +++ b/R/Subset.R @@ -80,6 +80,10 @@ Subset <- function(x, along, indices, drop = FALSE) { names(metadata[['dim']]) <- dim_names[-dims_to_drop] metadata[['dimensions']] <- dim_names[-dims_to_drop] } + if (length(metadata[['dim']]) == 0) { + metadata['dim'] <- list(NULL) + metadata['dimensions'] <- list(NULL) + } } else if (is.character(dim_names)) { names(metadata[['dim']]) <- dim_names metadata[['dimensions']] <- dim_names diff --git a/R/Utils.R b/R/Utils.R index cc9b059ba9b5844e4446ed7552820299ea8b45be..fde2c95c2599850b35bb9f56c6c4b63714cf4990 100644 --- a/R/Utils.R +++ b/R/Utils.R @@ -131,6 +131,11 @@ # It expects as inputs two named numeric vectors, and it extends them # with dimensions of length 1 until an ordered common dimension # format is reached. +# The first output is dims1 extended with 1s. +# The second output is dims2 extended with 1s. +# The third output is a merged dimension vector. If dimensions with +# the same name are found in the two inputs, and they have a different +# length, the maximum is taken. .MergeArrayDims <- function(dims1, dims2) { new_dims1 <- c() new_dims2 <- c() @@ -158,7 +163,7 @@ new_dims1 <- c(new_dims1, dims_to_add) new_dims2 <- c(new_dims2, dims2) } - list(new_dims1, new_dims2) + list(new_dims1, new_dims2, pmax(new_dims1, new_dims2)) } # This function takes two named arrays and merges them, filling with diff --git a/easyNCDF-manual.pdf b/easyNCDF-manual.pdf index 646d02d05780dd7561765a00788d52a1d3705863..621593b4134b376a16f33b53150f06027b4f1462 100644 Binary files a/easyNCDF-manual.pdf and b/easyNCDF-manual.pdf differ diff --git a/man/ArrayToNc.Rd b/man/ArrayToNc.Rd index 2eff8fddb99e3f87b33727f4894472dc4c1826c0..7e33ee0de66122a178ff7fdf0a00194270226bec 100644 --- a/man/ArrayToNc.Rd +++ b/man/ArrayToNc.Rd @@ -155,6 +155,17 @@ attr(a, 'variables') <- metadata names(dim(a)) <- c('lat', 'lon', 'time', 'var') ArrayToNc(a, 'tmp.nc') +# Global attributes can be specified +a <- array(rnorm(10), dim = c(a = 5, b = 2)) +attrs <- list(variables = + list(tas = list(var_attr_1 = 'test_1_var', + var_attr_2 = 2)), + global_attrs = list(global_attr_name_1 = 'test_1_global', + global_attr_name_2 = 2) + ) +attributes(a) <- c(attributes(a), attrs) +ArrayToNc(a, 'tmp.nc') + # Unlimited dimensions can be manually created a <- array(1:400, dim = c(5, 10, 4, 2)) metadata <- list(