Jump to content

Air mass: Difference between revisions

m
Python example
(Added C solution)
m (Python example)
Line 786:
85 10.07896219 10.08115981
90 34.32981136 34.36666557
</pre>
 
=={{header|Python}}==
<lang python>""" Rosetta Code task: Air_mass """
 
from math import sqrt, cos, exp
 
DEG = 0.017453292519943295769236907684886127134 # degrees to radians
RE = 6371000 # Earth radius in meters
dd = 0.001 # integrate in this fraction of the distance already covered
FIN = 10000000 # integrate only to a height of 10000km, effectively infinity
def rho(a):
""" the density of air as a function of height above sea level """
return exp(-a / 8500.0)
def height(a, z, d):
"""
a = altitude of observer
z = zenith angle (in degrees)
d = distance along line of sight
"""
return sqrt((RE + a)**2 + d**2 - 2 * d * (RE + a) * cos((180 - z) * DEG)) - RE
def column_density(a, z):
""" integrates density along the line of sight """
dsum, d = 0.0, 0.0
while d < FIN:
delta = max(dd, (dd)*d) # adaptive step size to avoid it taking forever:
dsum += rho(height(a, z, d + 0.5 * delta)) * delta
d += delta
return dsum
</lang>{{out}}
<pre>
Angle 0 m 13700 m
------------------------------------
0 1.000 1.000
5 1.004 1.004
10 1.015 1.015
15 1.035 1.035
20 1.064 1.064
25 1.103 1.103
30 1.154 1.154
35 1.220 1.220
40 1.304 1.304
45 1.412 1.412
50 1.553 1.553
55 1.739 1.739
60 1.992 1.992
65 2.352 2.352
70 2.895 2.895
75 3.796 3.796
80 5.539 5.539
85 10.079 10.081
90 34.330 34.367
</pre>
 
4,105

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.