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





example(Corr)

source("..R/PlotTimeSeries.R")
source("AtomicToArray.R")


#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"
endDates <- c("19901001", "19951001", "20001001", "20051001", "2010101")

attr(forecast, "Dataset") <- list(Dataset = "experimentA", Realization = sampleData$Datasets$exp$experiment$Members, 
                                  Variable = list(Variable = "Surface Temperature", hasCfname = "tos", withUnits = "K"),
                                  TemporalPeriod = list(hasStartyear = startDates,
                                               hasEndyear = endDates,
                                               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 = startDates,
                                                   hasEndyear = endDates,
                                                   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)))


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

forecast_anomaly <- smooth_ano_exp
class(forecast_anomaly) <- "multiCDM"
attr(forecast_anomaly, "Step") <- list(class = "Transform", type = "Anomaly", attribute = NULL)
attr(forecast_anomaly, "Dataset") <- attr(forecast, "Dataset")
attr(forecast_anomaly, "Dataset")$Dataset <- "Exp A"
attr(forecast_anomaly, "dimensionNames") <- c("dataset", "member", "sdate", "ftime")
attr(forecast_anomaly, "Command") <- list(generatedbyCommand = "Ano", fromPackage = "s2dverification",
                                  packageVersion = packageVersion("s2dverification"),
                                  withForecast = attr(forecast, "Dataset")$Dataset, 
                                  withReference = attr(reference, "Dataset")$Dataset
                                )
attr(forecast_anomaly, "Dataset")$Variable$hasVariable <- NULL


attr(forecast_anomaly, "hasNext")  <- "Correlation with reference"         

reference_anomaly <- smooth_ano_obs
class(reference_anomaly) <- "multiCDM"
attr(reference_anomaly, "Step") <- list(class = "Transform", type = "Anomaly", attribute = NULL)

attr(reference_anomaly, "dimensionNames") <- c("dataset", "member", "sdate", "ftime")
attr(reference_anomaly, "Dataset") <- attr(reference, "Dataset")

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

anomaly_plot <- PlotTimeSeries(forecast_anomaly) + PlotTimeSeries(reference_anomaly, add= T, 
                                                                  colour_along = NA, mean_along = NA)

# Create an object containing the Verification measure "correlation"
# and plot it
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


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

attr(s2dv, "Step") <- list(class = "Verification", type = "Correlation", attribute = "association")
attr(s2dv, "Dataset")$Variable = list(Variable = attr(forecast, "Dataset")$Variable$Variable, hasCfname = "tos", withUnits = NULL, hasVariable = NULL)
s2dvoutcome <- PlotTimeSeries(s2dv, test_identifier = "corr", interval_type = "line")


RMS <- s2dverification:::.RMS
s2dv <- AtomicToArray(forecast_anomaly, reference_anomaly, AtomicFun = "RMS", time_dim = 3, members_dim = 2, 
                      chunk_dim = 4, meanexp = FALSE, meanobs = TRUE, metadata = TRUE)

attr(s2dv, "Step") <- list(class = "Verification", type = "RMSE", attribute = "agreement")
attr(s2dv, "Dataset")$Variable = list(Variable = attr(forecast, "Dataset")$Variable$Variable, hasCfname = "tos", withUnits = NULL, hasVariable = NULL)
s2dvoutcome <- PlotTimeSeries(s2dv, test_identifier = "rms", interval_type = "line", file_name = "multiCDM_rms.eps", file_width = 9)



#Try the same function from  the easyVerification package
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

library(easyVerification)
EnsCorr <- easyVerification:::EnsCorr

easyV <- AtomicToArray(forecast_anomaly, reference_anomaly, AtomicFun = "EnsCorr", time_dim = 3, members_dim = 2, 
                      chunk_dim = 4, meanexp = FALSE, meanobs = TRUE, metadata = TRUE)
attr(easyV, "dimensionNames") <- c("dataset", "test", "ftime")
attr(easyV, "dimnames")[2] <- "corr"


easyvoutcome <- PlotTimeSeries(easyV, test_identifier = "corr")




