Вы находитесь на странице: 1из 22

An Introducti on to Meteorologi cal Tools in MATLAB

By Eric Hughes (ehughes1@umbc.edu)

The follow tutorial is intended to give the reader a quick overview of many
Meteorological applications of the many tools in MATLAB. The first part of the tutorial
will focus on the usage of the Mapping Toolbox. Here we will look at how to create map
projections and over-plot data onto them. The second part will look at a specific example
of over-plotting contour maps.

All the code used in this tutorial can be found at:

The data used in this tutorial can be found at:

Th e Ma p p i n g Ax i s

MATLAB offers a wide variety of powerful tools for projecting data onto global maps.
My intention here is to list a few examples using these tools in the simplest manner

Before we plot any data, we must first specify the type of map projection that we
want to use. Here are a few examples of some common map projections:

mercator eckert4

ortho stereo

Se t t i n g Up A Ma p Pr o j e c t i o n

Now that youre a bit familiar with a few different map projections, lets see how we can
create one for ourselves.

First, open up a new plotting window:


Now, lets create a map projection for this window. To do this we will want to use the
axesm command:


In the previous line we told axesm to make a ortho projection, 'MapProjection','ortho' ,
and centered this projection at the coordinates: Lat: 90 , Lon: 0 with the option
origin',[90,0] .

Use the framem command to put a frame around your mapping region:


Now that we have our axis setup, we will generally want to overlay the continents on this
projection. MATLAB provides the data necessary to draw the continents onto a map in
the file: coast.m:

load coast;

Loading this file gives us two new variables (which are put into current workspace): lat
and long. Well want to plot this data onto our map projection to view the continents:


Since we centered our projection at Lat: 90 and Lon 0, we see a nice map of the north
pole. The k option was used to tell plotm to plot the continental coastline in black.

It is often useful to put gridlines on a map; this is easily done with the command:


OK. So far we have manage to create map based on a specified projection type and
centered it on the North Pole. The rest of this tutorial will involve projecting data onto
this map, so it would be useful if we could create a shortcut that allowed us to make
these maps quicker. The easiest way to do this would be to create a script file that
contained all of the commands that we just wrote.

To do this in MATLAB, type

edit setMap.m

This will open the MATLAB file editor and create the file setMap.m ( you can choose
any filename that you would like, as long as it ends in .m )

In the editor, paste the following commands:

load coast


clear lat long;

Notice that this is everything these are all the commands that we just used, with the
exception of the last line. The last line clear lat lon merely remove the coastline
variables lat and long from our workspace. Its good practice to remove these variables
from your workspace after you have created your map. Otherwise, thing will become
more confusing when you are actually plotting data onto your map.

Now save this file and go back to the MATLAB command window and type:


Pretty cool, huh? Now, anytime that we want to create a map projection we just need to
type setMap.

OK. Now that weve taken a look at creating maps lets do something a bit more useful.
Lets plot some data on this map!!!

Pl o t t i n g Da t a o n a Ma p Pr o j e c t i o n

The data that Ill be using for the rest of these examples can be found at:


Download this file, put it in your current working directory, and type:

load mappingExample1.mat

This file contains the following variables:

data Information about the data (where it came from, the time it represents, )
gridRes the horizontal resolution of the data (lat x lon)
plevs the pressure level heights
temp temperature
u zonal wind speed
v meridional wind speed

When you load this data into your workspace, youll see:

Before we go too much further, well need to discuss a minor detail associated with
plotting data on a map projection. For each data point that we have (like temperature
data) we need to tell a plotting routine where to place this point on the map. Since this
data has a 1
x 1
resolution, we know that we will need to construct a lat and lon array

gLat = 90:-1:-90;
gLon = 0:359;

We now have arrays that look like the following:

glat: [90 ! -90] with dimensions (1 x 181)
glon: [0 ! 359] with dimensions (1 x 360)
temp: [ ??????? ] with dimensions (360 x 181)
For some routines (like any contour routines) you can input the following three arrays
and the routine will understand the where to place the temp data on the globe; based on
the dimensions of the data. Unfortunately, some routines are not that smart (like
quiverm) and you will need to tell them the lat and lon coordinates for every single data
point. Since well be using the quiver routine later on, well assume the routines are not
too intelligent and construct lat and lon arrays that associate a coordinate for every single
data point. In other words, we want:

glat: [90 ! -90] with dimensions (360 x 181)
glon: [0 ! 359] with dimensions (360 x 181)
temp: [ ??????? ] with dimensions (360 x 181)

This can be done by using the following command:

[gLat,gLon] = meshgrid(gLat,gLon);

Alright, now lets have some fun with this data!

Ma k i n g Co n t o u r Pl o t s o n Ma p Pr o j e c t i o n s

First, lets get our map projection:


To create a non-filled color contour, use the following command:


Notice that here we have made a contour of the data temp(:,:,1), which corresponds to the
horizontal temperature data on the plevs(1) height. Typing in plevs(1) into the command
window tell us that this is the 1000 mb height.

Now this plot looks OK, but a filled contour plot would look much better To make a
filled contour plot, type:


This plot looks a lot better . but what happened to the continents!!! I dont really have
a great explanation for why this happens, but I do have a very good rule to follow.
Whenever you do a new contour plot, reset your map projection. Thus, if we type:


Now we have a much better looking contour plot. You can add a title to this graph by

title('Temperature on the 1000 mb Surface')

Vi s u a l i z i n g Ve c t o r Fi e l d s

Since we have wind field data, it would be nice if would create a nice plot to visualize
this data. Fortunately, MATLAB makes this very simple with the quiver plotting
command. Using our map projection, lets create an arrow plot using quiver:

title('Wind Field on the 1000 mb surface')

Although this plot looks a bit busy, we can zoom in on it so see smaller features.

We can also use both the contourfm and quiver plotting commands to project the wind
fields onto a contour plot of the temperature:


Notice the usage of k in the quiver command. This tells quiver to plot black arrows
instead of the default blue arrows.

Zo n a l Me a n Pl o t s

Were going to take a break from map-projected plots to look at zonal mean plots. These
plots are fairly simple to construct, we just need to average over the zonal component of
the data. There are several ways to do this, but by far the simplest way to do this is to use
the dimension option on MATLABs mean function. Since our data is of the form:

360 x 181 x 18

We can see that the zonal component of this data is in the first dimension (360). Thus,
we can average over this dimension with:

temp_zmean = mean(temp,1)

However, the output of this procedure creates temp_zmean with the dimensions:

1 x 181 x 18

This first dimension is useless and will need to be removed:

temp_zmean = squeeze(temp_zmean)

and the dimension for temp_zmean is now:

181 x 18

As always, well need to tell MATLAB where to plot this data:

mlat = 90:-1:-90;

We can now do a simple contour of temp_zmean with the command:


Wow, this contour plot is pretty crummy how can we make it look better? Before I
answer that question, I need to explain something that I did in the previous line.

Notice that I used temp_zmean' instead of temp_zmean. Why did you use a
here? First of all, is MATLABs transpose operator. Since we gave
MATLAB mlat and plevs as vectors, the inputs looked like:

Inputs: mlat plevs temp_zmean
Input Dimensions: (181 x 1) (18 x 1) (18 x 181)

** MATLAB uses the order (Rows x Columns)

where mlat is the x-axis dimension (or number of columns) and plevs is the
y-axis dimension (or number of rows). If we had not used the transpose
operator here, we would be inputting:

Inputs: mlat plevs temp_zmean
Input Dimensions: (181 x 1) (18 x 1) (181 x 18)

and MATLAB would have given us an error.

OK, now lets try and fix this plot! First lets fix the axis. In the atmosphere, pressure will
generally range from 1000 mb ! 0 mb as the height increases from 0 km ! ! km. So
lets make our plot a bit more intuitive by making the pressure scale range from 1000 mb
! 0 mb:


Im not going to go into any real detail here in explaining what this line does, just know
that this is basically flipping the y-axis. Alright, now lets try and fix the range of this
contour plot.
Looking at this contour plot, it appears as if the contour routine is only drawing 2
contour (this isnt really true, but it appears that way). So lets first try and fix this by
telling the routine to plot more contours:


Here we told the contourf routine to plot 50 contours. But wait, I still dont see 50
contours?!?! Alright, well lets check the range of our data:


This tells us that our data ranges from -999 to ~300 K. By default, all contour routines
will define the contour levels by performing a linear interpolation between the min data
value and max data value. Therefore, when we tell contourf to make 50 contours, the
routine will make 50 equi-spaced contour levels between -999 and 300 K. But wait, you
cant have a temperature value of -999?!?! Many data models and instruments will use
the value -999 as a flag to signify that the data at this location is bad. Therefore, we
would like to set our contours to range between something more reasonable; perhaps 180
K ! 300 K. To do this, well need to give contourf a vector of predefined contour

clevels = 180:((300-180)/29):300;

This gives us a total of 30 values that range from 180 ! 300. Now, lets input this into
the contourf routine:


Now thats a pretty good looking zonal mean contour plot! But what if we wanted to
remove those black contour lines (youll see the importance of this later). This can be
easily done specifying the line style in the contourf command:


And now the black contour lines are gone. Now that we have our contour plot looking
quit pretty, lets label our axis:

ylabel('Pressure (mb)')
xlabel('Latitude (degrees)')
title('Zonanl Mean Temperature (K) ')

Now thats a good-looking plot!!!

As a final note, Id like to show you how to overplot one contour plot onto another. Lets
start with the contour plot that we just made:

ylabel('Pressure (mb)')
xlabel('Latitude (degrees)')
title('Zonanl Mean Temperature')
title('Zonanl Mean Temperature (K)')

Ov e r - Pl o t t i n g Mu l t i p l e Co n t o u r s

Now lets over-plot the mean zonal winds atop this zonal mean temperature plot. First,
well need to get the zonal mean of the zonal winds:

u_zmean = squeeze(mean(u,1));

Now lets plot this on top of our current plot. First well need to tell MATLAB to hold the
current plot:

hold on;

** Notice that we didnt have to do this with the mapping plots. By
default, the mapping tools will have this set as on.

We also want to keep the current color scale that we have (as seen on the color bar), so
well need to tell MATLAB to keep this fixed:


Now lets plot another contour on top of the current one:


Now you can see why we choose to remove the black contour lines on our original
temperature contour plot.

Overall, these examples are just a few simple examples of MATLABs graphical power.
As always, a full list of any of these functions and many more are listed in MATLABs
documentation files (type doc in the command window for quick access).

I hope that these examples have helped give you an introduction into MATLABs
Mapping Toolbox and other various uses frequently needed in the field of Atmospheric
Physics. A special thanks is given to Dr. Wallace Mcmillan for advice on certain
MATLAB tips.

Feel free to e-mail me any questions, comments, or suggestions regarding this tutorial at