#'Conversion of 'startR_array' or 'list' objects to 's2dv_cube'
#'
#'This function converts data loaded using startR package or s2dv 
#'Load function into a 's2dv_cube' object.
#'
#'@author Perez-Zanon Nuria, \email{nuria.perez@bsc.es}
#'@author Nicolau Manubens, \email{nicolau.manubens@bsc.es}
#'
#'@param object An object of class 'startR_array' generated from function 
#'  \code{Start} from startR package or a list output from function \code{Load} 
#'  from s2dv package.
#'@param remove_attrs_coords A logical value indicating whether to remove 
#'  the attributes of the coordinates (TRUE) or not (FALSE) when the data is 
#'  loaded from Start(). It is TRUE by default.
#'@param remove_null A logical value indicating whether to remove the elements 
#'  that are NULL (TRUE) or not (FALSE) of the output object. It is TRUE by 
#'  default.
#'
#'@return The function returns a 's2dv_cube' object to be easily used with 
#'functions \code{CST} from CSTools package.
#'
#'@seealso \code{\link{s2dv_cube}}, \code{\link[s2dv]{Load}}, 
#'\code{\link[startR]{Start}} and \code{\link{CST_Load}}
#'@examples
#'\dontrun{
#'library(startR)
#'repos <- '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc'
#'data <- Start(dat = repos,
#'              var = 'tas',
#'              sdate = c('20170101', '20180101'),
#'              ensemble = indices(1:20),
#'              time = 'all',
#'              latitude = 'all',
#'              longitude = indices(1:40),
#'              return_vars = list(latitude = 'dat', longitude = 'dat', time = 'sdate'),
#'              retrieve = TRUE)
#'data <- as.s2dv_cube(data)
#'class(data)
#'startDates <- c('20001101', '20011101', '20021101',
#'                 '20031101', '20041101', '20051101')
#'data <- Load(var = 'tas', exp = 'system5c3s', 
#'             nmember = 15, sdates = startDates,
#'             leadtimemax = 3, latmin = 27, latmax = 48,
#'             lonmin = -12, lonmax = 40, output = 'lonlat')
#'data <- as.s2dv_cube(data)
#'class(data)
#'}
#'@export
as.s2dv_cube <- function(object, remove_attrs_coords = TRUE, remove_null = TRUE) {
  
 if (is.list(object)) {
    if (is.null(object) || (is.null(object$mod) && is.null(object$obs))) {
      stop("The s2dv::Load call did not return any data.")
    }

    obs <- object
    obs$mod <- NULL
    object$obs <- NULL
    names(object)[[1]] <- 'data' # exp
    names(obs)[[1]] <- 'data' # obs

    remove_matches <- function(v, patterns) {
      if (length(v) > 0) {
        matches <- c()
        for (pattern in patterns) {
          matches <- c(matches, which(grepl(pattern, v)))
        }
        if (length(matches) > 0) {
          v <- v[-matches]
        }
      }
      v
    }

    harmonize_patterns <- function(v) {
      matches <- grepl('.*\\.nc$', v)
      if (sum(!matches) > 0) {
        match_indices <- which(!matches)
        v[match_indices] <- sapply(v[match_indices], function(x) paste0(x, '*'))
      }
      v <- glob2rx(v)
      v <- gsub('\\$.*\\$', '*', v)
      v
    }

    if (!is.null(obs$data)) {
      obs$Datasets$exp <- NULL
      obs$Datasets <- obs$Datasets$obs
      obs_path_patterns <- sapply(obs$Datasets, function(x) attr(x, 'source'))
      obs_path_patterns <- harmonize_patterns(obs_path_patterns)
    }

    if (!is.null(object$data)) {
      object$Datasets$obs <- NULL
      object$Datasets <- object$Datasets$exp
      exp_path_patterns <- sapply(object$Datasets, function(x) attr(x, 'source'))
      exp_path_patterns <- harmonize_patterns(exp_path_patterns)
    }

    if (!is.null(obs$data) && !is.null(object$data)) {
      obs$source_files <- remove_matches(obs$source_files, exp_path_patterns)
      obs$not_found_files <- remove_matches(obs$not_found_files, exp_path_patterns)
  
      object$source_files <- remove_matches(object$source_files, obs_path_patterns)
      object$not_found_files <- remove_matches(object$not_found_files, obs_path_patterns)
    }  

    result <- list()
    if (!is.null(object$data)) {
      # attrs
      object$attrs <- within(object, rm(list = c('data')))
      object <- within(object, rm(list = names(object$attrs)))
      dates <- object$attrs$Dates$start
      dim(dates) <- dim(object$data)[c('sdate','ftime')]
      object$attrs$Dates <- dates
      # dims
      object$dims <- dim(object$data)
      # coords
      object$coords <- sapply(names(dim(object$data)),function(x) NULL)
      # sdate
      object$coords$sdate <- object$attrs$load_parameters$sdates
      # lon
      if (!is.null(object$attrs$lon)) {
        if (isTRUE(remove_attrs_coords)) {
          object$coords$lon <- as.vector(object$attrs$lon)
        } else {
          object$coords$lon <- object$attrs$lon
        }
        object$attrs <- within(object$attrs, rm(lon))
      }
      # lat
      if (!is.null(object$attrs$lat)) {
        if (isTRUE(remove_attrs_coords)) {
          object$coords$lat <- as.vector(object$attrs$lat)
        } else {
          object$coords$lat <- object$attrs$lat
        }
        object$attrs <- within(object$attrs, rm(lat))
      }
      # member
      object$coords$member <- 1:object$dims['member']
      # dataset
      object$coords$dataset <- 1:object$dims['dataset']
      # ftime
      object$coords$ftime <- 1:object$dims['ftime']
      
      # remove NULL values
      if (isTRUE(remove_null)) {
        object <- .rmNullObs(object)
      }

      object <- object[c('data','dims','coords','attrs')]
      class(object) <- 's2dv_cube'
      result$exp <- object
    }
    if (!is.null(obs$data)) {
      # attrs
      obs$attrs <- within(obs, rm(list = c('data', 'not_found_files')))
      obs <- within(obs, rm(list = names(obs$attrs)))
      dates <- obs$attrs$Dates$start
      dim(dates) <- dim(obs$data)[c('sdate','ftime')]
      obs$attrs$Dates <- dates
      # dims
      obs$dims <- dim(obs$data)
      # coords
      obs$coords <- sapply(names(dim(obs$data)),function(x) NULL)
      ## sdate
      obs$coords$sdate <- obs$attrs$load_parameters$sdates
      # lon
      if (!is.null(obs$attrs$lon)) {
        if (isTRUE(remove_attrs_coords)) {
          obs$coords$lon <- as.vector(obs$attrs$lon)
        } else {
          obs$coords$lon <- obs$attrs$lon
        }
        obs$attrs <- within(obs$attrs, rm(lon))
      }
      # lat
      if (!is.null(obs$attrs$lat)) {
        if (isTRUE(remove_attrs_coords)) {
          obs$coords$lat <- as.vector(obs$attrs$lat)
        } else {
          obs$coords$lat <- obs$attrs$lat
        }
        obs$attrs <- within(obs$attrs, rm(lat))
      }
      # member
      obs$coords$member <- 1:obs$dims['member']
      # dataset
      obs$coords$dataset <- 1:obs$dims['dataset']
      # ftime
      obs$coords$ftime <- 1:obs$dims['ftime']
      
      # remove NULL values
      if (isTRUE(remove_null)) {
        obs <- .rmNullObs(obs)
      }
      
      obs <- obs[c('data','dims','coords','attrs')]
      class(obs) <- 's2dv_cube'
      result$obs <- obs
    }
    if (is.list(result)) {
      if (is.null(result$exp)) {
          result <- result$obs
      } else if (is.null(result$obs)) {
          result <- result$exp
      } else {
          warning("The output is a list of two 's2dv_cube' objects",
                  " corresponding to 'exp' and 'obs'.")
      }
    }
  
  } else if (inherits(object,'startR_array')) {
    # From Start:
    result <- list()
    result$data <- as.vector(object)

    ## dims
    dims <- dim(object)
    dim(result$data) <- dims
    result$dims <- dims

    ## coords
    result$coords <- sapply(names(dims), function(x) NULL)

    # Find coordinates
    FileSelector <- attributes(object)$FileSelectors
    VariablesCommon <- names(attributes(object)$Variables$common)
    dat <- names(FileSelector)[1]
    VariablesDat <- names(attributes(object)$Variables[[dat]])

    for (i_coord in names(dims)) {
      if (i_coord %in% names(FileSelector[[dat]])) { # coords in FileSelector
        coord_in_fileselector <- FileSelector[[dat]][which(i_coord == names(FileSelector[[dat]]))]
        if (length(coord_in_fileselector) == 1) {
          if (length(coord_in_fileselector[[i_coord]][[1]]) == dims[i_coord]) {
            if (remove_attrs_coords) {
              result$coords[[i_coord]] <- as.vector(coord_in_fileselector[[i_coord]][[1]])
            } else {
              result$coords[[i_coord]] <- coord_in_fileselector[[i_coord]][[1]]
            }
          } else {
            result$coords[[i_coord]] <- 1:dims[i_coord]
          }
        } else {
          print(paste0("Length of coordinate", i_coord, "in FileSelector is more than 1.")) # Can this be TRUE?
        }
      } else if (i_coord %in% VariablesCommon) { # coords in common
        coord_in_common <- attributes(object)$Variables$common[[which(i_coord == VariablesCommon)]]
        if (inherits(coord_in_common, "POSIXct")) {
          result$attrs$Dates <- coord_in_common
        }
        if (length(coord_in_common) == dims[i_coord]) {
          if (remove_attrs_coords) {
            if (inherits(coord_in_common, "POSIXct")) {
              result$coords[[i_coord]] <- coord_in_common
            } else {
              result$coords[[i_coord]] <- as.vector(coord_in_common)
            }
          } else {
            result$coords[[i_coord]] <- coord_in_common
          }
        } else {
          result$coords[[i_coord]] <- 1:dims[i_coord]
        }
      } else if (!is.null(VariablesDat)) { # coords in dat
        if (i_coord %in% VariablesDat) {
          coord_in_dat <- attributes(object)$Variables[[dat]][[which(i_coord == VariablesDat)]]
          if (inherits(coord_in_dat, "POSIXct")) {
            result$attrs$Dates <- coord_in_dat
          }
          if (length(coord_in_dat) == dims[i_coord]) {
            if (remove_attrs_coords) {
              if (inherits(coord_in_dat, "POSIXct")) {
                result$coords[[i_coord]] <- coord_in_dat
              } else {
                result$coords[[i_coord]] <- as.vector(coord_in_dat)
              }
            } else {
              result$coords[[i_coord]] <- coord_in_dat
            }
          } else {
            result$coords[[i_coord]] <- 1:dims[i_coord]
          }
        } else {
          result$coords[[i_coord]] <- 1:dims[i_coord]
        }
      } else { # missing other dims
        result$coords[[i_coord]] <- 1:dims[i_coord]
      }
    }
    # attrs
    ## Variables
    for (var_type in names(attributes(object)$Variables)) {
      if (!is.null(attributes(object)$Variables[[var_type]])) {
        for (var in names(attributes(object)$Variables[[var_type]])) {
          attr_variable <- attributes(object)$Variables[[var_type]][[var]]
          if (is.null(result$attrs$Dates)) {
            if (inherits(attr_variable, "POSIXct")) {
              result$attrs$Dates <- attr_variable
            }
          }
          result$attrs$Variable[[var]] <- attr_variable
        }
      }
    }

    ## Datasets
    if (length(names(FileSelector)) > 1) {
      # lon name
      known_lon_names <- .KnownLonNames()
      lon_name_dat <- names(dims)[which(names(dims) %in% known_lon_names)]
      # lat name
      known_lat_names <- .KnownLatNames()
      lat_name_dat <- names(dims)[which(names(dims) %in% known_lat_names)]
      result$attrs$Datasets <- names(FileSelector)
      for (i in 2:length(names(FileSelector))) {
        if (!is.null(lon_name_dat)) {
          if (any(result$coords[[lon_name_dat]] != as.vector(attributes(object)$Variables[[names(FileSelector)[i]]][[lon_name_dat]]))) {
            warning("'lon' values are different for different datasets. Only values from the first will be used.")
          }
        }
        if (!is.null(lat_name_dat)) {
          if (any(result$coords[[lat_name_dat]] != as.vector(attributes(object)$Variables[[names(FileSelector)[i]]][[lat_name_dat]]))) {
            warning("'lat' values are different for different datasets. Only values from the first will be used.")
          }
        }
      }
    } else {
      result$attrs$Datasets <- names(FileSelector)
    }

    ## when
    result$attrs$when <- Sys.time()

    ## source_files
    result$attrs$source_files <- attributes(object)$Files

    ## load_parameters
    result$attrs$load_parameters <- attributes(object)$FileSelectors
    
    ## remove NULL values
    if (isTRUE(remove_null)) {
      result <- .rmNullObs(result)
    }
    class(result) <- 's2dv_cube'
  } else {
    stop("The class of parameter 'object' is not implemented",
         " to be converted into 's2dv_cube' class yet.")
  }
  return(result)
}