This item contains three R scripts: two with functions for SPI calculations, the other demonstrating the procedure used to compute and store SPI values for GPCC and CRU data over South-Western Southern Africa.
The script station_SPI.R contains a function used to compute SPI at a list of point locations, SPI_grid.R contains functions to read in a gridded netCDF monthly rainfall file, compute SPI series at all grid points and write the result to RDS and netCDF files on disk. The code in run_SPI.R uses the functions in SPI_grid.R to compute SPI values at various timescales.
When a function fun from a script file has been sourced, documentation for it can be viewed using: docstring(fun)
Suggested directory structure:
scripts should be run from ${root}/R_scripts as the working directory
GPCC data may be obtained from: http://dx.doi.org/10.5676/DWD_GPCC/FD_M_V2020_025
CRU data are available from: https://crudata.uea.ac.uk/cru/data/hrg/
Using cdo (https://code.mpimet.mpg.de/projects/cdo) the GPCC data (for example, if the original download is named gpcc.nc) can be processed to the required domain using: cdo sellonlatbox,14,26,-35.5,-26 gpcc.nc gpcc_V2020_SWSA_025deg_14-26E_-35.5--26N
For version 2, the function SPImap, erroneously omitted from the original item, has been added and the description updated to fix minor errors.
Funding
South African National Research Foundation (NRF) grant 121047