# data format for input in CSIndicators SPEI functions
data_format_csindicators <- function(data, vars, var.list, lat, lon, dates){
    dict <- c('tas' = 'tmean', 
              'tasmax' = 'tmax', 
              'tasmin' = 'tmin', 
              'prlr' = 'pr', 
              'pet' = 'pet')
    dim.names <- names(dim(data$data))
    
    result <- list()
    for (var in vars){
        data.var <- list(data = data$data[,which(var.list == var),,,,,,,],
                        coords = list(latitude = lat, longitude = lon))
        
        # to keep original dims
        for(nn in dim.names){
            if(!(nn %in% names(dim(data.var$data)))){
                data.var$data <- s2dv::InsertDim(data.var$data, 
                                                    pos = 1, 
                                                    len = 1, 
                                                    name = nn)
            }
        }
        dim.order <- match(dim.names, names(dim(data.var$data)))
        data.var$data <- aperm(data.var$data, dim.order)

        # transform to s2dv_cube with metadata
        attr(data.var, 'class') <- 's2dv_cube'
        data.var$attrs$Dates <- dates

        # append all variables in a list of s2dv_cubes
        result <- append(result, list(data.var))
    
    }
    
    names(result) <- dict[vars]
    
    return(result)
}
