Julian Ramirez-Villegas
E-mail: j.r.villegas@cgiar.org, dawnpatrolmustaine@gmail.com
International Center for Tropical Agriculture, CIAT
Cali, Colombia
July 01, 2010
General description
Script Name: averageAndMapGCMFields.R
Language: R
Operating system: Windows, Linux
Version: >= 2.10.0
Licence: GPL >= 2.0
Requires packages: raster, rgdal, sp, maptools
This script was created for mapping of GCM anomalies and predicted values (actual
values). The script will provide a function through which you will be able to graph any
set of GCMs and their average, for any of 7 different timeslices, for three different SRES
emission scenarios, and for the baseline climatic scenario.
SRES emission scenarios that are available are SRES-A1B, SRES-A2, and SRES-B1. For
each of these emission scenarios, a set of timeslices are available as averages of 30 years
centered in the decade of interest: 2020s (2010-2039), 2030s (2020-2049), 2040s (2030-
2059), 2050s (2040-2069), 2060s (2050-2079), 2060s (2060-2089) and 2070s (2080-
2099).
For the baseline (20C3M), or 20th century data, the only time slice provided is the
average of the period 1961-1990.
Provided files
- averageAndMapGCMFields.R: The script containing the whole code to
- readme.txt: A text file containing general information
- gcm_chars.csv: A file with the characteristics of the available GCMs. Must be in
the same folder with the “averageAndMapGCMFields.R”
- global_adm0.dbf, global_adm0.shp, global_adm0.shx: Shapefile of country
boundaries at a global scale.
- howTo.doc: This document.
This tutorial comprises 2 simple steps. The first one just needs to be done once when you
are first installing R, whilst the second one needs to be done each time you open R to
perform a new mapping task.
Step 1: Downloading and installing R, and the required packages
Before using this script, you will need to grab the latest version of the R statistical
package from the internet. Go to the official R webpage http://www.r-project.org/, and
click on “download R”, select your mirror (the one from your country, or the closest one
if you don’t find your country). Click on your country’s mirror address, and you’ll be re-
directed to the download page, click on your platform (Linux, MacOS X, Windows), and
then click on “base” to get the R binaries or source files.
For Windows, you will only need the binaries. Download the 32-bit version, just to make
sure all packages work (sometimes they don’t under the 64-bit versions). Click on the
download link. Here we provide the link for R-2.11.1 for Windows (32-bit), using a
mirror located in Colombia:
http://www.laqee.unal.edu.co/CRAN/bin/windows/base/R-2.11.1-win32.exe
Open the executable and install R with the default options. This should give you no errors
at all.
Note: If you are behind a proxy server, you will need to modify your R target. To do
this, go to your R shortcut (on the desktop, the quick launch bar, or the start menu) and
right-click on it. Go to properties.
In the Shortcut tab of the properties window, go to target and write “--internet2” just one
space after the address that appears there. Should look like this:
Now click on apply and then OK. This should allow your installed R package to
download information from the internet passing through your network proxy server. This
option takes the proxy server information from your System settings (in Windows, should
be the Internet Explorer settings).
Now you will need to install some packages that will allow you to manipulate and display
Geographic data. These packages are: raster, rgdal, sp, maptools
Open R (by double clicking on your desktop shortcut or single-clicking your R
quicklaunch or start menu shortcut). Type in the R console:
install.packages(“sp”)
Select your mirror (your country or closest country). This will install the “sp” package
from CRAN. If the download fails, try again until the installation succeeds. Do the same
with the packages “raster”, “rgdal”, and “maptools”.
install.packages(“rgdal”)
install.packages(“raster”)
install.packages(“maptools”)
This should provide you with all the functions you need to source the
“averageAndMapGCMFields.R” script.
Note: This step needs to be done only the first time you’re installing R. Any further
mapping that you intend to do, should start from Step 2.
Step 2: Script sourcing and execution:
A. First of all, you will need to open R, then go to “File”, and click on “Change dir…”,
browse the folder where your two files (averageAndMapGCMFields.R, and
gcm_chars.csv) are located. Now you need to type in the R command prompt:
source(“averageAndMapGCMFields.R”)
We provide three additional files with this script: "global_adm0.dbf", "global_adm0.shx",
and "global_adm0.shp", all conforming a single shapefile (ESRI Shapefile), that can be
used as input to the mapping function in the script.
After sourcing the script, a function will become available:
mapGCMFields()
With the following arguments:
mapGCMFields(gcmList, drive, procdir, scenario, type, period, xn=-180, xx=180, yn=-
90, yx=90, wt=5, worldshapefile, temp=T, prec=T, writeRasterFiles=T)
- gcmList: Numeric vector. Indicates the number of the GCMs to be displayed, eg.
c(1,2,4,6,10,20). This list of GCMs will be automatically displayed after sourcing
the script.
- drive: String. Is the drive letter where the data is stored (eg. W:/). The standard
structure the script will follow is [drive]/climate_change/IPCC_CMIP3/. This is
typically the letter that you used to connect the network drive.
- procdir: String. Is the folder where you want to store your output charts (default
is C:/ for windows versions and /home/username/ for Linux distros). If you either
don’t provide anything or provide a path that does not exist, the function will use
the defaults.
- scenario: String. Can be 20C3M, SRES_A1B, SRES_A2, or SRES_B1, either in
upper or lower case. Anything different will produce an error.
- type: String. Can be “anomalies”, or “actual”, either in upper or lower case.
Anything different will produce an error.
- period: String. Can be 2010_2039, 2020_2049, 2030_2059, 2040_2069,
2050_2079, 2060_2089, 2070_2099. Anything different will produce an error.
- xn: Numeric. Minimum longitude of the bounding box (square area of the world)
you want to graph. Should not be less than -180 degrees.
- xx: Numeric. Maximum longitude of the bounding box (square area of the world)
you want to graph. Should not be greater than 180 degrees.
- yn: Numeric. Minimum latitude of the bounding box (square area of the world)
you want to graph. Should not be less than -90 degrees.
- yx: Numeric. Maximum latitude of the bounding box (square area of the world)
you want to graph. Should not be less than 90 degrees.
- wt: Numeric. Width in inches of the plot (multi-page PDF plots). Big numbers
will cause the PDF to be very heavy, so in this script we have limited the width
between 5 and 15 inches.
- worldshapefile: String. Path to the shapefile of administrative boundaries of the
world, not used if none provided or if the one provided does not exist in your file
system.
- temp: Logical (T/F). Will map temperature data if TRUE
- prec: Logical (T/F). Will map precipitation data if TRUE
- writeRasterFiles: Logical (T/F). Will write ESRI-ASCII files within procdir if
TRUE
B. Now you need to configure your network path so that you are able to access the data.
All the data are available in the network path \\172.22.33.76. You need to connect it
as a network drive.
First, open “My Computer” and click on “Tools” in the menu bar, then click on “Map
Network Drive”. In “Drive”, choose any letter, and in folder, type:
\\172.22.33.76\geodata. Check the “Reconnect at logon” box, and then click on “Connect
using a different user name”. In User name, type “guest”, and in password, type
“nopass”. Click OK, and then click on “Finish”. The network path should now be
connected.
C. You can now execute the function with any specified parameter set, such as:
output <- mapGCMFields(
gcmList=c(1,3,5,10),
drive="W:/",
procdir="C:/CIAT_work/_tools/packageTesting/GCMFields",
scenario="SRES_A1B",
type="anomalies",
period="2010_2039",
xn=-180, xx=180, yn=-90, yx=90,
wt=5,
worldshapefile="C:/CIAT_work/World_Shapefile/Countries/world_adm0.shp",
temp=TRUE,
prec=FALSE,
writeRasterFiles=FALSE
)
This will map the GCMs 1, 3, 5 and 10 (see the list in gcm-chars.csv) for the names of
the GCMs, for the emission scenario SRES-A1B, and the period 2010-2030 (2020s).
The function will calculate anomalies for temperature (temp=T), but not for precipitation
(prec=F), and won’t write the output raster files, but only the summarizing PDF file. It
will use the world shapefile located in the specified folder, and will store everything in
the specified procdir. The width of the PDF sheets is 5 inches, and the extent (geographic
coverage of the file is the whole world, from -180 to 180 degrees longitude, and from -90
to 90 degrees latitude).
D. The result of the function execution will be a PDF file with each page containing the
average predicted temperature and rainfall for the selected time slice, and the selected
set of models (each page containing a model), and the last two pages containing the
average temperature and precipitation among the set of GCMs, and their respective
standard deviations.
Naming of the PDF file is done as follows:
"Figs_[xn]WE[xx]WE[yn]NS[yx]NS_[type]_[scenario]_[period]_[T/P/TP].pdf"
Where the stuff in brackets are parameters of the function, explained in the Script
sourcing and execution section of this document.
If writeRasterFiles=T, the script will write a ESRI-ASCII raster file for temperature (if
temp=T) and a raster for precipitation (if prec=T) for each GCM, and a raster of the
multi-model mean (MMM) and multi-model standard deviation (MMSD). Naming of
these files is:
"AAIGrid_[AMT/TAR]_[xn]WE[xx]WE[yn]NS[yx]NS_[type]_[GCM/MMM/MMSD
]_[scenario]_[period]_[T/P/TP].asc"
Where AMT stands for Annual Mean Temperature and TAR stands for Total Annual
Rainfall. GCM/MMM/MMSD refers to the name of the GCM pattern stored in the file,
the Multi-Model Mean (MMM) and the Multi-Model Standard Deviation (MMSD).
General recommendations and final notes
We suggest you to create a text file as a template so that you don’t need to re-write the
whole set of function arguments each time you need to do some mapping.
Note: Although precipitation and temperature rasters can be mapped together in the same
PDF, we suggest you to map them separately, due to the formatting in some cases (when
the areas are too thin in width or height)