as.s2dv_cube() fails if latitude and/or longitude dimensions are not present and length(FileSelectors) > 1
(This is a template to report errors and bugs. Please fill in the relevant information and delete the rest.)
Hi me,
R and packages Version
R/4.2.1, CSTools
Summary
as.s2dv_cube() fails when the startR_array does not have any dimensions that are in .KnownLatNames()
or .KnownLonNames()
and there are multiple elements in FileSelectors
, because of this piece of code:
if (length(names(FileSelector)) > 1) {
# lon name
known_lon_names <- .KnownLonNames()
lon_name_dat <- names(dims)[which(names(dims) %in% known_lon_names)]
# lat name
known_lat_names <- .KnownLatNames()
lat_name_dat <- names(dims)[which(names(dims) %in% known_lat_names)]
result$attrs$Datasets <- names(FileSelector)
# TO DO: add dat_dim parameter
if (any(names(dims) %in% c('dat', 'dataset'))) {
dat_dim <- names(dims)[which(names(dims) %in% c('dat', 'dataset'))]
result$coords[[dat_dim]] <- names(FileSelector)
if (!remove_attrs_coords) attr(result$coords[[dat_dim]], 'indices') <- FALSE
}
for (i in 2:length(names(FileSelector))) {
if (!is.null(lon_name_dat)) {
if (any(result$coords[[lon_name_dat]] != as.vector(attributes(object)$Variables[[names(FileSelector)[i]]][[lon_name_dat]]))) {
warning("'lon' values are different for different datasets. ",
"Only values from the first will be used.")
}
}
if (!is.null(lat_name_dat)) {
if (any(result$coords[[lat_name_dat]] != as.vector(attributes(object)$Variables[[names(FileSelector)[i]]][[lat_name_dat]]))) {
warning("'lat' values are different for different datasets. ",
"Only values from the first will be used.")
}
}
}
}
If there are no known lat/lon names in names(dims), then names(dims)[which(names(dims) %in% known_lon_names)]
returns character(0)
, not NULL
. Therefore !is.null(lat_name_dat)
returns FALSE
even if lat_name_dat is empty, and the code fails when trying to access result$coords[[lat_name_dat]]
.
Proposed solution
Changing:
if (!is.null(lon_name_dat)) {
(...)
}
if (!is.null(lat_name_dat)) {
(...)
}
To:
if (!length(lon_name_dat) == 0L)) {
(...)
}
if (!length(lat_name_dat) == 0L)) {
(...)
}