Step() return cannot be one single value without assigning dim name
If the parameter 'output_dims' in Step() is one dimension with length = 1 (i.e., one single value), and the dimension name is not assigned in the function used in Step(), it returns the following ERROR:
Error in FUN(X[[i]], ...) : Expected 1st returned element by 'fun' to have 1 dimensions, but 0 found.
But if the length of the output is not 1, it works fine without assigning dim name.
Here is an example:
repos <- "/esarchive/exp/ecmwf/system5_m1/monthly_mean/tas_f6h/$var$_$sdate$.nc"
var <- 'tas'
sdate <- '20170101'
data <- Start(dat = repos,
var = var,
sdate = sdate,
ensemble = 'all',
time = indices(1), #Change to indices(1:2) -> works fine without the line below
latitude = indices(1:100),
longitude = indices(1:100),
return_vars = list(time = c('sdate'), longitude = NULL, latitude = NULL),
retrieve = FALSE
)
func <- function(x) {
a <- apply(x + 1, 2, mean)
# dim(a) <- c(time = length(a)) #Without this line, ERROR shows
return(a)
}
step <- Step(func, target_dims = c('ensemble', 'time'),
output_dims = c('time'))
wf <- AddStep(data, step)
res <- Compute(wf,
chunks = list(latitude = 1,
longitude = 1))
print(dim(res$output1))