Thursday, February 19, 2015

Tutorial: Use to make a beautiful map of "SST", Sea Ice and Snow in GrADS

This tutorial will draw upon many of the skills discussed in several other tutorials on this site, e.g., how to handle multiple files at once, or how to use  There isn't too much "new" information in this tutorial on how to use these different features and functions of GrADS, rather this tutorial will show you how to specifically make a very pretty map of sea-surface temperature, sea ice, and snow.  The final outcome is shown below.

For this tutorial you will need:

Snow Cover and Sea Ice on the Robinson Map Projection

So before we start, I want to give a full disclaimer: The plot is not actually showing any observed SST.  In this tutorial I'm going to use the 0.25 degree GFS surface temperature.  In practice the surface temperature should be roughly equal to the SST over the Oceans.

The first thing we need to do is open both the GFS data and the sea ice data.  This data will come from NOMADS and will be opened using the 'sdfopen' command.  Note, the files below are for February 2015, so keep in mind, that you will need to change the date on files if you wish to copy paste the example code below.



  'sdfopen 'gfsfile
  'sdfopen 'icefile

Now that the files are open we will simply set up the map.  Since we are using the Robinson Projection, we need to set the longitude to range from -180 to 180 and the latitude -90 to 90.  

   'set gxout shaded'
   'set mpdset hires'
   'set lon -180 180'
   'set lat -90 90'
   'set mproj robinson'
   'colormaps -l 272 307 0.5 -map jet' ;*Note the use of

Then we display the variable:

   'd tmpsfc'
   'xcbar -fs 4'

After a moment, the surface temperature will be displayed following the "jet" color map.
Now, we are going to mask out the land using the land basemap from

Note, in my version of, I did not want to include the frame around the map projection.  So I opened up and commented out the lines at the bottom that draw the rectangle:

   * Draw a new square frame around the plot
   * If you have 'set frame off' or 'set frame circle' before running basemap,
   * you may want to comment out the next 2 lines
   *'set line 1 1 6'
   *'draw rec 'x1' 'y1' 'x2' 'y2

We are going to use the "medium (M)" resolution option for the coastlines, and we are going to define a custom dark gray color for the land.

   'set rgb 73 80 80 80'
   'basemap L 73 0 M'         ;*L = Land, 73 = Fill Color, 0 = outline Color, M=Medium resolution

SST only!
  This will fill in the land areas with a gray map and you will have an image like this:

Now that we are done with the "SST" and the land mask, we are going to open up the sea-ice file and plot the sea ice.  It is important that you use the maskout function when plotting the sea-ice, otherwise you will overwrite all of the SST data with zeros.

  'set gxout shaded'
  'set dfile 2'                   ;*Sets Default file to file 2 (Sea ice)
  'set z 1'                        ;*Set z to 1
  'set t 1'                        ;*Set t to 1
  'set map 0 1 6'   
  'color 0 0.6 0.1 -kind dimgray->seashell->white'           ;*Note use of
  'd maskout(icecmsl,icecmsl-0.1)'                                     ;*Need to mask out ice.

After a few moments, the sea ice will plot and you will have an image like this:

Sea Ice and SST only!

Now this is all well and good, but our knowledge is limited to the ice over the oceans, we don't really get any sense of how ice and snow covers the land masses.  So the final touch that we'll add to the map is to plot the GFS snow water equivalent (SWE) using a similar color scale as ice cover.  To do that, we need to reset the file to our first file and then reset our time our vertical level.  Lastly, we plot SWE (again using maskout to avoid plotting over everything).

  'set dfile 1'
  'color 10 100 5 -kind dimgray->seashell->white'
  'set z 1'
  'set t 1'
  'set map 0 1 6'
  'd maskout(weasdsfc,weasdsfc-10)'

And after a few moments, you have a map like the one shown at the beginning of the tutorial.
You can play around with your minimum values for ice and snow, I used 0.1 for sea-ice and 10 mm for SWE.  That seemed to produce a good looking map.  Experiment as you like!  This plot is really basic, no special formatting outside of the actual plot (titles, etc).  Hopefully you enjoyed this tutorial, I know it was oddly specific, and didn't really present anything new, hopefully you learned something anyway!

Download Example Script

Wednesday, February 4, 2015

Reference Table of Common Metadata and Statistical Queries

Sorry everyone!  No pretty pictures to go along with this post!

In using GrADS, you likely want to access metadata from the file, for example: The calendar date of your current time step, or your current latitude range.  This post will serve as a reference table that includes code segments for many common metadata queries (most of which can be used to add more data to your plot).  In addition, it will include code segments that use 'set gxout stat' to query basis statistics about your data.  The code within the table can just be copy pasted into your scripts, you will then just have to follow the naming convention that I came up with.

The focus will be on the following queries:
  • 'q dims'
  • 'q file'
  • 'q gxinfo'
  • 'q xinfo'
  • 'q time'
  • 'q ctlinfo'
If you are unfamiliar with these commands, learn more here.

Get time step of file (dt)'q ctlinfo'
Get longitude resolution of file (dlon)'q ctlinfo'
Get latitude resolution of file (dlat)'q ctlinfo'
Get Number of time steps in file'q file'
Get Number of x-points in file'q file'
Get Number of y-points in file'q file'
Get Number of z-points in file'q file'
Get current longitude range
(if longitude is varying)
'q dims'
Get current latitude range
(if latitude is varying)
'q dims'
Get page coordinates of a specific lat/lon pair'q w2xy 'lon' 'lat
Get boundaries of plot in page units'q gxinfo'
Get current time range
(Including day of the week)
'q time'
Get graphics window size in pixels'q xinfo'

Below is a table showing how to use the 'set gxout stat' option to get some basic statistical properties of your variables.  Before any of these are used, you need to first to change your graphics out option to stat: 'set gxout stat' and then display your variable.  Example below:

     'set gxout stat'
     'd rh2m'

This will print out a list of data onto to GrADS text window, then the following table will be useful for pulling out various statistics

N (data points) N=sublin(result,11);N=subwrd(N,5)
Mean mean=sublin(result,12);mean=subwrd(mean,2)
Standard Deviationstd=sublin(result,13);std=subwrd(std,2)