1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
"""Pythonic wrappers for AACGM-V2 C functions that were depricated in the |
3
|
|
|
change from version 2.0.0 to version 2.0.2 |
4
|
|
|
|
5
|
|
|
Functions |
6
|
|
|
------------------------------------------------------------------------------- |
7
|
|
|
convert : Converts array location |
8
|
|
|
set_coeff_path : Previously set environment variables, no longer used |
9
|
|
|
subsol : finds subsolar geocentric longitude and latitude |
10
|
|
|
gc2gd_lat : Convert between geocentric and geodetic coordinates |
11
|
|
|
igrf_dipole_axis : Get Cartesian unit vector pointing at the IGRF north dipole |
12
|
|
|
------------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
References |
15
|
|
|
------------------------------------------------------------------------------- |
16
|
|
|
Laundal, K. M. and A. D. Richmond (2016), Magnetic Coordinate Systems, Space |
17
|
|
|
Sci. Rev., doi:10.1007/s11214-016-0275-y. |
18
|
|
|
------------------------------------------------------------------------------- |
19
|
|
|
""" |
20
|
|
|
|
21
|
|
|
from __future__ import division, absolute_import, unicode_literals |
22
|
|
|
import numpy as np |
|
|
|
|
23
|
|
|
import logbook as logging |
|
|
|
|
24
|
|
|
import aacgmv2 |
25
|
|
|
|
26
|
|
|
def convert(lat, lon, alt, date=None, a2g=False, trace=False, allowtrace=False, |
|
|
|
|
27
|
|
|
badidea=False, geocentric=False): |
28
|
|
|
"""Converts between geomagnetic coordinates and AACGM coordinates |
29
|
|
|
|
30
|
|
|
Parameters |
31
|
|
|
------------ |
32
|
|
|
lat : (float) |
33
|
|
|
Input latitude in degrees N (code specifies type of latitude) |
34
|
|
|
lon : (float) |
35
|
|
|
Input longitude in degrees E (code specifies type of longitude) |
36
|
|
|
alt : (float) |
37
|
|
|
Altitude above the surface of the earth in km |
38
|
|
|
date : (datetime) |
39
|
|
|
Datetime for magnetic field |
40
|
|
|
a2g : (bool) |
41
|
|
|
True for AACGM-v2 to geographic (geodetic), False otherwise |
42
|
|
|
(default=False) |
43
|
|
|
trace : (bool) |
44
|
|
|
If True, use field-line tracing, not coefficients (default=False) |
45
|
|
|
allowtrace : (bool) |
46
|
|
|
If True, use trace only above 2000 km (default=False) |
47
|
|
|
badidea : (bool) |
48
|
|
|
If True, use coefficients above 2000 km (default=False) |
49
|
|
|
geocentric : (bool) |
50
|
|
|
True for geodetic, False for geocentric w/RE=6371.2 (default=False) |
51
|
|
|
|
52
|
|
|
Returns |
53
|
|
|
------- |
54
|
|
|
lat_out : (float) |
55
|
|
|
Output latitude in degrees N |
56
|
|
|
lon_out : (float) |
57
|
|
|
Output longitude in degrees E |
58
|
|
|
""" |
59
|
|
|
if(np.array(alt).max() > 2000 and not trace and not allowtrace and |
60
|
|
|
badidea): |
61
|
|
|
estr = 'coefficients are not valid for altitudes above 2000 km. You' |
62
|
|
|
estr += ' must either use field-line tracing (trace=True ' |
63
|
|
|
estr += 'or allowtrace=True) or indicate you know this is a bad idea' |
64
|
|
|
logging.error(estr) |
65
|
|
|
raise ValueError |
66
|
|
|
|
|
|
|
|
67
|
|
|
# construct a code from the boolian flags |
68
|
|
|
bit_code = aacgmv2.convert_bool_to_bit(a2g=a2g, trace=trace, |
69
|
|
|
allowtrace=allowtrace, |
70
|
|
|
badidea=badidea, |
71
|
|
|
geocentric=geocentric) |
72
|
|
|
|
73
|
|
|
# convert location |
74
|
|
|
lat_out, lon_out, r_out = aacgmv2.convert_latlon_arr(lat, lon, alt, date, |
|
|
|
|
75
|
|
|
code=bit_code) |
76
|
|
|
|
77
|
|
|
return lat_out, lon_out |
78
|
|
|
|
79
|
|
|
def set_coeff_path(): |
80
|
|
|
"""This depricated routine used to set environment variables, and now is |
81
|
|
|
not needed. |
82
|
|
|
""" |
83
|
|
|
|
84
|
|
|
logging.warning("this routine is no longer needed") |
85
|
|
|
return |
86
|
|
|
|
87
|
|
|
def subsol(year, doy, ut): |
|
|
|
|
88
|
|
|
"""Finds subsolar geocentric longitude and latitude. |
89
|
|
|
|
90
|
|
|
Parameters |
91
|
|
|
------------ |
92
|
|
|
year : (int) |
93
|
|
|
Calendar year between 1601 and 2100 |
94
|
|
|
doy : (int) |
95
|
|
|
Day of year between 1-365/366 |
96
|
|
|
ut : (float) |
97
|
|
|
Seconds since midnight on the specified day |
98
|
|
|
|
99
|
|
|
Returns |
100
|
|
|
--------- |
101
|
|
|
sbsllon : (float) |
102
|
|
|
Subsolar longitude for the given date/time |
103
|
|
|
sbsllat : (float) |
104
|
|
|
Subsolar lattude for the given date/time |
105
|
|
|
|
106
|
|
|
Notes |
107
|
|
|
-------- |
108
|
|
|
Based on formulas in Astronomical Almanac for the year 1996, p. C24. |
109
|
|
|
(U.S. Government Printing Office, 1994). Usable for years 1601-2100, |
110
|
|
|
inclusive. According to the Almanac, results are good to at least 0.01 |
111
|
|
|
degree latitude and 0.025 degrees longitude between years 1950 and 2050. |
112
|
|
|
Accuracy for other years has not been tested. Every day is assumed to have |
113
|
|
|
exactly 86400 seconds; thus leap seconds that sometimes occur on December |
114
|
|
|
31 are ignored (their effect is below the accuracy threshold of the |
115
|
|
|
algorithm). |
116
|
|
|
After Fortran code by A. D. Richmond, NCAR. Translated from IDL |
117
|
|
|
by K. Laundal. |
118
|
|
|
""" |
119
|
|
|
yr = year - 2000 |
|
|
|
|
120
|
|
|
|
121
|
|
|
if year >= 2101: |
122
|
|
|
logging.error('subsol invalid after 2100. Input year is:', year) |
123
|
|
|
|
124
|
|
|
nleap = np.floor((year - 1601) / 4) |
125
|
|
|
nleap = nleap - 99 |
126
|
|
|
if year <= 1900: |
127
|
|
|
if year <= 1600: |
128
|
|
|
print('subsol.py: subsol invalid before 1601. Input year is:', year) |
129
|
|
|
ncent = np.floor((year - 1601) / 100) |
130
|
|
|
ncent = 3 - ncent |
131
|
|
|
nleap = nleap + ncent |
132
|
|
|
|
133
|
|
|
l0 = -79.549 + (-0.238699 * (yr - 4 * nleap) + 3.08514e-2 * nleap) |
|
|
|
|
134
|
|
|
g0 = -2.472 + (-0.2558905 * (yr - 4 * nleap) - 3.79617e-2 * nleap) |
|
|
|
|
135
|
|
|
|
136
|
|
|
# Days (including fraction) since 12 UT on January 1 of IYR: |
137
|
|
|
df = (ut / 86400 - 1.5) + doy |
|
|
|
|
138
|
|
|
|
139
|
|
|
# Addition to Mean longitude of Sun since January 1 of IYR: |
140
|
|
|
lf = 0.9856474 * df |
|
|
|
|
141
|
|
|
|
142
|
|
|
# Addition to Mean anomaly since January 1 of IYR: |
143
|
|
|
gf = 0.9856003 * df |
|
|
|
|
144
|
|
|
|
145
|
|
|
# Mean longitude of Sun: |
146
|
|
|
l = l0 + lf |
|
|
|
|
147
|
|
|
|
148
|
|
|
# Mean anomaly: |
149
|
|
|
grad = np.radians(g0 + gf) |
150
|
|
|
|
151
|
|
|
# Ecliptic longitude: |
152
|
|
|
lmrad = np.radians(l + 1.915 * np.sin(grad) + 0.020 * np.sin(2 * grad)) |
153
|
|
|
sinlm = np.sin(lmrad) |
154
|
|
|
|
155
|
|
|
# Days (including fraction) since 12 UT on January 1 of 2000: |
156
|
|
|
n = df + 365.0 * yr + nleap |
|
|
|
|
157
|
|
|
|
158
|
|
|
# Obliquity of ecliptic: |
159
|
|
|
epsrad = np.radians(23.439 - 4.0e-7 * n) |
160
|
|
|
|
161
|
|
|
# Right ascension: |
162
|
|
|
alpha = np.degrees(np.arctan2(np.cos(epsrad) * sinlm, np.cos(lmrad))) |
163
|
|
|
|
164
|
|
|
# Declination: |
165
|
|
|
delta = np.degrees(np.arcsin(np.sin(epsrad) * sinlm)) |
166
|
|
|
|
167
|
|
|
# Subsolar latitude: |
168
|
|
|
sbsllat = delta |
169
|
|
|
|
170
|
|
|
# Equation of time (degrees): |
171
|
|
|
etdeg = l - alpha |
172
|
|
|
nrot = np.round(etdeg / 360.0) |
173
|
|
|
etdeg = etdeg - 360.0 * nrot |
174
|
|
|
|
175
|
|
|
# Apparent time (degrees): |
176
|
|
|
aptime = ut / 240.0 + etdeg # Earth rotates one degree every 240 s. |
177
|
|
|
|
178
|
|
|
# Subsolar longitude: |
179
|
|
|
sbsllon = 180.0 - aptime |
180
|
|
|
nrot = np.round(sbsllon / 360.0) |
181
|
|
|
sbsllon = sbsllon - 360.0 * nrot |
182
|
|
|
|
183
|
|
|
return sbsllon, sbsllat |
184
|
|
|
|
185
|
|
|
def gc2gd_lat(gc_lat): |
186
|
|
|
"""Convert geocentric latitude to geodetic latitude using WGS84. |
187
|
|
|
|
188
|
|
|
Parameters |
189
|
|
|
----------- |
190
|
|
|
gc_lat : (array_like or float) |
191
|
|
|
Geocentric latitude in degrees N |
192
|
|
|
|
193
|
|
|
Returns |
194
|
|
|
--------- |
195
|
|
|
gd_lat : (same as input) |
196
|
|
|
Geodetic latitude in degrees N |
197
|
|
|
""" |
198
|
|
|
WGS84_e2 = 0.006694379990141317 - 1.0 |
|
|
|
|
199
|
|
|
return np.rad2deg(-np.arctan(np.tan(np.deg2rad(gc_lat)) / WGS84_e2)) |
200
|
|
|
|
201
|
|
|
def igrf_dipole_axis(date): |
|
|
|
|
202
|
|
|
"""Get Cartesian unit vector pointing at dipole pole in the north, |
203
|
|
|
according to IGRF |
204
|
|
|
|
205
|
|
|
Parameters |
206
|
|
|
------------- |
207
|
|
|
date : (dt.datetime) |
208
|
|
|
Date and time |
209
|
|
|
|
210
|
|
|
Returns |
211
|
|
|
---------- |
212
|
|
|
m: (np.ndarray) |
213
|
|
|
Cartesian 3 element vector pointing at dipole pole in the north |
214
|
|
|
(geocentric coords) |
215
|
|
|
|
216
|
|
|
Notes |
217
|
|
|
---------- |
218
|
|
|
IGRF coefficients are read from the igrf12coeffs.txt file. It should also |
219
|
|
|
work after IGRF updates. The dipole coefficients are interpolated to the |
220
|
|
|
date, or extrapolated if date > latest IGRF model |
221
|
|
|
""" |
222
|
|
|
import datetime as dt |
223
|
|
|
|
224
|
|
|
# get time in years, as float: |
225
|
|
|
year = date.year |
226
|
|
|
doy = date.timetuple().tm_yday |
227
|
|
|
year_days = int(dt.date(date.year, 12, 31).strftime("%j")) |
228
|
|
|
year = year + doy / year_days |
229
|
|
|
|
230
|
|
|
# read the IGRF coefficients |
231
|
|
|
with open(aacgmv2.IGRF_12_COEFFS, 'r') as f: |
|
|
|
|
232
|
|
|
lines = f.readlines() |
233
|
|
|
|
234
|
|
|
years = lines[3].split()[3:][:-1] |
235
|
|
|
years = np.array(years, dtype=float) # time array |
236
|
|
|
|
237
|
|
|
g10 = lines[4].split()[3:] |
238
|
|
|
g11 = lines[5].split()[3:] |
239
|
|
|
h11 = lines[6].split()[3:] |
240
|
|
|
|
241
|
|
|
# secular variation coefficients (for extrapolation) |
242
|
|
|
g10sv = np.float32(g10[-1]) |
243
|
|
|
g11sv = np.float32(g11[-1]) |
244
|
|
|
h11sv = np.float32(h11[-1]) |
245
|
|
|
|
246
|
|
|
# model coefficients: |
247
|
|
|
g10 = np.array(g10[:-1], dtype=float) |
248
|
|
|
g11 = np.array(g11[:-1], dtype=float) |
249
|
|
|
h11 = np.array(h11[:-1], dtype=float) |
250
|
|
|
|
251
|
|
|
# get the gauss coefficient at given time: |
252
|
|
|
if year <= years[-1]: |
253
|
|
|
# regular interpolation |
254
|
|
|
g10 = np.interp(year, years, g10) |
255
|
|
|
g11 = np.interp(year, years, g11) |
256
|
|
|
h11 = np.interp(year, years, h11) |
257
|
|
|
else: |
258
|
|
|
# extrapolation |
259
|
|
|
dt = year - years[-1] |
|
|
|
|
260
|
|
|
g10 = g10[-1] + g10sv * dt |
261
|
|
|
g11 = g11[-1] + g11sv * dt |
262
|
|
|
h11 = h11[-1] + h11sv * dt |
263
|
|
|
|
264
|
|
|
# calculate pole position |
265
|
|
|
B0 = np.sqrt(g10**2 + g11**2 + h11**2) |
|
|
|
|
266
|
|
|
|
267
|
|
|
# Calculate output |
268
|
|
|
m = -np.array([g11, h11, g10]) / B0 |
|
|
|
|
269
|
|
|
|
|
|
|
|
270
|
|
|
return m |
271
|
|
|
|