Computing Anomaly Correlation Coefficient
-----------------------------------------

**reference**: Mishra, N., Prodhomme, C., & Guemas, V. (2018). Multi-Model Skill Assessment of Seasonal Temperature and Precipitation Forecasts over Europe, 29–31. <https://link.springer.com/article/10.1007%2Fs00382-018-4404-z>

### 1.- Load data

In this case, the seasonal temperature forecasted, initialized in november, will be used to assess the EUROSIP multi-model seasonal forecasting system consists of a number of independent coupled seasonal forecasting systems integrated into a common framework. From September 2012, the systems include those from ECMWF, the Met Office, Météo-France and NCEP.

The parameters defined are the initializating month and the variable:


```{r cars}
mth = '11'
clim_var = 'tas'
```


The simulations available for these models are covering the period 1992-2012. So, the starting and ending dates can be defined by running the following lines:


```r
start <- as.Date(paste("1992", mth, "01", sep = ""), "%Y%m%d")
end <- as.Date(paste("2012", mth, "01", sep = ""), "%Y%m%d")
dateseq <- format(seq(start, end, by = "year"), "%Y%m%d")
``` 


The grid in which all data will be interpolated should be also specified. The observational dataset used in this example is the EraInterim. 


```r
grid <- "256x128"
obs <- "erainterim"
```

Using the `Load` function from **s2dverification package**, the data available can be loaded:


```r  
glosea5 <- list(path = '/esnas/exp/glosea5/specs-seasonal_i1p1/$STORE_FREQ$_mean/$VAR_NAME$-allmemb/$VAR_NAME$_$START_DATE$.nc')
StartData <- Load(clim_var, exp = list(glosea5, list(name = 'ecmwf/system4_m1'),
                                  list(name = 'ncep/system2_m1'),
                                  list(name = 'meteofrance/system5_m1')),
                  obs = obs, sdates = dateseq, leadtimemin = 2, leadtimemax = 4,
                  lonmin = -20, lonmax = 70, latmin = 25, latmax = 75, 
                  storefreq = "monthly", sampleperiod = 1,  nmember = 9, 
                  output = "lonlat", method = "bilinear", 
                  #maskmod = listmaskmod,  
                  #maskobs = listmaskobs, 
                  grid = paste("r", grid, sep = ""))
```


In these example the first two elements of `StartData` corresponds to arrays containing the simulated and the observed data:


```r
> dim(StartData$mod)
dataset  member   sdate   ftime     lat     lon 
      4       9      21       3      35      64 
> dim(StartData$obs)
dataset  member   sdate   ftime     lat     lon 
      1       1      21       3      35      64 
```      


Latitudes and longitudes of the common grid can be saved:


```r
Lat <- StartData$lat
Lon <- StartData$lon
```


### 2.- Computing anomalies

The anomalies are obtained by running the `AnoCorr` function:


```r
AnomDJF <- AnoCorr(StartData)[[1]]
```


The first element of the list returned by the function `AnoCorr` contains the anomaly data  for the simulations and the obsrevations:


```r
> str(AnomDJF)
List of 2
 $ ano_exp: num [1:4, 1:9, 1:21, 1:3, 1:35, 1:64] -1.647 -0.478 -3.327 -0.096 1.575 ...
 $ ano_obs: num [1, 1, 1:21, 1:3, 1:35, 1:64] 0.0235 1.546 1.3885 -0.344 -5.972 ...
```
 
str(AnomDJF) 
dim(AnomDJF$corr)
cor1 <- AnomDJF$corr 
cor1[,1, , 2,1] # Por qué no es diagonal??

pos <- apply(cor1, c(4, 5), function(x){which.max(x)})
str(pos)

length(which(pos < 0))

brks <- seq(0, 1, 1/10)

library(RColorBrewer)
blue <- c("#FFFFFF", brewer.pal(9, "Blues"))
red <- c("#FFFFFF", brewer.pal(9, "Reds"))
green <- c("#FFFFFF", brewer.pal(9, "Greens"))
orange <- c("#FFFFFF", brewer.pal(9, "Oranges"))

color <- c(blue, red, green, orange)

cor 1 <- cor1[pos == 4] + 4
cor 1 <- cor1[pos == 4] + 4
cor 1 <- cor1[pos == 4] + 4
cor 1 <- cor1[pos == 4] + 4

 
### 3.- Computing weights based on BOCM and correlations

Best OLS Combination Models 

You can also embed plots, for example:

```{r pressure, echo=FALSE}
plot(pressure)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
