CST_SaveNC <- function(data, destination, additional = NULL, add_attributes = NULL) {
  if (!inherits(data, 's2dv_cube')) {
    stop("Parameter 'data' must be of the class 's2dv_cube', ",
         "as output by CSTools::CST_Load.")
  }
  if (!is.array(additional) & !is.null(additional)) {
    if (!is.list(additional) | !all(unlist(lapply(additional, is.array)))) {
      stop("Parameter 'additional' must be an array or list of arrays.")
    }
  }
  if (!is.character(destination) & length(destination) > 1) {
    stop("Parameter 'destination' must be a character string of one element indicating the name of the file (including the folder if needed) where the data will be saved.")
  }
  #check add_attributes
  arrays <- list()
  toattr <- list()
  if (is.list(data)) {
    j = 1
    k = 1
    lnames <- names(data)
    subnames <- NULL
    for (i in 1 : length(data)) {
      if (!is.null(data[[i]])) {
        if (!is.array(data[[i]])) {
          # list
          if (is.list(data[[i]])) {
            subnames <- names(data[[i]])
            data[[i]] <- do.call("c", data[[i]])
          }
          # numeric or logical vector
          if (is.vector(data[[i]]) & !is.character(data[[i]])) {
            object <- data[[i]]
            dim(object) <- c(length(object))
            data[[i]] <- object
          } else if (is.data.frame(data[[i]])) {
            data[[i]] <- data.matrix(data[[i]])
          } else if (is.factor(data[[i]])) {
            object <- as.character(data[[i]])
            dim(object) <- c(length(object))
          } else if (is.POSIXct(data[[i]]) | is.POSIXlt(data[[i]])) {
          } else if (!is.character(data[[i]])) {
            stop(paste("Convert element", i, "of parameter data to array."))
          }
        }
        if (is.numeric(data[[i]]) | is.logical(data[[i]])) {
          arrays[[j]] <- data[[i]]
          names(arrays)[[j]] <- lnames[[i]]
         # if (length(attributes(data[[i]])) > 0) {
        #    metadata <- list(data = attributes(data[[i]]))
        #    attr(arrays[[j]], 'variables') <- metadata
        #  }
          j = j +1
        } else {
          array_true <- TRUE
          dim(array_true) <- 1
          #names(dim(array_true)) <- paste0("Dim_", j)
          names(dim(array_true)) <- lnames[[i]]
          arrays[[j]] <- array_true
          #metadata <- as.list(data[[i]])
          #metadata <- lapply(metadata, function(x){names(x) <- paste0("pos_", x)})
          #metadata <- list(data = list(a = c(data[[i]])))
          #metadata2 <- list(a = list(data[[i]]))
          #names(metadata2) <- paste0("Pos_", 1 : length(metadata2))
          
    #      metadata <- list(data = list(a = data[[i]])) #casibueno
    
          metadata2 <- as.list(data[[i]])
          if (!is.null(subnames) & length(subnames) == length(metadata2)) {
                names(metadata2) <- subnames
          } else {
                names(metadata2) <- paste0("Pos_", 1 : length(metadata2))    
          }
          metadata <- list(data = metadata2) 
          names(metadata) <- lnames[[i]]
          attr(arrays[[j]], 'variables') <- metadata
          j = j +1
        }
      }
    }
  } else if (is.array(data)) {
    if (is.numeric(data) | is.logical(data)) {
      arrays <- data
    }
    else {
      stop("The parameter 'data' must be one or a list of numeric or logical arrays.")
    }
  } else {
    stop("The parameter 'data' must be one or a list of numeric or logical arrays.")
  }
  #if (length(toattr) > 0 | !is.null(add_attributes)) {
  #    toattr[[k]] <-  add_attributes
  #    attr(arrays, 'variables') <- toattr
  #}
  ArrayToNc(arrays, file_path = destination)
}


## BASICS
destination <- "./MEDSCOPE/DataSaved1.nc"
additional = NULL
add_attributes = NULL
data <- 1 : 10 
dim(data) <- c(2, 5)
attr(data, 'class') <- 's2dv_cube'
CST_SaveNC(data, destination)

## BASICS
obj1 <- 1 : 10 
dim(obj1) <- c(a = 2, b = 5)
data <- list(obj1, obj1)
attr(data, 'class') <- 's2dv_cube'
CST_SaveNC(data, destination)

## BASICS
obj1 <- 1 : 10 
dim(obj1) <- c(aa = 2, bb = 5)
data <- list(rr = obj1, tt = obj1, xx = obj1)
attr(data, 'class') <- 's2dv_cube'
CST_SaveNC(data, destination)
#ArrayToNc(data, destination)

## BASICS
obj1 <- 1 : 10 
dim(obj1) <- c(aa = 2, bb = 5)
data <- list(1 : 10, obj1, obj1)
attr(data, 'class') <- 's2dv_cube'
#ArrayToNc(data, destination)
CST_SaveNC(data, destination)

## BASICS
obj1 <- 1 : 10 
dim(obj1) <- c(aa = 2, bb = 5)
data <- list(rr = as.character(obj1), tt = obj1, xx = obj1)
attr(data, 'class') <- 's2dv_cube'
CST_SaveNC(data, destination)


temperature <- array(rnorm(100 * 50 * 10), dim = c(100, 50, 10)) 
names(dim(temperature)) <- c('longitude', 'latitude', 'time') 
#ArrayToNc(list(temperature = temperature, temperature2 = temperature), file_path = './MEDSCOPE/example.nc')
data <- list(temperature = temperature, temperature2 = temperature)
attr(data, 'class') <- 's2dv_cube'
CST_SaveNC(data, destination)
data <- list(temperature = temperature) #falta nombre 
attr(data, 'class') <- 's2dv_cube'
CST_SaveNC(data, destination)


# WORKS:
library(easyNCDF)
sic_nohole <- array(rnorm(360*180*460), dim = c(lat = 180, lon = 360, time = 460))
lon <- as.numeric(0:359)
lat <- as.numeric(seq(-90, 90, length.out = 180))

names(dim(sic_nohole)) <- c('lat', 'lon','time')
metadata <- list(sic_nohole = list(units = '1'))
attr(sic_nohole, 'variables') <- metadata

dim(lon) <- length(lon)
names(dim(lon)) <- 'lon'
metadata <- list(lon = list(units = 'degrees_east'))
attr(lon, 'variables') <- metadata

dim(lat) <- length(lat)
names(dim(lat)) <- 'lat'
metadata <- list(lat = list(units = 'degrees_north'))
attr(lat, 'variables') <- metadata

#ArrayToNc(list(sic_nohole, lon, lat), './MEDSCOPE/tmp.nc')

datos <- list(sic_nohole, lon, lat)
attr(datos, 'class') <- 's2dv_cube'
CST_SaveNC(datos, './MEDSCOPE/tmp.nc')

attr(lonlat_prec, "class") <- 's2dv_cube'
CST_SaveNC(lonlat_prec, destination)
data <- lonlat_prec
