ArrayToMultiCDM <- function(var) {
  x <- var
  class(x) <- "multiCDM"
  attr(x, "additionalAttributes") <- attributes(x)
  attr(x, "Dataset") <- list(Dataset = NULL, Realization = NULL, 
                             Variable = list(Variable = NULL, hasCfname = NULL, withUnits = NULL),
                             TemporalPeriod = list(hasStartyear = NULL,
                                                   hasEndyear = NULL,
                                                   hasMonth = NULL),
                             TemporalInstant = list(hasInitDate = NULL),
                             SpatialExtent = list(lon = NULL, lat = NULL))
  
  attr(x, "Step") <- list(class = NULL, type = NULL, attribute = NULL)
  attr(x, "dimensionNames") <- names(dim(x))
  attr(x, "Command") <- list(generatedbyCommand = NULL, fromPackage = NULL,
                             packageVersion = NULL, withForecast = NULL, 
                             withReference = NULL)
  attr(x, "hasNext") <- NULL
  invisible(x)
}


AttachMetadata <- function(data) {
  if (class(data) != "multiCDM") {
  #Get the experimental dataset and convert it to netcdf
    x <- ArrayToMultiCDM(data$mod)
  }
  attr(x, "Dataset") <- list(Dataset = attributes(data$Datasets$exp)$names, Realization = NULL, 
                                    Variable = list(Variable = attr(data$Variable, "longname"), hasCfname = data$Variable$varName, withUnits = attr(data$Variable, "units")),
                                    TemporalPeriod = list(hasStartyear = sdates,
                                                          hasMonth = substr(sdates[1], 5, 6)),
                                    TemporalInstant = list(hasInitDate = sdates[1]),
                                    SpatialExtent = list(lon = as.vector(data$lon), lat = as.vector(data$lat)))
  #Get the experimental dataset and convert it to netcdf
  y <- ArrayToMultiCDM(data$obs)
  
attr(y, "Dataset") <- list(Dataset = attributes(data$Datasets$obs)$names, Realization = NULL, 
                           Variable = list(Variable = attr(data$Variable, "longname"), hasCfname = data$Variable$varName, withUnits = attr(data$Variable, "units")),
                           TemporalPeriod = list(hasStartyear = sdates,
                                                 hasMonth = substr(sdates[1], 5, 6)),
                           TemporalInstant = list(hasInitDate = sdates[1]),
                           SpatialExtent = list(lon = as.vector(data$lon), lat = as.vector(data$lat)))
  invisible(result <- list(mod = x, obs = y))
}


