Showing posts with label Misc. Show all posts
Showing posts with label Misc. Show all posts

Thursday, July 23, 2015

HCL Color Scales for GrADS

It's been a while since I've posted anything here.  I have been extraordinarily busy with research, and I have not had the time to keep up seriously with this blog.  However, I've recently been involved with several discussions about how to communicate scientific ideas with good figures.  One example of good figure practice is making sure your figures are readable for the color blind.  It's amazing that even today, many popular color scales do not meet this base objective.

Anyway, in these discussions I came across an open access BAMS paper discussing a shift from the traditional RGB color scales to an updated Hue-Chroma-Luminence (HCL) color scale.  So here is a short blog post on how to use this technique with GrADS.

Link to paper:  BAMS HCL paper

The paper includes a link to an online tool used to generate HCL color scales, with an option to output into GrADS RGB format.  So below are a couple of examples of GrADS figures using HCL color scales

Link to the HCL Wizard: http://hclwizard.org/

To access the HCL creator, just click on the link to the Online HCL Creator.  From there it should be pretty easy to follow, simply pick your color scale, and set the export option to GrADS.  It takes a little time to figure out how to use the sliders, but you'll get the hang of it.

A couple of examples from the GFS:





Other color science resources:
Some good information how to use color in your figures: http://betterfigures.org/
Color Brewer, another website with color scale creation, and modification: http://colorbrewer2.org/

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.


Result
Code
Get time step of file (dt)'q ctlinfo'
tdef=sublin(result,7);dt=subwrd(tdef,5)
Get longitude resolution of file (dlon)'q ctlinfo'
dlon=sublin(result,4);dlon=subwrd(dlon,5)
Get latitude resolution of file (dlat)'q ctlinfo'
dlat=sublin(result,5);dlat=subwrd(dlat,5)
Get Number of time steps in file'q file'
t=sublin(result,5);t=subwrd(t,12)
Get Number of x-points in file'q file'
xpts=sublin(result,5);xpts=subwrd(xpts,3)
Get Number of y-points in file'q file'
ypts=sublin(result,5);ypts=subwrd(ypts,6)
Get Number of z-points in file'q file'
zpts=sublin(result,5);zpts=subwrd(zpts,9)
Get current longitude range
(if longitude is varying)
'q dims'
lons=sublin(result,2)
lon1=subwrd(lons,6)
lon2=subwrd(lons,8)
Get current latitude range
(if latitude is varying)
'q dims'
lats=sublin(result,3)
lat1=subwrd(lats,6)
lat2=subwrd(lats,8)
Get page coordinates of a specific lat/lon pair'q w2xy 'lon' 'lat
xpos=subwrd(result,3)
ypos=subwrd(result,6)
Get boundaries of plot in page units'q gxinfo'
xlims=sublin(result,3);ylims=sublin(result,4)
xmin=subwrd(xlims,4);xmax=subwrd(xlims,6)
ymin=subwrd(ylims,4);ymax=subwrd(ylims,6)
Get current time range
(Including day of the week)
'q time'
time1=subwrd(result,3);weekday1=subwrd(result,6)
time2=subwrd(result,5);weekday2=subwrd(result,7)
Get graphics window size in pixels'q xinfo'
xpx=sublin(result,4);xpx=subwrd(xpx,4)
ypx=sublin(result,5);ypx=subwrd(ypx,4)

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

Stat
Code
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)
Maxmax=sublin(result,9);max=subwrd(max,5)
Minmin=sublin(result,9);min=subwrd(min,4)

Tuesday, June 17, 2014

Tutorial: Basic Font Control in GrADS

When using GrADS you will most likely run into a situation where you will have to use superscripts or subscripts, and greek letters.  This is actually really simple to do in GrADS.  This tutorial will provide some basic information on how to control the font in GrADS, including superscript/subscript syntax.

GrADS comes with 5 fonts:
  • font 0 -- Simplex Roman 
  • font 1 -- Complex Roman
  • font 2 -- Complex Italic
  • font 3 -- Complex Greek
  • font 4 -- Duplex Roman
  • font 5 -- Triplex Roman
The simplest way to change the font is by using the 'set font' command.  By default the font is set to value Simplex Roman (value 0).  To change the font using the set font command simply input the desired font number.  For example, to change the font to Complex Italic:

    'set font 2'

Font 3 is where the Greek symbols and characters are.  The image below shows the alphabet and the following Greek character representations of each letter (in both upper and lower case).
 
Greek Fonts 

The other font numbers all follow standard alpha-numeric characters printed out in slightly different styles.  If you want to see the specifics of each font, simply type the word "font" followed by the number you want to learn about.  For example to see the details of the Complex Greek font, simply type:

    'font 3'

If you wanted to mix fonts it would be cumbersome to try to draw a series of "partial" strings next to each other with interspersing 'set font' commands.  Luckily, GrADS has a feature that allows you to change the font within the string.  To change the font within a string, all you need to do is type the accent Grave: [`] (and yes I had to google what this was called) followed by the font number you are interested in.  For example:

    'draw string 4.5 4.5 pi symbol = `3p `0'

 This will write out the words: "pi symbol =" followed by the Greek symbol for pi (π).  The `0 is set in, to reset the font to the default after the symbol. 

You can use this same method to get superscripts and subscripts within your text.  Similarly to the font changes, you use the Grave [`] followed by the font location identifier.  The identifiers are shown below.
  • `a = Above (Superscript)
  • `n = Normal (Normal Text)
  • `b = Below (Subscript)
 So to write out x^2, you would type:

   'draw string x`a2`n'
 
 The `n is once again put in there for housecleaning, so you simply return to the normal font.  Putting it all together, we can write the formula for the area of a circle.

     'draw string 4.5 4.5 A = `3p`0r`a2`n'  
 
 or if π isn't your thing ...

    'draw string 4.5 4.5 A = (`3t`0/2)r`a2`n'


 
That is about it for this tutorial.  Hopefully you have a better understanding on how to use the fonts in GrADS.  I will mention that there is a way to import and use custom fonts, though I will not discuss how to do that here, if you want more information go to this websiteNow, as a final tip, the degrees symbol is represented by the period (.) in the Greek font:

   'draw string 4.5 4.5 `3.`0' 

 

Monday, July 15, 2013

A Few Updates

This post is not going to contain any new scripts/tutorials or information of that matter.  However, due to the (fairly unexpected) popularity of this site as well as a lack of recent updates I figured it might be worthwhile to provide a few updates.

  • The example scripts in a lot of the tutorials open files from the NOMADS data server, a lot of these scripts will not work now, as the dates used are no longer accessible through NOMADS.  This is easily fixed, simply update the date in the filename to be more recent.  So if you are having trouble with the example scripts, this is why.
  • I am currently working on a couple of new scripts, as well as a new tutorial, so keep a look out for these in the new future. 
  • I am working on updates for the NARR Plotter, as well as a few new color scales or colorset.gs

That is pretty much it for the updates, basically, a few new things coming through the pipe in the next week or so.  And remember, if you have any questions or want to learn how to do something that isn't here, let me know and I will see if I can help!

Thursday, May 16, 2013

Some tips on running GrADS from the Windows Command Line and .bat scripts

If you are using GrADS through a linux operating system, chances are you are familiar with running GrADS from the command line, so this post is mostly aimed at the Windows users.  This post will go through the basic instructions for running GrADS from your command line along with arguments and options.  So lets first begin with a question.

Why would you want to run GrADS from the command line if your mouse is perfectly capable of opening GrADS via the "double click?"

The best answer that I can give you is that running GrADS from the command line vs. double clicking gives you the option to include arguments.  Furthermore, the command syntax used is the same used in .bat scripts.

Now, there are a number of different options available that you can include on the command line when you open the program.  The most important is the -help option.  So if you type:

    'grads -help'

Into the command line, you will get a whole list of options you can include when you run the program.

 The options I have found to have been the most useful are these:

  • -l/p: Choosing to open GrADS in landscape or portrait mode
  • -b: running GrADS as a batch process (program is hidden from view)
  • -c: Allows you to execute a command (likely a script)
  • -x: Exits GrADS automatically after executing your command


There are other options that you can see with the -help command, but these are the ones I have found most useful.

Example: grads -lcx 'run script.gs'

This example would open GrADS in landscape mode and run the file "script.gs" and then exit after it was done.

Now, you can actually carry this command line method over to a Windows batch (.bat) script.  Basically, a batch script is similar to a GrADS script, in that it is a file that can execute a bunch of Windows commands at once.  Now, you can include a command to run GrADS with options within your batch script.

Now, this can be very useful as this way you can actually pass arguments from your windows script into your GrADS script.  For example, if you wanted to set your lat/lon boundaries in your windows script, or your display color, you can do that.  All you need to do, is set these variables inside your batch script, and call GrADS with the arguments attached.

Batch Script:
      set display="white"
      set minlat="20"
      set maxlat="50"
      set minlon="-125"
      set maxlon="-65"

     grads -lc 'run script.gs '%display%' '%minlat%' '%maxlat%' '%minlon%' '%maxlon%

GrADS Script:     function script(args)
     display=subwrd(args,1)
     minlat=subwrd(args,2)
     maxlat=subwrd(args,3)
     minlon=subwrd(args,4)
     maxlon=subwrd(args,5)

Now, what you see in the GrADS script, that may be new to you, is the function declaration at the top.  This is required if you want your script to read in the arguments that you laid out in the batch script.  All this line does, is essentially tell GrADS that you are looking for arguments.  You also see in the batch script you have spaces between each argument denoted by the ' '.  This allows you to separate your arguments in GrADS using the subwrd() function.

But the ability to pass arguments from the windows script into the GrADS script can be very helpful, especially if you are running scripts that need variable input (e.g., date or time).  As this method could allow you to run GrADS without having to constantly change the script around.

In any case, I just wanted to provide a few tips on running GrADS from the command line in Windows.  I didn't go too in depth into windows scripting (I leave that to brighter minds), but hopefully you have an idea as to how to run GrADS in a batch script as well as how to pass arguments from one to the other.



Monday, May 13, 2013

Tips on using the finite differencing functions in GrADS (e.g., hdivg, and hcurl): Getting rid the undefined strip along the map boundary

The finite differencing functions in GrADS are pretty handy, especially when it comes to plotting variables such as divergence. The finite differencing function used in GrADS is called cdiff(), and what cdiff() does, is simply take the finite difference of a certain variable. For example, if you wanted to take the chance in u-wind in the x-direction, cdiff would be called as such:

    cdiff(u,x)

It is important to note, that this is not du/dx, as it is taking the chance in u from one grid point to the next, and not the physical distance.  If you wanted du/dx, you would have to multiply the denominator by the grid-spacing.  A good example of using the cdiff function can be found here.

GrADS includes a couple of commonly needed functions that use cdiff: hdivg(), and hcurl().  These function use the finite difference method to plot horizontal divergence and vertical vorticity respectively.  A common issue that people may come across while using this function is the appearance of a "blank strip" along the boarder where the data is not plotted.  An example of this strip is below.
GFS 700hPa divergence (hr-1)

It may be hard to notice here, but you can clearly see a strip of white along the boundary.  The reason this appears is simple enough, basically you are plotting a difference, and at the edge, there is only one point, so there is nothing to plot on the edge points.  The way around this is very simple.  All you need to do, is set the domain one grid point larger than you intend to plot, define your variable, then fix the domain to your plot size, and plot it.  That way, you define the finite difference at the edge of your domain before plotting.  Of course, this method relies on having the data outside of your desired domain.  Anyway, the code to do this is simple enough:

    'sdfopen http://nomads.ncep.noaa.gov:9090/dods/gfs_hd/gfs_hd20130512/gfs_hd_12z'

    'set lat 20 52'
    'set lon -127 -66'
    'set gxout shaded'
    'set mpdset hires'
    'set display color white'
    'clear'

    'set gxout shaded'
    'set lev 700'
    'define var = hdivg(ugrdprs,vgrdprs)*3600'


    'set lat 21 51'
    'set lon -126 -67'

    'd var'
    'cbar'


As you can see the lat/lon boundaries extend out just a little bit farther when defining the variable, then are shrunk when making the actual plot.  The resulting image is:

GFS 700hPA divergence (hr-1)

This image is very similar to the image above, but you now clearly see that there is no "blank" strip along the borders of the image.

The finite difference functions in GrADS can be useful for a whole number of different purposes.  Hopefully these quick tips will help you better use these functions in the future.

A look at the different map projections in GrADS

There are a number of different possible map projections you can use in GrADS, and you may be familiar with some, or all of them, or perhaps you are only familiar with the standard default lat/lon projection used.  This entry will be less of a tutorial and more of just a detailed look at the different map projections you can use in GrADS.  However, before we get started, we do need a little tutorial section, just so you know the syntax for setting the map projection.  Setting the map project is extremely simple: Use the 'set mproj' command and provide it with a map projection from the list below:

1: latlon      Lat/lon projection with aspect ratio maintained (default)
2: scaled      Lat/lon aspect ratio is not maintained; plot fills entire plotting area
3: nps         North polar stereographic
4: sps         South polar stereographic
5: lambert     Lambert conformal conic projection
6: mollweide   Mollweide projection
7: orthogr     Orthographic projection
8: robinson    Robinson projection, requires set lon -180 180, set lat -90 90
9: off         No map is drawn; axis labels are not interpreted as lat/lon


Example:
    'set mproj nps'

That's all there is to it.  Once you have set your projection, you set the lat/lon boundaries as you normally would using the default settings, although certain map projections have intrinsic limits associated with them, which we will explore here.  So, in order to make the examples shown below, we will very simply plot vorticity from the GFS on a bunch of different map projections.  So starting out with the following code we will get this basic image, on the default cylindrical map projection,

    'sdfopen http://nomads.ncep.noaa.gov:9090/dods/gfs_hd/gfs_hd20130513/gfs_hd_00z'
    'set gxout shaded'
    'set lev 600'
    'set mpdset hires'
    'd abs(absvprs)'


Vorticity on a Defualt latlon map projection

So, now that we have that out of the way, lets look at the other map projections.

Scaled:

The scaled map projection does not differ much from the default cylindrical projection, except that it gets rid of the aspect ratio.  Basically, using this option will squish, or elongate the map to fit your specified page area.  The image below shows the transition.  To show the different more dramatically, I set the lat/lon coordinates to -40 to 40 instead of -90 to 90.

latlon vs. scaled map projections

Personally, I don't have much use for the "scaled" map projection, but it is always an option if you need to fit stuff together.

North/South Polar Stereographic NPS/SPS:

The NPS map projection can be useful if you are looking down at the north pole.  Pretend basically that you have the northern hemisphere, and you flatten it.  This would be the north polar stereographic projection.  For the southern hemisphere, simply take the same concept and apply it.  Now, while this map projection does not set latitude boundaries, my rule of thumb has been to set the minimum(maximum, depending on your hemisphere) latitude to be the equator.  I have trouble getting information if I look at southern hemisphere values using an nps projection.  The example below shows each full hemisphere using each projection.  Latitude set from 0 to 90 and -90 to 0.

NPS vs. SPS

Now, you don't need to have longitude set to cover the entire globe either, or the entire hemisphere.

Lambert:

The best way to think about the Lambert map projection is as if the Earth was projected onto a cone.  In fact, often time this projection is referred to as a "conic" projection as well.  Anyway, imagine the Earth is projected onto a cone with the north pole at it's point.  The Lambert projection is basically what the map would look like if the projection was unwrapped and then flattened.  The Lambert projection requires, that your latitude boundaries exist in the same hemisphere, otherwise the projection won't work.  The below example, shows the Lambert projection applied to the northern hemisphere for the longitude coordinates from -180 to 0 (western hemisphere)

Lambert Projection


Mollweide:

The Mollweide projection is often used to show global maps, but in GrADS there are no specific requirements for the latitude/longitude boundaries.  This projection can be neat, as it sort or combines a spherical look with a cylindrical look, with the meridians converging at the pole.  This gives a neat almost 3D appearance.

Mollweide projection

Orthographic Projection:

The orthographic projection is another neat projection that gives a 3D spherical appearance.  In GrADS there are strict requirements for the orthographic projection.  Your latitude boundaries must span -90 to 90.  Additionally, your longitude coordinates must span 180 degrees, no more, no less.  You have the option of choosing your longitude boundaries, so long as they cover 180 degrees of territory.  The result is an image similar to this one.


Orthographic projection


Robinson Projection:

The last map projection I am going to discuss is the "Robinson" Projection (The "off" projection is not really interesting, so I'm not going to talk about it) The Robinson projection is similar to the Mollweide projection, but with the poles kind of flattened out.  This is another commonly used map projection.  In GrADS this projection requires that lat/lon boundaries cover the entire globe.  Lat=-90 to 90, and Lon=-180 to 180 (0 to 360).


Robinson projection

So that's it for the different map projections in GrADS.  As always, each projection is useful depending on the situation, the map, the variable plotted, etc., etc.  I hope this post has given you a little more information regarding each different type of projection.  Just remember that some of them require specific lat/lon boundaries.  Aside from that, picking the right projection is just a matter of trial and error.

Thursday, May 2, 2013

A list of a few (very) useful GrADS scripts

As my second post, I am including a list of what I consider to be essential GrADS scripts for everyone's script library.  This list of scripts is not a master list containing script useful for a large number of unique GrADS applications, but rather a list of general scripts that are applicable to a wide range of uses.  Basically, scripts that everyone should have in their script folder.

Now, often times its nice to just put these scripts in the main GrADS folder, but I have a lot of my own scripts in there, so I made a separate sub-folder to contain these scripts, as they are often used in ALL of my other scripts, so it's nice to separate them.  Just remember, if you do this, you will need to include to full file path in your GrADS script; e.g., your command would be: 'useful_scripts/script.gs' vs. 'script.gs'.


Colors and Color definition
  • Rgbset.gs 
    • This script is a basic script that defines a number of different colors and color shades, greatly increasing your color options is included with the basic color set defined when you start the program.
  • Color.gs
    • This is perhaps the most useful script for defining colors for GrADS available on the internet.  This script makes it easy to construct detailed and continuous color scales without having to write out long and complicated lists using the 'set clevs' and 'set ccols' command.  Color.gs can be used to make the plot below using the following set of commands:
                    'color -30 110 1 -kind white->fuchsia->indigo->blue->cyan->yellow->red->darkred'  
                    'd sfctemp' 
Surface temperature plotted on a continuous scale using color.gs


Color Bars
  •  Cbar.gs
    • This script plots a very basic color bar either below, or to the right of your plot based on where you have the most available space.  This is perhaps the simplest way to plot a color bar in GrADS as it does not require you to pass any additional information.  However with simplicity comes limitations, more advanced color bar options will be addressed later on.
      • Other basic color bar scripts include:
        • cbarn.gs (plots color bar with triangular ends -also a few command options)
        • cbarm.gs   (same as cbarn, but better used with lots of colors -also a few command options)
        • cbarc.gs (plots color bar as a fan in the corner. Example Here)
        • diamondbar.gs (recently added to the GrADS-aholic script library.)
      • While you certainly do not need all of these scripts in your library, they can certainly be useful from time to time 
       

  • Xcbar.gs 
    • This is another script from the folks who brought you color.gs, this script is very useful as it gives you nearly fully control of your color bar, from where it goes, to the type of ends it has, to how even text is plotted next to it, to whether or not it has lines, etc.  As you may have guessed, the color bar plotted in the surface temperature image was made with xcbar.gs.

Other Useful Scripts
  • Hatch.gs 
    • This is another script from the group that developed color.gs and xcbar.gs.  This script is very useful if you want to include "hatching" in your plots.  This script is great, and includes a lot of versatility regarding the hatching style.  I included an example image made using hatch.gs.  The major disadvantage to this script is that it is obnoxiously slow.  Therefore I don't recommend it for use with operational plotting (as this will take forever with more than one or two timesteps), but rather for making figures for papers or presentations.
       
      Example of Hatched plot using hatch.gs

  • Basemap.gs
    • Basemap.gs is great for plotting base maps (more on base maps here).  This script (with associated .asc files) fills in land and water with different colors.  
That's all the basic scripts that I can think of for now.  Again, these are not necessarily scripts that are designed for specific purposes (e.g., plotting a sounding), but rather scripts that can be used to expand the plotting capability of the program itself.  Furthermore, many of the scripts that I upload to this site will require these scripts to function properly.  A more extensive list of scripts can be found here.