# Analogs AEMET with Trained Predictors

Description

```
library(CSTools)
library(zeallot)
library(multiApply)
library(s2dverification)
```

Load data


```
startdates <- '20161101'
grid <- 'r360x180'
era5_psl %<-% CST_Load(var = 'psl',
                 obs = 'era5', 
                 sdates =  startdates,
                 latmin = 40, latmax = 42, lonmin = -6, lonmax = -2,
                 output = 'lonlat', storefreq = "daily",  nprocs = 1, grid = grid,
                 configfile = "/esarchive/scratch/nperez/BSC.conf")
era5_tas %<-% CST_Load(var = 'tas',
                 obs = 'era5',
                 sdates =  startdates,
                 latmin = 40, latmax = 42, lonmin = -6, lonmax = -2,
                 output = 'lonlat', storefreq = "daily",  nprocs = 1, grid = grid, 
                 configfile = "/esarchive/scratch/nperez/BSC.conf")


sys4_psl %<-% CST_Load(var = 'psl',
                 exp = 'system4_m1',
                 sdates =  c('20151101','20161101', '20171101'), leadtimemax = 20,
                 latmin = 40, latmax = 42, lonmin = -6, lonmax = -2,
                 output = 'lonlat', storefreq = "daily",  nprocs = 1, grid = grid,
                 configfile = "/esarchive/scratch/nperez/BSC.conf")
sys4_tas %<-% CST_Load(var = 'tas',
                 exp = 'system4_m1',
                 sdates =  '20161101', leadtimemax = 20,
                 latmin = 40, latmax = 42, lonmin = -6, lonmax = -2,
                 output = 'lonlat', storefreq = "daily",  nprocs = 1, grid = grid,
                 configfile = "/esarchive/scratch/nperez/BSC.conf")

class(era5_psl)
class(era5_tas)
class(sys4_psl)
class(sys4_tas)

dim(era5_psl$data)
dim(era5_tas$data)
dim(sys4_psl$data)
dim(sys4_tas$data)

data <- 1:(360*180*5*30*7)
dim(data) <- c(dataset = 1, member = 7, sdate = 5, ftime = 30, lat = 180, lon = 360)
lat <- -89.5:89.5
lon <- 1:360
objeto <- list(data = data, lat = lat, lon = lon)
class(objeto) <- 's2dv_cube'


#save(era5_psl, era5_tas, sys4_psl, sys4_tas, file = './psl_tas_era5_sys4.RData')
# To load the RData file:
# load(file = './psl_tas_era5_sys4.RData')

# load the HR_observation from AEMET in NetCdf format
# We had problems with the rotated pole grid!!!!!! 
#canPath <- '/home/Earth/nperez/Downloads/sfcan$START_DATE$*_rot_mask.nc'

#hr_obs %<-% CST_i
#      Load(var = 'precipitation',
#                 exp = list(list(path = canPath)),
#                 sdates =  '19800101',
##                 latmin = 20, latmax = 22.5, lonmin = -2.5, lonmax = 2.5,
#                 output = 'lonlat', storefreq = "daily",  nprocs = 1) 

# el read.table nos devuelve un data.frame
hr_obs <- read.table('/home/Earth/nperez/Downloads/file.txt',
                     header = TRUE, 
	             sep = '\t')
hr_obs_master <- read.table('/home/Earth/nperez/Downloads/file.txt',
                     header = TRUE, 
                     sep = '\t')    



AnalogsPredictors(exp = list(sys4_psl$data, sys4_tas$data, sys4_psl$data), 
                  obs = list(era5_psl$data, era5_tas$data, era5_psl$data), 
                  obs_lat = era5_psl$lat, obs_lon = era5_psl$lon,
                  var_name = 'prec')
dyn.load(".so")
source("./R/CST_AnalogsPredictors.R")
