# cleaning:
rm(list=ls())

# load CSTools:
library(CSTools)

# define a path to SEAS5:
seas5_path <- list(list(name = 'SEAS5',
                        path = paste0("/esarchive/exp/ecmwf/system5c3s/",
                                      "$STORE_FREQ$_mean/$VAR_NAME$_f6h/",
                                      "$VAR_NAME$_$START_DATE$.nc")))

seas5 <- CST_Load(var = 'tas',
                  exp = seas5_path, obs = NULL,
                  sdates = c('20100501', '20110501', '20120501'),
                  leadtimemin = 2, leadtimemax = 4,
                  storefreq = 'monthly',
                  lonmin = -17.5, lonmax = 40,
                  latmin = 20, latmax = 52.5,
                  output = 'lonlat') 

# object class
class(seas5)

# elements names
names(seas5)

# variable and units:
seas5$Variable

# array dimensions (names and length):
dim(seas5$data)

# subset lon:
seas5$lon

# subset lat:
seas5$lat

# monthly dates:
seas5$Dates


# plot a map
library(s2dv)
dim(seas5$data)
?PlotEquiMap
PlotEquiMap(seas5$data[1, 1, 1, 1, , ],
            lon = as.vector(seas5$lon),
            lat = as.vector(seas5$lat),
            filled.cont = FALSE)

## By yourself: Plot a different forecast time

# plot a time series for a grid point
# e.g.: 22 N - 20E
which(seas5$lat == 22)
which(seas5$lon == 20)

plot(c(6,7,8), seas5$data[1, 1, 1, , 31, 21], type = 'l')

# Plot the PDF
PlotForecastPDF(seas5$data[1, , , 1, 31, 21])

# read the error and find a solution:
seas5_pdf <- seas5$data[1,,,1,31,21]
names(dim(seas5_pdf)) <- c('members', 'sdate')
PlotForecastPDF(seas5_pdf)

PlotForecastPDF(seas5_pdf, c(302, 304))

PlotForecastPDF(seas5_pdf, tercile.limits = c(302, 304),
                title = 'June forecast', var.name = "tas (K)",
                fcst.names = c(2010, 2011, 2012))  

# We need the observations to calculate the tercile limits and add the 
# observation to the plot.


### --------------- Use another library to load the data ----- #

library(ncdf4)

files <- list(
 file1 = "/esarchive/exp/ecmwf/system5c3s/monthly_mean/tas_f6h/tas_20100501.nc",
 file2 = "/esarchive/exp/ecmwf/system5c3s/monthly_mean/tas_f6h/tas_20110501.nc",
 file3 = "/esarchive/exp/ecmwf/system5c3s/monthly_mean/tas_f6h/tas_20120501.nc")

# Explore:
file <- nc_open(files[[1]])
file
ncatt_get(file, 'latitude')
ncvar_get(file, 'latitude')
lats <- ncvar_get(file, 'latitude')
# look for the positions manually or automatically
which(lats >= 20 & lats <= 52.5)
pos_lats <- which(lats >= 20 & lats <= 52.5) 

ncatt_get(file, 'longitude')
ncvar_get(file, 'longitude')
lons <- ncvar_get(file, 'longitude')
# look for the positions manually or automatically
which(lons >= -17.5 & lons <= 40) 
## WRONG!!!!
# try again
which(lons >= 360-17.5 | lons <= 40)
pos_lons <- which(lons >= 360-17.5 | lons <= 40)

?ncvar_get

tas <- ncvar_get(file, 'tas')
dim(tas_bm)
# lon, lat, member, ftime
tas <- tas[pos_lons, pos_lats, , 2:4]
dim(tas)
names(dim(tas)) <- c("longitude", "latitude", "member", "ftime")

nc_close(file)

## Repeat for all files:
tas_data <- NULL
for (i in 1:3) {
  file <- nc_open(files[[i]])
  tas <- ncvar_get(file, 'tas')
  tas <- tas[pos_lons, pos_lats, , 2:4]
  tas_data <- abind::abind(tas_data, tas, along = 5)
}
names(dim(tas_data)) <- c("longitude", "latitude", "member", "ftime", "sdate")

# Do we have metadata?
# Would we need to repeat this for a reference dataset?
# Would the reference dataset be in the same grid?
# If grids are different, an interpolation is required
# CST_Load does it on the fly
# Let's load two models and reference


# To run in a different terminal: 
## cd /esarchive/exp/cmcc/system35c3s/monthly_mean/tas_f6h
## ncdump -v lat tas_20110501.nc

## cd /esarchive/exp/ecmwf/system5c3s/monthly_mean/tas_f6h
## ncdump -v latitude tas_20110501.nc

## THERE IS A SHIFT OF HALF DEGREE!!

# load CSTools:
library(CSTools)
library(zeallot)

# define a path to SEAS5:
paths <- list(list(name = 'SEAS5',
                   path = paste0("/esarchive/exp/ecmwf/system5c3s/",
                                 "$STORE_FREQ$_mean/$VAR_NAME$_f6h/",
                                 "$VAR_NAME$_$START_DATE$.nc")), 
              list(name = 'CMCCS3',
                   path = paste0("/esarchive/exp/cmcc/system3c3s/",
                                 "$STORE_FREQ$_mean/$VAR_NAME$_f6h/",
                                 "$VAR_NAME$_$START_DATE$.nc")))

obs_path <- list(list(name = 'ERA5',
                      path = paste0("/esarchive/recon/ecmwf/era5/",
                                    "$STORE_FREQ$_mean/$VAR_NAME$_f1h/",
                                    "$VAR_NAME$_$YEAR$$MONTH$.nc")))
c(exp, obs) %<-% CST_Load(var = 'tas',
                          exp = paths, obs = obs_path,
                          sdates = c('20100501', '20110501', '20120501'),
                          leadtimemin = 2, leadtimemax = 4,
                          storefreq = 'monthly',
                          lonmin = -17.5, lonmax = 40,
                          latmin = 20, latmax = 52.5,
                          output = 'lonlat')

names(exp)
dim(exp$data)
dim(obs$data)

library(s2dv)
dim(exp$data)

PlotLayout(PlotEquiMap, 
           plot_dims = c('lat', 'lon'),
           var = exp$data[,1,1,1,,],
           lon = as.vector(exp$lon),
           lat = as.vector(exp$lat),
           #brks = seq(-1, 1, 0.2),
           filled.continents = FALSE, sizetit = NULL,
           toptitle = 'TWO MODELS',
           title_scale = 0.7,
           titles = c('SEAS5', 'CMCCS3'), bar_scale = 0.8)

PlotLayout(PlotEquiMap,
           plot_dims = c('lat', 'lon'),
           var = exp$data[,1:3,1,1,,],
           lon = as.vector(exp$lon),
           lat = as.vector(exp$lat),
           ncol = 2,
           nrow = 3, 
           filled.continents = FALSE, sizetit = NULL,
           toptitle = 'TWO MODELS',
           title_scale = 0.7,
           col_titles = c('SEAS5', 'CMCCS3'), bar_scale = 0.8)


c(exp, obs) %<-% CST_Load(var = 'tas',
                          exp = paths, obs = obs_path,
                          sdates = c('20100501', '20110501', '20120501'),
                          leadtimemin = 2, leadtimemax = 4,
                          storefreq = 'monthly',
                          lonmin = -17.5, lonmax = 40,
                          latmin = 20, latmax = 52.5,
                          output = 'lonlat',
                          grid = "r180x90")

PlotLayout(PlotEquiMap,
           plot_dims = c('lat', 'lon'),
           var = exp$data[,1:3,1,1,,],
           lon = as.vector(exp$lon),
           lat = as.vector(exp$lat),
           ncol = 2,
           nrow = 3, 
           filled.continents = FALSE, sizetit = NULL,
           toptitle = 'TWO MODELS',
           title_scale = 0.7,
           col_titles = c('SEAS5', 'CMCCS3'), bar_scale = 0.8)

