1 | ''' |
---|
2 | Functions to calculate thermodynamic quantities for the International Standard |
---|
3 | Atmosphere. |
---|
4 | ''' |
---|
5 | |
---|
6 | from __future__ import division |
---|
7 | from sys import path |
---|
8 | path.append('/Users/val/projects/vapylib') |
---|
9 | import numpy as n |
---|
10 | import constants |
---|
11 | |
---|
12 | isa_levels = constants.isa_levels |
---|
13 | lower_boundaries = constants.isa_lower_boundaries |
---|
14 | upper_boundaries = constants.isa_upper_boundaries |
---|
15 | sea_level_temperature = constants.isa_sea_level_temperature |
---|
16 | sea_level_pressure = constants.isa_sea_level_pressure |
---|
17 | temperature_gradient = constants.isa_temperature_gradient |
---|
18 | g = constants.gravitational_acceleration |
---|
19 | R = constants.dry_air_gas_constant |
---|
20 | |
---|
21 | def 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 | |
---|
32 | def 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 | |
---|
43 | def 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 | |
---|
74 | def 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 | |
---|