# Hands-on Part 1
## Goal
Learn how to use the whole startR workflow to calculate skill score.
In this case, the ensemble-adjusted Continuous Ranked Probability Score (CRPS) is used.
To make the process faster, the data size used is small here, so we can run the execution on workstation.

## 1. Define data
We want to verify the experimental data by the observational data.

```r
library(startR)

# exp data
  repos_exp <- paste0('/esarchive/exp/ecmwf/system4_m1/monthly_mean/',
                      '$var$_f6h/$var$_$sdate$.nc')
  sdates_exp <- sapply(2012:2016, function(x) paste0(x, sprintf('%02d', 1:12), '01'))

  exp <- Start(dat = repos_exp,
               var = 'tas',
               sdate = sdates_exp,
               time = indices(1),
               ensemble = indices(1:2),
               latitude = 'all',
               longitude = 'all',
               synonims = list(longitude = c('lon', 'longitude'),
                               latitude = c('lat', 'latitude')),
               retrieve = F)

# obs data
  repos_obs <- paste0('/esarchive/recon/ecmwf/erainterim/monthly_mean/',
                      '$var$_f6h/$var$_$sdate$.nc')
  sdates_obs <- (sapply(2012:2016, function(x) paste0(x, sprintf('%02d', 1:12))))

  obs <- Start(dat = repos_obs,
                     var = 'tas',
                     sdate = sdates_obs,
                     time = indices(1),
                     latitude = 'all',
                     longitude = 'all',
                     synonims = list(longitude = c('lon', 'longitude'),
                                     latitude = c('lat', 'latitude')),
                     retrieve = F)
```


## 2. Define operation and workflow
Check the function by typing `?SpecsVerification::EnsCrps`. What are the inputs and outputs of this function?

```r
  func <- function(x, y) {
    # CRPS
    crps <- mean(SpecsVerification::EnsCrps(x, y, R.new = Inf))

    # RMSSS
    # exp: [sdate, member_exp]
    # obs: [sdate, member_obs]
    # obs only has [sdate] now. Add one more dim for it.
    y <- s2dv::InsertDim(y, posdim = 2, lendim = 1, name = 'ensemble')
    rmsss <- mean(s2dv:::.RMSSS(x, y, time_dim = 'sdate', memb_dim = 'ensemble', pval = FALSE)$rmsss)

    return(list(crps = crps, rmsss = rmsss))
  }
  step <- Step(func, target_dims = list(c('sdate', 'ensemble'), c('sdate')),
                     output_dims = list(crps = NULL, rmsss = NULL)) # NULL)
  wf <- AddStep(list(exp, obs), step)
```

## 3. Execute locally

```r
  res <- Compute(wf$crps,
                 chunks = list(latitude = 2,
                               longitude = 2))
```

## 4. Check the results

1. Check the list structure. What is the length of the list?
```r
 str(res)
List of 2
 $ crps : num [1, 1, 1, 1:256, 1:512] 1.51 1.55 1.58 1.6 1.55 ...
 $ rmsss: num [1, 1, 1, 1:256, 1:512] 0.989 0.989 0.988 0.988 0.988 ...
```

2. What does the dimension look like?
```r
dim(res$crps)
      dat       var      time  latitude longitude 
        1         1         1       256       512 
dim(res$rmsss)
      dat       var      time  latitude longitude
        1         1         1       256       512
```

3. Plot the map  
Use `s2dv::PlotEquiMap` or other tools to plot the map.
```r
  library(s2dv)
  # manually create the lon and lat vector
  lon <- seq(from = 0, to = 359, length.out = 512)
  lat <- seq(from = 90, to = -90, length.out = 256)

  # Set the color bar 
  brks_crps <- seq(min(res$crps, na.rm = TRUE), max(res$crps, na.rm = TRUE), length.out = 11)
  brks_rmsss <- seq(min(res$rmsss, na.rm = TRUE), max(res$rmsss, na.rm = TRUE), length.out = 11)

  PlotEquiMap(res$crps[1, 1, 1, , ], lon, lat,
              color_fun = clim.palette('yellowred'), brks = brks_crps,
              filled.continents = FALSE, triangle_ends = c(TRUE, TRUE),
              toptitle = 'ECMWF monthly mean tas CRPS 2012-2016', title_scale = 0.6, fileout = 'crps.png')

  PlotEquiMap(res$rmsss[1, 1, 1, , ], lon, lat,
              color_fun = clim.palette('yellowred'), brks = brks_rmsss,
              filled.continents = FALSE, triangle_ends = c(TRUE, TRUE),
              toptitle = 'ECMWF monthly mean tas RMSSS 2012-2016', title_scale = 0.6, fileout = 'rmsss.png')

```

4. We create lat and lon vectors by ourselves above. However, the actual values are slightly different.
Can you get the lon and lat vectors from the data attributes?

```r
  repos_exp <- paste0('/esarchive/exp/ecmwf/system4_m1/monthly_mean/',
                      '$var$_f6h/$var$_$sdate$.nc')
  sdates_exp <- sapply(2012:2016, function(x) paste0(x, sprintf('%02d', 1:12), '01'))

  exp <- Start(dat = repos_exp,
               var = 'tas',
               sdate = sdates_exp,
               time = indices(1),
               ensemble = indices(1:2),
               latitude = 'all',
               longitude = 'all',
               synonims = list(longitude = c('lon', 'longitude'),
                               latitude = c('lat', 'latitude')),
               return_vars = list(latitude = NULL, longitude = NULL),
               retrieve = F)

  lon <- as.vector(attr(exp, 'Variables')$common$longitude)
  lat <- as.vector(attr(exp, 'Variables')$common$latitude)

```

