#R script for generating multiCDM class, with correlation object
library(s2dverification)
example(Corr)
library(easyVerification)

#Define the basic multiCDM class
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Create forecast and observation objects belonging to the multiCDM class
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  
forecast <- sampleData$mod
class(forecast) <- "multiCDM"
attr(forecast, "Step") <-"RawData"
  attr(forecast, "Dataset") <- list(Dataset = "experimentA", Realization = sampleData$Datasets$exp$experiment$Members, 
                           Variable = list(hasCfname = "tos", withUnits = "k"),
                         TemporalPeriod = list(hasStartyear = sampleData$Dates$start,
                                               hasEndyear = sampleData$Dates$end,
                                               hasMonth = substr(startDates[1], 5, 6)),
                         TemporalInstant = list(hasInitDate = sampleData$Datasets$exp$experiment$InitializationDates[1]),
                          SpatialExtent = list(lon = as.vector(sampleData$lon), lat = as.vector(sampleData$lat)))

reference <- sampleData$obs
class(reference) <- "multiCDM"
attr(reference, "Step") <-"RawData"
attr(reference, "Dataset") <- list(Dataset = "obsA", Realization = "members?", 
                             Variable = list(hasCfname = "tos", withUnits = "k"),
                             TemporalPeriod = list(hasStartyear = substr(startDates[1], 1, 4),
                                               hasEndyear = substr(rev(startDates)[1], 1, 4),
                                               hasmonth = substr(startDates[1], 5, 6)),
                             SpatialExtent = list(lon = as.vector(sampleData$lon), lat = as.vector(sampleData$lat)))


#Calculate the anomalies
#~~~~~~~~~~~~~~~~~~~~~~~

forecast_anomaly <- ano_exp
class(forecast_anomaly) <- "multiCDM"
attr(forecast_anomaly, "Step") <- "Transform"
attr(forecast_anomaly, "Dataset") <- attr(forecast, "Dataset")
attr(forecast_anomaly, "Dataset")$Dataset <- "Exp A anomaly"

attr(forecast, "Command") <- list(generatedbyCommand = "Ano", fromPackage = "s2dverification",
                                  packageVersion = packageVersion("s2dverification"),
                                  withForecast = attr(forecast, "Dataset")$Dataset, 
                                  withReference = attr(reference, "Dataset")$Dataset
                                )
attr(forecast_anomaly, "hasNext")  <- "Correlation with reference"         

reference_anomaly <- ano_obs
class(reference_anomaly) <- "multiCDM"
attr(reference_anomaly, "Step") <-"Transform"
attr(reference_anomaly, "Dataset")$Dataset <- "Obs A anomaly"
attr(reference, "Command") <- list(generatedbyCommand = "Ano", fromPackage = "s2dverification",
                                        packageVersion = packageVersion("s2dverification"),
                                        withForecast = NA, 
                                        withReference = attr(reference, "Dataset")$Dataset 
                                )
attr(reference_anomaly, "hasNext")  <- "Correlation with forecast"                    

# Create an object containing the Verification measure "correlation"
# Note: The "clim" multiCDM is being ignored atm
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
source("git/s2dverification/multiCDM/AtomicToArray.R")

Corr <- s2dverification:::.Corr
verification_object <- AtomicToArray(forecast_anomaly, reference_anomaly, AtomicFun = "Corr", time_dim = 3, members_dim = 2, 
                      chunk_dim = 4, meanexp = F, meanobs = T, metadata = T)

attr(verification_object, "Dataset")$Variable$hasVariable <- "Correlation between expA and obsA  monthly mean surface temperature"
attr(verification_object, "dimensionNames") <- c("dataset", "test", "ftime")
attr(verification_object,"dimnames")[[2]]   <- c("test", "p_val", "conf_low", "conf_high")

#Create an Outcome containing the ggplot object and corresponding metadata
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

source("git/s2dverification/R/PlotTimeSeries.R")
outcome <- PlotTimeSeries(verification_object)


