<p><b>mhoffman@lanl.gov</b> 2012-05-18 12:54:23 -0600 (Fri, 18 May 2012)</p><p>BRANCH COMMIT -- land ice<br>
Modified python grid preprocessing scripts to support I/O with the netCDF4 python module.  This allows these scripts to work on Jaguar.<br>
</p><hr noshade><pre><font color="gray">Modified: 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        2012-05-17 18:59:56 UTC (rev 1919)
+++ branches/land_ice_projects/grid_tools/add_land_ice_variables_to_mpas_grid.py        2012-05-18 18:54:23 UTC (rev 1920)
@@ -4,7 +4,18 @@
 # 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 *
+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')
 
 # Check to see if a grid file was specified on the command line.
 # If not, land_ice_grid.nc is used.
@@ -25,7 +36,10 @@
 
 
 # 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;)
+if netCDF_module == 'Scientific.IO.NetCDF':
+    fileout = NetCDFFile(&quot;land_ice_grid.nc&quot;,&quot;w&quot;)
+else:
+    fileout = NetCDFFile(&quot;land_ice_grid.nc&quot;,&quot;w&quot;,format=filein.file_format)
 
 
 
@@ -43,7 +57,11 @@
     elif dim == 'nTracers': 
         pass  # Do nothing - we don't want this dimension 
     else:    # Copy over all other dimensions
-        fileout.createDimension(dim, filein.dimensions[dim])
+      if netCDF_module == 'Scientific.IO.NetCDF':
+        dimvalue = filein.dimensions[dim]
+      else:
+        dimvalue = len(filein.dimensions[dim])
+      fileout.createDimension(dim, dimvalue)
 # Create nVertLevelsPlus2 dimension
 # fileout.createDimension('nVertLevelsPlus2', filein.dimensions['nVertLevels'] + 2)
 
@@ -53,34 +71,45 @@
 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)
+   if netCDF_module == 'Scientific.IO.NetCDF':
+     datatype = thevar.typecode() 
+   else:
+     datatype = thevar.dtype
+   newVar = fileout.createVariable(varname, datatype, thevar.dimensions)
    newVar[:] = thevar[:]
+   # Create nVertLevelsPlus2 dimension - no longer used
        
 
 # 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', ))
+if netCDF_module == 'Scientific.IO.NetCDF':
+    nVertLevels = fileout.dimensions['nVertLevels']
+    datatype = 'd'
+else:
+    nVertLevels = len(filein.dimensions['nVertLevels'])
+    datatype = filein.variables['xCell'].dtype  # Get the datatype for double precision float
+#  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', datatype, ('nVertLevels', ))
 newvar[:] = numpy.zeros(newvar.shape)
 # Assign default values to layerThicknessFractions.  By default they will be uniform fractions.  Users can modify them in a subsequent step, but doing this here ensures the most likely values are already assigned. (Useful for e.g. setting up Greenland where the state variables are copied over but the grid variables are not modified.)
-newvar[:] = 1.0 / fileout.dimensions['nVertLevels']
+newvar[:] = 1.0 / nVertLevels
 
-newvar = fileout.createVariable('thickness', 'd', ('Time', 'nCells'))
+newvar = fileout.createVariable('thickness', datatype, ('Time', 'nCells'))
 newvar[:] = numpy.zeros(newvar.shape)
-newvar = fileout.createVariable('bedTopography', 'd', ('Time', 'nCells'))
+newvar = fileout.createVariable('bedTopography', datatype, ('Time', 'nCells'))
 newvar[:] = numpy.zeros(newvar.shape)
-newvar = fileout.createVariable('normalVelocity', 'd', ('Time', 'nEdges', 'nVertLevels'))
+newvar = fileout.createVariable('normalVelocity', datatype, ('Time', 'nEdges', 'nVertLevels'))
 newvar[:] = numpy.zeros(newvar.shape)
-newvar = fileout.createVariable('temperature', 'd', ('Time', 'nCells', 'nVertLevels'))
+newvar = fileout.createVariable('temperature', datatype, ('Time', 'nCells', 'nVertLevels'))
 #newvar = fileout.createVariable('temperature', 'd', ('Time', 'nCells', 'nVertLevelsPlus2'))
 newvar[:] = numpy.zeros(newvar.shape)
 # These boundary conditions are currently part of mesh, and are time independent.  If they change, make sure to adjust the dimensions here and in Registry.
-newvar = fileout.createVariable('beta', 'd', ( 'nCells',))
+newvar = fileout.createVariable('beta', datatype, ( 'nCells',))
 newvar[:] = numpy.zeros(newvar.shape)
-newvar = fileout.createVariable('sfcMassBal', 'd', ('nCells',))
+newvar = fileout.createVariable('sfcMassBal', datatype, ('nCells',))
 newvar[:] = numpy.zeros(newvar.shape)
-newvar = fileout.createVariable('sfcAirTemp', 'd', ('nCells',))
+newvar = fileout.createVariable('sfcAirTemp', datatype, ('nCells',))
 newvar[:] = numpy.zeros(newvar.shape)
-newvar = fileout.createVariable('basalHeatFlux', 'd', ('nCells',))
+newvar = fileout.createVariable('basalHeatFlux', datatype, ('nCells',))
 newvar[:] = numpy.zeros(newvar.shape)
 
 # Assign the global attributes
@@ -88,11 +117,11 @@
 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)
+# Note: dir(file)  with Scientific.IO includes the following entries for functions in addition to the global attributes: 'close', 'createDimension', 'createVariable', 'flush', 'sync'.  NetCDF4 has a whole bunch more!
+print &quot;File had &quot;, len(dir(filein)) - 5, &quot;global attributes.  Copied on_a_sphere and sphere_radius.&quot;
+print &quot;Global attributes and functions: &quot;, dir(filein)
 
-
 filein.close()
 fileout.close()
+
+print 'Successfully created land_ice_grid.nc'

Modified: branches/land_ice_projects/test_cases/dome/setup_dome_initial_conditions.py
===================================================================
--- branches/land_ice_projects/test_cases/dome/setup_dome_initial_conditions.py        2012-05-17 18:59:56 UTC (rev 1919)
+++ branches/land_ice_projects/test_cases/dome/setup_dome_initial_conditions.py        2012-05-18 18:54:23 UTC (rev 1920)
@@ -2,7 +2,18 @@
 # Generate initial conditions for dome land ice test case
 
 import sys, numpy
-from Scientific.IO.NetCDF import *
+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')
 from math import sqrt
 
 # Check to see if a grid file was specified on the command line.
@@ -21,7 +32,10 @@
 # Open the file, get needed dimensions
 try:
     gridfile = NetCDFFile(gridfilename,'r+')
-    nVertLevels = gridfile.dimensions['nVertLevels']
+    if (netCDF_module == 'Scientific.IO.NetCDF'):
+         nVertLevels = gridfile.dimensions['nVertLevels']
+    else:
+         nVertLevels = len(gridfile.dimensions['nVertLevels'])
     if nVertLevels != 9:
          print 'nVerLevels in the supplied file was ', nVertLevels, '.  Were you expecting 9?'
     # Get variables
@@ -81,4 +95,5 @@
 gridfile.variables['basalHeatFlux'][:] = G
 
 gridfile.close()
+print 'Successfully added dome initial conditions to ', gridfilename
 

</font>
</pre>