Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Like this presentation? Why not share!

- 20090701 Climate Data Staging by Henning Bergmeyer 1605 views
- ICLR Friday Forum: Climate data in ... by glennmcgillivray 313 views
- Data recovery saudi arabia by Dolphin Data Lab 97 views
- ESRI User Conference 2014 - A Locat... by Francisco Ramos 489 views
- Market reports on saudi arabia risi... by SharonWilliams123 160 views
- Temperature Experts in Middle East:... by dileep_ps 341 views

4,407 views

Published on

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

No Downloads

Total views

4,407

On SlideShare

0

From Embeds

0

Number of Embeds

114

Shares

0

Downloads

81

Comments

0

Likes

1

No embeds

No notes for slide

- 1. Climate data in R with the raster package<br />Jacob van Etten<br />
- 2. Packages<br />There are many packages specifically created for R that allow you to do specialized tasks.<br />One of these packages is raster, created by Robert Hijmans and Jacob van Etten (mainly the former, though).<br />The raster package allows you to work with geographical grid (raster) data.<br />
- 3. Get raster in RStudio<br />Click on the “Packages” tab in the lower right corner.<br />Click “Install Packages”.<br />Type “raster” and click on “Install”.<br />Leave “Install dependencies” checked. This will also get some other essential packages.<br />
- 4. Load the package<br />With the following command, we load the package into R. Make sure you put this in the first line of your new script.<br />library(raster)<br />help(package="raster")<br />Thesecondfunctiongivesyouanoverview of thefunctions in thepackage.<br />
- 5. The raster() function<br />The main function to read raster data into R is called (very conveniently) raster.<br />?raster<br />Let’s make a raster!<br />r1 <- raster()<br />r1<br />As you can see, there are no values in the raster. Next thing to solve.<br />
- 6. Adding values<br />How many values do we need to fill the raster? The function ncell() will tell us.<br />n <- ncell(r1)<br />Let’s make a vector with n random values between 0 and 1 with the function runif().<br />vals<- runif(n)<br />And we add the values to the raster.<br />values(r1) <- vals<br />
- 7. Raster graphics<br />We make a picture of the raster we just made.<br />plot(r1, main=“My first raster map in R”)<br />Now let’s take a look at the different options that plot() gives.<br />?plot<br />Click “Plot a Raster* object”.<br />Also, take a look at the examples and try some if you want.<br />
- 8. Real data<br />Let’s get some real data to play with.<br />http://goo.gl/4488T<br />This is a raster representing current conditions (a bit over 1 MB).<br />Unzip the file, and put it in a (new) folder.<br />Now make this folder your working directory in R.<br />setwd(“D:/yourfolder”)<br />
- 9. Getting raster data into R<br />Reading this data into R is really easy now.<br />r2 <- raster(“current_bio_1_1.asc”)<br />What class is this raster?<br />class(r2)<br />Plot this raster.<br />
- 10. Cutting an area of interest<br />The function extents requires a vector of 4 values: {xmin, xmax, ymin, ymax}. For instance:<br />newExtent <- extent(c(60, 100, 0, 40))<br />Orchooseyourownarea of interest, forinstanceusing Google Earth.<br />Then cut the new extent out of r2 and visualize.<br />r3 <- crop(r2, newExtent)<br />plot(r3)<br />
- 11. Raster algebra<br />It is very convenient to calculate with rasters.<br />Try this and visualize the result.<br />r4 <- r3 + sqrt(r3)<br />What happens when you do the following and why?<br />r5 <- r2 + r3<br />
- 12. Some operations<br />Aggregating cells means the grid becomes coarser. By default the function aggregate() will take the mean of the cells it will aggregate.<br />r6 <- aggregate(r2, fact=2)<br />Now take a look at the examples under ?aggregate and try to understand what happens.<br />
- 13. Interpolation<br />See if you can work this out for yourself.<br />Take a look at the first example of <br />?interpolate<br />
- 14. Sources of data<br />For an overview of a lot of relevant climate and weather data, visit this website:<br />http://iridl.ldeo.columbia.edu/<br />
- 15. Moreover...<br />Worldclim data are global climate data (get it using the raster package, getData function)<br />NCDC-NOAA – Global Summary of Day, weather data from thousands of stations (weatherData package)<br />CCAFS data <br />
- 16. Worldclim<br />Precipitation at 10 minute resolution<br />wc <- getData(“worldclim”, var=“prec”, res=10)<br />plot(wc)<br />
- 17. Global Summary of Day<br />Available from: ftp://ftp.ncdc.noaa.gov/pub/data/gsod/<br />These data are massive.<br />Use the weatherData package to download these data.<br />

No public clipboards found for this slide

×
### Save the most important slides with Clipping

Clipping is a handy way to collect and organize the most important slides from a presentation. You can keep your great finds in clipboards organized around topics.

Be the first to comment