# 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 in the following practice

## 1. Define longitude and latitude
```r
  repos <- "/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?

4. Now, change
        -  `latitude = values(list(lat.min, lat.max))` to `latitude = 'all'`
        -  `longitude = values(list(lon.min, lon.max))` to `longitude = 'all'`

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
               )
```
5. Run the above script. Check the dimensions and the values of longitude and latitude.

6. Now, change
        -  `grid = 'r360x181'` to `grid = '/esarchive/exp/ecmwf/system5c3s/monthly_mean/tas_f6h/tas_20170101.nc'`

Check the dimensions again. Is it different from the original script?

