<p><b>mhoffman@lanl.gov</b> 2012-03-03 17:07:43 -0700 (Sat, 03 Mar 2012)</p><p>BRANCH COMMIT<br>
Created python script that converts a periodic_hex grid (e.g. output.nc) to a (somewhat distorted) rectangular grid that can be viewed more easily (e.g. in ncview).  This is a workaround to sanity check test case output since many fields are not read in by Paraview with the MPAS netCDF file format.<br>
</p><hr noshade><pre><font color="gray">Added: branches/land_ice_projects/test_cases/dome/convert_to_regular_grid_netcdf.py
===================================================================
--- branches/land_ice_projects/test_cases/dome/convert_to_regular_grid_netcdf.py                                (rev 0)
+++ branches/land_ice_projects/test_cases/dome/convert_to_regular_grid_netcdf.py        2012-03-04 00:07:43 UTC (rev 1588)
@@ -0,0 +1,82 @@
+#!/usr/bin/python
+# Script to convert selected variables from an MPAS run and create a new netCDF file
+# that has those variables on a rectangular grid.  The resulting gridded data appears 
+# distorted but is adequate for quickly checking output.  This is likely to only work 
+# with grids created with periodic_hex where the order of cells in each 1d array (e.g. 'nCells')
+# is row by row (or is it column by column).  This is a workaround to allow visualization 
+# in, e.g. ncview since Paraview has some limitations as to which variables can be read in.
+# Currently, the script is setup to regrid any 2d and 3d variables that have dimensions of both Time
+# and nCells.  The output file has the name 'converted.nc'.  The output file is a little quirky, 
+# but does work with ncview.
+
+import sys, os, glob, shutil, numpy
+from Scientific.IO.NetCDF import *
+# from NetCDF import *
+from optparse import OptionParser
+
+parser = OptionParser()
+parser.add_option(&quot;-f&quot;, &quot;--file&quot;, dest=&quot;filename&quot;, help=&quot;file to convert&quot;, metavar=&quot;FILE&quot;)
+options, args = parser.parse_args()
+if not options.filename:
+        parser.error(&quot;Filename is a required input.&quot;)
+
+# Get some information about the input file
+filein = NetCDFFile(options.filename,'r')
+# These need to be specified!!!!  Perhaps these should be supplied as arguments on the command line
+nx = 30
+ny = 35
+times = filein.variables['xtime']
+time_length = times.shape[0]
+vert_levs = filein.dimensions['nVertLevels']
+
+# Define the new file to be output - this should be made a variant of the input file name
+fileout = NetCDFFile(&quot;converted.nc&quot;,&quot;w&quot;)
+# Create the new x,y dimensions for the new file
+fileout.createDimension('nx',nx)
+fileout.createDimension('ny',ny)
+# Copy over all the dimensions to the new file
+for dim in filein.dimensions.keys():
+    # print 'DIMENSION: ', dim
+    # print 'HAS VALUE: ', filein.dimensions[dim]
+    fileout.createDimension(dim, filein.dimensions[dim])
+
+# Loop over all variables in file and find the ones we want to convert
+for var in filein.variables.keys():
+   # print var
+   thevar = filein.variables[var]
+   dimflag = 0
+   # Look for variables that have these two dimensions
+   for dim in thevar.dimensions:
+       if dim == 'nCells':
+           # print '  has', dim
+           dimflag += 1
+       if dim == 'Time':
+           # print '  has', dim
+           dimflag += 1
+   if dimflag == 2:
+       # print '       HAS BOTH'
+       # Convert this variable
+       dims2use = ()
+       lastdim = 0
+       for dim in thevar.dimensions:
+           if dim == 'nCells':
+              dims2use = dims2use + ('ny', 'nx')
+           else:
+              dims2use = dims2use + (dim,)
+           if dim == 'nVertLevels':
+              lastdim = vert_levs
+           elif dim == 'nVertLevelsPlus2':
+              lastdim = vert_levs + 2
+       # print dims2use
+       # print thevar.typecode()
+       newVar = fileout.createVariable(var,thevar.typecode(),dims2use)
+       originalvalue = thevar[:]
+       # Determine how to reshape
+       if lastdim == 0:
+          newVar[:] = originalvalue.reshape(time_length,ny,nx)
+       else:
+          newVar[:] = originalvalue.reshape(time_length,ny,nx,lastdim)
+
+
+filein.close()
+fileout.close()


Property changes on: branches/land_ice_projects/test_cases/dome/convert_to_regular_grid_netcdf.py
___________________________________________________________________
Added: svn:executable
   + *

</font>
</pre>