-
Notifications
You must be signed in to change notification settings - Fork 73
/
astronomy.py
198 lines (158 loc) · 6.05 KB
/
astronomy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2011
# Author(s):
# Martin Raspaud <martin.raspaud@smhi.se>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Astronomy module.
Parts taken from http://www.geoastro.de/elevaz/basics/index.htm
"""
import datetime
import numpy as np
F = 1 / 298.257223563 # Earth flattening WGS-84
A = 6378.137 # WGS84 Equatorial radius
MFACTOR = 7.292115E-5
def jdays2000(utc_time, hour=12):
"""Get the days since year 2000.
"""
return _days(utc_time - datetime.datetime(2000, 1, 1, hour, 0))
def jdays1970(utc_time, hour=0):
"""Get the days since year 1950.
"""
return _days(utc_time - datetime.datetime(1970, 1, 1, hour, 0))
def jdays1950(utc_time, hour=0):
"""Get the days since year 1950.
"""
return _days(utc_time - datetime.datetime(1950, 1, 1, hour, 0))
def jdays1900(utc_time, hour=12):
"""Get the days since year 1950.
"""
return _days(utc_time - datetime.datetime(1900, 1, 1, hour, 0))
def jdays(utc_time):
"""Get the julian day of *utc_time*.
"""
return jdays2000(utc_time) + 2451545
def _days(dt):
"""Get the days (floating point) from *d_t*.
"""
"""
return (dt.days +
(dt.seconds +
dt.microseconds / (1000000.0)) / (24 * 3600.0))"""
def get_day(timedelta):
return (timedelta.days +
(timedelta.seconds +
timedelta.microseconds / (1000000.0)) / (24 * 3600.0))
try:
days = get_day(dt)
except AttributeError:
days = np.array([get_day(x) for x in dt])
return days
def gmst(utc_time):
"""Greenwich mean sidereal utc_time, in radians.
As defined in the AIAA 2006 implementation:
http://www.celestrak.com/publications/AIAA/2006-6753/
"""
ut1 = jdays2000(utc_time) / 36525.0
theta = 67310.54841 + ut1 * (876600 * 3600 + 8640184.812866 + ut1 *
(0.093104 - ut1 * 6.2 * 10e-6))
return np.deg2rad(theta / 240.0) % (2 * np.pi)
def _lmst(utc_time, longitude):
"""Local mean sidereal time, computed from *utc_time* and *longitude*.
In radians.
"""
return gmst(utc_time) + longitude
def sun_ecliptic_longitude(utc_time):
"""Ecliptic longitude of the sun at *utc_time*.
"""
jdate = jdays2000(utc_time) / 36525.0
# mean anomaly, rad
m_a = np.deg2rad(357.52910 +
35999.05030*jdate -
0.0001559*jdate*jdate -
0.00000048*jdate*jdate*jdate)
# mean longitude, deg
l_0 = 280.46645 + 36000.76983*jdate + 0.0003032*jdate*jdate
d_l = ((1.914600 - 0.004817*jdate - 0.000014*jdate*jdate)*np.sin(m_a) +
(0.019993 - 0.000101*jdate)*np.sin(2*m_a) + 0.000290*np.sin(3*m_a))
# true longitude, deg
l__ = l_0 + d_l
return np.deg2rad(l__)
def sun_ra_dec(utc_time):
"""Right ascension and declination of the sun at *utc_time*.
"""
jdate = jdays2000(utc_time) / 36525.0
eps = np.deg2rad(23.0 + 26.0/60.0 + 21.448/3600.0 -
(46.8150*jdate + 0.00059*jdate*jdate -
0.001813*jdate*jdate*jdate) / 3600)
eclon = sun_ecliptic_longitude(utc_time)
x__ = np.cos(eclon)
y__ = np.cos(eps) * np.sin(eclon)
z__ = np.sin(eps) * np.sin(eclon)
r__ = np.sqrt(1.0 - z__ * z__)
# sun declination
declination = np.arctan2(z__, r__)
# right ascension
right_ascension = 2 * np.arctan2(y__, (x__ + r__))
return right_ascension, declination
def _local_hour_angle(utc_time, longitude, right_ascension):
"""Hour angle at *utc_time* for the given *longitude* and
*right_ascension*
longitude in radians
"""
return _lmst(utc_time, longitude) - right_ascension
def get_alt_az(utc_time, lon, lat):
"""Return sun altitude and azimuth from *utc_time*, *lon*, and *lat*.
lon,lat in degrees
What is the unit of the returned angles and heights!? FIXME!
"""
lon = np.deg2rad(lon)
lat = np.deg2rad(lat)
ra_, dec = sun_ra_dec(utc_time)
h__ = _local_hour_angle(utc_time, lon, ra_)
return (np.arcsin(np.sin(lat)*np.sin(dec) +
np.cos(lat) * np.cos(dec) * np.cos(h__)),
np.arctan2(-np.sin(h__), (np.cos(lat)*np.tan(dec) -
np.sin(lat)*np.cos(h__))))
def cos_zen(utc_time, lon, lat):
"""Cosine of the sun-zenith angle for *lon*, *lat* at *utc_time*.
utc_time: datetime.datetime instance of the UTC time
lon and lat in degrees.
"""
lon = np.deg2rad(lon)
lat = np.deg2rad(lat)
r_a, dec = sun_ra_dec(utc_time)
h__ = _local_hour_angle(utc_time, lon, r_a)
return (np.sin(lat)*np.sin(dec) + np.cos(lat) * np.cos(dec) * np.cos(h__))
def sun_zenith_angle(utc_time, lon, lat):
"""Sun-zenith angle for *lon*, *lat* at *utc_time*.
lon,lat in degrees.
The angle returned is given in degrees
"""
return np.rad2deg(np.arccos(cos_zen(utc_time, lon, lat)))
def observer_position(time, lon, lat, alt):
"""Calculate observer ECI position.
http://celestrak.com/columns/v02n03/
"""
lon = np.deg2rad(lon)
lat = np.deg2rad(lat)
theta = (gmst(time) + lon) % (2 * np.pi)
c = 1 / np.sqrt(1 + F * (F - 2) * np.sin(lat)**2)
sq = c * (1 - F)**2
achcp = (A * c + alt) * np.cos(lat)
x = achcp * np.cos(theta) # kilometers
y = achcp * np.sin(theta)
z = (A * sq + alt) * np.sin(lat)
vx = -MFACTOR*y # kilometers/second
vy = MFACTOR*x
vz = 0
return (x, y, z), (vx, vy, vz)