# CST_Save
library(CSTools)
ls("package:CSTools")
str(areave_data) # doesn't follow CST format
str(lonlat_data) # doesn't follow CST format
str(lonlat_prec) 

library(easyNCDF)
library(lubridate)
destination <- "./MEDSCOPE/DataSaved1.nc"
CST_SaveNC <- function(data, destination, additional = 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.")
  }
  if (any(sapply(data, is.null))) {
    warning("NULL elements on parameter 'data' aren't included in the file.")
    data <- data[-which(sapply(data, is.null))]
  }
  arrays <- lapply(data, sapply, correct_type)
  if (!is.null(additional)) {
    arrays <- list(arrays, additional)
  }
  ArrayToNc(arrays, file_path = destination)
}


data <- list(a = 1:10, b = NULL, c = list())



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

additional <- 1:10
dim(additional) <- c(a = length(additional))
additional <- list(a = 1:10, b=NULL, c =1:3)
additional <- list(a = 1:10, b=NULL, c =lonlat_prec$lon)
additional <- list(a = lonlat_prec$lon, b=lonlat_prec$lon, c =lonlat_prec$lon)
getwd()
ArrayToNc(lonlat_prec[[2]], file_path = destination)
is.list(lonlat_prec)
str(lonlat_prec)
is.array(lonlat_prec$data)
lapply(lonlat_prec, function(x){is.array(x)})
lapply(lonlat_prec, function(x){is.vector(x)})
class(lonlat_prec$lon)
lapply(lonlat_prec, correct_type)

object <- 1:10
object <- data.frame(x = 1 : 10, y = 2 : 11).
object <- c("North", "East", "South", "South")
object <- factor(object)

object <- c(13.2, 12.4, 13.2, 7.333)
object <- factor(object)
c <- 1:10
dim(c) <- c(2,5)
object <- list(a <- 1:10, b <- object, c = c, d =list( 1:10, "a"))


#correct_type <- function(object) {
#  if (is.list(object)) {
#    object <- lapply(object, .correct_type)
#  } else {
#    object <- .correct_type(object)
#  }
#  return(object)
#}

lapply(data, sapply, correct_type)

correct_type <- function(object) {
  print(names(head(object)[1]))
  if (!is.null(object)) {
    if (!is.array(object)) {
      if (is.vector(object) | is.POSIXt(object) | is.POSIXct(object) | is.POSIXlt(object)) {
        dim(object) <- c(length(object))
        print(1)
      } else if (is.data.frame(object)) {
        object <- data.matrix(object)
        print(3)
      } else if (is.factor(object)) {
        object <- as.character(object)
        dim(object) <- c(length(object))
        print(4)
      } else {
        stop("The object cannot be converted to array.")
      }
    }
    return(object)
  }
}
object <- correct_type(object)
is.array(object)

length(object)
object <- lonlat_prec[[2]]
object <- correct_type(object)
obj <- correct_type(lonlat_prec[[1]])
is.array(obj)
