Air mass: Difference between revisions

(Add Swift)
Line 290:
90 34.32981136 34.36666557
</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
'''Preliminaries'''
<lang jq>def pi: 4 * (1|atan);
 
def radians: . * pi / 180;
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
# Input: a number
# Output: a string with $digits fractional decimal digits, with proper rounding
def fmt($width; $digits):
. as $in
| tostring
| index(".") as $ix
| if test("[eE]") then .
elif $ix
then pow(10; $digits) as $p
| ($in * $p | round | tostring) as $s
| if test("[eE]") then $s
else ($s | index(".")) as $ix
| if $ix then $s[:$ix + 1] + $s[$ix+1: $ix+1+$digits]
else $s[:-$digits] + "." + $s[-$digits:]
end
end
else . + "." + "0" * digits
end
| lpad($width);</lang>
'''Physics'''
<lang jq># constants
def RE: 6371000; # radius of earth in meters
def DD: 0.001; # integrate in this fraction of the distance already covered
def FIN: 1e7; # integrate only to a height of 10000km, effectively infinity
# The density of air as a function of height above sea level.
def rho: (-./8500) | exp;
 
# a = altitude of observer (in m)
# z = zenith angle (in degrees)
# d = distance along line of sight (in m)
def height($a; $z; $d):
(RE + $a) as $aa
| (($aa * $aa + $d * $d - 2 * $d * $aa * ((180-$z)|radians|cos) )|sqrt ) - RE;
# Integrates density along the line of sight.
def columnDensity($a; $z):
{ sum: 0, d: 0 }
| until (.d >= FIN;
([DD, DD * .d] | max) as $delta # adaptive step size to avoid it taking forever
| .sum = .sum + ((height($a; $z; .d + 0.5 * $delta))|rho) * $delta
| .d += $delta )
| .sum ;
def airmass(a; z): columnDensity(a; z) / columnDensity(a; 0);
 
"Angle 0 m 13700 m",
"------------------------------------",
( range(0; 91; 5)
| "\(lpad(2)) \(airmass(0; .)|fmt(11;8)) \(airmass(13700; .)|fmt(11;8))" )</lang>
{{out}}
<pre>
Angle 0 m 13700 m
------------------------------------
0 1.00000000 1.00000000
5 1.00380963 1.00380965
10 1.01538466 1.01538475
15 1.03517744 1.03517765
20 1.06399053 1.06399093
25 1.10305937 1.10306005
30 1.15418974 1.15419083
35 1.21998076 1.21998246
40 1.30418931 1.30419190
45 1.41234169 1.41234567
50 1.55280404 1.55281025
55 1.73875921 1.73876915
60 1.99212000 1.99213665
65 2.35199740 2.35202722
70 2.89531368 2.89537287
75 3.79582352 3.79596149
80 5.53885809 5.53928113
85 10.07896219 10.08115981
90 34.32981136 34.36666557
</pre>
 
 
=={{header|Julia}}==
2,442

edits