Downloading CHIRPS rainfall data

rainfall
R
raster
Author

Will Sheahan, Justin Millar, and Ellen Ferriss

Published

March 1, 2023

About CHIRPS

The Climate Hazards Group InfraRed Precipitation with Station data (CHIRPS) is a quasi-global rainfall dataset spanning 1981 to present. The rainfall data is available as daily, pentadaily (5-day), and monthly raster files at a resolution of 0.05°. This post covers how to obtain CHIRPS data using the PATHtools package for projects requiring areal data, e.g., summary data for health districts or regions. It also provides a brief introduction to the chirps R package, which downloads data at user-defined points, e.g., health facilities or households.

Learn more about CHIRPS at UCSB’s Climate Hazards Center.

Areal data

The PATHtools rainfall() function allows you to extract estimates of average rainfall across one or more areas saved as project shapefiles. First, install PATHtools.

install.packages("devtools", repos = "http://cran.us.r-project.org")
package 'devtools' successfully unpacked and MD5 sums checked

The downloaded binary packages are in
    C:\Users\eferriss\AppData\Local\Temp\RtmpwjwdPW\downloaded_packages
devtools::install_github("PATH-Global-Health/PATHtools")

Next, read in a shapefile of Zambia’s provinces from the package and obtain daily rainfall estimates for each polygon (province). Note the arguments for the daily_rainfall() function: The start_date and end_date are the beginning and end boundaries of your time period, and must be supplied in “YYYY-MM-DD” format. The shapefile shp should be an sf object of the POLYGON or MULTIPOLYGON class.

library(PATHtools)

#Load shapefile
shp <- load_shapefile(country = "Zambia", admin_level = 1)

#Obtain daily rainfall for 2012-01-01 through 2012-01-31 in WIDE FORMAT
start_date <- "2012-01-01"
end_date <- "2012-01-31"
rain_areal <- daily_rainfall(c(start_date, end_date),shp)

#View rainfall data for first 5 days
rain_areal[,1:6]
# A tibble: 10 × 6
   ADM1         `chirps_2012-01-01` `chirps_2012-01-02` chirps…¹ chirp…² chirp…³
   <chr>                      <dbl>               <dbl>    <dbl>   <dbl>   <dbl>
 1 Central                   17.0                12.5     1.66    0.0755  0.783 
 2 Copperbelt                23.1                 6.61    1.59    1.06    4.49  
 3 Eastern                   15.3                28.4     5.44    3.35   14.0   
 4 Luapula                    2.52                3.81    2.97   16.5     6.14  
 5 Lusaka                    17.6                 8.10    0.332   0       0     
 6 Muchinga                   4.34                8.79    9.76   11.3     9.30  
 7 Northern                   0.219               2.43    2.39   19.8    11.1   
 8 Northwestern               7.48                6.93    4.14    1.34    1.33  
 9 Southern                  13.2                 0.784   0.0663  0       0.0169
10 Western                    6.81                4.67    0.210   0.634   0     
# … with abbreviated variable names ¹​`chirps_2012-01-03`, ²​`chirps_2012-01-04`,
#   ³​`chirps_2012-01-05`

The default data format from daily_rainfall() is wide. To obtain the data in long format, set the argument long equal to TRUE.

#Obtain daily rainfall for 2012-01-01 through 2012-01-31 in LONG FORMAT
rain_areal_long <- daily_rainfall(c(start_date, end_date),shp, long = TRUE)

#View rainfall data for first 5 days in Central Province
rain_areal_long[1:5,]
# A tibble: 5 × 3
  ADM1    date       rainfall
  <chr>   <date>        <dbl>
1 Central 2012-01-01  17.0   
2 Central 2012-01-02  12.5   
3 Central 2012-01-03   1.66  
4 Central 2012-01-04   0.0755
5 Central 2012-01-05   0.783 

Finally, to obtain CHIRPS rasters, set the argument output_raster equal to TRUE.

#Obtain daily rainfall rasters for 2012-01-01 through 2012-01-31
rain_areal_raster <- daily_rainfall(c(start_date, end_date), shp, output_raster = TRUE)

#Plot raster for January 1, 2012
terra::plot(rain_areal_raster$`chirps_2012-01-01`)

Point data

To get estimated daily rainfall at specified locations, such as health facilities, we can use the chirps package. Here, we’ll create a data.frame of points and download their data.

install.packages("chirps", repos = "http://cran.us.r-project.org")
package 'chirps' successfully unpacked and MD5 sums checked

The downloaded binary packages are in
    C:\Users\eferriss\AppData\Local\Temp\RtmpwjwdPW\downloaded_packages
library(chirps)

#Create points object
points <- data.frame(lon = c(28.287, 28.213, 28.637, 28.446), lat = c(-15.407, -12.802, -12.959, -14.447))

#Obtain daily rainfall for 2012-01-01 through 2012-01-31
start_date <- "2012-01-01"
end_date <- "2012-01-31"
rain_point <- get_chirps(points, c(start_date, end_date), server = "ClimateSERV")

#View first few rows
head(rain_point)
      id   lon    lat       date chirps
   <int> <dbl>  <dbl>     <date>  <dbl>
1:     1 28.29 -15.41 2012-01-01  14.62
2:     1 28.29 -15.41 2012-01-02   7.31
3:     1 28.29 -15.41 2012-01-03   0.00
4:     1 28.29 -15.41 2012-01-04   0.00
5:     1 28.29 -15.41 2012-01-05   0.00
6:     1 28.29 -15.41 2012-01-06   0.00

Aggregating data from daily to monthly

To get monthly data, we aggregate the above objects.

library(tidyverse)
library(lubridate)

#Aggregate areal data to monthly totals
rain_areal_long_monthly <- rain_areal_long %>%
  group_by(ADM1, month = floor_date(date, "month")) %>%
  summarise(rainfall = sum(rainfall))

rain_areal_long_monthly
# A tibble: 10 × 3
# Groups:   ADM1 [10]
   ADM1         month      rainfall
   <chr>        <date>        <dbl>
 1 Central      2012-01-01     253.
 2 Copperbelt   2012-01-01     325.
 3 Eastern      2012-01-01     325.
 4 Luapula      2012-01-01     259.
 5 Lusaka       2012-01-01     237.
 6 Muchinga     2012-01-01     265.
 7 Northern     2012-01-01     219.
 8 Northwestern 2012-01-01     237.
 9 Southern     2012-01-01     163.
10 Western      2012-01-01     172.
#Aggregate point data to monthly totals
rain_point_monthly <- rain_point %>%
  group_by(lon, lat, month = floor_date(date, "month")) %>%
  summarise(chirps = sum(chirps))

rain_point_monthly
# A tibble: 4 × 4
# Groups:   lon, lat [4]
    lon   lat month      chirps
  <dbl> <dbl> <date>      <dbl>
1  28.2 -12.8 2012-01-01   321.
2  28.3 -15.4 2012-01-01   214.
3  28.4 -14.4 2012-01-01   269.
4  28.6 -13.0 2012-01-01   330.

Citation

BibTeX citation:
@online{sheahan,justinmillar,andellenferriss2023,
  author = {Will Sheahan, Justin Millar, and Ellen Ferriss},
  title = {Downloading {CHIRPS} Rainfall Data},
  date = {2023-03-01},
  url = {https://github.com/PATH-Global-Health/MNTD-tech-docs/posts/rainfall},
  langid = {en}
}
For attribution, please cite this work as:
Will Sheahan, Justin Millar, and Ellen Ferriss. 2023. “Downloading CHIRPS Rainfall Data.” March 1, 2023. https://github.com/PATH-Global-Health/MNTD-tech-docs/posts/rainfall.