agg_ini_end <- function(x, ini, end, indices = NULL, method, na.rm ,ncores) {
  # to make the code work with both options:
  if (!is.null(ini) && is.null(indices)) {
    # list of vectors for the indices from ini to end pairs
    indices <- lapply(1:length(ini), function(x) { 
                    ini[x]:end[x]})
    plotting_attr <- list(ini_ftime = ini, end_ftime = end)
  } else {
    # take the first and last element of each indices list for time_bounds saving
    ini <- unlist(lapply(indices, function(x){x[1]}))
    end <- unlist(lapply(indices, function(x){x[length(x)]}))
    indices <- lapply(1:length(ini), function(x) {
                      ini[x]:end[x]})
    plotting_attr <- list(names(indices))
  }
  original_dims <- names(dim(x[[1]]$data))
  if (method == 'average') {
    x[[1]]$data <- Apply(x[[1]]$data,
                         target_dim = 'time',
                         function(y, ind) {
                           res <- sapply(1:length(indices), 
                                         function(z) {
                                           mean(y[indices[[z]]],
                                                na.rm = na.rm)
                                         })
                           if (is.null(dim(res))) {
                             dim(res) <- c(length(res))
                           }
                           return(res)
                         },
                         ind = indices,
                         output_dims = 'time',
                         ncores = ncores)$output1
  } else if (method == 'accumulated') {
    x[[1]]$data <- Apply(x[[1]]$data,
                         target_dim = 'time',
                         function(y, ind) {
                           res <- sapply(1:length(indices), 
                                         function(z) {
                                           sum(y[indices[[z]]],
                                               na.rm = na.rm)
                                         })
                           if (is.null(dim(res))) {
                             dim(res) <- c(length(res))
                           }
                           return(res)
                         },
                         ind = indices,
                         output_dims = 'time',
                         ncores = ncores)$output1
  } else {
    stop("Unknown method")
  }
  # Check in case only 1 aggregation only is requested to add time dim back:
  if (!('time' %in% names(dim(data[[1]]$data)))) {
    dim(x[[1]]$data) <- c(dim(x[[1]]$data), time = 1)
  }
  x[[1]]$data <- Reorder(x[[1]]$data, original_dims)
  x[[1]]$dims <- dim(x[[1]]$data)
  x[[1]]$coords$time <- 1:length(ini) 
  attr(x[[1]]$coords$time, "indices") <- TRUE
  tmp_dates <- Subset(x[[1]]$attrs$Dates,
                      along = 'time',
                      indices = ini) 
  x[[1]]$attrs$time_bounds$start <- tmp_dates
  x[[1]]$attrs$time_bounds$end <- Subset(x[[1]]$attrs$Dates,
                                         along = 'time',
                                         indices = end)
  attributes(x[[1]]$attrs$time_bounds)$plotting_attr <- plotting_attr
  x[[1]]$attrs$Dates <- tmp_dates 
  return(x)
}

