Professional Documents
Culture Documents
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.
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
Now that youre a bit familiar with a few different map projections, lets see how we can
create one for ourselves.
figure;
Now, lets create a map projection for this window. To do this we will want to use the
axesm command:
axesm('MapProjection','ortho','origin',[90,0])
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:
framem
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:
plotm(lat,long,'k')
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:
gridm
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.
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 )
load coast
figure;
axesm('MapProjection','ortho','origin',[90,0])
framem
plotm(lat,long,'k')
gridm
Now save this file and go back to the MATLAB command window and type:
setMap
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!!!
http://userpages.umbc.edu/~ehughes1/MappingExamples/mappingExample1.mat
Download this file, put it in your current working directory, and type:
load mappingExample1.mat
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 1o x 1o resolution, we know that we will need to construct a lat and lon array
as:
gLat = 90:-1:-90;
gLon = 0:359;
[gLat,gLon] = meshgrid(gLat,gLon);
setMap
contourm(gLat,gLon,temp(:,:,1))
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:
contourfm(gLat,gLon,temp(:,:,1))
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:
setMap
contourfm(gLat,gLon,temp(:,:,1))
Now we have a much better looking contour plot. You can add a title to this graph by
typing:
setMap
quiverm(gLat,gLon,u(:,:,1),v(:,:,1))
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:
setMap
contourfm(gLat,gLon,temp(:,:,1))
quiverm(gLat,gLon,u(:,:,4),v(:,:,4),'k')
Notice the usage of k in the quiver command. This tells quiver to plot black arrows
instead of the default blue arrows.
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
temp_zmean = squeeze(temp_zmean)
181 x 18
mlat = 90:-1:-90;
contourf(mlat,plevs,temp_zmean')
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:
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:
set(gca,'ydir','reverse','ylim',[0,1000])
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:
contourf(mlat,plevs',temp_zmean',50)
set(gca,'ydir','reverse','ylim',[0,1000])
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:
min(min(temp_zmean))
max(max(temp_zmean))
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
levels:
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:
contourf(mlat,plevs',temp_zmean',clevels)
set(gca,'ydir','reverse','ylim',[0,1000])
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:
contourf(mlat,plevs',temp_zmean',clevels,'LineStyle','none')
set(gca,'ydir','reverse','ylim',[0,1000])
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) ')
colorbar
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:
figure;
contourf(mlat,plevs',temp_zmean',clevels,'LineStyle','none')
set(gca,'ydir','reverse','ylim',[0,1000])
ylabel('Pressure (mb)')
xlabel('Latitude (degrees)')
title('Zonanl Mean Temperature')
colorbar
title('Zonanl Mean Temperature (K)')
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;
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:
caxis(caxis)
contour(mlat,plevs',u_zmean',ulevels,'k')
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
ehguhes1@umbc.edu.