#R script for generating multiCDM class, with correlation object
library(s2dverification)
library(easyVerification)
library(s2dverification)
data_path <- system.file('sample_data', package = 's2dverification')
expA <- list(name = 'experiment', path = file.path(data_path, 
                                                   'model/$EXP_NAME$/$STORE_FREQ$_mean/$VAR_NAME$_3hourly',
                                                   '$VAR_NAME$_$START_DATE$.nc'))
obsX <- list(name = 'observation', path = file.path(data_path,
                                                    '$OBS_NAME$/$STORE_FREQ$_mean/$VAR_NAME$',
                                                    '$VAR_NAME$_$YEAR$$MONTH$.nc'))

# Now we are ready to use Load().
startDates <- c('19851101', '19901101', '19951101', '20001101', '20051101')
sampleData <- Load('tos', list(expA), list(obsX), startDates,
                   output = 'lonlat', latmin = 27, latmax = 48, 
                   lonmin = -12, lonmax = 40)



# Doing example calculations
clim <- Clim(sampleData$mod, sampleData$obs)
ano_exp <- Ano(sampleData$mod, clim$clim_exp)
ano_exp <- Smoothing(ano_exp)
ano_obs <- Ano(sampleData$obs, clim$clim_obs)
ano_obs <- Smoothing(ano_obs)

#Define the basic multiCDM class
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
setClass("multiCDM", representation(data = "array", Dataset = "list", Transform = "list",
                                    Calibration = "list", Verification = "list", Outcome = "list",
                                    hasNext = "character"), 
         prototype(data = array(0), 
                   Dataset =  NULL,
                   Transform = NULL,
                   Calibration = NULL,
                   Verification = NULL,
                   Outcome = NULL,
                   hasNext = NULL))

#Create forecast and observation objects belonging to the multiCDM class
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

forecast <- new("multiCDM")
forecast@data <- sampleData$mod
forecast@Dataset <- list(Dataset = "experimentA", Realization = sampleData$Datasets$exp$experiment$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)),
                         TemporalInstant = list(hasInitMonth = substr(startDates[1], 5, 6), 
                                                hasInitYear = substr(startDates[], 1, 4)),
                          SpatialExtent = list(lon = as.vector(sampleData$lon), lat = as.vector(sampleData$lat)))

reference <- new("multiCDM")
reference@data <- sampleData$mod
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 <- new("multiCDM")
forecast_anomaly@data <- ano_exp
forecast_anomaly@Dataset <- forecast@Dataset
forecast_anomaly@Dataset$Dataset <- "Exp A anomaly"
forecast@Transform <- list(Command = list(generatedbyCommand = "Ano", fromPackage = "s2dverification",
                                               packageVersion = packageVersion("s2dverification")),
                                withForecast = forecast@Dataset$Dataset, 
                                withReference = reference@Dataset$Dataset, 
                                withAspect = "association")
forecast_anomaly@hasNext  <- "Correlation with reference"         

reference_anomaly <- new("multiCDM")
reference_anomaly@data <- ano_exp
reference_anomaly@Dataset <- forecast@Dataset
reference_anomaly@Dataset$Dataset <- "Obs A anomaly"
reference@Transform <- list(Command = list(generatedbyCommand = "Ano", fromPackage = "s2dverification",
                                               packageVersion = packageVersion("s2dverification")),
                                withForecast = NA, 
                                withReference = reference@Dataset$Dataset, 
                                withAspect = "association")
reference_anomaly@hasNext  <- "Correlation with forecast"                    


# Create an object containing the Verification measure "correlation"
# Note: The "clim" multiCDM is being ignored atm
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~4
example <- new("multiCDM")                              
example@data <- corr
example@Dataset <-  reference_anomaly@Dataset
      
example@Verification <- list(Command = list(generatedbyCommand = "Corr", fromPackage = "s2dverification",
                                            packageVersion = packageVersion("s2dverification")),
                             withForecast = forecast_anomaly@Dataset$Dataset, 
                             withReference = reference_anomaly@Dataset$Dataset, 
                             withAspect = "association")
example@hasNext  <- "PlotTimeSeries"                           

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

source("git/s2dverification/R/PlotTimeSeries.R")



