Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mattecologist/744d6c8def2c858897a5 to your computer and use it in GitHub Desktop.
Save mattecologist/744d6c8def2c858897a5 to your computer and use it in GitHub Desktop.
##############################################################################
# title : Download and create raster stack CRU CL2.0 Data.R;
# purpose : create R raster stack of CRU CL 2.0 data;
# producer : prepared by A. Sparks;
# last update : in Los Baños, Laguna, PI May 2014;
# inputs : CRU CL2.0 Climate data;
# outputs : ;
# remarks 1 : Downloads and calculates min/max T and generates raster stacks of precip, min/max/mean T;
##############################################################################
#### Libraries ####
library(raster)
##### End Libraries ####
##### Download and read CRU data files ####
## create a temp file and directory for downloading files
tf <- tempfile()
## mean monthly diurnal temperature range ####
download.file("http://www.cru.uea.ac.uk/cru/data/hrg/tmc/grid_10min_dtr.dat.gz", tf)
dtr <- read.table(tf, header = FALSE, colClasses = "numeric", nrows = 566262) # use header, colClasses and nrows to speed input into R
## mean monthly temperature ####
download.file("http://www.cru.uea.ac.uk/cru/data/hrg/tmc/grid_10min_tmp.dat.gz", tf)
tmp <- read.table(tf, header = FALSE, colClasses = "numeric", nrows = 566262) # use header, colClasses and nrows to speed input into R
## mean monthly precipitation #####
download.file("http://www.cru.uea.ac.uk/cru/data/hrg/tmc/grid_10min_pre.dat.gz", tf)
pre <- read.table(tf, header = FALSE, colClasses = "numeric", nrows = 566268) # use header, colClasses and nrows to speed input into R
pre <- pre[, c(-15, -16, -17, -18, -19, -20, -21, -22, -23, -24, -25, -26)] # remove CVs of precip from table
#### calculate tmax and tmin from tmp and dtr (see: http://www.cru.uea.ac.uk/cru/data/hrg/tmc/readme.txt) #####
tmx <- tmp[, c(3:14)]+(0.5*dtr[, c(3:14)])
tmx <- cbind(tmp[, 1:2], tmx)
tmn <- tmp[, c(3:14)]-(0.5*dtr[, c(3:14)])
tmn <- cbind(tmp[, c(1:2)], tmn)
##### column names and later layer names for raster stack objects ####
months <- c('jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec')
##### GIS work ####
## set up a raster object to use as the basis for converting CRU data to raster objects at 10 arc minute resolution ####
wrld <- raster(nrows = 900, ncols = 2160, ymn = -60, ymx = 90, xmn = -180, xmx = 180)
## Create raster objects using cellFromXY and generate a raster stack
## create.stack takes pre, tmp, tmn and tmx and creates a raster object stack of 12 month data
create.stack <- function(wvar, xy, wrld, months){
x <- wrld
cells <- cellFromXY(x, wvar[, c(2, 1)])
for(i in 3:14){
x[cells] <- wvar[, i]
if(i == 3){y <- x} else y <- stack(y, x)
}
names(y) <- months
return(y)
rm(x)
}
pre.stack <- create.stack(pre, xy, wrld, months)
tmn.stack <- create.stack(tmn, xy, wrld, months)
tmx.stack <- create.stack(tmx, xy, wrld, months)
tmp.stack <- create.stack(tmp, xy, wrld, months)
#eos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment