# Hands-on 1: Load data by startR

## Goal
Use startR to load the data and learn how to adjust data while loading data.

## 0. Load required packages

```r
# Clean the session
rm(list = ls())

library(startR)
```
## 1. Load data from data repository (esarchive/)

#TODO: update it
**Data description**:  
This sample data set contains a small cutout of gridded seasonal precipitation 
forecast data from the Copernicus Climate Change ECMWF-System 5 forecast system.
Specifically, for the 'prlr' (precipitation) variable, for the first 6 forecast 
ensemble members, daily values, for all 31 days in March following the forecast 
starting dates in November of years 2010 to 2012, for a small 4x4 pixel cutout in 
a region in the North-Western Italian Alps (44N-47N, 6E-9E). The data resolution is 1 degree.

Use the above information to define the variable, start dates, longitude and latitude.

```r
#TODO: update the path
  # Use this one if on workstation or nord3 (have access to /esarchive)
  path_exp <- "/esarchive/exp/meteofrance/system7c3s/monthly_mean/$var$_f6h/$var$_$syear$.nc"

  # Use this one if on Marenostrum4 and log in with PATC2021 account
  path_exp <- paste0('/gpfs/scratch/nct01/nct01127/d3_R_handson/esarchive/',
                  'exp/ecmwf/system5c3s/daily_mean/',
                  '$var$_s0-24h/$var$_$sdate$.nc')

  var <- 'tas'
  sdate_hcst <- paste0(1993:2016, '1101')
  sdate_fcst <- '20201101'
  lon.min <- -20
  lon.max <- 40
  lat.min <- 20
  lat.max <- 80
```

Use Start() to load the data.

```r
  hcst <- Start(dat = path_exp,
                var = var,
                syear = sdate_hcst,
                ensemble = 'all',
                time = 1:2,
                latitude = values(list(lat.min, lat.max)),
                latitude_reorder = Sort(),
                longitude = values(list(lon.min, lon.max)),
                longitude_reorder = CircularSort(-180, 180),
                synonims = list(syear = c('syear', 'sdate'),
                                latitude = c('lat', 'latitude'),
                                longitude = c('lon', 'longitude')),
                return_vars = list(time = 'syear',
                                   longitude = NULL, latitude = NULL),
                retrieve = TRUE)
```
   
```r
  fcst <- Start(dat = path_exp,
                var = var,
                syear = sdate_fcst,
                ensemble = 'all',
                time = 1:2, 
                latitude = values(list(lat.min, lat.max)),
                latitude_reorder = Sort(),
                longitude = values(list(lon.min, lon.max)),
                longitude_reorder = CircularSort(-180, 180),
                synonims = list(syear = c('syear', 'sdate'),
                                latitude = c('lat', 'latitude'),
                                longitude = c('lon', 'longitude')),
                return_vars = list(time = 'syear',
                                   longitude = NULL, latitude = NULL),
                retrieve = TRUE)
```


# Create an date array from hcst dates
Check the time attributes of hcst: Is it correct? 

To load the corresponding obs data, we can use these time values as the selectors in obs Start() call.

```r
dim(attributes(hcst)$Variables$common$time)
syear  time
   24     2

str(attributes(hcst)$Variables$common$time)
 POSIXct[1:48], format: "1993-12-01" "1994-12-01" "1995-12-01" "1996-12-01" "1997-12-01" ...

# Adjust the day to the correct month
attributes(hcst)$Variables$common$time <- attributes(hcst)$Variables$common$time - lubridate::days(1)

date_string <- format(attributes(hcst)$Variables$common$time, '%Y%m')
sdate_obs <- array(date_string, dim = c(syear = 24, time = 2))
```

```r
  path_obs <- '/esarchive/recon/ecmwf/era5/monthly_mean/$var$_f1h-r1440x721cds/$var$_$syear$.nc'

  obs <- Start(dat = path_obs,
               var = var,
               syear = sdate_obs,
               split_multiselected_dims = TRUE,
               latitude = values(list(lat.min, lat.max)),
               latitude_reorder = Sort(),
               longitude = values(list(lon.min, lon.max)),
               longitude_reorder = CircularSort(-180, 180),
               transform = CDORemapper,
#TODO: Change to relative path
#               transform_params = list(grid = './griddes_system7c3s.txt',
               transform_params = list(grid = '/esarchive/scratch/aho/git/startR/inst/doc/tutorial/PATC2023/griddes_system7c3s.txt',
                                       method = 'bilinear'),
               transform_vars = c('latitude', 'longitude'),
               synonims = list(syear = c('syear', 'sdate'),
                               latitude = c('lat', 'latitude'),
                               longitude = c('lon', 'longitude')),
               return_vars = list(time = 'syear',
                                  longitude = NULL, latitude = NULL),
               retrieve = TRUE)
```

## 2. Check if the datasets are consistent

```r
lat_hcst <- attributes(hcst)$Variables$common$latitude
lon_hcst <- attributes(hcst)$Variables$common$longitude
lat_obs <- attributes(obs)$Variables$common$latitude
lon_obs <- attributes(obs)$Variables$common$longitude

identical(c(lat_obs), c(lat_hcst))
[1] TRUE
identical(c(lon_obs), c(lon_hcst))
[1] TRUE

time_hcst <- attributes(hcst)$Variables$common$time
time_obs <- attributes(obs)$Variables$common$time

identical(format(time_hcst, '%Y%m'), format(time_obs, '%Y%m'))
[1] TRUE
```
