<p><b>mhoffman@lanl.gov</b> 2013-03-07 12:49:09 -0700 (Thu, 07 Mar 2013)</p><p>BRANCH COMMIT - land ice<br>
<br>
Combined the viz scripts for plotting and animating an mpas land ice field.  Also changed the default behavior so only cells with ice are plotted.  Also added some logic to make a guess at the proper point size for plotting the scatter plot.  Check the help for more info on the various options.<br>
</p><hr noshade><pre><font color="gray">Deleted: branches/land_ice_projects/grid_tools/animate_mpas_field.py
===================================================================
--- branches/land_ice_projects/grid_tools/animate_mpas_field.py        2013-03-07 17:55:03 UTC (rev 2553)
+++ branches/land_ice_projects/grid_tools/animate_mpas_field.py        2013-03-07 19:49:09 UTC (rev 2554)
@@ -1,166 +0,0 @@
-#!/usr/bin/python
-# Script capable of plotting most MPAS fields.  Invoke with 'python plot_mpas_field.py --help for details about how to use.
-# Matt Hoffman, June 14, 2012
-
-import numpy, time
-from optparse import OptionParser
-import matplotlib.pyplot as plt
-try:
-  from Scientific.IO.NetCDF import NetCDFFile
-  netCDF_module = 'Scientific.IO.NetCDF'
-except ImportError:
-  try:
-    from netCDF4 import Dataset as NetCDFFile
-    netCDF_module = 'netCDF4'
-  except ImportError:
-      print 'Unable to import any of the following python modules:'
-      print '  Scientific.IO.NetCDF </font>
<font color="gray">  netcdf4 '
-      print 'One of them must be installed.'
-      raise ImportError('No netCDF module found')
-
-
-print &quot;** Gathering information.  (Invoke with 'python plot_mpas_field.py --help' for more details. All arguments are optional)&quot;
-parser = OptionParser()
-parser.add_option(&quot;-f&quot;, &quot;--file&quot;, dest=&quot;filename&quot;, help=&quot;file to visualize; default: output.nc&quot;, metavar=&quot;FILE&quot;)
-parser.add_option(&quot;-v&quot;, &quot;--var&quot;, dest=&quot;variable&quot;, help=&quot;variable to visualize; use 'uMag' for velocity magnitude; default: thickness&quot;, metavar=&quot;VAR&quot;)
-parser.add_option(&quot;-l&quot;, &quot;--level&quot;, dest=&quot;vertlevel&quot;, help=&quot;vertical level to visualize (0 based); default: 0&quot;, metavar=&quot;LEVEL&quot;)
-parser.add_option(&quot;-s&quot;, &quot;--save&quot;, action=&quot;store_true&quot;, dest=&quot;saveimages&quot;, help=&quot;include this flag to save animation as series of .png files&quot;)
-parser.add_option(&quot;-n&quot;, &quot;--nodisp&quot;, action=&quot;store_true&quot;, dest=&quot;hidefigs&quot;, help=&quot;include this flag to not display plot after animation is complete&quot;)
-parser.add_option(&quot;-x&quot;, &quot;--max&quot;, dest=&quot;maximum&quot;, help=&quot;maximum for color bar&quot;, metavar=&quot;MAX&quot;)
-parser.add_option(&quot;-m&quot;, &quot;--min&quot;, dest=&quot;minimum&quot;, help=&quot;minimum for color bar&quot;, metavar=&quot;MIN&quot;)
-parser.add_option(&quot;-g&quot;, &quot;--log&quot;, action=&quot;store_true&quot;, dest=&quot;log&quot;, help=&quot;include this flag to plot on log scale&quot;, metavar=&quot;LOG&quot;)
-parser.add_option(&quot;-k&quot;, &quot;--mask&quot;, action=&quot;store_true&quot;, dest=&quot;mask&quot;, help=&quot;include this flag to mask non-ice cells with gray&quot;, metavar=&quot;MASK&quot;)
-parser.add_option(&quot;-p&quot;, &quot;--printval&quot;, action=&quot;store_true&quot;, dest=&quot;printval&quot;, help=&quot;include this flag to print numeric cell values (slow)&quot;, metavar=&quot;PRINTVAL&quot;)
-
-options, args = parser.parse_args()
-
-if not options.filename:
-        print &quot;No filename provided. Using output.nc.&quot;
-        options.filename = &quot;output.nc&quot;
-
-if not options.vertlevel:
-        print &quot;No vertical level provided. Using level 0 (surface), if level is needed.&quot;
-        vert_level = 0
-else:
-        vert_level = int(options.vertlevel)
-        print &quot;Using vertical level &quot; + options.vertlevel
-
-if not options.variable:
-        print &quot;No variable provided. Using 'thickness'.&quot;
-        varname = 'thickness'
-else:
-        varname = options.variable
-        print &quot;Using variable &quot; + varname
-
-
-f = NetCDFFile(options.filename,'r')
-
-# Get grid stuff
-xtime = f.variables['xtime']  # Not needed unless trying to print actual time stamp
-xCell = f.variables['xCell']
-yCell = f.variables['yCell']
-xEdge = f.variables['xEdge']
-yEdge = f.variables['yEdge']
-angleEdge = f.variables['angleEdge']
-
-# get the requested variable
-if varname == 'uMag':
-   uReconstructX = f.variables['uReconstructX']
-   uReconstructY = f.variables['uReconstructY']
-   var = numpy.zeros(uReconstructX.shape)
-   var[:] = (uReconstructX[:] ** 2 + uReconstructY[:] **2)**0.5
-   dims = uReconstructX.dimensions
-else:
-   var = f.variables[varname]
-   dims = var.dimensions
-
-# Determine what the appropriate X &amp; Y values are:
-if 'nCells' in dims:
-   x = xCell[:]
-   y = yCell[:]
-elif 'nEdges' in dims:
-   x = xEdge[:]
-   y = yEdge[:]
-elif 'nVertices' in dims:
-   x = xVertex[:]
-   y = yVertex[:]
-
-if options.mask:
-        thickness = f.variables['thickness']
-
-# Get the needed slice and determine the plot title.  Make some assumptions about how dimensions are arranged:
-plottitle = varname
-if 'Time' in dims:
-   time_length = var.shape[0]  # Assume time is the first dimension
-   if 'nVertLevels' in dims:
-      plottitle = plottitle + ', for layer ' + str(vert_level) 
-      var_slice = var[:,:,vert_level]
-   else:
-      var_slice = var[:,:]
-else:
-   print &quot;Time is not a dimension of this variable.  Unable to animate it!&quot;
-   sys.exit()
-
-if options.log:
-        var_slice = numpy.log10(var_slice + 1.0e-5)
-        plottitle = plottitle + ', log scale'
-
-# Determine color axis max &amp; min values to use.
-if not options.maximum:
-   maxval = var_slice[:].max()
-else:
-   maxval = float(options.maximum)
-
-if not options.minimum:
-   minval = var_slice[:].min()
-else:
-   minval = float(options.minimum)
-
-
-# MAKE THE PLOT
-print '** Beginning to create plot.'
-plt.ion()
-fig = plt.figure(1, facecolor='w', figsize=(8, 6), dpi=200)
-ax = fig.add_subplot(111, aspect='equal')
-
-markersize = 70
-markershape = 'h'
-
-
-for t in range(0, time_length):
-    ax.cla()
-    plt.scatter(xCell[:], yCell[:], markersize, var_slice[t,:], marker=markershape, edgecolors='none', vmin=minval, vmax=maxval)
-    if t==0:
-            plt.colorbar()
-    if options.mask:
-       maskindices = numpy.nonzero(thickness[t,:] &lt;= 0.0)[:]
-       plt.scatter(numpy.take(xCell,maskindices), numpy.take(yCell,maskindices), markersize+2, '0.9', marker=markershape, edgecolors='none')
-    if options.printval:
-       for iCell in range(xCell[:].shape[0]):
-            plt.text(xCell[iCell], yCell[iCell], '{0:.1f}'.format(var_slice[t,iCell]), horizontalalignment='center', verticalalignment='center',  fontsize=3) 
-       dpi=300
-    else:
-       dpi=150
-
-    plt.title( plottitle + ' at time ' + xtime[t,:].tostring().strip() ) #str(t) )
-    plt.draw()
-    time.sleep(0.05)
-    if options.saveimages:
-        plotname =  varname + '.' + '{0:04d}'.format(t) + '.'  + options.filename + '.png' 
-        #plt.ioff()
-        plt.savefig(plotname,dpi=dpi)
-        #plt.ion()
-        print 'Saved plot as ' + plotname
-
-plt.ioff()
-print &quot;Plotted &quot; + varname + &quot;.&quot;
-
-
-if options.hidefigs:
-     print &quot;Plot display disabled with -n argument.&quot;
-else:
-     print 'Showing plot...  Close plot window to exit.'
-     plt.show()
-
-f.close()
-

Modified: branches/land_ice_projects/grid_tools/plot_mpas_field.py
===================================================================
--- branches/land_ice_projects/grid_tools/plot_mpas_field.py        2013-03-07 17:55:03 UTC (rev 2553)
+++ branches/land_ice_projects/grid_tools/plot_mpas_field.py        2013-03-07 19:49:09 UTC (rev 2554)
@@ -2,7 +2,7 @@
 # Script capable of plotting most MPAS fields.  Invoke with 'python plot_mpas_field.py --help for details about how to use.
 # Matt Hoffman, June 14, 2012
 
-import numpy
+import numpy, time
 from optparse import OptionParser
 import matplotlib.pyplot as plt
 try:
@@ -23,13 +23,15 @@
 parser = OptionParser()
 parser.add_option(&quot;-f&quot;, &quot;--file&quot;, dest=&quot;filename&quot;, help=&quot;file to visualize; default: output.nc&quot;, metavar=&quot;FILE&quot;)
 parser.add_option(&quot;-v&quot;, &quot;--var&quot;, dest=&quot;variable&quot;, help=&quot;variable to visualize; use 'uMag' for velocity magnitude; default: thickness&quot;, metavar=&quot;VAR&quot;)
-parser.add_option(&quot;-t&quot;, &quot;--time&quot;, dest=&quot;time&quot;, help=&quot;time step to visualize (0 based), default: 0&quot;, metavar=&quot;TIME&quot;)
-parser.add_option(&quot;-l&quot;, &quot;--level&quot;, dest=&quot;vertlevel&quot;, help=&quot;vertical level to visualize (0 based); default: 0&quot;, metavar=&quot;LEVEL&quot;)
-parser.add_option(&quot;-s&quot;, &quot;--save&quot;, action=&quot;store_true&quot;, dest=&quot;saveimages&quot;, help=&quot;include this flag to save plot as .png file&quot;)
-parser.add_option(&quot;-n&quot;, &quot;--nodisp&quot;, action=&quot;store_true&quot;, dest=&quot;hidefigs&quot;, help=&quot;include this flag to not display plots (usually used with -s)&quot;)
+parser.add_option(&quot;-t&quot;, &quot;--time&quot;, dest=&quot;time&quot;, help=&quot;time step to visualize (0 based); Give 'a' to animate all time levels or a single integer for a single plot of one time level; default: 0&quot;, metavar=&quot;TIME&quot;)
+parser.add_option(&quot;-l&quot;, &quot;--vertlevel&quot;, dest=&quot;vertlevel&quot;, help=&quot;vertical level to visualize (0 based); default: 0&quot;, metavar=&quot;LEVEL&quot;)
+parser.add_option(&quot;-s&quot;, &quot;--save&quot;, action=&quot;store_true&quot;, dest=&quot;saveimages&quot;, help=&quot;include this flag to save animation as series of .png files&quot;)
+parser.add_option(&quot;-n&quot;, &quot;--nodisp&quot;, action=&quot;store_true&quot;, dest=&quot;hidefigs&quot;, help=&quot;include this flag to not display plot after animation is complete&quot;)
 parser.add_option(&quot;-x&quot;, &quot;--max&quot;, dest=&quot;maximum&quot;, help=&quot;maximum for color bar&quot;, metavar=&quot;MAX&quot;)
 parser.add_option(&quot;-m&quot;, &quot;--min&quot;, dest=&quot;minimum&quot;, help=&quot;minimum for color bar&quot;, metavar=&quot;MIN&quot;)
 parser.add_option(&quot;-g&quot;, &quot;--log&quot;, action=&quot;store_true&quot;, dest=&quot;log&quot;, help=&quot;include this flag to plot on log scale&quot;, metavar=&quot;LOG&quot;)
+parser.add_option(&quot;-k&quot;, &quot;--maskoff&quot;, action=&quot;store_true&quot;, dest=&quot;maskoff&quot;, help=&quot;include this flag to display all cells instead of just non-ice cells&quot;, metavar=&quot;MASKOFF&quot;)
+parser.add_option(&quot;-p&quot;, &quot;--printval&quot;, action=&quot;store_true&quot;, dest=&quot;printval&quot;, help=&quot;include this flag to print numeric cell values (slow)&quot;, metavar=&quot;PRINTVAL&quot;)
 
 options, args = parser.parse_args()
 
@@ -37,13 +39,6 @@
         print &quot;No filename provided. Using output.nc.&quot;
         options.filename = &quot;output.nc&quot;
 
-if not options.time:
-        print &quot;No time provided. Using time 0, if time is needed.&quot;
-        time_slice = 0
-else:
-        time_slice = int(options.time)
-        print &quot;Using time level &quot; +  options.time
-
 if not options.vertlevel:
         print &quot;No vertical level provided. Using level 0 (surface), if level is needed.&quot;
         vert_level = 0
@@ -58,26 +53,31 @@
         varname = options.variable
         print &quot;Using variable &quot; + varname
 
+
 f = NetCDFFile(options.filename,'r')
 
 # Get grid stuff
-#times = f.variables['xtime']  # Not needed unless trying to print actual time stamp
-try:
-  xCell = f.variables['xCell']
-  yCell = f.variables['yCell']
-except:
-  print 'xCell and/or yCell is/are missing.  Might not be a problem.'
-try:
-  xEdge = f.variables['xEdge']
-  yEdge = f.variables['yEdge']
-except:
-  print 'xEdge and/or yEdge is/are missing.  Might not be a problem.'
-try:
-  angleEdge = f.variables['angleEdge']
-except:
-  print 'angleEdge is missing.  Might not be a problem.'
+xtime = f.variables['xtime'][:]  # Not needed unless trying to print actual time stamp
 
+if not options.time:
+        print &quot;No time provided. Using time 0, if time is needed.&quot;
+        time_slices = 0
+else:
+        if options.time=='a':
+           time_slices = numpy.arange( xtime.shape[0] )  # this is an easier way to get the time length than querying the dimension since the way to do that depends on which NetCDF module is being used...
+           print &quot;Animating all&quot; + str(time_slices.size) + &quot; time levels.&quot; 
+        else:
+           time_slices = int(options.time)
+           print &quot;Using time level &quot; +  options.time
 
+xCell = f.variables['xCell']
+yCell = f.variables['yCell']
+xEdge = f.variables['xEdge']
+yEdge = f.variables['yEdge']
+angleEdge = f.variables['angleEdge']
+nCells = xCell.shape[0]  # This is an easier way to get nCells than querying the nCells dimension because the way to do that is module-specific
+
+
 # get the requested variable
 if varname == 'uMag':
    uReconstructX = f.variables['uReconstructX']
@@ -100,21 +100,21 @@
    x = xVertex[:]
    y = yVertex[:]
 
+if not options.maskoff:
+        thickness = f.variables['thickness']
+
 # Get the needed slice and determine the plot title.  Make some assumptions about how dimensions are arranged:
 plottitle = varname
 if 'Time' in dims:
-   plottitle = plottitle + ', at time ' + str(time_slice)
+   time_length = var.shape[0]  # Assume time is the first dimension
    if 'nVertLevels' in dims:
       plottitle = plottitle + ', for layer ' + str(vert_level) 
-      var_slice = var[time_slice,:,vert_level]
+      var_slice = var[:,:,vert_level]
    else:
-      var_slice = var[time_slice,:]
+      var_slice = var[:,:]
 else:
-   if 'nVertLevels' in dims:
-      plottitle = plottitle + ', for layer ' + str(vert_level) 
-      var_slice = var[:,vert_level]
-   else:
-      var_slice = var[:]
+   print &quot;Time is not a dimension of this variable.  Unable to animate it!&quot;
+   sys.exit()
 
 if options.log:
         var_slice = numpy.log10(var_slice + 1.0e-5)
@@ -134,19 +134,60 @@
 
 # MAKE THE PLOT
 print '** Beginning to create plot.'
-fig = plt.figure(1, facecolor='w')
+plt.ion()
+fig = plt.figure(1, facecolor='w', figsize=(8, 6), dpi=200)
 ax = fig.add_subplot(111, aspect='equal')
-plt.scatter(x[:], y[:], 60, var_slice, marker='h', edgecolors='none', vmin=minval, vmax=maxval)
-plt.colorbar()
-plt.title( plottitle )
-plt.draw()
-print &quot;Plotted &quot; + varname + &quot;.&quot;
 
-if options.saveimages:
-        plotname =  varname + '.' + options.filename + '.png' 
-        plt.savefig(plotname)
+# make an educated guess about how big the markers should be.
+if nCells**0.5 &lt; 60.0:
+  markersize=  max( int(round(  3600.0/(nCells**0.5)  )), 1)
+  markershape = 'h'  # use hexes if the points are big enough, otherwise just dots
+else:
+  markersize=  max( int(round(  1800.0/(nCells**0.5)  )), 1)
+  markershape = '.'
+print 'Using a markersize of ', markersize
+
+if isinstance(time_slices, int) ==1:
+   iterlist = [time_slices]
+else:
+   iterlist = time_slices
+
+for t in iterlist:
+    ax.cla()
+
+    if options.maskoff:
+       maskindices = numpy.arange(nCells)
+    else:
+       maskindices = numpy.nonzero(thickness[:][t,:] &gt; 0.0)[:]
+    plt.scatter(x[maskindices], y[maskindices], markersize, var_slice[t,maskindices], marker=markershape, edgecolors='none', vmin=minval, vmax=maxval)
+
+    if t==0:
+       plt.colorbar()
+
+
+    if options.printval:
+       for iCell in range(x.shape[0]):
+            plt.text(x[iCell], y[iCell], '{0:.1f}'.format(var_slice[t,iCell]), horizontalalignment='center', verticalalignment='center',  fontsize=3) 
+       dpi=300
+    else:
+       dpi=150
+
+    plt.title( plottitle + ' at time ' + xtime[t,:].tostring().strip() ) #str(t) )
+    plt.xlim( (x.min(), x.max() ) )
+    plt.ylim( (y.min(), y.max() ) )
+    plt.draw()
+    time.sleep(0.05)
+    if options.saveimages:
+        plotname =  varname + '.' + '{0:04d}'.format(t) + '.'  + options.filename + '.png' 
+        #plt.ioff()
+        plt.savefig(plotname,dpi=dpi)
+        #plt.ion()
         print 'Saved plot as ' + plotname
 
+plt.ioff()
+print &quot;Plotted &quot; + varname + &quot;.&quot;
+
+
 if options.hidefigs:
      print &quot;Plot display disabled with -n argument.&quot;
 else:

</font>
</pre>