CDORemap() doesn't reorder the unlimited dimension to the last position
Reported by @jramon, CDORemap() doesn't work well for certain cases. The error message is like:
Error in R_nc4_def_var_double: NetCDF: NC_UNLIMITED in the wrong index
Name of variable that the error occurred on: "var"
[1] "----------------------"
[1] "Var: var"
[1] "Ndims: 4"
[1] "Dimids: "
[1] 3 2 1 0
Error in ncvar_add(nc, vars[[ivar]], verbose = verbose, indefine = TRUE) :
Error in ncvar_add, defining var var
The error shows if the input array reaches all the following conditions:
(1) dimension number is more than 4 (2) more than 3 dimensions are not of length 1 (3) the last dimension is not lat or lon.
For example:
data5 <- array(rnorm(1000), dim = c(window = 9, var = 2, lat = 10, lon = 12, ens = 1))
res <- CDORemap(data_array = data5,
lats = 1:10, lons = 1:12,
grid = 'r100x50', method = 'con', crop = T)
The error comes from NcToArray(), which expects to have the unlimited dimension as the last dimension. CDORemap() doesn't reorder the dimensions well for the problematic case above. At line 780, check dim(subset)
and we can see that the "time" dimension is not at the last position.
@jramon the workaround now is to make "longitude" the last dimension. I'll report here when the bug is solved.
Best,
An-Chi