Weather regime analysis
========================

s2dverification contains a set of functions to compute weather regime classifications. The goal fo this analysis is to generate a set of patterns able to characterize the different circulation structures occurring each month/season. 

In case you want to investigate the s2dverification functionalities you can:

- Load s2dverification with the following command:

```r
library(s2dverification)
```

- Check the documentation which is available here:

https://cran.r-project.org/web/packages/s2dverification/s2dverification.pdf


### 1- Load files with the `Load ()` function. 
- Sea level pressure (psl): this has been selected as the circulation variable, however other variables such as geopotential at 500 hPa can be also used.
- Region: traditional Euro-Atlantic domain [85.5ºW-45ºE; 27-81ºN]]
- Datasets: seasonal predictions from ECMWF System 4 ([**Molteni et al. 2011**](https://www.ecmwf.int/sites/default/files/elibrary/2011/11209-new-ecmwf-seasonal-forecast-system-system-4.pdf) and ERA-Interim reanalysis ([**Dee et al. 2011**](http://onlinelibrary.wiley.com/doi/10.1002/qj.828/pdf)) as a reference dataset.
- Period: for illustration we have only selected 20 years (1990-2010), although the full period available can be employed (1981-2017). 

```r
sdates <- paste0(1990:2010, '1201')
data <- Load('psl', exp = 'system4_m1',
             obs = 'erainterim',
             sdates = sdates,
             storefreq='daily',
             leadtimemin = 1, leadtimemax = 31,
             latmin = 27, latmax = 81,
             lonmin = 274.5, lonmax = 45,output = 'lonlat')
```
The object returned by `Load()` contains, among others, two arrays with the 
requested data, one for the experimental data and the other for observational 
data.

### 2- Daily anomalies based on a smoothed climatology
The weather regimes classification is based on daily anomalies, which have been computed by following these steps: 

- Daily climatology is computed by using the Clim() function.

```r
clim<-Clim(var_exp = data$mod,var_obs = data$obs,memb=F)
```
- A LOESS filter has been applied to the climatology to remove the short-term variability and retain the annual cycle and to better isolate quasi stationary periods.

```r
Loess<-function(clim,loess_span){
  data<-data.frame(ensmean=clim,day=1:length(clim))
  loess_filt<-loess(ensmean~day,data,span=loess_span)
  output<-predict(loess_filt)
  return(output)
}
clim_smoothed_obs<-aperm(apply(clim$clim_obs,c(1:length(dim(clim$clim_obs)))[-which(names(dim(clim$clim_obs))=='ftime')],Loess,loess_span=1),c(2,1,3,4))
clim_smoothed_exp<-aperm(apply(clim$clim_exp,c(1:length(dim(clim$clim_exp)))[-which(names(dim(clim$clim_exp))=='ftime')],Loess,loess_span=1),c(2,1,3,4))
```
- Then the anomalies are computed with the `Ano()` function, as the deviations of the sea level pressure from the smoothed climatologies.

```r
anom_obs<-Ano(data$obs,clim_smoothed_exp)
anom_exp<-Ano(data$mod,clim_smoothed_exp)
```

### 3- Weather regimes in observations 

`WeatherRegimes()` is used to define the clusters corresponding to the ERA-Interim classification. 
- It uses the k-means algorithm to produces a partition of all states in a predefined number of clusters (`ncenters`), which maximizes the ratio of variance among the clusters centroids and the intra-cluster variance.
- Cluster partitions are obtained from an iterative process in which the centroid coordinates selected are those that produces the maximum variance ratio (by default, `nstart = 30, iter.max = 100`).
- To reduce the dimensionality of the problem EOF can be used, however in this example we have not computed EOFs just to take into account the extreme values (`EOFS=FALSE`). 
- Anomalies introduced in the k-means function should be latitude weighted (`lat_weights=TRUE`), therefore latitude values are introduced as input (`lat` input).

```r
WR_obs<-WeatherRegime(data=anom_obs,EOFS=FALSE,lat_weights = TRUE,lat=data$lat,ncenters = 4)
```

`WeatherRegime()` provides a list with 6 elements (`$composite`, `$pvalue`, `$cluster`,`$center`, `$cluster_length`, `$cluster_values`, `$persistence` and `$frequency`) which are the needed parameters for the weather regimes assessment. 

### 4- Visualisation of the observed weather regimes

To plot the composite maps of each regime and the mean frequencies of each cluster, we have employed the `PlotLayout()` and `PlotEquiMap()` functions available in s2dverifcation. 

 ```r
titles<-paste0('freq = ',round(WR_obs$frequency,1),'%')
PlotLayout(PlotEquiMap,c(1,2),lon=data$lon,lat=data$lat,var=WR_obs$composite/100,
           titles=sapply('freq = ',paste0,paste0(round(WR_obs$frequency,1),'%')),filled.continents=F,
           axelab=F,draw_separators = T,subsampleg = 1,brks=seq(-16,16,by=2),
           bar_extra_labels = c(2,0,0,0),fileout='observed_regimes.png')
```

<img src="observed_regimes.png" />


### 5- Weather regimes in the predictions

Predicted anomalies for each day,month,member and lead time are matched with the observed clusters (obtained in step 3). The matching is done in terms of the minimum Eucledian distance `method='distance'`, although it can be also done in terms of spatial correlation `method='ACC'`. This method guarantees that the predicted weather regimes have very similar spatial structures to the observed predictions, which is essential for the verification of the predicted weather regimes. 

 ```r
reference<-drop(WR_obs$composite)
names(dim(reference))<-c('lat','lon','nclust')
WR_exp<-RegimesAssign(var_ano=anom_exp,ref_maps=reference,lats=lats)
```
### 6- Visualisation of the predicted weather regimes

The outputs of `RegimesAssign()` have been represented to visually compare the predicted and observed weather regimes. 

```
PlotLayout(PlotEquiMap,c(1,2),lon=data$lon,lat=data$lat,var=WR_exp$composite/100,
           titles=sapply('freq = ',paste0,paste0(round(WR_exp$frequency,1),'%')),
           filled.continents=F,
           axelab=F,draw_separators = T,subsampleg = 1,brks=seq(-16,16,by=2),
           bar_extra_labels = c(2,0,0,0),fileout='predicted_regimes.png')
```
<img src="predicted_regimes.png" />

