18
Climate data in R with the raster package Jacob van Etten

Climate data in R with the raster package

Embed Size (px)

DESCRIPTION

Presentation by Jacob van Etten.CCAFS workshop titled "Using Climate Scenarios and Analogues for Designing Adaptation Strategies in Agriculture," 19-23 September in Kathmandu, Nepal.

Citation preview

Page 1: Climate data in R with the raster package

Climate data in R with the raster package

Jacob van Etten

Page 2: Climate data in R with the raster package

Packages

There are many packages specifically created for R that allow you to do specialized tasks.

One of these packages is raster, created by Robert Hijmans and Jacob van Etten (mainly the former, though).

The raster package allows you to work with geographical grid (raster) data.

Page 3: Climate data in R with the raster package

Get raster in RStudio

Click on the “Packages” tab in the lower right corner.

Click “Install Packages”.

Type “raster” and click on “Install”.

Leave “Install dependencies” checked. This will also get some other essential packages.

Page 4: Climate data in R with the raster package

Load the package

With the following command, we load the package into R. Make sure you put this in the first line of your new script.

library(raster)help(package="raster")

The second function gives you an overview of the functions in the package.

Page 5: Climate data in R with the raster package

The raster() function

The main function to read raster data into R is called (very conveniently) raster.?rasterLet’s make a raster!r1 <- raster()r1As you can see, there are no values in the raster. Next thing to solve.

Page 6: Climate data in R with the raster package

Adding values

How many values do we need to fill the raster? The function ncell() will tell us.n <- ncell(r1)Let’s make a vector with n random values between 0 and 1 with the function runif().vals<- runif(n)And we add the values to the raster.values(r1) <- vals

Page 7: Climate data in R with the raster package

Raster graphics

We make a picture of the raster we just made.plot(r1, main=“My first raster map in R”)Now let’s take a look at the different options that plot() gives.?plotClick “Plot a Raster* object”.Also, take a look at the examples and try some if you want.

Page 8: Climate data in R with the raster package

Real data

Let’s get some real data to play with.http://goo.gl/4488TThis is a raster representing current conditions (a bit over 1 MB).Unzip the file, and put it in a (new) folder.Now make this folder your working directory in R.setwd(“D:/yourfolder”)

Page 9: Climate data in R with the raster package

Getting raster data into R

Reading this data into R is really easy now.r2 <- raster(“current_bio_1_1.asc”)

What class is this raster?class(r2)

Plot this raster.

Page 10: Climate data in R with the raster package

Cutting an area of interest

The function extents requires a vector of 4 values: {xmin, xmax, ymin, ymax}. For instance:newExtent <- extent(c(60, 100, 0, 40))Or choose your own area of interest, for instance using Google Earth.Then cut the new extent out of r2 and visualize.r3 <- crop(r2, newExtent)plot(r3)

Page 11: Climate data in R with the raster package

Raster algebra

It is very convenient to calculate with rasters.Try this and visualize the result.

r4 <- r3 + sqrt(r3)

What happens when you do the following and why?

r5 <- r2 + r3

Page 12: Climate data in R with the raster package

Some operations

Aggregating cells means the grid becomes coarser. By default the function aggregate() will take the mean of the cells it will aggregate.r6 <- aggregate(r2, fact=2)Now take a look at the examples under ?aggregate and try to understand what happens.

Page 13: Climate data in R with the raster package

Interpolation

See if you can work this out for yourself.

Take a look at the first example of

?interpolate

Page 14: Climate data in R with the raster package

Sources of data

For an overview of a lot of relevant climate and weather data, visit this website:

http://iridl.ldeo.columbia.edu/

Page 15: Climate data in R with the raster package

Moreover...

Worldclim data are global climate data (get it using the raster package, getData function)

NCDC-NOAA – Global Summary of Day, weather data from thousands of stations (weatherData package)

CCAFS data

Page 16: Climate data in R with the raster package

Worldclim

Precipitation at 10 minute resolution

wc <- getData(“worldclim”, var=“prec”, res=10)

plot(wc)

Page 17: Climate data in R with the raster package

Global Summary of Day

Available from: ftp://ftp.ncdc.noaa.gov/pub/data/gsod/

These data are massive.

Use the weatherData package to download these data.

Page 18: Climate data in R with the raster package