<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Biospherica &#187; R</title>
	<atom:link href="http://joewheatley.net/tag/r/feed/" rel="self" type="application/rss+xml" />
	<link>http://joewheatley.net</link>
	<description>Earth Vegetation</description>
	<lastBuildDate>Tue, 17 Aug 2010 14:01:24 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Visualizing Drought</title>
		<link>http://joewheatley.net/visualizing-drought/</link>
		<comments>http://joewheatley.net/visualizing-drought/#comments</comments>
		<pubDate>Sat, 06 Mar 2010 15:24:43 +0000</pubDate>
		<dc:creator>joe</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[time-series]]></category>
		<category><![CDATA[water stress]]></category>

		<guid isPermaLink="false">http://joewheatley.net/?p=1877</guid>
		<description><![CDATA[The impacts of drought depend on time-scale. On short time-scales, drought means dry soil. On long time-scales, it means dry rivers and empty reservoirs. A region may simultaneously experience dry conditions on one time-scale and wet conditions on another e.g. wet soil but low streamflow or visa versa.
Standardized Precipitation Index (SPI) is a widely used [...]]]></description>
			<content:encoded><![CDATA[<p>The impacts of drought depend on time-scale. On short time-scales, drought means dry soil. On long time-scales, it means dry rivers and empty reservoirs. A region may simultaneously experience dry conditions on one time-scale and wet conditions on another e.g. wet soil but low streamflow or visa versa.</p>
<p><strong><a href="http://iridl.ldeo.columbia.edu/maproom/.Global/.Precipitation/SPI.html" target="_blank">Standardized Precipitation Index</a></strong> (SPI) is a widely used measure of drought which can be defined for any time-scale of interest. For any location, SPI is normally distributed with zero mean and unit standard deviation. Index values &gt; 2 indicate exceptionally wet conditions for that location, values &lt; -2 indicate exceptionally dry conditions for that location, etc. Historical precipitation is the only input needed to compute SPI.</p>
<p>Australia experienced drought between 2002 and 2007. The image below shows SPI computed for a location in the drought-prone Murray-Darling basin of New South Wales. The time-series run from Jan 1948 to Jan 2010 and the index was calculated for time-scales from 1 to 12 months. Precipitation data is from <strong><a href="http://www.esrl.noaa.gov/psd/data/gridded/data.ncep.reanalysis.derived.surfaceflux.html" target="_blank">NCEP Reanalysis</a></strong> [1] in a 1.875° × 1.875° grid cell centred at 30°S 145°E.</p>
<p style="text-align: center;">
<p style="text-align: left;">
<p style="text-align: center;"><a href="http://joewheatley.net/wp-content/uploads/2010/03/SPI.png"><img class="aligncenter size-large wp-image-1888" title="SPI" src="http://joewheatley.net/wp-content/uploads/2010/03/SPI-1024x320.png" alt="SPI" width="1024" height="320" /></a></p>
<p style="text-align: left;">
<p style="text-align: left;">The drought of 2002 to 2007 shows up very clearly. It was preceeded by a wet period between 2005 and 2001. While 2009 showed an episode of severe drought at short time-scales, SPI at was normal/wet at longer time-scales during 2009. Agricultural yields recovered.</p>
<h2 style="text-align: left;">Calculating SPI-<em>M</em></h2>
<p style="text-align: left;">
<p style="text-align: left;">
<p>Empirical rainfall probability distributions are far from normal (gaussian) and often approximate a shifted gamma distribution. The empirical cumulative probability distributions are used to transform the rainfall time-series into time-series of percentile probabilities. A normally distributed precipitation index is found by pretending that these percentile probabilities derive from a standard cumulative normal distribution and inverting to find the index values.</p>
<p>This is simple in <em>R</em>. If the vector <em>data </em>contains rainfall infall data, then:</p>
<p><code>fit.cdf &lt;- ecdf(data)<br />
cdfs &lt;- sapply(data,fit.cdf)<br />
SPI &lt;- qnorm(cdfs)<br />
</code></p>
<p>Tha rainfall data are M-month moving averages (current and previous months). A separate index is calculated for each calendar month to remove seasonality. The R code used to compute SPI values (based in NCEP Reanalysis or other data sets such as <strong><a href="http://www.gewex.org/gpcp.html" target="_blank">GCPC</a></strong>) is <a href="http://joewheatley.net/wp-content/uploads/2010/03/SPI.txt" target="_blank"><strong>here</strong></a>.</p>
<p>[1] The NCEP/NCAR 40-year reanalysis project, Bull. Amer. Meteor. Soc., 77,  437-470, 1996</p>
]]></content:encoded>
			<wfw:commentRss>http://joewheatley.net/visualizing-drought/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ensemble Prediction</title>
		<link>http://joewheatley.net/ensemble-prediction/</link>
		<comments>http://joewheatley.net/ensemble-prediction/#comments</comments>
		<pubDate>Tue, 02 Feb 2010 16:33:23 +0000</pubDate>
		<dc:creator>joe</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[weather forecast]]></category>

		<guid isPermaLink="false">http://joewheatley.net/?p=1770</guid>
		<description><![CDATA[Weather is unpredictable. Small differences in initial conditions can develop into big differences in the pattern of circulation, in the timing and location of cyclones, rainfall etc. This is true no matter how good the initial observing system is.
The approach taken by organisations such as ECMWF or NCEP is to re-run numerical forecast models with [...]]]></description>
			<content:encoded><![CDATA[<p>Weather is unpredictable. Small differences in initial conditions can develop into big differences in the pattern of circulation, in the timing and location of cyclones, rainfall etc. This is true no matter how good the initial observing system is.</p>
<p>The approach taken by organisations such as ECMWF or NCEP is to re-run numerical forecast models with a range of carefully chosen initial conditions. The collection of runs is called the <em>ensemble</em>. Ensemble prediction systems (EPS) give <em>probabilistic forecasts</em> for variables such as rainfall, temperature etc. Current operational EPS have 20 (GFS)  or 51 (ECMWF) ensemble members from which the probability distributions are derived. ECMWF give an overview of their system<strong> <a href="http://www.ecmwf.int/about/information_leaflets/EPS.pdf" target="_blank">here</a></strong>. The probability distributions capture part of the intrinsic uncertainty in weather or climate.</p>
<p>The graph below shows histograms of 20 ensemble member temperatures near some major cities. The data were extracted from NCEP <strong><a href="http://www.nco.ncep.noaa.gov/pmb/products/gens/" target="_blank">GENS</a> </strong>16-day 2m temperature forecast produced at 00UTC 2 Feb 2010 (i.e GFS forecasts for 18 Feb).</p>
<p style="text-align: center;"><a href="http://joewheatley.net/wp-content/uploads/2010/02/ensembleHistograms2.png"><img class="size-large wp-image-1799 aligncenter" title="ensembleHistograms2" src="http://joewheatley.net/wp-content/uploads/2010/02/ensembleHistograms2-1024x985.png" alt="ensembleHistograms2" width="528" height="508" /></a></p>
<p>The maps below show some corresponding ensemble statistics for the entire globe (1° resolution, equal area cylindrical projection).</p>
<p style="text-align: center;"><a href="http://joewheatley.net/wp-content/uploads/2010/02/ensembleStats.png"><img class="size-large wp-image-1793 aligncenter" title="ensembleStats" src="http://joewheatley.net/wp-content/uploads/2010/02/ensembleStats-1024x619.png" alt="ensembleStats" width="1024" height="619" /></a></p>
<p style="text-align: left;">The upper map indicates that forecast uncertainty (standard error) is high between 40° and 60° in both hemispheres (related to the chaotic behaviour of  jet streams.) Currently, 16 day temperatures north of Lake Baikal in Siberia are very uncertain, for example. The contours indicate ensemble median temperatures.</p>
<p style="text-align: left;">Skewness in ensemble temperatures is shown in the lower map. For example, large negative skewness is found in north central US, eastern mediterranean, and Paraguay/Mato Grosso. This suggests tail risk of low temperatures relative to ensemble mean in these areas.</p>
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: center;">
<p>EPS is the future of weather and climate forecasting. These systems produce huge amounts of data. Building useful applications of EPS is both a challenge and an opportunity.</p>
<p>For anyone interested, the <em><strong>R</strong></em> code used to produce these graphs is given <strong><a href="http://joewheatley.net/wp-content/uploads/2010/02/ensemble.R" target="_blank">here</a></strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://joewheatley.net/ensemble-prediction/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NCEP Global Forecast System</title>
		<link>http://joewheatley.net/ncep-global-forecast-system/</link>
		<comments>http://joewheatley.net/ncep-global-forecast-system/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 11:14:32 +0000</pubDate>
		<dc:creator>joe</dc:creator>
				<category><![CDATA[Climate]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[GFS]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[weather forecast]]></category>

		<guid isPermaLink="false">http://joewheatley.net/?p=1192</guid>
		<description><![CDATA[Just about everyone is familiar with weather maps. There are many situations where it is useful to combine the underlying numerical weather data with other types of information. Accessing  the weather data is a necessary first step.
The output from the U.S. National Centers for Environmental Prediction (NCEP) Global  Forecast System (GFS) is freely available. The [...]]]></description>
			<content:encoded><![CDATA[<p>Just about everyone is familiar with weather maps. There are many situations where it is useful to combine the underlying numerical weather data with other types of information. Accessing  the weather data is a necessary first step.</p>
<p>The output from the U.S. National Centers for Environmental Prediction (<strong><a href="http://www.nco.ncep.noaa.gov/" target="_blank">NCEP</a></strong>) Global  Forecast System (GFS) is freely available. The surface resolution of the model is ≈ 0.3º× 0.3°. The model runs every 6 hours, producing forecasts at 3-hourly intervals extending out to 16 days. As an example of output from GFS, the map (below) shows the predicted average  temperature at 2 metres over the entire globe for the next 24 hr (date of this post). The map shows predicted cold conditions in Europe, and the continuing heatwave in Australia.</p>
<p><a href="http://joewheatley.net/wp-content/uploads/2009/12/t2m.jpeg"><img class="aligncenter size-large wp-image-1685" title="t2m" src="http://joewheatley.net/wp-content/uploads/2009/12/t2m-1024x572.jpg" alt="t2m" width="1024" height="572" /></a></p>
<h3>How the map was made</h3>
<p><strong><a href="http://www.nco.ncep.noaa.gov/pmb/products/gfs/" target="_blank">GFS forecasts</a></strong> are in a format called GRIB2. According to Wikipedia, <em><strong>&#8220;</strong>GRIB (GRIdded Binary) is a mathematically concise data format commonly used in meteorology to store historical and forecast weather data.&#8221; </em>GRIB files contain physical fields such as temperature, humidity etc defined on a spatial grid, as well as boundary conditions such as vegetation type and elevation. The data might be assimilated from observations, or output from a forecast model.</p>
<p>The first step is to translate the GRIB into a raster format such as <strong><a href="http://www.unidata.ucar.edu/software/netcdf/" target="_blank">netcdf</a></strong> which can be read in <em>R</em><strong><em>. </em></strong>For example, the GRIB2 file <em>gfs.2009121700/gfs.t00z.sfluxgrbf03.grib2</em> contains the 3-hr forecast surface data on 17 Dec 2009  produced at 00 UTC (midnight universal time). An inventory of the data contained in this file can be seen <strong><a href="http://www.nco.ncep.noaa.gov/pmb/products/gfs/gfs.t00z.sfluxgrbf00.grib2.shtml" target="_blank">here</a></strong>. Download this forecast as <em>temp.grb</em></p>
<p><code>loc=file.path("ftp://ftp.ncep.noaa.gov/pub/data/nccf/com/gfs/prod/gfs.2009121700/gfs.t00z.sfluxgrbf03.grib2")<br />
download.file(loc,"temp.grb",mode="wb")</code></p>
<p>To read <em>temp.grb</em> a utility called <strong><a href="http://www.cpc.noaa.gov/products/wesley/wgrib2/" target="_blank">wgrib2</a></strong> needs to be installed on your system. Then data such as land fraction can extracted into a netcdf file <em>LAND.nc</em> using the <em>R</em> shell command</p>
<p><code>shell("wgrib2 -s temp03.grb | grep :LAND: | wgrib2 -i temp00.grb -netcdf LAND.nc",intern=T)</code></p>
<p>The ncdf package can now be used to read the contents of <em>LAND.nc</em>.</p>
<p><code>library(ncdf)<br />
landFrac &lt;-open.ncdf("LAND.nc")<br />
land &lt;- get.var.ncdf(landFrac,"LAND_surface")<br />
x &lt;- get.var.ncdf(landFrac,"longitude")<br />
y &lt;- get.var.ncdf(landFrac,"latitude")</code></p>
<p>The 1152×576 matrix <em>land</em> takes values 1 for land and 0 for water (sea-ice is 1). x and y are the longitude and latitude of the non-uniform GFS grid.</p>
<p>2m temperature data can be read in the same way. The average of the first 8  forecasts was called <em>t2m.mean</em> and plotted using <em>image.plot()</em> from the fields package:</p>
<p><code>library(fields)<br />
rgb.palette &lt;- colorRampPalette(c("snow1","snow2","snow3","seagreen","orange","firebrick"), space = "rgb")#colors<br />
image.plot(x,y,t2m.mean,col=rgb.palette(200),axes=F,main=as.expression(paste("GFS 24hr Average 2M Temperature",day,"00 UTC",sep="")),axes=F,legend.lab="o C")<br />
contour(x,y,land,add=TRUE,lwd=1,levels=0.99,drawlabels=FALSE,col="grey30") #add land outline </code></p>
]]></content:encoded>
			<wfw:commentRss>http://joewheatley.net/ncep-global-forecast-system/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Mapping Biomes</title>
		<link>http://joewheatley.net/mapping-biomes/</link>
		<comments>http://joewheatley.net/mapping-biomes/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 22:00:15 +0000</pubDate>
		<dc:creator>joe</dc:creator>
				<category><![CDATA[Agriculture]]></category>
		<category><![CDATA[Land Cover Maps]]></category>
		<category><![CDATA[ecosystem model]]></category>
		<category><![CDATA[R]]></category>

		<guid isPermaLink="false">http://joewheatley.net/?p=915</guid>
		<description><![CDATA[Recently (2008) the European Space Agency produced GlobCover (ESA GlobCover Project, led by MEDIAS-France), the highest resolution (300m) global land cover map to date. GlobCover uses 21 primary land cover classes and many more sub-classes. Land cover classification (LCC) schemes divide the earth into biomes. Biomes are the simplest way to classify vegetation which can [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: left;">Recently (2008) the <em>European Space Agency</em> produced <strong><a href="http://ionia1.esrin.esa.int/" target="_blank">GlobCover</a></strong> (<span style="word-spacing: 1.5px; line-height: 1.5;"><span><em>ESA GlobCover Project</em>, led by MEDIAS-France</span></span>), the highest resolution (300m) global land cover map to date. GlobCover uses 21 primary land cover classes and many more sub-classes. Land cover classification (LCC) schemes divide the earth into <em>biomes</em>. Biomes are the simplest way to classify vegetation which can be applied globally. LCC makes sense because the boundaries between different ecosystems (<em>ecotones</em>) are sharp. However, definitions vary and there is no agreed standard set of biomes.<sup>[1]</sup></p>
<p style="text-align: left;">
<h3 style="text-align: left;">GlobCover Example</h3>
<p>Puntarenas is a province on the pacific coast of Costa Rica.  The province has a typical mix of tropical land cover. This includes some spectacular examples of Pacific Rainforest, notably on the Osa Peninsula. Puntarenas has an area of ≈ 11,000 sq. km or about 120,000 GlobCover pixels.</p>
<p style="text-align: center;">
<p>13 land cover types are present in the GlobCover map below. The barplot on the right shows the total amounts present in each class.</p>
<p style="text-align: center;"><a href="http://joewheatley.net/wp-content/uploads/2009/11/puntarenas.png"><img class="aligncenter size-large wp-image-1330" title="puntarenas" src="http://joewheatley.net/wp-content/uploads/2009/11/puntarenas-1024x825.png" alt="puntarenas" width="922" height="743" /></a></p>
<p style="text-align: center;">
<p style="text-align: center;"><a href="http://joewheatley.net/wp-content/uploads/2009/11/legend1.png"><img class="aligncenter size-large wp-image-1212" title="legend" src="http://joewheatley.net/wp-content/uploads/2009/11/legend1-1023x362.png" alt="legend" width="716" height="253" /></a></p>
<p>The GlobCover legend (above) has mixed land cover classes, where more than one biome occurs  inside a map pixel. This is especially true in the man-made biomes (agriculture) . For example, there are three cropland land cover types depending on the relative amounts of other vegetation present.</p>
<h3 style="text-align: left;">GlobCover map making in <em>R</em></h3>
<p><em>R</em> is a programming language, not a specialised geographic information system (GIS) such as GRASS or commercial packages. However applications of <em>R</em> to spatial problems is a growth industry.<sup>[2]</sup> <a href="http://blog.revolution-computing.com/2009/10/geographic-maps-in-r.html" target="_blank"><br />
</a></p>
<p>A GlobCover map similar to the above can be produced for any area of interest. The <em>Geospatial Data Abstraction Library</em> (GDAL) should be installed on your system. <strong><a href="http://fwtools.maptools.org/" target="_blank">FWTools</a></strong> is the place to go. You also need <em>R</em> packages <em>sp</em> and <em>rgdal</em> installed. The regional GLOBCOVER map for Central America can be downloaded from ESA <strong><a href="ftp://us-ext-nas.eo.esa.int/regional/" target="_blank">here</a></strong>. GlobCover is in GeoTiff format i.e. a Tiff image file which contains georeferencing information. The following GDAL command (from command line, or run from R using <em>shell</em>) creates a 4° x 4° submap centred on Costa Rica.<br />
<code><br />
gdalwarp GLOBCOVER_200412_200606_V2.2_CentralAmerica_Reg.tif -te -86 8 -82 12 costaRica.tif<br />
</code></p>
<p style="text-align: left;"><em>costaRica.tif</em> is read into <em>R</em> using the <em>rgdal</em> package:</p>
<p><code>library(rgdal)<br />
costa &lt;- readGDAL("costaRica.tif")</code></p>
<p style="text-align: left;"><em>costa</em> has class <em>SpatialGridDataFrame</em>, which is a class defined in the package <em>sp</em> (loaded when <em>rgdal</em> is loaded).</p>
<p style="text-align: left;">Administative boundaries for Costa Rica were obtained from Global Administrative Areas <a href="http://www.gadm.org/" target="_blank">www.gadm.org</a> (see <strong><a href="http://blog.revolution-computing.com/2009/10/geographic-maps-in-r.html" target="_blank">Revolution R blog post</a></strong>)</p>
<p><code>con &lt;- url("http://gadm.org/data/rda/CRI_adm1.RData")<br />
load(con)<br />
close(con)</code></p>
<p style="text-align: left;">Costa Rican provinces are now contained in the object <em>gadm</em> of class <em>SpatialPolygonsDataFrame</em>. The boundaries of Puntarenas province (excluding Cocos Island) are extracted as follows:</p>
<p><code>Polygons(list(Polygon(gadm@polygons[[6]]@Polygons[[27]]@coords),Polygon(gadm@polygons[[6]]@Polygons[[25]]@coords)),"puntarenas")<br />
temp &lt;- SpatialPolygons(list(temp),proj4string=CRS(proj4string(gadm)))<br />
punt.sp &lt;- SpatialPolygonsDataFrame(temp, data.frame(cbind(2,2), row.names=c("puntarenas")))  # puntarenas<br />
</code></p>
<p style="text-align: left;">The <em>overlay()</em> method  is used to extract the land cover map <em>puntarenas</em> from <em>costa:</em><em> </em></p>
<p style="text-align: left;"><code>puntarenas &lt;- costa<br />
puntarenas.overlay &lt;- overlay(costa,punt.sp)  # 1 in interior of puntarenas polygons, 0 outside<br />
puntarenas$band1 &lt;- costa$band1*puntarenas.overlay</code></p>
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">Unfortunately <em>overlay() </em>is rather slow, because it applies <em>point.in.polygon()</em> to the entire raster. Eventually <em>puntarenas<em> </em></em>appears as a<em> SpatialGridDataFrame</em><em> </em>which can be plotted using standard <em>R</em> tools such as <em>image()</em>.</p>
<p style="text-align: left;">The code needed to generate the above plot is <strong><a href="http://joewheatley.net/wp-content/uploads/2009/11/costa.R" target="_blank">here</a></strong>.</p>
<h3 style="text-align: left;">footnotes</h3>
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">[1] For example, the International Biosphere-Geosphere Programme (<strong><a href="http://www.igbp.net/">IGBP</a></strong>) land cover legend used 17 biomes. The University of Maryland map used 14 biomes. At much lower resolution, numerical weather forecasting models such as US National Center for Climate Prediction Global Forecasting System (NCEP-GFS) also use alternative schemes.</p>
<p style="text-align: left;">
<p style="text-align: center;"><a href="../wp-content/uploads/2009/11/LCTable.png"><img title="LCTable" src="../wp-content/uploads/2009/11/LCTable.png" alt="LCTable" width="526" height="306" /></a></p>
<p style="text-align: center;">
<p style="text-align: left;">[2] Roger S. Bivand, Edzer J. Pebesma, and Virgilio Gómez-Rubio.  <em>Applied Spatial Data Analysis with R</em>.  Springer, New York, 2008</p>
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
]]></content:encoded>
			<wfw:commentRss>http://joewheatley.net/mapping-biomes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.396 seconds -->
