Source code for pycaps.diagnostic.varinfo

#!/usr/bin/env python2.7
#Code: varinfo.py (Python Script)
#Author: Nate Snook (CASA/CAPS/SoM)
#Written: May 2009
#Last modified:  24 Sept. 2015
#
#Modification History:
#
#  24 Sept. 2015 -- Nate Snook (Added verbose option to show full variable attributes) 
#  10 Nov. 2015 -- Nate Snook (Converted stand-alone script to function for pycaps)
#
#Purpose:
#   This simple code gives information on the dimensions, maximum, minimum, and
#   average values of a variable in an ARPS .hdf file.  Good for spotting
#   gross errors (e.g. NaN values, unrealistic windspeeds, etc).

from PyNIO import Nio
import numpy

[docs]def var_info(var, source, verbose = False, format = 'hdf'): """ Gives information on the dimensions, maximum, minimum, and average values of a variable in an HDF or NetCDF file. Good for spotting gross errors (e.g. NaN values, unrealistically high or low values). Verbose mode also includes information on NetCDF or HDF attritubes and dimensions. Args: var: the name of the variable to update (as stored in the file) source: the path to the file the data are stored in verbose: OPTIONAL -- Passing verbose as True will give extra data on HDF/NetCDF variable attributes and dimensions. Default is False. format: OPTIONAL -- The format your data are stored in (default: hdf). Valid options are 'hdf' and 'netcdf'. Returns: <<nothing>> (prints variable information to the terminal window) """ #----------------------------------------------------# if format in ['netcdf', 'NetCDF', 'NETCDF']: #The NetCDF exceptions listed here are specific to ARPS. If you are using a different code #that uses these variable names, you may need to remove them from this list to avoid errors. netcdf_exceptions = ['x_stag', 'y_stag', 'z_stag', 'Title', 'Conventions', 'false_easting', 'History', 'cmnt01', 'cmnt02'] filefmt = 'netcdf' if not (var in item for item in netcdf_exceptions): var = var.upper() else: pass else: filefmt = 'hdf' try: dumpfile = Nio.open_file(source, mode = 'r', options = None, history='', format=filefmt) except: '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: thevariable = dumpfile.attributes[var] #It might also be an attribute we can read. 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 == 'netcdf'): print 'Reading NetCDF formatted data...' sfc_height = dumpfile.variables['ZPSOIL'][0,:,:] #...calculate it from zpsoil[sfc] and zp. zp = dumpfile.variables['ZP'][:,:,:] agl = zp - sfc_height else: print 'Reading HDF4 formatted data...' 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 '* Invalid variable: ' + var print '*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*' print ' ' print 'Information for "' + str(var) + '" in "' + str(source) + '":' print '-----------------------------' print 'Dimensions (using numpy.shape) are: ' + str(numpy.shape(thevariable)) print 'Minimum: ' + str(thevariable.min()) print 'Maximum: ' + str(thevariable.max()) print 'Average: ' + str(numpy.average(thevariable)) print '-----------------------------' if (verbose == True): try: var_attributes = getattr(dumpfile.variables[var], 'attributes') print 'NetCDF or HDF attributes:' for attr_name in dumpfile.variables[var].attributes.keys(): var_attr_value = getattr(dumpfile.variables[var], attr_name) print ' ' + str(attr_name) + ': ' + str(var_attr_value) print '----------------------------' print 'Full NetCDF or HDF attributes, with variable types:' print ' ' + str(var_attributes) print '----------------------------' print 'NetCDF or HDF type (Nio code): ' + str(dumpfile.variables[var].typecode()) print 'NetCDF or HDF variable dimension names: ' + str(dumpfile.variables[var].dimensions) except: pass