#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
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#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 <- ano_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
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

example <- corr
class(example) <- "multiCDM"
attr(example, "Step") <-"Verification"
attr(example, "Dataset") <-  attr(forecast_anomaly, "Dataset")
attr(example, "Dataset")$TemporalPeriod <- NULL

attr(example, "Command") <- list(generatedbyCommand = "Corr", fromPackage = "s2dverification",
                                 packageVersion = packageVersion("s2dverification"),
                             withForecast = attr(forecast_anomaly, "Dataset")$Dataset, 
                             withReference = attr(reference_anomaly, "Dataset")$Dataset, 
                             withAspect = "association")
attr(example, "hasNext")  <- "PlotTimeSeries"                           
attr(example, "dimensionNames") <- list("dataset", "member", "statistic", "ftime")
#Create an "Outcome" containing the ggplot object and corresponding metadata
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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



