<p><b>mhoffman@lanl.gov</b> 2012-03-05 15:26:29 -0700 (Mon, 05 Mar 2012)</p><p>BRANCH COMMIT<br>
<br>
Moving MPAS-to-regular-grid conversion script to the grid_tools project.<br>
</p><hr noshade><pre><font color="gray">Copied: branches/land_ice_projects/grid_tools/covert_mpas_grid_to_regular_grid_netcdf.py (from rev 1592, branches/land_ice_projects/test_cases/dome/convert_to_regular_grid_netcdf.py)
===================================================================
--- branches/land_ice_projects/grid_tools/covert_mpas_grid_to_regular_grid_netcdf.py         (rev 0)
+++ branches/land_ice_projects/grid_tools/covert_mpas_grid_to_regular_grid_netcdf.py        2012-03-05 22:26:29 UTC (rev 1593)
@@ -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("-f", "--file", dest="filename", help="file to convert", metavar="FILE")
+options, args = parser.parse_args()
+if not options.filename:
+        parser.error("Filename is a required input.")
+
+# 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("converted.nc","w")
+# 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()
Deleted: 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        2012-03-05 21:59:42 UTC (rev 1592)
+++ branches/land_ice_projects/test_cases/dome/convert_to_regular_grid_netcdf.py        2012-03-05 22:26:29 UTC (rev 1593)
@@ -1,82 +0,0 @@
-#!/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("-f", "--file", dest="filename", help="file to convert", metavar="FILE")
-options, args = parser.parse_args()
-if not options.filename:
-        parser.error("Filename is a required input.")
-
-# 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("converted.nc","w")
-# 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()
</font>
</pre>