diff --git a/R/ArrayToNetCDF.R b/R/ArrayToNetCDF.R index 2b573f639be3db0d7daadd2f278efbe4d5b50ff7..0d8a87b8a7dbae7c958b77beab1cc2df74f0c874 100644 --- a/R/ArrayToNetCDF.R +++ b/R/ArrayToNetCDF.R @@ -297,6 +297,14 @@ ArrayToNetCDF <- function(arrays, file_path) { } ncatt_put(ncdf_object, defined_vars[[var_counter]]$name, 'coordinates', var_info[['coordinates']]) } + attrs_to_skip <- which(names(var_info) %in% c('addOffset', 'scaleFact', 'coordinates')) + attrs_to_add <- names(var_info) + if (length(attrs_to_skip) > 0) { + 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]]) + } var_counter <- var_counter + 1 } } diff --git a/R/CDORemap.R b/R/CDORemap.R index 24809c3f94f4ae37190068360af75d288f92188f..7d6ff39157809f7b28709d5bc4207766e2d620b8 100644 --- a/R/CDORemap.R +++ b/R/CDORemap.R @@ -79,19 +79,39 @@ CDORemap <- function(data_array = NULL, lons, lats, grid, method, } } else { if (!(lon_dim %in% names(dim(lons)))) { - stop("Parameter 'lon' must have the same longitude dimension name as the 'data_array'.") + if (!return_array) { + if (any(names(dim(lons)) %in% known_lon_names)) { + lon_dim <- names(dim(lons))[which(names(dim(lons)) %in% known_lon_names)] + names(dim(data_array))[2] <- lon_dim + } else { + stop("Parameter 'lon' must have a longitude dimension.") + } + } else { + stop("Parameter 'lon' must have the same longitude dimension name as the 'data_array'.") + } } if (length(dim(lons)) > 1 && !(lat_dim %in% names(dim(lons)))) { stop("Parameter 'lon' must have the same latitude dimension name as the 'data_array'.") } } if (is.null(names(dim(lats)))) { - if (length(dim(lats)) > 1) { + if (length(dim(lats)) == 1) { + names(dim(lats)) <- lat_dim + } else { stop("Parameter 'lats' must be provided with dimension names.") } } else { if (!(lat_dim %in% names(dim(lats)))) { - stop("Parameter 'lat' must have the same latitude dimension name as the 'data_array'.") + if (!return_array) { + if (any(names(dim(lats)) %in% known_lat_names)) { + lat_dim <- names(dim(lats))[which(names(dim(lats)) %in% known_lat_names)] + names(dim(data_array))[1] <- lat_dim + } else { + stop("Parameter 'lat' must have a latitude dimension.") + } + } else { + stop("Parameter 'lat' must have the same latitude dimension name as the 'data_array'.") + } } if (length(dim(lats)) > 1 && !(lon_dim %in% names(dim(lats)))) { stop("Parameter 'lat' must have the same longitude dimension name as the 'data_array'.") diff --git a/R/Utils.R b/R/Utils.R index 806ad92d448b709655a06d34aae3d28f3f7f4310..210cdfafe5b36af8391cadf7532a18f6499c1ce3 100644 --- a/R/Utils.R +++ b/R/Utils.R @@ -341,12 +341,14 @@ if (!is.null(work_piece[['progress_amount']])) { cat("\n") } - cat(paste0("! Warning: the dataset with index ", tail(work_piece[['indices']], 1), - " in '", work_piece[['dataset_type']], "' is originally on ", - "a grid coarser than the common grid and it has been ", - "extrapolated. Check the results carefully. It is ", - "recommended to specify as common grid the coarsest grid ", - "among all requested datasets via the parameter 'grid'.\n")) + if (!explore_dims) { + cat(paste0("! Warning: the dataset with index ", tail(work_piece[['indices']], 1), + " in '", work_piece[['dataset_type']], "' is originally on ", + "a grid coarser than the common grid and it has been ", + "extrapolated. Check the results carefully. It is ", + "recommended to specify as common grid the coarsest grid ", + "among all requested datasets via the parameter 'grid'.\n")) + } } # Now calculate if the user requests for a lonlat subset or for the # entire field diff --git a/man/ArrayToNetCDF.Rd b/man/ArrayToNetCDF.Rd index b857eaf2396b05aa9f0dcd26eea5dbc249bf520e..fcfb77005610e6eb13fa044e3b0c49e2a158c876 100644 --- a/man/ArrayToNetCDF.Rd +++ b/man/ArrayToNetCDF.Rd @@ -13,8 +13,8 @@ ArrayToNetCDF(arrays = list(temperature = array(1:9, c(3, 3))), \item{Via the dimension names of each provided array:}{The dimension names of each of the provided arrays will be interpreted as names for the dimensions of the NetCDF files. Read further for special dimension names that will trigger special behaviours, such as 'time' and 'var'.\cr E.g:\cr \code{ -temperature <- array(rnorm(10 * 50 * 100), dim = c(10, 50, 100)) -names(dim(temperature)) <- c('time', 'latitude', 'longitude') +temperature <- array(rnorm(100 * 50 * 10), dim = c(100, 50, 10)) +names(dim(temperature)) <- c('longitude', 'latitude', 'time') ArrayToNetCDF(list(temperature = temperature), file_path = 'example.nc') } } diff --git a/man/CDORemap.Rd b/man/CDORemap.Rd index 7960d7bbf5395f06d921f9465f55c9e7aeb59365..fa539dc6569f2b1540ba765b174b7e0edf854ce0 100644 --- a/man/CDORemap.Rd +++ b/man/CDORemap.Rd @@ -14,7 +14,7 @@ CDORemap(data_array = NULL, lons, lats, grid, method, \item{data_array}{Multidimensional numeric array to be interpolated. If provided, it must have at least a longitude and a latitude dimensions, identified by the array dimension names. The names for these dimensions must be one of the recognized by s2dverification (can be checked with \code{s2dverification:::.KnownLonNames()} and \code{s2dverification:::.KnownLatNames()}).} \item{lons}{Numeric vector or array of longitudes of the centers of the grid cells. Its size must match the size of the longitude/latitude dimensions of the input array.} \item{lats}{Numeric vector or array of latitudes of the centers of the grid cells. Its size must match the size of the longitude/latitude dimensions of the input array.} - \item{grid}{Character string specifying either a name of a grid (recognized by CDO; e.g.: 'r256x128', 't106grid') or a path to another NetCDF file which to read the grid from (a single grid must be defined in such file).} + \item{grid}{Character string specifying either a name of a target grid (recognized by CDO; e.g.: 'r256x128', 't106grid') or a path to another NetCDF file which to read the target grid from (a single grid must be defined in such file).} \item{method}{Character string specifying an interpolation method (recognized by CDO; e.g.: 'con', 'bil', 'bic', 'dis'). The following long names are also supported: 'conservative', 'bilinear', 'bicubic' and 'distance-weighted'.} \item{avoid_writes}{The step of permutation is needed when the input array has more than 3 dimensions and none of the longitude or latitude dimensions in the right-most position (CDO would not accept it without permuting previously). This step, executed by default when needed, can be avoided for the price of writing more intermediate files (whis usually is unconvenient) by setting the parameter \code{avoid_writes = TRUE}.} \item{crop}{Whether to crop the data after interpolation with 'cdo sellonlatbox' (TRUE) or to extend interpolated data to the whole world as CDO does by default (FALSE). If \code{crop = TRUE} then the longitude and latitude borders which to crop at are taken as the limits of the cells at the borders ('lons' and 'lats' are perceived as cell centers), i.e. the resulting array will contain data that covers the same area as the input array. This is equivalent to specifying \code{crop = 'preserve'}, i.e. preserving area. If \code{crop = 'tight'} then the borders which to crop at are taken as the minimum and maximum cell centers in 'lons' and 'lats', i.e. the area covered by the resulting array may be smaller if interpolating from a coarse grid to a fine grid. The parameter 'crop' also accepts a numeric vector of custom borders which to crop at: c(western border, eastern border, southern border, northern border). }