#!/bin/bash

## ############################################################################
##
## The 2024 Lancet Countdown in Europe report
## Indicator 1.3.6: Climatic Suitability for Ticks
##
## Script 01:  Compute climatic suitability
## 
## Authors: 
## - Martín Lotto Batista, ORCID: 0000-0002-9437-5270
## - Prof. Rachel Lowe, ORCID: 0000-0003-3939-7343
##
## Contact: martin.lotto@bsc.es
## 
## ############################################################################

module load CDO/1.9.8-foss-2015a

mkdir tmp

cdo -f nc -copy -sellonlatbox,-32,87,27.5,72 -setname,2d d2m_era5_land_global_1951_2022.grib tmp/d2m.nc
cdo -f nc -copy -sellonlatbox,-32,87,27.5,72 -setname,2t t2m_era5_land_global_1951_2022.grib tmp/t2m.nc

cdo -b F32 -setattribute,2d@units="degC" -addc,-273.15 tmp/d2m.nc tmp/d2mc.nc
cdo -b F32 -setattribute,2t@units="degC" -addc,-273.15 tmp/t2m.nc tmp/t2mc.nc

cdo merge tmp/t2mc.nc tmp/d2mc.nc tmp/norh.nc

cdo -selvar,hu -aexpr,'hu=100*(exp((17.625*2d)/(243.04+2d)) / exp((17.625*2t) / (243.04+2t)))' tmp/norh.nc tmp/rh.nc

rm tmp/norh.nc

cdo expr,'hu=hu>=45' tmp/rh.nc tmp/rhs.nc
cdo -setattribute,2t@units="suit" -expr,'2t=(2t>=10)&&(2t<=26)' tmp/t2mc.nc tmp/t2ms.nc 

cdo merge tmp/t2ms.nc tmp/rhs.nc tmp/suit.nc
cdo -setvar,'Climate suitability for malaria transmission' -setattribute,s@units="suit" -expr,'s=(2t==1)&&(hu==1)' tmp/suit.nc tmp/suit2.nc
cdo yearsum tmp/suit2.nc final_out/ixodes_suit_1951_2022.nc

rm -r tmp

# END