source: trunk/MESOSCALE/PLOT/PYTHON/pywrf.example.r39/misc/isa.py @ 183

Last change on this file since 183 was 183, checked in by aslmd, 14 years ago

MESOSCALE: PYTHON: added pywrf r39 to act as a reference for futher developments

File size: 3.3 KB
Line 
1'''
2Functions to calculate thermodynamic quantities for the International Standard
3Atmosphere.
4'''
5
6from __future__ import division
7from sys import path
8path.append('/Users/val/projects/vapylib')
9import numpy as n
10import constants 
11
12isa_levels = constants.isa_levels
13lower_boundaries = constants.isa_lower_boundaries
14upper_boundaries = constants.isa_upper_boundaries
15sea_level_temperature = constants.isa_sea_level_temperature
16sea_level_pressure = constants.isa_sea_level_pressure
17temperature_gradient = constants.isa_temperature_gradient
18g = constants.gravitational_acceleration
19R = constants.dry_air_gas_constant
20
21def which_level(geopotential_height):
22    # basic check
23    if geopotential_height < lower_boundaries[0] \
24        or geopotential_height > upper_boundaries[-1]:
25        message = 'z must be > ' + str(lower_boundaries[0]) + ' and < ' \
26            + str(upper_boundaries[-1])
27        raise 'BadGeopHgt', message
28    for level_idx in range(isa_levels):
29        if geopotential_height <= upper_boundaries[level_idx]:
30            return level_idx
31   
32def temperature_calculator(geopotential_height):
33    level = which_level(geopotential_height)
34    temperature = sea_level_temperature
35    for level_idx in range(level):
36        temperature += temperature_gradient[level_idx] \
37            * (upper_boundaries[level_idx] - lower_boundaries[level_idx])
38    temperature += temperature_gradient[level] \
39            * (geopotential_height - lower_boundaries[level])
40    return temperature
41
42
43def pressure_calculator(geopotential_height):
44    # following Richard's notes (eqn 3.3)
45    level = which_level(geopotential_height)
46    integral = 0
47    for level_idx in range(level):
48        lower_boundary_temperature = \
49            temperature_calculator(lower_boundaries[level_idx])
50        if temperature_gradient[level_idx] != 0.:
51            integral += n.log(  \
52                (lower_boundary_temperature \
53                + (upper_boundaries[level_idx] - lower_boundaries[level_idx]) \
54                * temperature_gradient[level_idx]) / lower_boundary_temperature \
55                ) / temperature_gradient[level_idx]
56        else:
57            integral += \
58             (upper_boundaries[level_idx] -lower_boundaries[level_idx]) \
59             / lower_boundary_temperature
60    lower_boundary_temperature = \
61        temperature_calculator(lower_boundaries[level])
62    if temperature_gradient[level] != 0.:
63        integral += n.log(  \
64            (lower_boundary_temperature \
65            + (geopotential_height - lower_boundaries[level]) \
66            * temperature_gradient[level]) / lower_boundary_temperature \
67            ) / temperature_gradient[level]
68    else:
69        integral += \
70         (geopotential_height -lower_boundaries[level]) \
71         / lower_boundary_temperature
72    return sea_level_pressure * n.exp( -g / R * integral)
73
74def find_height(pressure,tolerance=1):
75    residue = 1e6
76    top = upper_boundaries[-1]
77    bottom = lower_boundaries[0]
78    while abs(residue) > tolerance:
79        midpoint = (top + bottom) / 2
80        midpoint_pressure = pressure_calculator(midpoint)
81        residue = pressure - midpoint_pressure
82        #print midpoint, top, bottom, midpoint_pressure, residue, abs(residue), tolerance
83        # assuming p decreases with height...
84        if residue > 0:
85            top = midpoint
86        else:
87            bottom = midpoint
88    return int(midpoint)
89
90
91
92
Note: See TracBrowser for help on using the repository browser.