Source code for pycaps.diagnostic.vardump

#!/usr/bin/env python2.7

#Code: vardump.py (Python Script)
#Author: Nate Snook (CASA/CAPS/SoM)
#Written: May 2009
#Modification History:  
#   19 Feb. 2014 -- Nate Snook (added netcdf support and updated script to use argparse)
#   24 Sept. 2015 --Nate Snook (added support for dumping dimensions)
#   11 Nov. 2015 -- Nate Snook (converted from a stand-alone script to a function for pycaps)

from PyNIO import Nio
import numpy

[docs]def var_dump(var, source, filefmt='hdf'): """ Dumps the contents of a given variable, attribute, or dimension from a NetCDF or HDF file to the terminal window. Good for sanity checks or when you want to confirm a dimension or attribute is correctly set (var_info works well for this too.) Args: var: The name of the variable you want to dump. source: The HDF or NetCDF file containing the data you want to dump. For best results, provide a full path. filefmt: OPTIONAL -- The format of the file you're reading from (default: hdf). Valid options are 'hdf' and 'netcdf'. Returns: <<nothing>> (dumps the requested information to the terminal window) """ #----------------------------------------------------# #ARPS uses unusual capitalization standards when writing to a NetCDF file; this list #should take care of making sure you can successfully read the desired file even if #the variable entered by the user is not capitalized properly. if filefmt in ['netcdf', 'NetCDF', 'NETCDF']: netcdf_exceptions = ['x_stag', 'y_stag', 'z_stag', 'Title', 'Conventions', 'false_easting', 'History', 'cmnt01', 'cmnt02'] if not (var in item for item in netcdf_exceptions): var = var.upper() numpy.set_printoptions(threshold='nan') try: dumpfile = Nio.open_file(source, mode = 'r', options = None, history='', format=filefmt) except: print 'Attempting to open file without format specification...' dumpfile = Nio.open_file(source, mode='r', options=None, history='') try: #If the variable is one of the exact ones stored in the history dump file... try: thevariable = dumpfile.variables[var][:] #...simply gather data from that variable. except: try: thevariable = dumpfile.attributes[var] #It might also be an attribute we can read. except: thevariable = dumpfile.dimensions[var] #...or possibly a dimension. except: #If not, we may still be able to do something... if (var == 'AGL') or (var == 'agl'): #If the user wants height above ground level (AGL)... if (filefmt in ['netcdf', 'NetCDF', 'NETCDF']): sfc_height = dumpfile.variables['ZPSOIL'][0,:,:] #...calculate it from zpsoil[sfc] and zp. zp = dumpfile.variables['ZP'][:,:,:] else: sfc_height = dumpfile.variables['zpsoil'][0,:,:] #...calculate it from zpsoil[sfc] and zp. zp = dumpfile.variables['zp'][:,:,:] agl = zp - sfc_height print '=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=' for level in arange(0,len(zp[:]),1): print 'Level ' + str(level + 1) + ' is between ' + str(agl[level,:,:].min()) + ' and ' + str(agl[level,:,:].max()) + 'm AGL.' print '=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=' thevariable = 'Look up. --^' elif (var == 'ptprt') or (var == 'ptpert'): #If the user wants potential temperature perturbation... pt = dumpfile.variables['pt'][:,:,:] #...then calculate it from potential temperature (pt) and ptbar. ptbar = dumpfile.variables['ptbar'][:,:,:] ptprt = numpy.zeros((len(pt[:]),len(pt[1][:]), len(pt[1][1][:]))) ptprt[:,:,:] = pt[:,:,:] - ptbar[:,:,:] #perturbation = actual value - average value thevariable = ptprt else: print ' ' print '*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*' print ' ERROR: Unsupported variable: "' + var print '*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*' print 'Contents of the variable "' + str(var) + '" in "' + source + '":' print '-----------------------------' try: print thevariable[:] except: print str(thevariable) #Dimensions may require output as a string. print '-----------------------------'