The Climate Prediction Center's (CPC) global rainfall data, 1979 to present, 0.5 degree resolution (approximately 50 km), is one of the few high quality, long-term, observation-based, daily rainfall products available for free. Although raw data is available at CPC's ftp site, obtaining and processing the data is not straightforward.
Some issues with the raw CPC data:
The package raincpc addresses the above drawbacks by providing functionality to download, process and visualize the raw rainfall data from CPC. Following are some examples.
After installing the package, load the package along with SDMTools, raster, and ggplot2.
library(raincpc)
library(SDMTools)
library(raster)
library(ggplot2)
raincpc comes with the function cpc_get_rawdata
to download the data from CPC. Following example extracts rainfall data for June 15-19 of 2014.
cpc_get_rawdata(2014, 6, 15, 2014, 6, 19)
Once the data is downloaded, the function cpc_read_rawdata
can be used to read the binary data and convert it to a raster for analysis and graphics.
rain1 <- cpc_read_rawdata(2014, 6, 15)
print(rain1)
#> class : RasterLayer
#> dimensions : 360, 720, 259200 (nrow, ncol, ncell)
#> resolution : 0.5, 0.5 (x, y)
#> extent : 0, 360, -90, 90 (xmin, xmax, ymin, ymax)
#> coord. ref. : NA
#> data source : in memory
#> names : layer
#> values : 0, 160.5 (min, max)
Extract the rainfall for all the five days, and then compute the total rainfall for June 15-19. This rainfall total is used in the following graphics.
rain2 <- cpc_read_rawdata(2014, 6, 16)
rain3 <- cpc_read_rawdata(2014, 6, 17)
rain4 <- cpc_read_rawdata(2014, 6, 18)
rain5 <- cpc_read_rawdata(2014, 6, 19)
rain_tot <- rain1 + rain2 + rain3 + rain4 + rain5
print(rain_tot)
#> class : RasterLayer
#> dimensions : 360, 720, 259200 (nrow, ncol, ncell)
#> resolution : 0.5, 0.5 (x, y)
#> extent : 0, 360, -90, 90 (xmin, xmax, ymin, ymax)
#> coord. ref. : NA
#> data source : in memory
#> names : layer
#> values : 0, 287 (min, max)
Here is the plot of total rainfall for June 15-19 2014.
png("gfx_rain1.png", width = 800, height = 600)
plot(rain_tot,
breaks = c(0, 80, 160, 240, 320),
col = c("grey", "red", "green", "blue"),
main = "Rainfall (mm) June 15 - June 19 2014")
garbage <- dev.off()
Although it is easy to display the data using the plot method for a RasterLayer, the quality of the graphic is not great. Here is a plot of the same data using ggplot2. First, create a function to convert the data in a ggplot-friendly format.
raster_ggplot <- function(rastx) {
require(SDMTools)
stopifnot(class(rastx) == "RasterLayer")
gfx_data <- getXYcoords(rastx)
# lats need to be flipped
gfx_data <- expand.grid(lons = gfx_data$x, lats = rev(gfx_data$y),
stringsAsFactors = FALSE, KEEP.OUT.ATTRS = FALSE)
gfx_data$rain <- rastx@data@values
return (gfx_data)
}
Use the above function to plot the RasterLayer object using ggplot2.
rain_gg <- raster_ggplot(rain_tot)
rain_gg$rain_chunks <- cut(rain_gg$rain, breaks = c(0, 80, 160, 240, 320),
include.lowest = TRUE)
gfx_gg <- ggplot(data = rain_gg)
gfx_gg <- gfx_gg + geom_raster(aes(lons, lats, fill = rain_chunks))
gfx_gg <- gfx_gg + scale_fill_manual(values = c("grey", "red", "green", "blue"))
gfx_gg <- gfx_gg + theme(axis.text = element_blank(), axis.ticks = element_blank())
gfx_gg <- gfx_gg + labs(x = NULL, y = NULL, fill = "Rain (mm)")
gfx_gg <- gfx_gg + ggtitle("Rainfall June 15 - June 19 2014")
png("gfx_rain2.png", width = 800, height = 600)
plot(gfx_gg)
garbage <- dev.off()
SDMTools has functionality to extract regional data from the RasterLayer object. In order to extract the data, say over Southeast Asia, specify a lat-lon bounding box as a data frame and extract the data for this box.
lon_vals <- seq(100.75, 130.75, 0.5)
lat_vals <- seq(-10.75, 20.75, 0.5)
reg_box <- expand.grid(lons = lon_vals, lats = lat_vals,
stringsAsFactors = FALSE, KEEP.OUT.ATTRS = FALSE)
reg_box$rain <- extract.data(reg_box, rain_tot)
reg_box$rain_chunks <- cut(reg_box$rain, breaks = c(0, 80, 160, 240, 320),
include.lowest = TRUE)
Use ggplot to plot the regional rainfall over Southeast Asia.
gfx_gg <- ggplot(data = reg_box)
gfx_gg <- gfx_gg + geom_raster(aes(lons, lats, fill = rain_chunks))
gfx_gg <- gfx_gg + scale_fill_manual(values = c("grey", "red", "green", "blue"))
gfx_gg <- gfx_gg + theme(axis.text = element_blank(), axis.ticks = element_blank())
gfx_gg <- gfx_gg + labs(x = NULL, y = NULL, fill = "Rain (mm)")
gfx_gg <- gfx_gg + ggtitle("Rainfall over Southeast Asia June 15 - June 19 2014")
png("gfx_rain3.png")
plot(gfx_gg)
garbage <- dev.off()