# s2dverification issue https://earth.bsc.es/gitlab/es/s2dverification/issues/228

library(s2dverification)
# Example without NA's
var <- 1 : (2 * 36)
dim(var) <- c(lat = 2, time = 36)
res <- Season(var, posdim = 2, monini = 1, moninf = 6, monsup = 8)
dim(res)
res
# the function performes the mean for the selected season:
mean(var[1,c(6,7,8)])

# Example with NA's in a time step out of the selected season:
var[, 1] <- NA
res <- Season(var, posdim = 2, monini = 1, moninf = 6, monsup = 8)
dim(res)
res

# Example with NA's in a time step in the selected season:
# It returns NA
var <- 1 : (2 * 36)
dim(var) <- c(lat = 2, time = 36)
var[, 7] <- NA
res <- Season(var, posdim = 2, monini = 1, moninf = 6, monsup = 8)
dim(res)
res

# Mean remove the NA's:
var <- 1 : 10
mean(var, na.rm = T)
mean(var, na.rm = F)
var[1] <- NA
mean(var, na.rm = T)
sum(var[2 : 10]) / 9
mean(var, na.rm = F)

moninf <- 6
monsup <- 8
ndimtime  <-  dim(var)['time']
ind <- NULL
moninf : monsup
for (i in moninf : monsup) {
   ind <- cbind(ind, seq(i, ndimtime, 12))
}
names(dim(ind)) <- c("season", "time")
#ind <- sort(ind)
library(multiApply)
na.rm = T
dim(ind) <- c(time = length(ind))
Apply(list(var, ind), 'time', fun = mean, na.rm = na.rm)


