Diurnal Temperature Variation (DTR) Indicator and Wind Capacity Factor
======================================================================

This tool computes the diurnal temperature variation indicator as a proxy for energy demand and the wind capacity factor as a proxy for energy supply. The diurnal temperature indicator is the number of days in a season where the daily temperature variation (tasmax - tasmin) exceeds the vulnerability threshold. Here, the vulnerability threshold is based on the mean daily temperature variation for the reference period (1960 -1990) + 5 degrees. This definition was proposed in a study conducted for the DALKIA energy company. The capacity factor is computed using the manufacturer provided power curves that relate power output to steady winds blowing at hub height. The capacity factor can be derived from power curve values easily, by dividing power output by the nominal capacity of the turbine. Here the 100-m wind heights are extrapolated from the surface wind speeds.

*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 heatwaves and coldwaves duration in the North Atlantic - European Sector [20ºW-50ºE; 30ºN-80º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')
```

Five 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/Threshold.R')
source('https://earth.bsc.es/gitlab/nperez/magic.bsc/raw/develop-bugfixes-0.0.0/R/SeasonSelect.R')
source('https://earth.bsc.es/gitlab/es/s2dverification/raw/develop-Magic/vignettes/heatcoldwaves/WaveDuration.R')
source('https://earth.bsc.es/gitlab/es/s2dverification/raw/develop-Magic/vignettes/heatcoldwaves/WaveIntensity.R')
source('https://earth.bsc.es/gitlab/es/s2dverification/raw/develop-Magic/vignettes/heatcoldwaves/WaveStart.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- Heatwaves

The heatwaves duration is the number of consecutive days for which the maximum temperature is exceeding a threshold over a minimum number of days during summer.

The next image shows an example of 17 days heatwave duration in a year corresponding to the red bars. Five days is the minimum number of consecutive days considered in the example.

<img src = 'heatWAVE_duration_def.png' />

#### 2.1- Defining heatwaves threshold

In this example, the threshold is defined as the 90th percentile of maximum temperature during the period 1971-2000. 

The historical simulations for the CMIP5 model should be loaded by the function `Start` from `s2dverification` package:


```r
var0 <- 'tasmax'
start_climatology <- '1971-01-01'
end_climatology <- '2000-12-31'
lat.min <- 30;  lat.max <- 80
lon.min <- -20; lon.max <- 50
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)
```


#<img src = "Latitudinal_Coldwave_rcp85_2020-2040_90thpercentile_1971-2000.png" />
