<p><b>mhoffman@lanl.gov</b> 2012-03-05 14:59:42 -0700 (Mon, 05 Mar 2012)</p><p>BRANCH COMMIT<br>
<br>
Created a python script that takes an MPAS grid (e.g. grid generated with periodic_hex that contains the default shallow water variables) and creates a new netCDF file that contains the same grid but with land ice variables.<br>
</p><hr noshade><pre><font color="gray">Added: branches/land_ice_projects/grid_tools/add_land_ice_variables_to_mpas_grid.py
===================================================================
--- branches/land_ice_projects/grid_tools/add_land_ice_variables_to_mpas_grid.py                                (rev 0)
+++ branches/land_ice_projects/grid_tools/add_land_ice_variables_to_mpas_grid.py        2012-03-05 21:59:42 UTC (rev 1592)
@@ -0,0 +1,73 @@
+#!/usr/bin/python
+# Script to create a grid with land ice variables from an MPAS grid.
+# I've only tested it with a periodic_hex grid, but it should work with any MPAS grid.
+# Currently variable attributes are not copied (and periodic_hex does not assign any, so this is ok).  If variable attributes are added to periodic_hex, this script should be modified to copy them (looping over dir(var), skipping over variable function names &quot;assignValue&quot;, &quot;getValue&quot;, &quot;typecode&quot;).
+
+import sys, 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.  Include by using: -f FILENAME&quot;)
+
+
+# Get some information about the input file
+filein = NetCDFFile(options.filename,'r')
+# vert_levs = filein.dimensions['nVertLevels']
+
+
+# Define the new file to be output - this should perhaps be made a variant of the input file name
+fileout = NetCDFFile(&quot;land_ice_grid.nc&quot;,&quot;w&quot;)
+
+
+
+# Copy over all the dimensions to the new file
+# Note: looping over dimensions seems to result in them being written in seemingly random order.
+#       I don't think this matters but it is not aesthetically pleasing.
+#       It may be better to list them explicitly as I do for the grid variables, 
+#       but this way ensures they all get included and is easier.
+for dim in filein.dimensions.keys():
+    fileout.createDimension(dim, filein.dimensions[dim])
+# Create nVertLevelsPlus2 dimension
+fileout.createDimension('nVertLevelsPlus2', filein.dimensions['nVertLevels'] + 2)
+
+# Copy over all of the required grid variables to the new file
+vars2copy = ('latCell', 'lonCell', 'xCell', 'yCell', 'zCell', 'indexToCellID', 'latEdge', 'lonEdge', 'xEdge', 'yEdge', 'zEdge', 'indexToEdgeID', 'latVertex', 'lonVertex', 'xVertex', 'yVertex', 'zVertex', 'indexToVertexID', 'cellsOnEdge', 'nEdgesOnCell', 'nEdgesOnEdge', 'edgesOnCell', 'edgesOnEdge', 'weightsOnEdge', 'dvEdge', 'dcEdge', 'angleEdge', 'areaCell', 'areaTriangle', 'cellsOnCell', 'verticesOnCell', 'verticesOnEdge', 'edgesOnVertex', 'cellsOnVertex', 'kiteAreasOnVertex')
+for varname in vars2copy:
+   thevar = filein.variables[varname]
+   newVar = fileout.createVariable(varname,thevar.typecode(),thevar.dimensions)
+   newVar[:] = thevar[:]
+       
+
+# Create the land ice variables (all the shallow water vars can be ignored)
+# Note: it may be necessary to make sure the Time dimension has size 1, rather than the 0 it defaults to.  For now, letting it be 0 which seems to be fine.
+newvar = fileout.createVariable('layerThicknessFractions', 'd', ('nVertLevels', ))
+newvar[:] = numpy.zeros(newvar.shape)
+newvar = fileout.createVariable('thickness', 'd', ('Time', 'nCells'))
+newvar[:] = numpy.zeros(newvar.shape)
+newvar = fileout.createVariable('bedTopography', 'd', ('Time', 'nCells'))
+newvar[:] = numpy.zeros(newvar.shape)
+newvar = fileout.createVariable('beta', 'd', ('Time', 'nCells'))
+newvar[:] = numpy.zeros(newvar.shape)
+newvar = fileout.createVariable('normalVelocity', 'd', ('Time', 'nEdges', 'nVertLevels'))
+newvar[:] = numpy.zeros(newvar.shape)
+newvar = fileout.createVariable('tracers', 'd', ('Time', 'nCells', 'nVertLevelsPlus2', 'nTracers'))
+newvar[:] = numpy.zeros(newvar.shape)
+
+# Assign the global attributes
+# Copy over the two attributes that are required by MPAS.  If any others exist in the input file, give a warning.
+setattr(fileout, 'on_a_sphere', getattr(filein, 'on_a_sphere'))
+setattr(fileout, 'sphere_radius', getattr(filein, 'sphere_radius'))
+# If there are others that need to be copied, this script will need to be modified.  This Warning indicates that:
+# Note: dir(file) includes the following entries for functions in addition to the global attributes: 'close', 'createDimension', 'createVariable', 'flush', 'sync'
+if len(dir(filein)) - 5 != 2:
+    print &quot;WARNING: File had &quot;, len(dir(filein)) - 5, &quot;global attributes.  Expected 2.  The script may need to be modified.&quot;
+    print &quot;Global attributes and functions: &quot;, dir(filein)
+
+
+filein.close()
+fileout.close()


Property changes on: branches/land_ice_projects/grid_tools/add_land_ice_variables_to_mpas_grid.py
___________________________________________________________________
Added: svn:executable
   + *

</font>
</pre>