Visualizing Weather Patterns in Mathematica 7

MonthName Day, Year

Ulises Cervantes-Pimentel and Andrew Moylan, Kernel Developers, Kernel Technology

Weather visualizations are very interesting---there are television channels that thrive by showing nothing else! Online, there are several sources (http://www.wunderground.com/US/Region/US/2xWindSpeed.html) for specific maps of current weather conditions. Generally these are produced and mantained by government agencies or other large organisations. But with Mathematica 7, you can easily produce completely customizable up-to-date weather visualizations on your own computer!

As usual, this is made possible by Mathematica's tight integration of several areas of functionality. Two features new to Mathematica 7 that enable this particular application are powerful new vector visualization functions (http://www.wolfram.com/products/mathematica/newin7/content/VectorAndFieldVisualization/) and built-in weather data (http://www.wolfram.com/products/mathematica/newin7/content/CurrentAndHistoricalWeatherData/).

Vector visualization has been present in Mathematica since Version 2 (XXXX: check this fact or remove it). In Mathematica 7 it has been dramatically improved, adding modern techniques in vector data visualization and new algorithms developed at Wolfram Research. Traditional arrow-based vector plots, new methods based on automatic stream line placement, support for vector glyphs, and high resolution images produced using line integral convolutions are all part of the new vector visualization system.

(XXXX: show this without the code)

GraphicsGrid[
{{ VectorPlot[{Cos[y]-Sin[x]^3,-.1 y-Sin[x]}
,{x,-2,2},{y,-2,2},VectorColorFunction->"RedBlueTones"],
StreamPlot[{3*x*y*Cos[Norm[{x,y}]]+x*y*Norm[{x,y}]*Sin[Norm[{x,y}]],(2*y^2-x^2)*Cos[Norm[{x,y}]]-x*x*Norm[{x,y}]*Sin[Norm[{x,y}]]},{x,-3,3},{y,-3,3},StreamColorFunction->"Rainbow"]},
{StreamDensityPlot[{Sin[4x],-y},{x,-1,1},{y,-1,1},Mesh->8,LightingAngle->Automatic,ColorFunction->"Pastel"],LineIntegralConvolutionPlot[{{Cos[x+y^3],Sin[y+x^3]},{Automatic,500,128}},{x,-2,2},{y,-2,2},ColorFunction->GrayLevel,LightingAngle->0,LineIntegralConvolutionScale->3]}},ImageSize->Large]

Meanwhile, the new WeatherData (http://reference.wolfram.com/mathematica/ref/WeatherData.html) function provides access to data from all public weather stations worldwide. Real-time and historical data on temperature, pressure, wind speed, and many other variables is immediately available for mathematical analysis and visualization.

Comparing the daily mean temperatures in Cairo and Sydney for the last 3 years is this easy:

DateListPlot[WeatherData[#, "MeanTemperature", {{2006, 1, 1}, {2009, 1,1}, "Day"}]&/@{"Cairo","Sydney"}]

Let's see how to combine these features to produce our own weather maps. Here's a stream plot of the current wind direction across Italy:

Show[Graphics[{GrayLevel[.7],CountryData["Italy","Polygon"]}],ListStreamPlot[Table[{{x,y},-Through[{Sin,Cos}[WeatherData[{y,x},"WindDirection"] °]]},{x,6,18,3},{y,36,48,3}]]]

Not bad for one line of code! We used the CountryData (http://reference.wolfram.com/mathematica/ref/CountryData.html) function, which was introduced in Mathematica 6, to obtain a polygonal representation of Italy.

Now let's build up some more detailed visualizations step-by-step. Here's a function that gives a polygonal outline of a country or region:

outline[region_]:=Graphics[{GrayLevel[0.2],AbsoluteThickness[1.5],CountryData[region,"Polygon"]/.Polygon->Line}];

Stream plots of wind direction like the one we produced above can be a little deceptive, because they don't convey any information about wind speed. We can use WeatherData (http://reference.wolfram.com/mathematica/ref/WeatherData.html) to include wind speed information:

windVelocity[x_,y_]:=-WeatherData[{y,x},"WindSpeed"]{1/Cos[y °],1} Through[{Sin,Cos}[WeatherData[{y,x},"WindDirection"]°]]

Here's a tabulation of  wind velocity vectors as a function of latitude and longitude:

data=Table[{{x,y},windVelocity[x,y]},{x,113,153,4},{y,-43,-11,4}]//Flatten[#,1]&;

These are all the ingredients we need to produce a customized ListVectorPlot (http://reference.wolfram.com/mathematica/ref/ListVectorPlot.html):

Show[ListVectorPlot[data,VectorColorFunction->"RustTones",AspectRatio->Automatic,VectorPoints->25,InterpolationOrder->None,VectorScale->5/100],outline["Australia"]]

Using our wind speed data we can revisit ListStreamPlot. A stream plot is a good option when the positioning of the underlying data points on a rectangular grid might be distracting in a ListVectorPlot. In the following stream plot, we color stream lines according to the local wind speed (brighter colors indicate higher wind speeds):

Show[ListStreamPlot[data,StreamColorFunction->"RustTones",AspectRatio->Automatic,InterpolationOrder->None,StreamPoints->{Automatic,0.5}],outline["Australia"]]

A lot is going on behind the scenes in ListStreamPlot (http://reference.wolfram.com/mathematica/ref/ListStreamPlot.html). Note the way stream lines are automatically positioned to have uniform density across the plot.

Let's further customize our weather visualization. Here's a tabulation of wind velocity and temperature over Africa:

temperatureData= Table[{{x, y}, {windVelocity[x,y], WeatherData[{y, x}, "Temperature"]}}, {x, -20, 50, 5}, {y, -35, 35, 5}]//Flatten[#,1]&//Cases[#,u_/;FreeQ[u,"NotAvailable"]]&;

(The last line filters out weather stations that are currently offline.)

The new function ListVectorDensityPlot (http://reference.wolfram.com/mathematica/ref/ListVectorDensityPlot.html) visualizes vector data together with an accompanying scalar field:

Show[ListVectorDensityPlot[temperatureData,ColorFunction->"TemperatureMap",AspectRatio->Automatic,Mesh->20,VectorPoints->25,VectorStyle->Black,InterpolationOrder->None,MaxRecursion->1,VectorScale->5/100],outline["Africa"]]

A slight variation on the above code (download the <a>source notebook</a> for this article) produces this pressure / wind map of the United States:

XXXX: hide these two input cells:

pressureData= Table[{{x, y}, {windVelocity[x,y], WeatherData[{y, x}, "Pressure"]}}, {x, -126, -66, 3}, {y, 24, 51, 3}]//Flatten[#,1]&//Cases[#,u_/;FreeQ[u,"NotAvailable"]]&;

Show[ListVectorDensityPlot[pressureData,ColorFunction->"IslandColors",AspectRatio->Automatic,Mesh->20,VectorPoints->25,VectorStyle->Black,InterpolationOrder->None,MaxRecursion->1,VectorScale->5/100],outline["UnitedStates"]]

These interesting and informative visualizations of real-time weather conditions require just a few lines of code. Where to from here? We could combine it with Dynamic (http://reference.wolfram.com/mathematica/ref/Dynamic.html) to produce a constantly up-to-date weather map. We could produce an animation of historical weather conditions using ListAnimate (http://reference.wolfram.com/mathematica/ref/ListAnimate.html). We could produce weather visualizations for any country or the whole world, taking advantage of the new integrated Geodesy system (http://www.wolfram.com/products/mathematica/newin7/content/IntegratedGeodesyAndGIS/) to visualize in Mercator or any other cartographic projection. The possibilities are limitless. What will your weather map do?