---
title: "Malaria Suitability Indicator"
author: "Earth Sciences department, Barcelona Supercomputing Center (BSC)"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteEngine{knitr::knitr}
  %\VignetteIndexEntry{Malaria Suitability Indicator}
  %\usepackage[utf8]{inputenc}
---

Climate Suitability for Malaria Transmission
-----------------------------


## Introduction

Climate sensitive infectious diseases are a subject of major concern in the context of a changing climate. Malaria is an infectious disease caused by parasites of the genus _Plasmodium_ and transmitted by _Anopheles_ mosquitoes (Boualam et al., 2021). Eradication efforts managed to achieve complete elimination of endemic circulation of malaria in Europe, although sporadic transmission events are frequently reported in travelers coming from endemic areas. Studies evaluating drivers of malaria elimination found that high socio-economic conditions and increased life expectancy are key determinants in keeping this disease out of the continent (Fischer et al., 2020). On the other hand, the risk of malaria resurgence is present as climatic conditions become more suitable for both the parasite and mosquitoes transmission (Zhao et al., 2016).

The [Lancet Countdown in Europe collaboration](https://www.lancetcountdown.org/europe/) was launched in 2021 with the purpose of using a variety of indicators to monitor trends in the impact of climate on dynamic of different health processes. As part of this initiative, the Climate Suitability for Malaria Transmission indicator was adapted from the [Global Lancet Countdown Collaboration](https://www.lancetcountdown.org/) (Romanello et al., 2021) to track the annual number of months suitable of malaria transmission between 1950 and 2021.This indicator is a threshold based model that overlaps the climatic and environmental requirements of _Anopheles_ mosquitoes and _Plasmodium vivax_, which was the endemic parasite in the continent until the mid 1970s (Boualam et al., 2021).

Although the `CSIndicators` was initially developed for providing a set of tools useful in the context of agricultural practices, it is also possible to expand its applications to the computation of other type of indicators. In this vignette, the Climate Suitability for Malaria indicator will be computed using the `TotalTimeExceedingThreshold()` function. The output will be an multidimensional array containing the number of months per year suitable for malaria transmission.


## Load libraries

In addition to the `CSIndicators` package, the `CSTools` and `s2dv` packages will be used for data extraction and visualization.

```R
library(startR)
library(CSTools)
library(CSIndicators)
library(s2dv)
library(dplyr)
library(ggplot2)
```

## 1. Data
### 1.1 Area of interest
The number of months suitable for malaria transmission between 2010 and 2020 will be computed for the Iberian Peninsula.

<img width="50%" src="vignettes/figures/MI_IberianPeninsula_Wiki_2003.png"/>

Source: [NASA](https://visibleearth.nasa.gov/images/64573/spain-and-portugal)

### 1.2 Data extraction
The Malaria indicator is computed with climate variables extracted from the [ERA5-Land reanalysis dataset](https://cds.climate.copernicus.eu/cdsapp#!/dataset/reanalysis-era5-land?tab=overview) (Muñoz-Sabater et al., 2021), which span between 1950 and present day, at a 9km resolution. These variables are 2 meter temperature (`tas`), 2 meter dew point temperature (`tdps`) and total precipitation (`prlr`). These data are produced by the Copernicus service and extracted and reprocessed by the **Earth Sciences department** from the **Barcelona Supercomputing Center**.

The function `CST_Start` from the `CSTools` package allows the user to access the processed data and delivers the data in the form of an `s2dv_cube` object. These multidimensional objects store data and metadata in several elements. For further information about these objects visit [Data retrieval and storage](https://cran.r-project.org/package=CSTools/vignettes/Data_Considerations.html). 

`CST_Start` has its requirements for extracting data. First, the path to the files is introduced using *whitecards* delimited by dollar signs. Second, the temporal extent and resolution of the output can be provided with `sdates`, which takes a vector of all dates of interest. The spatial extent is inputted within `lonmax`, `lonmin`, `latmax` and `latmin.`

```R
# Path to file locations using whitecards
path_ERA5_CDS <- '/esarchive/recon/ecmwf/era5land/monthly_mean/$var$_f1h/$var$_$sdate$.nc'

# Temporal extent and resolution:
# We will work with all months from 2010 to 2020
year_in <- 2010 # initial year
year_fi <- 2020 # last year
lonmax = 5  ; lonmin = 350;
latmax = 45 ; latmin = 35 ;

sdates <- sapply(2010:2020, function(x) paste0(x, sprintf('%02d', 1:12)))
names(dim(sdates)) <- c("time","sdate")

# Extract data
vars <- c('tas', 'tdps', 'prlr')

out <- NULL
for(var in vars) {
out[[var]] <- CST_Start(dat = list(list(path = path_ERA5_CDS)),
                var = var,
                sdate = sdates,
                merge_across_dims = TRUE,
                split_multiselected_dims = TRUE,
                lat = values(list(latmin, latmax)),
                lat_var = 'lat',
                lat_reorder = Sort(decreasing = TRUE),
                lon = values(list(lonmin,lonmax)),
                lon_reorder = CircularSort(0, 360),
                lon_var = 'lon',
                synonims = list(lon = c('lon', 'longitude'),
                                lat = c('lat', 'latitude')),
                return_vars = list(lon = 'dat', lat = 'dat'),
                retrieve = TRUE)
}
```

The output object is a list with the three `s2dv_cube` objects with dimensions
```R
dim(out$tas$data)
# dat  var   time    sdate     lat     lon 
#   1    1     12       11     101     151 
```
And it is possible to have a look at the data:
```R
# A summary of the entire dataset
summary(out$tas$data)
#  Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
# 263.4   281.6   286.6   287.3   292.8   306.6  862224 
```

```R
# Sneak peek of a sample of the temperature data in Jan 2010
out$tas$data[1, 1, 1, 1, , ][15:20, 15:20]
#         [,1]     [,2]     [,3]     [,4]     [,5]     [,6]
#[1,] 276.8824 276.7627 276.5422 276.3982 276.3127 276.2817
#[2,] 276.7096 276.7052 276.6004 276.5182 276.4211 276.3108
#[3,] 276.5989 276.5732 276.4767 276.4524 276.4432 276.3395
#[4,] 276.6771 276.5388 276.3120 276.2111 276.2662 276.3592
#[5,] 276.4989 276.5763 276.5851 276.4370 276.3131 276.2575
#[6,] 275.5886 275.9590 276.3600 276.3049 276.0576 275.8066
```

## 2. Data transformation
### 2.1 Temperature and dew point temperature
First, it is necessary to transform units from Kelvin (K) to Degrees Celsius (C)
```R
for (var in c('tas', 'tdps')) {
  out[[var]]$data <- out[[var]]$data - 273.15
}

summary(out$tas$data)
#  Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
#  -9.7     8.5    13.5    14.1    19.6    33.5  862224 
```

### 2.3 Relative humidity
Then, it is possible to calculate relative humidity (hurs) using temperature ($`T`$) and dew point temperature ($`T_d`$) with the August-Roche-Magnus equation (Alduchov 1996):

```math
RH=100*\frac{exp(\frac{aT_d}{b+T_d})}{exp(\frac{aT}{c+T})}
```
Where ($`a`$) and ($`b`$) are the coefficients 17.625 and 243.04, respectively.

```R
# Create a new s2dv object with relative humidity
source_files_list <- lapply(vars, function(vars) out[[vars]]$attrs$source_files)

out$hurs <- s2dv_cube(data = 100 * (exp((17.625 * out$tdps$data) / (243.04 + out$tdps$data)) / 
                                  exp((17.625 * out$tas$data) / (243.04 + out$tas$data))),
                      lon = out$tas$lon,
                      lat = out$tas$lat,
                      Variable = list(varName = "rh", level = NULL),
                      Dates = out$tas$attrs$Dates,
                      Datasets = out$tas$attrs$Datasets,
                      when = Sys.time(),
                      source_files = abind::abind(source_files_list, along = 2))

out$hurs$Variable <- numeric()

attr(out$hurs$Variable, "units") <- "%"
attr(out$hurs$Variable, "longname") <- "near-surface relative humidity"

summary(out$hurs$data)
#  Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
#  19.4    55.7    68.5    65.4    77.1    95.0  862224 
```

### 2.2 Precipitation
Precipitation is extracted from the hourly reanalaysis dataset and delivered in monthly meters per second. The next step is to transform it into accumulated mm per month
```R
# To compute the accumulated precipitation per month it is necessary to multiply by:
# - total number of seconds in one hour: 3600
# - total number of hours in a day: 24
# - average number of days in a month: 365.25/12=30.44
# - change factor from meter to mm: 1000
out$prlr$data <- out$prlr$data * 3600 * 24 * 30.44 * 1000

summary(out$prlr$data)
#  Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
#  0.0    20.1    47.5    59.9    83.9   622.1  862224 
```

## 3. Compute indicator
The suitability for malaria transmission indicator estimates the number of months suitable for transmission of _P. vivax_ according to the following thresholds:

* Accumulated monthly precipitation above 80 mm (Lyon et al., 2017),
* Monthly temperature between 14.5ºC and 33ºC (Grover-Kopec et al., 2006), and
* Monthly relative humidity above 60% (Grover-Kopec et al., 2006)

```R
# Create a new s2dv object with malaria suitability
out$malariaSuit <- s2dv_cube(data = ifelse(out$tas$data >= 14.5 & 
                                    out$tas$data <= 33 & 
                                    out$hurs$data >= 60 & 
                                    out$prlr$data >= 80, 1, 0),
                             lon = out$tas$coords$lon,
                             lat = out$tas$coords$lat,
                             Variable = list(varName = "malar_suit", level = NULL),
                             Dates = out$tas$attrs$Dates,
                             Datasets = out$tas$attrs$Datasets,
                             when = Sys.time(),
                             source_files = abind::abind(source_files_list, along = 2))

out$malariaSuit$Variable <- numeric()

attr(out$malariaSuit$Variable, "units") <- "none"
attr(out$malariaSuit$Variable, "longname") <- "suitability for malaria transmission"

# Compute number of number of months per year that are suitable
# (i.e. suitability = 1)
malariaInd <- CST_TotalTimeExceedingThreshold(out$malariaSuit,
                                              op = ">=",
                                              threshold = 1)

# Sneak peek of the data
malariaInd$data[1, 1, 1, , ][15:20, 15:20]
#      [,1] [,2] [,3] [,4] [,5] [,6]
# [1,]    0    0    0    0    0    1
# [2,]    0    0    0    0    0    0
# [3,]    0    0    0    0    0    0
# [4,]    0    0    0    0    0    0
# [5,]    1    0    0    0    0    0
# [6,]    1    1    1    1    1    0

summary(malariaInd$data)
#  Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
#  0.00    0.00    0.00    0.63    1.00    6.00   71852 
```

## 4. Results
### 4.1. Map
Using the function `PlotEquiMap()` from the `s2dv` package, it is possible to have a visual inspection of the number of suitable months of a particular year. In this example, the first and last years of the time series.

```R
PlotLayout(PlotEquiMap, c('lat', 'lon'),
           var = malariaInd$data[1, 1, c(1, dim(malariaInd$data)[[3]]) , , ],
           nrow = 1, ncol = 2,
           lon = malariaInd$attr$lon,
           lat = malariaInd$attr$lat,
           filled.continents = FALSE,
           brks = seq(0, 12, by = 1),
           toptitle = "Number of months per year suitable for malaria transmission in the Iberian Peninsula",
           titles = c(as.character(year_in), as.character(year_fi)),
           title_scale = 0.4,
           coast_width = 2,
           filled.oceans = TRUE,
           country.borders = TRUE,
           intylat = 1,
           intxlon = 1)
```
<img width="70%" src="vignettes/figures/MI_MapSuitability_2022-8.png"/>

### 4.2 Time series
The annual mean number of months suitable for malaria transmission can be extracted with the `MeanDims()` function from the `s2dv` package. Then, a plot can be created with functions from the `dplyr` and `ggplot2` packages. Additionally, the `ggplot()` function allows the user to compute a linear trend in the data.

```R
pt <- data.frame(year = seq(year_in, year_fi, by = 1),
           ind = c(MeanDims(malariaInd$data, c('lon', 'lat'), na.rm = TRUE))) %>% 
  ggplot(aes(x = year, y = ind)) +
  geom_line() +
  scale_x_continuous(breaks = seq(year_in, year_fi, by = 1)) +
  geom_smooth(method = 'lm', formula = y ~ x, alpha = 0.3, linetype = "dashed", linewidth = 0.3, se = FALSE) +
  labs(x = "Year", y = "Mean number of months suitable \n for malaria transmission") +
  theme_bw()
```
<img width="70%" src="vignettes/figures/MI_TimeSeries_2022-8.png"/>

### 4.3 Percentage change
Additionally, it is useful to showcase the percentage change between the first and
last years of the time series, calculated as

```math
PC=100*\frac{Last\:value - First\:value}{First\:value}
```

```R
first <- mean(malariaInd$data[1, 1, 1, , ], na.rm = TRUE)
last <- mean(malariaInd$data[1, 1, dim(malariaInd$data)[[3]] , , ], na.rm = TRUE)
change <- round((last-first) / first * 100, 0)
change
# [1] 47
```

## 5. Conclusions
The number of months suitable for malaria transmission appears to have increased in the Iberian Peninsula between 2010 and 2020. When comparing the year 2020 with the year 2010, there was a 47% increase in the number of months suitable for malaria transmission.

## References

* Alduchov O. Improved Magnus Form Approximation of Saturation Vapor Pressure. _J Appl Meteorl Climatol. 1996; 35(4),601-609._ [URL](https://journals.ametsoc.org/view/journals/apme/35/4/1520-0450_1996_035_0601_imfaos_2_0_co_2.xml)
* Benali A, Nunes JP, Freitas FB, et al. Satellite-derived estimation of environmental suitability for malaria vector development in Portugal. _Remote Sens Environ. 2014;145:116-130._ DOI:10.1016/J.RSE.2014.01.014
* Boualam MA, Pradines B, Drancourt M, Barbieri R. Malaria in Europe: a historical perspective. _Front Med. 2021; 8:876._ DOI:10.3389/FMED.2021.691095/BIBTEX
* Fischer L, Gültekin N, Kaelin M, Fehr J, Schlagenhauf P. Rising temperature and its impact on receptivity to malaria transmission in Europe: a systematic review. _Travel Med Infect Dis. 2020;36:101815._ DOI:10.1016/J.TMAID.2020.101815
* Grover-Kopec EK, Blumenthal MB, Ceccato P, Dinku T, Omumbo JA, Connor SJ. Web-based climate information resources for malaria control in Africa. _Malar J. 2006;5(1):1-9._ DOI:10.1186/1475-2875-5-38/FIGURES/5
* Lyon B, Dinku T, Raman A, Thomson MC. Temperature suitability for malaria climbing the Ethiopian Highlands. _Environ Res Lett. 2017;12(6):064015._ DOI:10.1088/1748-9326/AA64E6
* Muñoz-Sabater J, Dutra E, Agustí-Panareda A, et al. ERA5-Land: A state-of-the-art global reanalysis dataset for land applications. _Earth Syst Sci Data. 2021;13(9):4349-4383._ DOI:10.5194/ESSD-13-4349-2021
* Romanello M, McGushin A, Di Napoli C, et al. The 2021 report of the Lancet Countdown on health and climate change: code red for a healthy future. _Lancet. 2021;398(10311):1619-1662._ DOI:https://doi.org/10.1016/S0140-6736(21)01787-6
* Zhao X, Smith DL, Tatem AJ. Exploring the spatiotemporal drivers of malaria elimination in Europe. _Malar J. 2016;15(1):1-13._ DOI:10.1186/S12936-016-1175-Z/TABLES/5

