#!/usr/bin/python -tt #======================================================================= # General Documentation """This module has a single public function. See function docstring for description. """ #----------------------------------------------------------------------- # Additional Documentation # # RCS Revision Code: # $Id: press.py,v 1.2 2004/05/26 21:43:25 jlin Exp $ # # Modification History: # - 08 Apr 2004: Original by Johnny Lin, Computation Institute, # University of Chicago. Passed passably reasonable tests. # # Notes: # - Written for Python 2.2.2. # - Module docstrings can be tested using the doctest module. To # test, execute "python press.py". # - See import statements throughout for list of packages and modules # required. # # Copyright (c) 2004 by Johnny Lin. For licensing, distribution # conditions, contact information, and additional documentation see # the URL http://www.johnny-lin.com/py_pkgs/atmqty/doc/. #======================================================================= #---------------- Module General Import and Declarations --------------- #- Set module version number etc. to package version number etc.: import package_version __version__ = package_version.version __author__ = package_version.author __date__ = package_version.date __credits__ = package_version.credits del package_version #--------------------------- Public Function --------------------------- def press(variables, p0=1000.0, missing=1e+20): """Calculate pressure. The primary purpose of this function is to calculate pressure given temperature and potential temperature (this is needed to interpolate from isentropic surfaces to pressure levels). Currently, this function only accomplishes that primary purpose. There are, however, a number of different ways to calculate pressure from a variety of state (primary and derived) variables, and this function is written so those methods can be easily added in the future. The reference pressure for potential temperature is set to 1000 hPa by default. To use a different reference pressure, set p0 to a different value when calling the method. Method Arguments: * variables: A dictionary of input state variables, where for each key:value pair the key labels what state variable it is and the value is the state variable. The value is a Numeric floating point array of any number of dimensions and size. All state variables must be arrays of the same shape and size. Argument variables can have any number of items. Key values may be any value, but the values that are understood by this function include: + 'T': Temperature [K]. + 'theta': Potential temperature [K]. Any other entries in the variables dictionary will be ignored. * p0: Potential temperature reference pressure [hPa]. Floating point scalar. Optional. * missing: If argument variables has missing values, this is the missing value value. Floating point scalar. Default is 1e+20. Optional. In this function, the following combination of input state vari- ables are supported for calculating pressure: * Temperature and potential temperature The function will automatically apply the correct equation depend- ing on what key values are available in the argument variables dictionary. An error is returned if none of the above combina- tions are present in argument variables. Output: * Pressure [hPa]. Numeric floating point array of same dimensions and size as the arrays in the key:value pairs of argument varia- bles. If there are any missing values in output, those values are set to the value in argument missing from the input. If there are missing values in the output due to math errors and missing is set to None, output will fill those missing values with the MA default value of 1e+20. Reference: * Wallace, J. M., and P. V. Hobbs (1977): Atmospheric Science: An Introductory Survey. San Diego, CA: Academic Press, ISBN 0-12-732950-1, p. 69. Example without missing values: >>> import press >>> import Numeric as N >>> theta = N.array([273.1, 279.404, 277.75073]) >>> T = N.array([273.1, 280.2, 278.4]) >>> a = press.press({'theta':theta, 'T':T}) >>> ['%.8g' % a[i] for i in range(len(a))] ['1000', '1010', '1008.2'] Example with missing values: >>> import press >>> import Numeric as N >>> theta = N.array([273.1, 1e+20, 277.75073]) >>> T = N.array([273.1, 280.2, 278.4]) >>> a = press.press({'theta':theta, 'T':T}, missing=1e20) >>> ['%.8g' % a[i] for i in range(len(a))] ['1000', '1e+20', '1008.2'] """ import MA import Numeric as N from atmconst import AtmConst from is_numeric_float import is_numeric_float #- Do some error checks on input: for akey in ['T','theta']: if is_numeric_float(variables[akey]) != 1: raise TypeError, "press: Arg not Numeric floating" if is_numeric_float(N.array(p0)) != 1: raise TypeError, "press: p0 not Numeric floating" #- Atmospheric constants: const = AtmConst() kappa_inv = 1./const.kappa #- Calculate pressure depending on what the inputs are: if variables.has_key('T') and variables.has_key('theta'): if missing == None: T = MA.masked_array(variables['T']) theta = MA.masked_array(variables['theta']) else: T = MA.masked_values(variables['T'], missing, copy=0) theta = MA.masked_values(variables['theta'], missing, copy=0) press = p0 / ( (theta/T)**kappa_inv ) else: raise ValueError, "press: Bad input keys" #- Return pressure: return MA.filled(press, missing) #-------------------------- Main: Test Module ------------------------- #- Define additional examples for doctest to use: __test__ = { 'Additional Example 1': """ >>> import press >>> import Numeric as N >>> T = N.array([173.1, 180.2, 178.4]) >>> theta = N.array([225.58289, 233.18352, 250.48191]) >>> a = press.press({'theta':theta, 'T':T}, p0=1010.) >>> ['%.8g' % a[i] for i in range(len(a))] ['400.00002', '410.00002', '308.2'] >>> a = press.press({'theta':theta, 'T':T}, p0=1010) Traceback (most recent call last): ... TypeError: press: p0 not Numeric floating >>> theta = [400., 410., 308.2] >>> a = press.press({'theta':theta, 'T':T}) Traceback (most recent call last): ... TypeError: press: Arg not Numeric floating >>> a = press.press({'theta':theta}) Traceback (most recent call last): ... KeyError: 'T' """ } #- Execute doctest if module is run from command line: if __name__ == "__main__": """Test the module. Tests the examples in all the module documentation strings, plus __test__. The parent directory is added to sys.path for this module testing case. """ import doctest, sys, os sys.path.append(os.pardir) doctest.testmod(sys.modules[__name__]) # ===== end file =====