# Hands-on 2: Spatial grid interpolation

## Goal
To learn how to use Start() to load the data and do the interpolation.
In this use case, we only use Start() and load the data in the local memory.
The default transformation function is startR::CDORemapper, a wrapper function of s2dv::CDORemap that uses cdo inside.

In detail, we are going to:  
(1) Learn different ways to assign longitude and latitude in Start().  
(2) Use parameter '<dimname>_reorder' to specify the sorted order.  
(3) Use transform-related parameters.  

## 1. Define longitude and latitude
```r
  # Use this one if on workstation or nord3
  repos <- "/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc"
  # Use this one if on Marenostrum4 and log in with PATC2021 account
  repos <- paste0('/gpfs/scratch/nct00/nct00016/d3_02_R-hands_on/tmp-esarchive/',
                  'exp/ecmwf/system5_m1/monthly_mean/',
                  '$var$_f6h/$var$_$sdate$.nc')
  var <- 'tas'
  sdate <- c('20170101')
  lon.min <- 0
  lon.max <- 359.9
  lat.min <- -90
  lat.max <- 90

  data <- Start(dat = repos,
                var = var,
                sdate = sdate,
                ensemble = indices(1),
                time = 'all',
                latitude = values(list(lat.min, lat.max)),
                latitude_reorder = Sort(),
                longitude = values(list(lon.min, lon.max)),
                longitude_reorder = CircularSort(0, 360),
                synonims = list(latitude = c('lat', 'latitude'),
                             longitude = c('lon', 'longitude')),
                return_vars = list(time = 'sdate',
                                   longitude = 'dat', 
                                   latitude = 'dat'),
                retrieve = TRUE
               )
```

1. Run the above script. Check the dimensions, the warning messages, and the values of 
longitude and latitude. What is the range of longitude and latitude?

2. Why 'lon.max' is 359.9 but not 360? What will happen if it is 360?

3. Now, change  
    -  `latitude_reorder = Sort()` to `latitude_reorder = Sort(decreasing = TRUE)`  
    -  `longitude_reorder = CircularSort(0, 360)` to `longitude_reorder = CircularSort(-180, 180)`  
    -  Set `lon.min <- -180` and `lon.max <- 179.9`  

Check the values of longitude and latitude again. Is it different from the original script?


## 2. Interpolation
Now, let us add in the transformation parameters.
```r
  data <- Start(dat = repos,
                var = var,
                sdate = sdate,
                ensemble = indices(1),
                time = 'all',
                latitude = values(list(lat.min, lat.max)),
                latitude_reorder = Sort(),
                longitude = values(list(lon.min, lon.max)),
                longitude_reorder = CircularSort(0, 360),
              ## transformation
                transform = CDORemapper,
                transform_extra_cells = 2,
                transform_params = list(grid = 'r360x181',
                                        method = 'conservative',
                                        crop = c(lon.min, lon.max, 
                                                 lat.min, lat.max)),
                transform_vars = c('latitude', 'longitude'),
                apply_indices_after_transform = FALSE,
              ##
                synonims = list(latitude = c('lat', 'latitude'),
                             longitude = c('lon', 'longitude')),
                return_vars = list(time = 'sdate',
                                   longitude = NULL,
                                   latitude = NULL),
                retrieve = TRUE
               )
```
4. Run the above script. Check the dimensions and the values of longitude and latitude.

