Insurance Risk Indices
========================


Insurance Risk Indices are an ensemble of indices relevant for insurance industry. These indices are based on Expert Team on Climate Change Detection Indices (ETCCDI). There are currently 5 available indices to compute for extreme: heat (tx90p), cold (tn10p), wind (wx), drought (ccd) and flooding (rx5day). The individual indices can be combined into a single index with or without weighting for each component. This combined index is roughly analogous to the Actuaris Climate Risk Index.

*Note: This vignette has been written to process and plot the output of an ESMValTool namelist configured with a specific set of parameters. If you wish to run and plot the outputs for another set of parameters, you need to re-run the corresponding namelist with the desired parameters, interpolate the output NetCDF files to a common grid, and modify the vignette code accordingly.*

The following example will compute the 5 different indices in the North Atlantic - European Sector [60ºW-40ºE; 30ºN-70ºN] using the CMIP5 projection for the 8.5 scenario.

### 1- Load dependencies
This example requires the following system libraries:


- libssl-dev
- libnecdf-dev
- cdo


You will need to install specific versions of certain R packages as follows:
```r
library(devtools)
install_git('https://earth.bsc.es/gitlab/es/startR', branch = 'develop-hotfixes-0.0.2')
```

Six functions should be loaded by running the following lines in R, until integrated into the new magic.bsc package.

```r
source('https://earth.bsc.es/gitlab/nperez/magic.bsc/raw/develop-bugfixes-0.0.0/R/Climdex.R')
source('https://earth.bsc.es/gitlab/nperez/magic.bsc/raw/develop-bugfixes-0.0.0/R/Threshold.R')
source('https://earth.bsc.es/gitlab/nperez/magic.bsc/raw/develop-bugfixes-0.0.0/R/DailyAno.R')
source('https://earth.bsc.es/gitlab/nperez/magic.bsc/raw/develop-bugfixes-0.0.0/R/WeightedMean.R')
source('https://earth.bsc.es/gitlab/nperez/magic.bsc/raw/develop-bugfixes-0.0.0/R/CombineIndices.R')
source('https://earth.bsc.es/gitlab/nperez/magic.bsc/raw/develop-bugfixes-0.0.0/R/SeasonSelect.R')
```


All the other R packages involved can be installed directly from CRAN and loaded as follows:
```r
library(s2dverification)
library(startR)
library(multiApply)
library(ggplot2)
library(climdex.pcic)
library(parallel)
```

### 2- Computing the Extreme Heat Index

The Extreme Heat Index (*tx90p*) is defined as the percentage of days when the maximum temperature exceeds the 90th percentile. 

#### 2.1- Extreme Heat Index Climatology

In order to evaluate the future projections, it is necessary to compute the index during a reference historical period. In this example, the period 1971⨪2000 has been selected.
The next steps should be followed:

- First the historical period data is loaded with the function `Start()`. The full description and examples of its use can be found here: https://earth.bsc.es/gitlab/es/startR. The object returned by `Start` will have at least one time and two spatial dimensions. 


```r
var0 <- 'tasmax'
start_climatology <- '1971-01-01'
end_climatology <- '2000-12-31'
lat.min <- 30;  lat.max <- 70
lon.min <- -60; lon.max <- 40
climatology_filenames <- paste0("/esarchive/scratch/pbretonn/cmip5-cp4cds/historical/day/tasmax/",
                                "tasmax_day_IPSL-CM5A-LR_historical_r1i1p1_19500101-20051231.nc")

reference_data <- Start(model = climatology_filenames, var = var0, var_var = 'var_names', 
                        time = values(list(as.POSIXct(start_climatology), as.POSIXct(end_climatology))),
                        lat = values(list(lat.min, lat.max)), lon = values(list(lon.min, lon.max)), 
                        lon_var = 'lon', lon_reorder = CircularSort(0, 360), return_vars = list(time = 'model', 
                        lon = 'model', lat = 'model'), retrieve = TRUE)
metadata <- attributes(reference_data)
```
```r
> dim(reference_data)
model   var  time   lat   lon 
    1     1 10957    21    28 
```
- To remove seasonality effects, the anomaly is computed: first, the day of the year, from 1 to 365, is identified for the 30 years period; then, the anomaly for each day and gridpoint is computed by applying the `DailyAno` function; finally, the names of the dimensions are restablished. 

```r
dates <- attributes(reference_data)$Variables$dat1$time
dates <- as.PCICt(dates, cal = attributes(dates)$variables$time$calendar, format = "%Y%m%d")
jdays <- as.numeric(strftime(dates, format = "%j"))

anomaly_data <- Apply(list(reference_data), target_dims = which(names(dim(reference_data)) == "time"), 
                      AtomicFun = "DailyAno", jdays = jdays, ncores = 4)$output1
names(dim(anomaly_data))[1] <- "time"
```

- This data can be detrended by applying `Trend` function. In order to remove the trend from the `reference_data`, the correction is calculated by subtracting the `detrended_data` to the `anomaly_data`. Again, the original dimensional information is restored.
 
```r
detrended_data <- Trend(anomaly_data, posTR = which(names(dim(anomaly_data)) == "time"))
diff <- anomaly_data - detrended_data$detrended
diff <- aperm(diff, c(2,3,1,4,5))
detrended_data <- reference_data - diff
attributes(detrended_data) <- metadata
```

- For each gridpoint and day of the year (from the 1st of January to the 31st of December), the maximum temperature on the position at the 90 percent of the series will be calculated as the threshold. 

```r
quantile <- 0.9
base_range <- as.numeric(c(substr(start_climatology, 1, 4), substr(end_climatology, 1, 4)))
thresholds <- Threshold(detrended_data, base_range = as.numeric(base_range), 
                        qtiles = quantile, ncores = detectCores() -1)
```
```r
> dim(thresholds)
jdays model   var   lat   lon 
  365     1     1    21    28 
```

- By indicating the metric and introducing the threshold, `Climdex()` function will return the extreme heat index during the reference period.


```r
base_index <- Climdex(data = detrended_data, metric = 'tx90p', quantiles = thresholds, 
                      ncores = detectCores() - 1)
```

The output of ´Climdex´ function will be a ´list()´ object. Index values are saved in the ´base_index$result´ label.


```r
> str(base_index)
List of 2
 $ result: num [1:30, 1, 1, 1:21, 1:28] 8.22 12.33 4.38 8.49 9.59 ...
 $ years : chr [1:30] "1971" "1972" "1973" "1974" ...
> dim(base_index$result)
 year model   var   lat   lon 
   30     1     1    21    28
```

- Finally, the standard deviation is computed in order to standardize the index. Notice that, by definition, the mean of the percentage of the number of days exceeding the 90th percentile is 10. Only standard deviation is computed.

```r
base_sd <- Apply(list(base_index$result), target_dims = list(c(1)), AtomicFun = "sd")$output1
```

#### 2.2- Extreme Heat Index Climate Projection

The Extreme Heat Index is computed for the period 2020-2040 by following the next steps:

- Loading data by using `Start()` function. 

```r
start_model <- '2020-01-01'
end_model <- '2040-12-31'
model_filenames <- paste0("/esarchive/scratch/pbretonn/cmip5-cp4cds/rcp85/day/tasmax/",
                          "tasmax_day_IPSL-CM5A-LR_rcp85_r1i1p1_20060101-22051231.nc")

rcp_data <- Start(model = model_filenames, var = var0, var_var = 'var_names',
                  time = values(list(as.POSIXct(start_model), as.POSIXct(end_model))),
                  lat = values(list(lat.min, lat.max)), lon = values(list(lon.min, lon.max)), 
                  lon_var = 'lon', lon_reorder = CircularSort(0, 360), 
                  return_vars = list(time = 'model', lon = 'model', lat = 'model'),
                  retrieve = TRUE)
```
```r
> dim(rcp_data)
model   var  time   lat   lon 
    1     1  7671    21    28 
> summary(rcp_data)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  221.6   276.7   283.6   283.4   291.2   321.1 
```

- Now the index can be computed by considering the threshold obtain for the reference period.


```r
projection_index <- Climdex(data = rcp_data, metric = 'tx90p', quantiles = thresholds, 
                            ncores = detectCores() - 1)
```

- Its normalized with mean 10 and the standard deviation of the reference period.


```r
base_mean <- 10
base_sd <- InsertDim(base_sd, 1, dim(projection_index$result)[1])
HeatExtremeIndex <- (projection_index$result - base_mean) / base_sd
```


#### 2.3- Visualitzation of Extreme Heat Index

A Map of the mean index values during the examined period 2020-2040 is obtained and save in PNG format in the working directory with the name: "tx90p_IPSL-CMIP5_85rpc_2020-01-01_2040-12-31.png"

```r
lat <- attr(rcp_data, "Variables")$dat1$lat
lon <- attr(rcp_data, "Variables")$dat1$lon
title <- paste0("Index for tx90p ", substr(start_model, 1, 4), "-̣̣",
                  substr(end_model, 1, 4), " 85rcp CMIP5-IPSL")
breaks <- -8 : 8
PlotEquiMap(Mean1Dim(HeatExtremeIndex, which(names(dim(HeatExtremeIndex)) == "year")), lon = lon, 
            lat = lat, filled.continents = FALSE, toptitle = title, brks=breaks, 
            fileout = paste0('tx90p_IPSL-CMIP5_85rpc_', start_model, "_", end_model, ".png"))
```

<img src="tx90p_IPSL-CMIP5_85rpc_2020-01-01_2040-12-31.png"/>


The inland average of the Extreme Heat Index can be computed to plot its time evolution using `WeigthedMean` function. `Smoothing()` returns the smoothed time series for a 3 year moving window which can be modified using `runmeanlen` parameter.

```r
masc <- Start(model = '/esarchive/exp/ipsl-cm5a-lr/cmip5-rcp85_i1p1/constant/r0i0p0/fx/sftlf/sftlf.nc', 
	            var = 'sftlf', var_var = 'var_names', lat = values(list(lat.min, lat.max)),
              lon = values(list(lon.min, lon.max)), lon_reorder = CircularSort(0,360), 
	            return_vars = list(lon = 'model', lat = 'model'), retrieve = TRUE)
temporal <- WeightedMean(HeatExtremeIndex, lon = lon, lat = lat, mask = drop(masc))
temporal_3ysmooth <- Smoothing(temporal, runmeanlen = 3, numdimt = 1)
```

The next code should be run to plot and save the original average and the 3 year smoothed data.

```r
png("Temporal_Inland_ExtremeHeatIndex_IPSL-CMIP5_85rcp_2020-2040.png", width = 8, height = 5, units = 'in', 
    res = 100, type = "cairo")
  plot(substr(start_model, 1, 4):substr(end_model, 1, 4), temporal, type = "l", lty = 5, lwd = 2, bty = 'n', 
       xlab = "Time (years)", ylab = "Extreme Heat Index", main = "Inland average Extreme Heat Index")
  lines(substr(start_model, 1, 4):substr(end_model, 1, 4), temporal_3ysmooth, col = "darkgreen", lwd = 2)
  legend('bottomright', c('Anual', '3 years smooth'), col = c(1, 'darkgreen'), lty = c(5, 1), lwd = 2, bty = 'n')
dev.off()
```

<img src="Temporal_Inland_ExtremeHeatIndex_IPSL-CMIP5_85rcp_2020-2040.png" />


### 3- Extreme Cold Index

The Extreme Cold Index (*tn10p*) is defined as the percentage of days when the minimum temperature falls behind the 10th percentile. The procedure will be equivalent to the previous Extreme Heat Index example.

#### 3.1- Extreme Cold Index Climatology

- To load the historical simulations:

```r
var0 <- 'tasmin'
climatology_filenames <- paste0("/esarchive/scratch/pbretonn/cmip5-cp4cds/historical/day/tasmin/",
                                "tasmin_day_IPSL-CM5A-LR_historical_r1i1p1_19500101-20051231.nc")

reference_data <- Start(model = climatology_filenames, var = var0, var_var = 'var_names',
                        time = values(list(as.POSIXct(start_climatology), as.POSIXct(end_climatology))),
                        lat = values(list(lat.min, lat.max)), lon = values(list(lon.min, lon.max)),
                        lon_var = 'lon', lon_reorder = CircularSort(0, 360),
                        return_vars = list(time = 'model', lon = 'model', lat = 'model'), retrieve = TRUE)
metadata <- attributes(reference_data)
```


- To compute anomalies: 


```r
dates <- attributes(reference_data)$Variables$dat1$time
dates <- as.PCICt(dates, cal = attributes(dates)$variables$time$calendar, format = "%Y%m%d")
jdays <- as.numeric(strftime(dates, format = "%j"))

anomaly_data <- Apply(list(reference_data), target_dims = which(names(dim(reference_data)) == "time"),
                      AtomicFun = "DailyAno", jdays = jdays, ncores = 4)$output1
names(dim(anomaly_data))[1] <- "time"
```

- To detrend the data: 

```r
detrended_data <- Trend(anomaly_data, posTR = which(names(dim(anomaly_data)) == "time"))
diff <- anomaly_data - detrended_data$detrended
diff <- aperm(diff, c(2,3,1,4,5))
detrended_data <- reference_data - diff
attributes(detrended_data) <- metadata
```

- To obtain the thresholds (the minimum temperature on the position at the 10 percent of the series): 

```r
quantile <- 0.1
base_range <- as.numeric(c(substr(start_climatology, 1, 4), substr(end_climatology, 1, 4)))
thresholds <- Threshold(detrended_data, base_range = as.numeric(base_range), qtiles = quantile, 
                        ncores = detectCores() -1)
```

- To compute the Extreme Cold Index during the reference period and its standard deviation:

```r
base_index <- Climdex(data = detrended_data, metric = 'tx10p', quantiles = thresholds, 
                      ncores = detectCores() - 1)
base_sd <- Apply(list(base_index$result), target_dims = list(c(1)), AtomicFun = "sd")$output1
```

#### 3.2- Extreme Cold Index Climate Projection

The Extreme Cold Index is computed for the period 2020-2040 by following the next steps:

- Loading data by using `Start()` function.

```r
model_filenames <- paste0("/esarchive/scratch/pbretonn/cmip5-cp4cds/rcp85/day/tasmin/",
                          "tasmin_day_IPSL-CM5A-LR_rcp85_r1i1p1_20060101-22051231.nc")

rcp_data <- Start(model = model_filenames, var = var0, var_var = 'var_names',
                  time = values(list(as.POSIXct(start_model), as.POSIXct(end_model))),
                  lat = values(list(lat.min, lat.max)), lon = values(list(lon.min, lon.max)), 
                  lon_var = 'lon', lon_reorder = CircularSort(0, 360), return_vars = list(time = 'model', 
                  lon = 'model', lat = 'model'), retrieve = TRUE)
```
- To compute the index an standardize it:

```r
projection_index <- Climdex(data = rcp_data, metric = 'tx10p', quantiles = thresholds, 
                            ncores = detectCores() - 1)
base_mean <- 10
base_sd <- InsertDim(base_sd, 1, dim(projection_index$result)[1])
ColdExtremeIndex <- (projection_index$result - base_mean) / base_sd
```


#### 3.3- Visualitzation of Extreme Cold Index

- Map of the mean index values during the examined period 2020-2040:

```r
title <- paste0("Index for tn10p ", substr(start_model, 1, 4), "-", substr(end_model, 1, 4), 
                " 85rcp CMIP5-IPSL")
breaks <- -8 : 8
PlotEquiMap(Mean1Dim(ColdExtremeIndex, which(names(dim(ColdExtremeIndex)) == "year")), lon = lon, lat = lat,
            filled.continents = FALSE, toptitle = title, brks=breaks, 
            fileout = paste0('tn10p_IPSL-CMIP5_85rpc_', start_model, "_", end_model, ".png"))
```

<img src="tn10p_IPSL-CMIP5_85rpc_2020-01-01_2040-12-31.png"/>


- Evolution of inland average of the Extreme Cold Index:

```r
temporal <- WeightedMean(ColdExtremeIndex, lon = lon, lat = lat, mask = drop(masc))
temporal_3ysmooth <- Smoothing(temporal, runmeanlen = 3, numdimt = 1)

png("Temporal_Inland_ExtremeColdIndex_IPSL-CMIP5_85rcp_2020-2040.png", width = 8, height = 5, units= 'in', 
    res = 100, type = "cairo")
plot(substr(start_model, 1, 4):substr(end_model, 1, 4), temporal, type = "l", lty = 5, lwd = 2, bty = 'n', 
     xlab = "Time (years)", ylab = "Extreme Cold Index", main = "Inland average Extreme Cold Index")
lines(substr(start_model, 1, 4):substr(end_model, 1, 4), temporal_3ysmooth, col = "darkgreen",lwd = 2)
legend('bottomleft', c('Anual', '3 years smooth'), col= c(1, 'darkgreen'), lty = c(5, 1), lwd = 2, 
        bty = 'n')
dev.off()
```


<img src="Temporal_Inland_ExtremeColdIndex_IPSL-CMIP5_85rcp_2020-2040.png" />



### 4- Extreme Wind Power Index

The Extreme Wind Power Index (*Wx*) is defined as the percentage of days when wind power exceeds the 90th percentile being wind power proportional to the third power of wind speed. Then, the procedure will be equivalent at the previous examples.

#### 4.1- Extreme Wind Power Index Climatology

- To load the historical simulations for the daily mean wind speed and convert them to wind power:

```r
var0 <- 'sfcWind'
climatology_filenames <- paste0("/esarchive/scratch/pbretonn/cmip5-cp4cds/historical/day/sfcWind/",
                                "sfcWind_day_IPSL-CM5A-LR_historical_r1i1p1_19500101-20051231.nc")

reference_data <- Start(model = climatology_filenames, var = var0, var_var = 'var_names',
                        time = values(list(as.POSIXct(start_climatology), as.POSIXct(end_climatology))),
                        lat = values(list(lat.min, lat.max)), lon = values(list(lon.min, lon.max)),
                        lon_var = 'lon', lon_reorder = CircularSort(0, 360),
                        return_vars = list(time = 'model', lon = 'model', lat = 'model'), retrieve = TRUE)
metadata <- attributes(reference_data)
reference_data <- 0.5 * 1.23 * (reference_data ** 3) 
```


- To obtain the thresholds (the wind power value on the position at the 90 percent of the series): 

*Notice that wind speed data is not detrended.*

```r
quantile <- 0.9
base_range <- as.numeric(c(substr(start_climatology, 1, 4), substr(end_climatology, 1, 4)))
thresholds <- Threshold(reference_data, base_range = as.numeric(base_range), qtiles = quantile, 
                        ncores = detectCores() -1)
```

- To compute the Extreme Wind Power Index during the reference period and its standar deviation:

```r
base_index <- Climdex(data = reference_data, metric = 'Wx', quantiles = thresholds, 
                      ncores = detectCores() - 1)
base_sd <- Apply(list(base_index$result), target_dims = list(c(1)), AtomicFun = "sd")$output1
```

#### 4.2- Extreme Wind Power Index Climate Projection

The Extreme Cold Index is computed for the period 2020-2040 by following the next steps:

- Loading wind speed daily mean data and convert to wind power

```r
model_filenames <- paste0("/esarchive/scratch/pbretonn/cmip5-cp4cds/rcp85/day/sfcWind/",
                          "sfcWind_day_IPSL-CM5A-LR_rcp85_r1i1p1_20060101-22051231.nc")

rcp_data <- Start(model = model_filenames, var = var0, var_var = 'var_names',
                  time = values(list(as.POSIXct(start_model), as.POSIXct(end_model))),
                  lat = values(list(lat.min, lat.max)), lon = values(list(lon.min, lon.max)), 
                  lon_var = 'lon', lon_reorder = CircularSort(0, 360), return_vars = list(time = 'model', 
                  lon = 'model', lat = 'model'), retrieve = TRUE)
rcp_data <- 0.5 * 1.23 * (rcp_data ** 3) 
```
- To compute the index and standardize it:

```r
projection_index <- Climdex(data = rcp_data, metric = 'Wx', quantiles = thresholds, 
                            ncores = detectCores() - 1)
base_mean <- 10
base_sd <- InsertDim(base_sd, 1, dim(projection_index$result)[1])
WindPowerExtremeIndex <- (projection_index$result - base_mean) / base_sd
```


#### 4.3- Visualitzation of Extreme Wind Power Index

- Map of the mean index values during the examined period 2020-2040:

```r
title <- paste0("Index for Wx ", substr(start_model, 1, 4), "-", substr(end_model, 1, 4), 
          " 85rcp CMIP5-IPSL")
breaks <- seq(-4, 4, 0.5)
PlotEquiMap(Mean1Dim(WindPowerExtremeIndex, which(names(dim(WindPowerExtremeIndex)) == "year")), lon = lon, 
            lat = lat, filled.continents = FALSE, toptitle = title, brks=breaks, 
            fileout = paste0('wx_IPSL-CMIP5_85rpc_', start_model, "_", end_model, ".png"))
```

<img src="wx_IPSL-CMIP5_85rpc_2020-01-01_2040-12-31.png"/>


- Evolution of inland average of the Extreme Wind Power Index:

```r
temporal <- WeightedMean(WindPowerExtremeIndex, lon = lon, lat = lat, mask = drop(masc))
temporal_3ysmooth <- Smoothing(temporal, runmeanlen = 3, numdimt = 1)

png("Temporal_Inland_ExtremeWindPowerIndex_IPSL-CMIP5_85rcp_2020-2040.png", width = 8, height = 5, 
    units= 'in', res = 100,     type = "cairo")
plot(substr(start_model, 1, 4):substr(end_model, 1, 4), temporal, type = "l", lty = 5, lwd = 2, bty = 'n', 
     xlab = "Time (years)", ylab = "Extreme Wind Power Index", 
     main = "Inland average Extreme Wind Power Index")
lines(substr(start_model, 1, 4):substr(end_model, 1, 4), temporal_3ysmooth, col = "darkgreen",lwd = 2)
legend('bottomleft', c('Anual', '3 years smooth'), col= c(1, 'darkgreen'), lty = c(5, 1), lwd = 2, 
        bty = 'n')
dev.off()
```


<img src="Temporal_Inland_ExtremeWindPowerIndex_IPSL-CMIP5_85rcp_2020-2040.png" />

### 5- Extreme Drought Index

The Extreme Drought Index (*cdd*), which measures the maximum length of a dry spell, is defined as the maximum number of consecutive days with the daily precipitation amount lower than 1 mm. Then, the procedure will be equivalent at the previous examples.

#### 5.1- Extreme Drought Index Climatology

- To load the historical simulations for the daily precipitation amount variable and convert units to mm from kg·m^-2·s^-1:

```r
var0 <- 'pr'
climatology_filenames <- paste0("/esarchive/scratch/pbretonn/cmip5-cp4cds/historical/day/pr/",
                           "pr_day_IPSL-CM5A-LR_historical_r1i1p1_19500101-20051231.nc")
reference_data <- Start(model = climatology_filenames, var = var0, var_var = 'var_names',
                        time = values(list(as.POSIXct(start_climatology), as.POSIXct(end_climatology))),
                        lat = values(list(lat.min, lat.max)), lon = values(list(lon.min, lon.max)), 
                        lon_var = 'lon', lon_reorder = CircularSort(0, 360), 
                        return_vars = list(time = 'model', lon = 'model', lat = 'model'), retrieve = TRUE)
metadata <- attributes(reference_data)
reference_data <- reference_data * 60 * 60 * 24
```

- To compute the Extreme Drought Index during the reference period and its standar deviation and mean:

*Note: Precipitation data is not detrended. Furthermore, this index doesn't require to compute a threshold as `Climdex` function integrates the threshold of precipitation amount lower than 1 mm internally. However, this case requires the calculation of the mean.*

```r
base_index <- Climdex(data = reference_data, metric = 'cdd',  ncores = detectCores() - 1)
base_mean <-  Apply(list(base_index$result), target_dims = list(c(1)), AtomicFun = "mean")$output1
base_sd <- Apply(list(base_index$result), target_dims = list(c(1)), AtomicFun = "sd")$output1
```

#### 5.2- Extreme Drought Index Climate Projection

The Extreme Drought Index is computed for the period 2020-2040 by following the next steps.

- Loading data by using `Start()` function and converting to mm:

```r
model_filenames <- paste0("/esarchive/scratch/pbretonn/cmip5-cp4cds/rcp85/day/pr/",
                          "pr_day_IPSL-CM5A-LR_rcp85_r1i1p1_20060101-22051231.nc")
rcp_data <- Start(model = model_filenames, var = var0, var_var = 'var_names',
                  time = values(list(as.POSIXct(start_model), as.POSIXct(end_model))),
                  lat = values(list(lat.min, lat.max)), lon = values(list(lon.min, lon.max)), 
                  lon_var = 'lon', lon_reorder = CircularSort(0, 360), 
                  return_vars = list(time = 'model', lon = 'model', lat = 'model'),
                  retrieve = TRUE)
rcp_data <- rcp_data * 60 * 60 * 24
```
- To compute the index and standardize it:

```r
projection_index <- Climdex(data = rcp_data, metric = 'cdd', ncores = detectCores() - 1)
base_mean <- InsertDim(base_mean, 1, dim(projection_index$result)[1])
base_sd <- InsertDim(base_sd, 1, dim(projection_index$result)[1])
DroughtExtremeIndex <- (projection_index$result - base_mean) / base_sd
```


#### 5.3- Visualitzation of Extreme Drought Index

- Map of the mean index values during the examined period 2020-2040:

```r
title <- paste0("Index for cdd ", substr(start_model, 1, 4), "-", substr(end_model, 1, 4), 
                " 85rcp CMIP5-IPSL")
breaks <- seq(-4, 4, 0.5)
PlotEquiMap(Mean1Dim(DroughtExtremeIndex, which(names(dim(DroughtExtremeIndex)) == "year")), lon = lon, 
            lat = lat, filled.continents = FALSE, toptitle = title, brks=breaks, 
            fileout = paste0('cdd_IPSL-CMIP5_85rpc_', start_model, "_", end_model, ".png"))
```

<img src="cdd_IPSL-CMIP5_85rpc_2020-01-01_2040-12-31.png"/>


- Evolution of inland average of the Extreme Drought Index:

```r
temporal <- WeightedMean(DroughtExtremeIndex, lon = lon, lat = lat, mask = drop(masc))
temporal_3ysmooth <- Smoothing(temporal, runmeanlen = 3, numdimt = 1)
png("Temporal_Inland_ExtremeDroughtIndex_IPSL-CMIP5_85rcp_2020-2040.png", width = 8, height = 5, units= 'in', 
    res = 100,  type = "cairo")
plot(substr(start_model, 1, 4):substr(end_model, 1, 4), temporal, type = "l", lty = 5, lwd = 2, bty = 'n', 
     xlab = "Time (years)", ylab = "Extreme Drought Index", main = "Inland average Extreme Drought Index")
lines(substr(start_model, 1, 4):substr(end_model, 1, 4), temporal_3ysmooth, col = "darkgreen",lwd = 2)
legend('bottomleft', c('Anual', '3 years smooth'), col= c(1, 'darkgreen'), lty = c(5, 1), lwd = 2, 
        bty = 'n')
dev.off()
```


<img src="Temporal_Inland_ExtremeDroughtIndex_IPSL-CMIP5_85rcp_2020-2040.png" />

### 6- Extreme Flooding Index

The Extreme Flooding Index (*rx5day*) is defined as the maximum precipitation amount in 5 consecutive days. 

#### 6.1- Extreme Flooding Index Climatology

The daily precipitation amount variable converted to mm is already loaded in `reference_data` variable.

The Extreme Flooding Index during the reference period and its standard deviation and mean can be calculated by executing:

```r
base_index <- Climdex(data = reference_data, metric = 'rx5day',  ncores = detectCores() - 1)
base_mean <-  Apply(list(base_index$result), target_dims = list(c(1)), AtomicFun = "mean")$output1
base_sd <- Apply(list(base_index$result), target_dims = list(c(1)), AtomicFun = "sd")$output1
```

#### 6.2- Extreme Flooding Index Climate Projection

The corresponding data is loaded in `rcp_data`. The standarized index is obtained by running:

```r
projection_index <- Climdex(data = rcp_data, metric = 'rx5day', ncores = detectCores() - 1)
base_mean <- InsertDim(base_mean, 1, dim(projection_index$result)[1])
base_sd <- InsertDim(base_sd, 1, dim(projection_index$result)[1])
FloodingExtremeIndex <- (projection_index$result - base_mean) / base_sd
```


#### 6.3- Visualitzation of Extreme Flooding Index

- Map of the mean index values during the examined period 2020-2040:

```r
title <- paste0("Index for rx5day ", substr(start_model, 1, 4), "-", substr(end_model, 1, 4),
                " 85rcp CMIP5-IPSL")
breaks <- seq(-4, 4, 0.5)
PlotEquiMap(Mean1Dim(FloodingExtremeIndex, which(names(dim(FloodingExtremeIndex)) == "year")), lon = lon, 
            lat = lat, filled.continents = FALSE, toptitle = title, brks=breaks, 
            fileout = paste0('rx5day_IPSL-CMIP5_85rpc_', start_model, "_", end_model, ".png"))
```

<img src="rx5day_IPSL-CMIP5_85rpc_2020-01-01_2040-12-31.png"/>


- Evolution of inland average of the Extreme Flooding Index:

```r
temporal <- WeightedMean(FloodingExtremeIndex, lon = lon, lat = lat, mask = drop(masc))
temporal_3ysmooth <- Smoothing(temporal, runmeanlen = 3, numdimt = 1)
png("Temporal_Inland_ExtremeFloodingIndex_IPSL-CMIP5_85rcp_2020-2040.png", width = 8, height = 5, 
    units= 'in', res = 100, type = "cairo")
plot(substr(start_model, 1, 4):substr(end_model, 1, 4), temporal, type = "l", lty = 5, lwd = 2, bty = 'n', 
     xlab = "Time (years)", ylab = "Extreme Flooding Index", 
     main = "Inland average Extreme Flooding Index")
lines(substr(start_model, 1, 4):substr(end_model, 1, 4), temporal_3ysmooth, col = "darkgreen",lwd = 2)
legend('bottomleft', c('Anual', '3 years smooth'), col= c(1, 'darkgreen'), lty = c(5, 1), lwd = 2, 
       bty = 'n')
dev.off()
```


<img src="Temporal_Inland_ExtremeFloodingIndex_IPSL-CMIP5_85rcp_2020-2040.png" />


### 7- Combining Indices

The individual indices can be combined into a single index with or without weighting for each component. This combined index is roughly analogous to the Actuaris Climate Risk Index (see http://actuariesclimateindex.org/home/). Extreme Indices should be saved in the same `list` object.

```r
indices <- list()
indices[[1]] <- HeatExtremeIndex
indices[[2]] <- ColdExtremeIndex
indices[[3]] <- WindPowerExtremeIndex
indices[[4]] <- DroughtExtremeIndex
indices[[5]] <- FloodingExtremeIndex
```
To select the weight for each component may interest to see their time evolution in a single plot:

```r
indices.matrix <- unlist(lapply(indices, WeightedMean, lon = lon, lat = lat, mask = drop(masc)))
dim(indices.matrix) <- c(21, 5)
png("Temporal_Inland_ExtremeIndices_IPSL-CMIP5_85rcp_2020-2040.png", width = 8, height = 5, 
    units= 'in', res = 100, type = "cairo")
matplot(2020:2040, indices.matrix, type = 'l', bty = "n", xlab = "Time (years)", ylab = "Indices",
        main = "Inland Extreme Indices rcp 8.5 2020-2040", lwd = 2, lty = seq(5, 1, -1), col = c('red', 'darkorange', 
        'black', 'darkgreen', 'purple'))
legend(2035, 5, c('Heat', 'Cold', 'Wind Power','Drought', 'Flooding'), lwd = 2, lty = seq(5, 1, -1), 
       col = c('red', 'darkorange', 'black', 'darkgreen', 'purple'), bty = 'n')
dev.off()
```

<img src="Temporal_Inland_ExtremeIndices_IPSL-CMIP5_85rcp_2020-2040.png" />


If the `weights` parameter is defined as `NULL`, all indices will be equally weighted if `operation` parameter is set as `mean` (by default). To define other `weights` a vector of length equal to the number of considered indices (5 in this example) and with total sum equal to 1. 

In this example, the extreme heat index will be less weighted than the others: 4 % heat, 24 % cold, 24 % wind power, 24 % drought, 24 % flooding indices.


```r
aci <- CombineIndices(indices, weights = c(0.04, 0.24, 0.24, 0.24, 0.24))
```


A spatial visulitzation can be performs by executing:


```r
title <- paste0("Combine Indices ", substr(start_model, 1, 4), "-", substr(end_model, 1, 4), 
                " 85rcp CMIP5-IPSL")
breaks <- seq(-4, 4, 0.5)
PlotEquiMap(Mean1Dim(aci, which(names(dim(aci)) == "year")), lon = lon, 
            lat = lat,  filled.continents = FALSE, toptitle = title, brks=breaks, 
            fileout = paste0('aci_IPSL-CMIP5_85rpc_', start_model, "_", end_model, ".png"))
```

<img src="aci_IPSL-CMIP5_85rpc_2020-01-01_2040-12-31.png" />


### 8- Projection Comparison for a selected season

The next example compares the Extreme Heat Index for two different projections 8.5 and 2.6 scenarios for summer.

It is necessary to reload the maximum temperature daily data during for historical simulations to select summer season:

*Notice that other seasons can be selected by modifying the parameter `seasons` in `SeasonSelect()` function to boreal winter (`'DJF'`), spring (`'MMA'`) and autumn (`'SON'`).*


```r
var0 <- 'tasmax'
climatology_filenames <- paste0("/esarchive/scratch/pbretonn/cmip5-cp4cds/historical/day/tasmax/",
                                "tasmax_day_IPSL-CM5A-LR_historical_r1i1p1_19500101-20051231.nc")
reference_data <- Start(model = climatology_filenames, var = var0, var_var = 'var_names', 
                        time = values(list(as.POSIXct(start_climatology), as.POSIXct(end_climatology))),
                        lat = values(list(lat.min, lat.max)), lon = values(list(lon.min, lon.max)), 
                        lon_var = 'lon', lon_reorder = CircularSort(0, 360), 
                        return_vars = list(time = 'model', lon = 'model', lat = 'model'), retrieve = TRUE)
reference_data <- SeasonSelect(reference_data, seasons = 'JJA')
```

To obtain the thresholds values and the standard deviation of the model during the reference period the same code as in paragraph 2.1 should be performed:

```r
jdays <- rep(1:92, 30)
anomaly_data <- Apply(list(reference_data), target_dims = 'time', output_dims = 'time',
                      AtomicFun = "DailyAno", jdays = jdays, ncores = 4)$output1
detrended_data <- Trend(anomaly_data, posTR = which(names(dim(anomaly_data)) == "time"))
diff <- anomaly_data - detrended_data$detrended
detrended_data <- reference_data - diff
quantile <- 0.9
base_range <- as.numeric(c(substr(start_climatology, 1, 4), substr(end_climatology, 1, 4)))
thresholds <- Threshold(detrended_data, base_range = as.numeric(base_range), 
                        qtiles = quantile, ncores = detectCores() -1)
base_index <- Climdex(data = detrended_data, metric = 'tx90p', quantiles = thresholds, 
                      ncores = detectCores() - 1)
base_sd <- Apply(list(base_index$result), target_dims = list(c(1)), AtomicFun = "sd")$output1
```

The two projections are loaded by running:


```r
model_filenames <- paste0("/esarchive/scratch/pbretonn/cmip5-cp4cds/",
                          c("rcp85/day/tasmax/tasmax_day_IPSL-CM5A-LR_rcp85_r1i1p1_20060101-22051231.nc",
                            "rcp26/day/tasmax/tasmax_day_IPSL-CM5A-LR_rcp26_r1i1p1_20060101-22051231.nc"))
rcp_data <- Start(model = model_filenames, var = var0, var_var = 'var_names',
                  time = values(list(as.POSIXct(start_model), as.POSIXct(end_model))),
                  lat = values(list(lat.min, lat.max)), lon = values(list(lon.min, lon.max)), 
                  lon_var = 'lon', lon_reorder = CircularSort(0, 360), 
                  return_vars = list(time = 'model', lon = 'model', lat = 'model'),
                  retrieve = TRUE)
rcp_data <- SeasonSelect(rcp_data, seasons = 'JJA')
```


- Now the index can be computed by considering the threshold obtain for the reference period for each projection.


```r
projection_index <- Climdex(rcp_data, metric = 'tx90p', quantiles = thresholds, 
                            ncores = detectCores() - 1)
```

- Its normalized with mean 10 and the standard deviation of the reference period.


```r
base_mean <- 10
base_sd <- InsertDim(InsertDim(base_sd,1, 2), 1, dim(projection_index$result)[1])
JJAHeatExtremeIndex <- (projection_index$result - base_mean) / base_sd
```


#### 8.3 Visualitzation of Summer Extreme Heat Index for both scenarios

Spatial and temporal representation will be plotted in the same image by using `par()`:


```r
breaks <- -8 : 8
temporal <- WeightedMean(JJAHeatExtremeIndex, lon = lon, lat = lat, mask = drop(masc))
temporal_3ysmooth <- Smoothing(temporal, runmeanlen = 3, numdimt = 1)

png("Summer_ExtremeHeatIndex_IPSL-CMIP5_85-26rcp_2020-2040.png", width = 8, height = 5, units = 'in', 
    res = 100, type = "cairo")
par(mfrow=c(3,2))
PlotEquiMap(Mean1Dim(JJAHeatExtremeIndex[,1,,], 1), lon = lon, 
            lat = lat, filled.continents = FALSE, toptitle = "8.5 rcp", brks = breaks, drawleg = FALSE, title_scale = 0.8)
plot(substr(start_model, 1, 4):substr(end_model, 1, 4), temporal[,1], type = "l", lty = 5, lwd = 2, bty = 'n', 
     xlab = "", ylab = "", main = "Inland average", cex.main = 0.9, cex.axis = 0.9)
lines(substr(start_model, 1, 4):substr(end_model, 1, 4), temporal_3ysmooth[,1], col = "darkgreen", lwd = 2)
legend('bottomright', c('Anual', '3 years smooth'), col = c(1, 'darkgreen'), lty = c(5, 1), lwd = 2, bty = 'n', cex = 0.9)
PlotEquiMap(Mean1Dim(JJAHeatExtremeIndex[,2,,], 1), lon = lon, 
            lat = lat, filled.continents = FALSE, toptitle = "2.6 rcp", brks = breaks, drawleg = FALSE, title_scale = 0.8)
plot(substr(start_model, 1, 4):substr(end_model, 1, 4), temporal[,2], type = "l", lty = 5, lwd = 2, bty = 'n', 
      xlab = "", ylab = "", main = "Inland average", cex.main = 0.9, cex.axis = 0.9)
lines(substr(start_model, 1, 4):substr(end_model, 1, 4), temporal_3ysmooth[,2], col = "darkgreen", lwd = 2)
legend('bottomright', c('Anual', '3 years smooth'), col = c(1, 'darkgreen'), lty = c(5, 1), lwd = 2, bty = 'n', cex = 0.9)
ColorBar(brks = breaks, vertical = FALSE, extra_margin = c(4,1.5,0.5,0.5))
dev.off()             

```

<img src="Summer_ExtremeHeatIndex_IPSL-CMIP5_85-26rcp_2020-2040.png" />