TEXT
TEXT
1/1
R code for SPI calculations from monthly gridded or point rainfall data
software
posted on 2023-08-10, 06:54 authored by Stefaan ConradieStefaan Conradie, Bruce Hewitson, Piotr WolskiThis 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
|-- data
||- CRU_404_05deg_SWSA_16-28E_-35--23N.nc
|`- gpcc_V2020_SWSA_025deg_14-26E_-35.5--26N.nc
|-- figures
|-- R_scripts
||- run_SPI.R
||- SPI_grid.R
|`- station_SPI.R
Required R pacakges:
SCI
tidyverse
SCI
tidyverse
data.table
metR
docstring
magrittr
ncdf4
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:
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.