Skip to content

Instantly share code, notes, and snippets.

@minrk
Created August 1, 2011 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save minrk/1119098 to your computer and use it in GitHub Desktop.
Save minrk/1119098 to your computer and use it in GitHub Desktop.
#--------------------------------------------------------------------------
#
# Copyright (C) 2007-2008 by Arlindo da Silva <dasilva@opengrads.org>
# All Rights Reserved.
#
# This program is free software# you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation# using version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY# without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program# if not, please consult
#
# http://www.gnu.org/licenses/licenses.html
#
# or write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA
#
#------------------------------------------------------------------------
"""
This module defines "magic commands" for using GrADS interactively
from within iPython. This module is usually imported from the
configuration file "ipythonrc-grads". At any time, on-line help is
available with these commands:
help(ipygrads) This document
qref PyGrADS quick reference
examples Some PyGrADS examples
Based on command line arguments supplied by the user, the top level
script "pygrads" defines key environments variables necessary for this
module to start a GrADS connection, defining the global object "ga".
You can use "ga" to interact with GrADS directly:
ga("q config")
However, a number of shortcuts are provided so that rarely one needs
to explicitly type "ga.cmd(...)". The following generic GrADS commands
are available:
clear/c, define, disable, display/d, draw, enable,
Open/o, Print/pp, printim/pim, Set/s, query/q, reinit
Notice that some commands (e.g., "Open") are capitalized to avoid
conflict with standard Python keywords.
In addition, any generic GrADS command can be entered by starting the
line with a period and a space ('. '), for example example
. open model.ctl
. display ps
The following convenience aliases are provided:
cb run cbarn (for color bar script)
sh set gxout shaded
yat run gxyat
These shortcuts run specific methods from module "grads" on the GrADS
object "ga":
dd $name runs the "imp" method, displaying a NumPy array in
GrADS; this is equivalent to
ga.imp('<display>',$name)
eof $name compute EOS using Singular Value Decomposition; the
*time* dimension contains the eofs; this equivalent to:
$name,$name_d,$name_c = ga.eof('$name')
ii $name runs the "imp" method, importing a NumPy array into
GrADS; this is equivalent to
ga.imp("$name",$name)
oo $fname runs the "open" method, setting "fh"
qq $what runs the "query" method, setting "qh"
xx $name runs the "exp" method, returning a NumPy array and an
associated grid with coordinate information.
This magic command is equivalent to
$name = ga.exp("$name")
$lon = $name.grid.lon
$lat = $name.grid.lat
$lev = $name.grid.lev
$time = $name.grid.time
The shortcuts "dd/ii/xx" are somewhat limited interfaces to the
"imp"/"exp" methods as they do not work on expressions; use
"ga.imp()"/"ga.exp()" directly for a more flexible alternative.
For additional information on these and other methods, consult the
documnentation for the main classes:
help('grads')
help('GrADS')
help('GaNum')
help('GaLab')
help('gacm')
"""
__version__ = '1.0.6'
from grads import *
import os
if HAS_GALAB:
try:
from mpl_toolkits.basemap import cm as bacm
except:
try:
from matplotlib.toolkits.basemap import cm as bacm
except:
print "Cannot load basemap colormaps (bacm)"
from IPython import InteractiveShell
ip = InteractiveShell.instance()
def grads_ (self, arg=' '):
ip.ex("from grads import GrADS; ga=GrADS(%s)"%arg)
def gradsc (self, arg):
ip.ex("from grads import GrADS; ga=GrADS(Bin='gradsc',%s)"%arg)
def gradsnc (self, arg):
ip.ex("from grads import GrADS; ga=GrADS(Bin='gradsnc',%s)"%arg)
def gradshdf (self, arg):
ip.ex("from grads import GrADS; ga=GrADS(Bin='gradshdf',%s)"%arg)
def gacbarn (self,arg):
ip.ex("ga.cmd('run cbarn')")
def gaclear (self,arg):
ip.ex("ga.cmd('clear')")
def gacmd (self, arg):
ip.ex("ga.cmd('%s')"%arg)
def gadefine (self, arg):
ip.ex("ga.cmd('define %s')"%arg)
def gaenable (self, arg):
ip.ex("ga.cmd('enable %s')"%arg)
def gadisable (self, arg):
ip.ex("ga.cmd('disable %s')"%arg)
def gadisplay (self, arg):
ip.ex("ga.cmd('display %s')"%arg)
def gaquery (self, arg):
ip.ex("ga.cmd('query %s')"%arg)
def gapim (self, arg):
ip.ex("ga.cmd('printim %s')"%arg)
def gayat (self, arg):
ip.ex("ga.cmd('gxyat %s')"%arg)
def gapgx (self, arg=' '):
ip.ex("ga.cmd('print %s')"%arg)
def gaopen (self, arg):
ip.ex("ga.cmd('open %s')"%arg)
def gaOpen (self, arg):
ip.ex("fh=ga.open('%s')"%arg)
def gaQuery (self, arg):
ip.ex("qh=ga.query('%s')"%arg)
def gaqref (self, arg):
text = """
Basic GrADS commands:
clear/c, define, disable, display/d, draw, enable,
Open/o, Print/pp, printim/pim, Set/s, query/q, reinit
. $cmd executes any GrADS command $cmd
Convenient aliases:
cb run cbarn (for color bar script)
sh set gxout shaded
yat run gxyat
GrADS Methods:
dd $name runs the "imp" method, displaying a NumPy array in
GrADS; this is equivalent to
ga.imp('<display>',$name)
eof $name compute EOS using Singular Value Decomposition; the
*time* dimension contains the eofs; this equivalent to:
$name,$name_d,$name_c = ga.eof('$name')
ii $name runs the "imp" method, importing a NumPy array into
GrADS; this is equivalent to
ga.imp("$name",$name)
oo $fname runs the "open" method, setting "fh"
qq $what runs the "query" method, setting "qh"
xx $name runs the "exp" method, returning a NumPy array and an
associated grid with coordinate information.
This magic command is equivalent to
$name = ga.exp("$name")
$lon = $name.grid.lon
$lat = $name.grid.lat
$lev = $name.grid.lev
$time = $name.grid.time
"""
ip.ex('print """%s"""'%text)
def gaexample (self, arg):
text = """
Open a file and display a variable:
[1] ga-> q config
[2] ga-> o model
[3] ga-> d ts
[4] ga-> . draw title Surface Temperature (Kelvin)
Export variable "ts", change units in python, and display it in GrADS:
[] ga-> xx ts
[] ga-> ts = ts - 273
[] ga-> c
[] ga-> sh
[] ga-> dd ts
[] ga-> cb
[] ga-> . draw title Surface Temperature (Celsius)
Display the modified variable in Python with Matplotlib:
[] ga-> contourf(lon,lat,ts)
[] ga-> title('Surface Temperature (Celsius)')
[] ga-> figure(2)
[] ga-> plot(lat,ts[:,0]))
[] ga-> title('Surface Temperature (Celsius) at lon=0')
Export an expression, change it in Python, and import it back:
[] ga-> . set lev 300
[] ga-> uv = ga.exp('ua*ua+va*va')
[] ga-> speed = sqrt(uv)
[] ga-> ii speed
[] ga-> d speed
[] ga-> . draw title Wind Speed
[] ga-> cb
If all you wanted to do was to plot the wind speed, you can do this directly,
without nthe need to explicitly importing it first:
[] ga-> dd speed
[] ga-> . draw title Wind Speed
[] ga-> cb
If you have Matplotlib with the Basemap toolkit installed, you can do
most of your plotting in python. Shaded contours can be produced with
the command:
[] ga-> ga.contourf('ts')
[] ga-> title('Surface Temperature')
If instead you would like a continuous color scale do this
[] ga-> clf()
[] ga-> ga.imshow('ts')
If all you want is a plain image covering all of your graphics window
to later be exported to GoogleEarth then
[] ga-> clf()
[] ga-> ga.implain('ts')
A pseudo-color plot, just like one would obtain with "set gxout grfill",
can be produced with
[] ga-> clf()
[] ga-> ga.pcolor('ts')
Here is how to plot a variable on a blue marble background
[] ga-> ga.blue_marble('on')
[] ga-> s lon -180 180
[] ga-> ga.imshow('ua')
[] ga-> title('Zonal Wind')
You can also select your map projection, e.g.
[] ga-> ga.basemap('npo')
[] ga-> ga.contour('zg(lev=300)')
[] ga-> title('300 hPa Heights')
If in any moment you would like to save the graphics window just type
[] ga-> savefig('myfile.png')
For more information type:
[] ga-> help galab
[] ga-> help pylab
Terminating your session:
[] quit()
Computing EOFs (experimental):
% pygrads slp_djf.nc
[] ga-> s t 1 41
[] ga-> eof djfslp
[] ga-> contourf(lon,lat,djfslp[0,:,:])
[] ga-> s t 1
[] ga-> sh
[] ga-> dd djfslp
[] ga-> . draw title NAO Pattern
"""
ip.ex('print """%s"""'%text)
def gareinit (self,arg):
ip.ex("ga.cmd('reinit')")
def gashaded (self,arg):
ip.ex("ga.cmd('set gxout shaded')")
def gaset (self, arg):
ip.ex("ga.cmd('set %s')"%arg)
def gax (self, arg):
ip.ex("%s = ga.exp('%s');lon=%s.grid.lon; lat=%s.grid.lat;lev=%s.grid.lev;time=%s.grid.time"%(arg,arg,arg,arg,arg,arg))
def gaeof (self, arg):
ip.ex("%s, %s_s, %s_pc = ga.eof('%s');lon=%s.grid.lon; lat=%s.grid.lat;lev=%s.grid.lev;time=%s.grid.time"%(arg,arg,arg,arg,arg,arg,arg,arg))
def gai (self, arg):
ip.ex("ga.imp('%s',%s)"%(arg,arg))
def gad (self, arg):
ip.ex("ga.imp('<display>',%s)"%(arg))
# Expose the magic commands
ip.define_magic('grads', grads_)
ip.define_magic('gradsc', gradsc)
ip.define_magic('gradsnc', gradsnc)
ip.define_magic('gradshdf',gradshdf)
ip.define_magic('cb', gacbarn)
ip.define_magic('c', gaclear)
ip.define_magic('clear', gaclear)
ip.define_magic('define', gadefine)
ip.define_magic('d', gadisplay)
ip.define_magic('display', gadisplay)
ip.define_magic('enable', gaenable)
ip.define_magic('examples', gaexample)
ip.define_magic('disable', gadisable)
ip.define_magic('.', gacmd)
ip.define_magic('Open', gaopen)
ip.define_magic('o', gaopen)
ip.define_magic('oo', gaOpen)
ip.define_magic('open', gaopen)
ip.define_magic('pim', gapim)
ip.define_magic('printim', gapim)
ip.define_magic('Print', gapgx)
ip.define_magic('pp', gapgx)
ip.define_magic('yat', gayat)
ip.define_magic('s', gaset)
ip.define_magic('Set', gaset)
ip.define_magic('sh', gashaded)
ip.define_magic('q', gaquery)
ip.define_magic('query', gaquery)
ip.define_magic('qref', gaqref)
ip.define_magic('qq', gaQuery)
ip.define_magic('reinit', gareinit)
ip.define_magic('xx', gax)
ip.define_magic('ii', gai)
ip.define_magic('dd', gad)
ip.define_magic('eof', gaeof)
# Retrieve GraDS options from environment (set by "pygrads" top script)
Bin = os.getenv('GA_BIN',default='gradshdf')
Cmd = os.getenv('GA_CMD',default='')
Opts = os.getenv('GA_OPT',default='')
Files = os.getenv('GA_FIL')
if Cmd!='': Opts = '-c "'+Cmd+'" '+Opts
# if Opts=='': Opts=None
# Instantiate the GrADS object
# If someone has an alternative to making ga global,
# I'd like to know about it.
# --------------------------------------------------
global ga
if HAS_GALAB:
ga = GaLab(Bin=Bin,Opts=Opts)
elif HAS_GANUM and False:
ga = GaNum(Bin=Bin,Opts=Opts)
else:
ga = GrADS(Bin=Bin,Opts=Opts)
# Change prompt to something familiar
ip.prompt_in1 = ' [\#] ga-> '
# reset prompt and colors, to get this config to actually have an effect:
# this setting should really be in an IPython config file, not an exec script,
# in which case these lines would be unnecessary:
ip.init_displayhook()
ip.magic_colors(ip.colors)
# if you set up a Profile, then this should replace the banner, rather than being
# a print statement:
print """
Welcome to PyGrADS, a GrADS-based iPython environment.
For more information, type 'help(ipygrads)'
? -> Introduction and overview of IPython's features.
help -> Python's own help system.
qref -> PyGrADS quick reference
examples -> Some PyGrADS examples
object? -> Details about 'object'. ?object also works, ?? prints more.
"""
if Files:
for file in Files.split(' '):
try:
fh = ga.open(file,Quiet=True)
print "Successfully opened <%s> as File %d"%(file,fh.fid)
except:
print "Failed to open GrADS file <%s>"%file
#!/usr/bin/env python
"""An interactive GrADS shell based on iPython. It requires module
"grads" (interface to GrADS executable via bi-directional pipes) and
module "ipygrads" (iPython shortcuts through definition of convenient
magic commands). The command line interface supports most of the
command line arguments of the classic GrADS. For more information
type
pygrads -h
"""
import os
import sys
import IPython
from string import *
from optparse import OptionParser # Command-line args
try:
import matplotlib # don't import pylab directly
has_pylab = True
except:
has_pylab = False
#---------------------------------------------------------------------
def get_rc ():
"""Returns the RC file name, creating a new if it does not exist."""
rcgrads = """
# File automatically generated by pygrads, do not edit
import numpy, grads, ipygrads
from numpy import *
from grads import *
from ipygrads import *
global ga
"""
# Get RC file name
# ----------------
if os.name is 'nt':
home = os.getenv('HOMEPATH',default='')
pygadir = os.getenv('PYGRADSDIR',\
default=os.path.join(home,'_pygrads'))
rcfname = os.path.join(pygadir,'pygradsrc.ini')
else:
home = os.getenv('HOME',default='')
pygadir = os.getenv('PYGRADSDIR',\
default=os.path.join(home,'.pygrads'))
rcfname = os.path.join(pygadir,'pygradsrc')
# Create pygrads RC file if necessary
# ----------------------------------
if os.path.exists(rcfname):
print 'Using existing pygrads rc file <%s>'%rcfname
else:
# Do we have a rc directory?
# -------------------------
if os.path.exists(pygadir) is False:
try:
os.mkdir(pygadir)
except:
raise IOError, 'Cannot create directory <%s>'%pygadir
# Write default RC file
# ---------------------
try:
fd = open(rcfname,mode='w')
fd.write(rcgrads)
fd.close()
print 'Created pygrads rc file <%s>'%rcfname
except:
raise IOError, 'Cannot create pygrads rc file <%s>'%rcfname
# All done
# --------
return rcfname
#---------------------------------------------------------------------
print ""
print " Welcome to PyGrADS, a GrADS-based iPython environment. "
print ""
# Parse command line options
parser = OptionParser(usage="Usage: %prog [options] [files_to_open]",
version='pygrads-1.0.2' )
parser.add_option("-x", "--binary", dest="gabin", default='grads',
help="path to grads binary (default=grads)" )
parser.add_option("-C", "--classic", dest="gabin", default='grads',
action="store_const", const='gradsc',
help="starts GrADS classic (gradsc)" )
parser.add_option("-D","--dap", dest="gabin", default='grads',
action="store_const", const='gradsdap',
help="starts GrADS DODS (gradsdap)" )
parser.add_option("-d","--dods", dest="gabin", default='grads',
action="store_const", const='gradsdods',
help="starts GrADS with OPeNDAP (gradsdods)" )
parser.add_option("-H","--hdf4", dest="gabin", default='grads',
action="store_const", const='grads',
help="starts GrADS HDF-4 (gradshdf)" )
parser.add_option("-n","--nc", dest="gabin", default='grads',
action="store_const", const='gradsnc4',
help="starts GrADS NetCDF (gradsnc4)" )
parser.add_option("-N","--nc4", dest="gabin", default='grads',
action="store_const", const='gradsnc',
help="starts GrADS NetCDF (gradsnc" )
parser.add_option("-b", "--batch",
action="store_true", dest="batch", default=False,
help="start GrADS in batch mode")
parser.add_option("-c", "--command", dest="gacmd", default='',
help="run a GrADS command on startup (e,g., a script)" )
parser.add_option("-g", "--geometry", dest="geom", default='',
help="set size of graphics window, e.g., 800x600" )
parser.add_option("-l", "--landscape",
action="store_false", dest="port", default=False,
help="start GrADS in landscape mode")
parser.add_option("-m", "--metasize", dest="N", default='',
help="set metafile buffer size to N" )
parser.add_option("--nopylab",
action="store_false", dest="pylab", default=has_pylab,
help="start iPython without pylab")
parser.add_option("-p", "--portrait",
action="store_true", dest="port", default=False,
help="start GrADS in portrait mode")
parser.add_option("-W", "--wideline",
action="store_true", dest="port", default=False,
help="use X server wide lines (faster) instead of s/w (better)")
parser.add_option("-X", "--xmode",
dest="xmode", default='Plain',
help="""Mode for exception reporting. The valid modes are Plain, Context, and Verbose. 'Plain' similar to python's normal traceback printing.
'Context' prints 5 lines of context source code around each line in the traceback. 'Verbose' is similar to Context, but additionally prints the variables
currently visible where the exception happened (shortening their
strings if too long). Default is 'Plain'.""")
(options, filenames) = parser.parse_args()
# Form GrADS command line
gaopt = ''
if options.batch: gaopt = '-b '
if options.geom != '': gaopt = gaopt+"-g "+options.geom+" "
if options.port == False: gaopt = gaopt+'-l '
else: gaopt = gaopt+'-p '
# Define parameters to instantiate GrADS further down
# Note: global variables do not quite work for this,
# hence environment vars
os.environ['GA_BIN'] = options.gabin
os.environ['GA_CMD'] = options.gacmd
os.environ['GA_OPT'] = gaopt
os.environ['GA_FIL'] = join(filenames,sep=' ')
# Reset command line args so that IPython does what we want
# ---------------------------------------------------------
rcfname = get_rc()
# rcfname ignored in IPython 0.11
sys.argv = [ 'pygrads', '--no-banner', '--no-confirm-exit',
'--InteractiveShell.xmode=%s'%options.xmode,
'-i', rcfname
]
if options.pylab:
sys.argv.append('--pylab')
# Start IPython
# -------------
from IPython.frontend.terminal import ipapp
ipapp.launch_new_instance()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment