University of Cape Town
Browse

R code for SPI calculations from monthly gridded or point rainfall data

Version 2 2023-08-10, 06:54
Version 1 2021-09-16, 11:40
software
posted on 2023-08-10, 06:54 authored by Stefaan ConradieStefaan Conradie, Bruce Hewitson, Piotr Wolski

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


|-- 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
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: 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

History

Department/Unit

Climate System Analysis Group

Usage metrics

    Climate System Analysis Group (CSAG)

    Categories

    Licence

    Exports

    RefWorks
    BibTeX
    Ref. manager
    Endnote
    DataCite
    NLM
    DC