-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathsunposition.py
1322 lines (1155 loc) · 60 KB
/
sunposition.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# The MIT License (MIT)
#
# Copyright (c) 2025 Samuel Bear Powell
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import os
import sys
import argparse
import re
import numpy as np
import time
import datetime
import warnings
try:
#scipy is required for numba's linear algebra routines to work
import numba
import scipy
except:
numba = None
VERSION = '1.2.1'
_arg_parser = argparse.ArgumentParser(prog='sunposition',description='Compute sun position parameters given the time and location')
_arg_parser.add_argument('--version',action='version',version=f'%(prog)s {VERSION}')
_arg_parser.add_argument('--citation',action='store_true',help='Print citation information')
_arg_parser.add_argument('-t','--time',type=str,default='now',help='"now" or date and time in ISO8601 format or a (UTC) POSIX timestamp')
_arg_parser.add_argument('-lat','--latitude',type=float,default=51.48,help='observer latitude, in decimal degrees, positive for north')
_arg_parser.add_argument('-lon','--longitude',type=float,default=0.0,help='observer longitude, in decimal degrees, positive for east')
_arg_parser.add_argument('-e','--elevation',type=float,default=0,help='observer elevation, in meters')
_arg_parser.add_argument('-T','--temperature',type=float,default=14.6,help='temperature, in degrees celcius')
_arg_parser.add_argument('-p','--pressure',type=float,default=1013.0,help='atmospheric pressure, in millibar')
_arg_parser.add_argument('-a','--atmos_refract',type=float,default=None,help='Atmospheric refraction at sunrise and sunset, in degrees. Omit to compute automatically, spa.c uses 0.5667')
_arg_parser.add_argument('-dt',type=float,default=0.0,help='difference between earth\'s rotation time (TT) and universal time (UT1)')
_arg_parser.add_argument('-r','--radians',action='store_true',help='Output in radians instead of degrees')
_arg_parser.add_argument('--csv',action='store_true',help='Comma separated values (time,dt,lat,lon,elev,temp,pressure,az,zen,RA,dec,H)')
_arg_parser.add_argument('--jit',action='store_true',help='Enable Numba acceleration (likely to cause slowdown for a single computation!)')
def main(args=None, **kwargs):
"""Run sunposition command-line tool.
If run without arguments, uses sys.argv, otherwise arguments may be
specified by a list of strings to be parsed, e.g.:
main(['--time','now'])
or as keyword arguments:
main(time='now')
or as an argparse.Namespace object (as produced by argparse.ArgumentParser)
Parameters
----------
args : list of str or argparse.Namespace, optional
Command-line arguments. sys.argv is used if not provided.
version : bool
If true, print the version information and quit
citation : bool
If true, print citation information and quit
time : str
"now" or date and time in ISO8601 format or a UTC POSIX timestamp
latitude : float
observer latitude in decimal degrees, positive for north
longitude : float
observer longitude in decimal degrees, positive for east
elevation : float
observer elevation in meters
temperature : float
temperature, in degrees celcius
pressure : float
atmospheric pressure, in millibar
atmos_refract : float
atmospheric refraction at sunrise and sunset, in degrees
dt : float
difference between Earth's rotation time (TT) and universal time (UT1)
radians : bool
If True, output in radians instead of degrees
csv : bool
If True, output as comma separated values (time, dt, lat, lon, elev, temp, pressure, az, zen, RA, dec, H)
"""
if args is None and not kwargs:
args = _arg_parser.parse_args()
elif isinstance(args,(list,tuple)):
args = _arg_parser.parse_args(args)
for kw in kwargs:
setattr(args,kw,kwargs[kw])
if args.citation:
print("Algorithm:")
print(" Ibrahim Reda, Afshin Andreas, \"Solar position algorithm for solar radiation applications\",")
print(" Solar Energy, Volume 76, Issue 5, 2004, Pages 577-589, ISSN 0038-092X,")
print(" doi:10.1016/j.solener.2003.12.003")
print("Implemented by Samuel Powell, 2016-2025, https://github.com/s-bear/sun-position")
return 0
enable_jit(args.jit)
t = time_to_datetime64(args.time)
lat, lon, elev = args.latitude, args.longitude, args.elevation
temp, p, ar, dt = args.temperature, args.pressure, args.atmos_refract, args.dt
rad = args.radians
az, zen, ra, dec, h = sunposition(t, lat, lon, elev, temp, p, ar, dt, radians=rad)
if args.csv:
#machine readable
print(f'{t}, {dt}, {lat}, {lon}, {elev}, {temp}, {p}, {az:0.6f}, {zen:0.6f}, {ra:0.6f}, {dec:0.6f}, {h:0.6f}')
else:
dr = 'rad' if args.radians else 'deg'
ts = time_to_iso8601(t)
print(f"Computing sun position at T = {ts} + {dt} s")
print(f"Lat, Lon, Elev = {lat} deg, {lon} deg, {elev} m")
print(f"T, P = {temp} C, {p} mbar")
print("Results:")
print(f"Azimuth, zenith = {az:0.6f} {dr}, {zen:0.6f} {dr}")
print(f"RA, dec, H = {ra:0.6f} {dr}, {dec:0.6f} {dr}, {h:0.6f} {dr}")\
return 0
def enable_jit(en = True):
global _ENABLE_JIT
if en and numba is None:
warnings.warn('JIT unavailable (requires numba and scipy)',stacklevel=2)
#We set the _ENABLE_JIT flag regardless of whether numba is available, just to test that code path!
_ENABLE_JIT = en
def disable_jit():
enable_jit(False)
def jit_enabled():
if _ENABLE_JIT:
return _jit_test()
return False
def arcdist(p0, p1, *, radians=False, jit=None):
"""Angular distance between azimuth,zenith pairs
Parameters
----------
p0 : array_like, shape (..., 2)
p1 : array_like, shape (..., 2)
p[...,0] = azimuth angles, p[...,1] = zenith angles
radians : boolean (default False)
If False, angles are in degrees, otherwise in radians
jit : bool, optional
override module jit settings. True to enable Numba acceleration (default if Numba is available), False to disable.
Returns
-------
ad : array_like, shape is broadcast(p0,p1).shape
Arcdistances between corresponding pairs in p0,p1
In degrees by default, in radians if radians=True
"""
#formula comes from translating points into cartesian coordinates
#taking the dot product to get the cosine between the two vectors
#then arccos to return to angle, and simplify everything assuming real inputs
p0,p1 = np.broadcast_arrays(p0, p1)
if radians:
if jit: return _arcdist_jit(p0,p1)
else: return _arcdist(p0,p1)
else:
if jit: return _arcdist_deg_jit(p0,p1)
else: return _arcdist_deg(p0,p1)
_np_microseconds = np.dtype('datetime64[us]')
def time_to_datetime64(t):
'''Convert various date/time formats to microsecond `numpy.datetime64`.
When parameter `t` is a numeric type, it is assumed to be a POSIX-style
timestamp, in seconds since the 1970-01-01 epoch.
When `t` contains `datetime.datetime`s they're converted to UTC first using
`datetime.datetime.astimezone(datetime.timezone.utc)`, which assumes the
datetime is in local time if it's not timezone aware.
When `t` contains `str`s they're parsed per ISO 8601, with some variations
accepted. Notable, "now" uses `time.time_ns()` to obtain the current time.
The parser uses approximately the following grammar:
<DATETIME> := <DATE> ('T'|' ') <TIME> [<TIMEZONE>]
<DATE> := ['+'|'-'] <YEAR> ['-'|'/'] <MONTH> ['-'|'/'] <DAY>
<TIME> := <HOUR> [':'] <MINUTE> [[':'] <SECOND>]
<TIMEZONE> := 'Z' | ('+'|'-') <HOUR> [[':'] <MINUTE>]
Note that <YEAR> is a 0-padded 4-digit number, <MONTH>, <DAY>, <HOUR>, and
<MINUTE> are similarly 0-padded 2-digit numbers. <SECOND> is a 0-padded
2-digit number with an optional fractional part: '01' and '01.234' are both
valid <SECOND>s. <YEAR>, <MONTH>, and <DAY> may be fewer digits when the
separators (- or /) them are included, which eliminates any ambiguity.
Parameters
----------
t : array_like of various date/time formats
Returns
-------
t_microseconds : array of datetime64[us]
'''
t = np.asarray(t)
# [()] unwrap scalar values out of np.array
if np.issubdtype(t.dtype, np.datetime64):
#NB: issubdtype can't differentiate between different units of datetime64
# ie. it says datetime64[s] is the same as datetime64[us]
return t.astype(_np_microseconds)[()]
elif np.issubdtype(t.dtype, str):
return _time_string_to_i64_vec(t).astype(_np_microseconds)[()]
elif np.issubdtype(t.dtype, datetime.datetime):
return _time_datetime_to_datetime64(t).astype(_np_microseconds)[()]
else:
#assume posix timestamp (seconds)
return np.round(t*1e6).astype(_np_microseconds)[()]
def time_to_iso8601(t):
'''Convert various date/time formats to ISO8601 timestamp strings'''
t = time_to_datetime64(t)
s = _time_i64_to_string_vec(t.astype(np.int64))
if s.shape == (): return str(s)
return s
def julian_day(t, *, jit=None):
"""Convert timestamps from various formats to Julian days
Parameters
----------
t : array_like
datetime.datetime, numpy.datetime64, ISO8601 strings, or POSIX timestamps (str, float, int)
jit : bool or None
override module jit settings, to True/False to enable/disable numba acceleration
Returns
-------
jd : ndarray
datetimes converted to fractional Julian days
"""
t = time_to_datetime64(t).astype(np.int64)
if jit is None: jit = _ENABLE_JIT
if jit:
_jit_check()
jd = _julian_day_vec_jit(t)
else:
jd = _julian_day_vec(t)[()]
return jd
def sunposition(t, latitude, longitude, elevation, temperature=None, pressure=None, atmos_refract=None, delta_t=0, *, radians=False, jit=None):
"""Compute the observed and topocentric coordinates of the sun as viewed at the given time and location.
Parameters
----------
t : array_like of datetime, datetime64, str, or float
datetime.datetime, numpy.datetime64, ISO8601 strings, or POSIX timestamps (float or int)
latitude, longitude : array_like of float
decimal degrees, positive for north of the equator and east of Greenwich
elevation : array_like of float
meters, relative to the WGS-84 ellipsoid
temperature : None or array_like of float, optional
celcius, default is 14.6 (global average in 2013)
pressure : None or array_like of float, optional
millibar, default is 1013 (global average in ??)
atmos_refract : None or array_like of float, optional
Atmospheric refraction at sunrise and sunset, in degrees. None to compute automatically, spa.c default is 0.5667.
delta_t : array_like of float, optional
seconds, default is 0, difference between the earth's rotation time (TT) and universal time (UT)
radians : bool, optional
return results in radians if True, degrees if False (default)
jit : bool, optional
override module jit settings. True to enable Numba acceleration (default if Numba is available), False to disable.
Returns
-------
azimuth_angle : ndarray, measured eastward from north
zenith_angle : ndarray, measured down from vertical
right_ascension : ndarray, topocentric
declination : ndarray, topocentric
hour_angle : ndarray, topocentric
"""
if temperature is None:
temperature = 14.6
if pressure is None:
pressure = 1013
if jit is None:
jit = _ENABLE_JIT
t = time_to_datetime64(t).astype(np.int64)
if jit:
_jit_check()
args = np.broadcast_arrays(t, latitude, longitude, elevation, temperature, pressure, atmos_refract, delta_t)
for a in args: a.flags.writeable = False
sp = _sunpos_vec_jit(*args)
else:
sp = _sunpos_vec(t, latitude, longitude, elevation, temperature, pressure, atmos_refract, delta_t)
if radians:
sp = tuple(np.deg2rad(a) for a in sp)
sp = tuple(a[()] for a in sp) #unwrap np.array() from scalars
return sp
def topocentric_sunposition(t, latitude, longitude, elevation, delta_t=0, *, radians=False, jit=None):
"""Compute the topocentric coordinates of the sun as viewed at the given time and location.
Parameters
----------
t : array_like of datetime, datetime64, str, or float
datetime.datetime, numpy.datetime64, ISO8601 strings, or POSIX timestamps (float or int)
latitude, longitude : array_like of float
decimal degrees, positive for north of the equator and east of Greenwich
elevation : array_like of float
meters, relative to the WGS-84 ellipsoid
delta_t : array_like of float, optional
seconds, default is 0, difference between the earth's rotation time (TT) and universal time (UT)
radians : bool, optional
return results in radians if True, degrees if False (default)
jit : bool, optional
override module jit settings. True to enable Numba acceleration (default if Numba is available), False to disable.
Returns
-------
right_ascension : ndarray, topocentric
declination : ndarray, topocentric
hour_angle : ndarray, topocentric
"""
if jit is None:
jit = _ENABLE_JIT
t = time_to_datetime64(t).astype(np.int64)
if jit:
_jit_check()
args = np.broadcast_arrays(t, latitude, longitude, elevation, delta_t)
for a in args: a.flags.writeable = False
sp = _topo_sunpos_vec_jit(*args)
else:
sp = _topo_sunpos_vec(t, latitude, longitude, elevation, delta_t)
if radians:
sp = tuple(np.deg2rad(a) for a in sp)
sp = tuple(a[()] for a in sp) #unwrap np.array() from scalars
return sp
def observed_sunposition(t, latitude, longitude, elevation, temperature=None, pressure=None, atmos_refract=None, delta_t=0, *, radians=False, jit=None):
"""Compute the observed coordinates of the sun as viewed at the given time and location.
Parameters
----------
t : array_like of datetime, datetime64, str, or float
datetime.datetime, numpy.datetime64, ISO8601 strings, or POSIX timestamps (float or int)
latitude, longitude : array_like of float
decimal degrees, positive for north of the equator and east of Greenwich
elevation : array_like of float
meters, relative to the WGS-84 ellipsoid
temperature : None or array_like of float, optional
celcius, default is 14.6 (global average in 2013)
pressure : None or array_like of float, optional
millibar, default is 1013 (global average in ??)
atmos_refract : None or array_like of float, optional
Atmospheric refraction at sunrise and sunset, in degrees. None to compute automatically, spa.c default is 0.5667.
delta_t : array_like of float, optional
seconds, default is 0, difference between the earth's rotation time (TT) and universal time (UT)
radians : bool, optional
return results in radians if True, degrees if False (default)
jit : bool, optional
override module jit settings. True to enable Numba acceleration (default if Numba is available), False to disable.
Returns
-------
azimuth_angle : ndarray, measured eastward from north
zenith_angle : ndarray, measured down from vertical
"""
return sunpos(t, latitude, longitude, elevation, temperature, pressure, atmos_refract, delta_t, radians=radians, jit=jit)[:2]
sunpos = sunposition
topocentrict_sunpos = topocentric_sunposition
observed_sunpos = observed_sunposition
## Numba decorators ##
def _empty_decorator(f = None, *args, **kw):
if callable(f):
return f
return _empty_decorator
if numba is not None:
# register_jitable informs numba that a function may be compiled when
# called from jit'ed code, but doesn't jit it by default
register_jitable = numba.extending.register_jitable
# overload informs numba of an *alternate implementation* of a function to
# use within jit'ed code -- we use it to provide a jit-able version of polyval
overload = numba.extending.overload
#njit compiles code -- we use this for our top-level functions
njit = numba.njit
_ENABLE_JIT = not numba.config.DISABLE_JIT and not os.environ.get('NUMBA_DISABLE_JIT',False)
else:
#if numba is not available, use _empty_decorator instead
njit = _empty_decorator
overload = lambda *a,**k: _empty_decorator
register_jitable = _empty_decorator
_ENABLE_JIT = False
## machinery for jit_enabled()
def _jit_test_impl():
#return False if within Python
return False
@overload(_jit_test_impl)
def _jit_test_impl_jit():
def _jit_test_impl_jit():
#return True if within JIT'ed code
return True
return _jit_test_impl_jit
@njit
def _jit_test():
return _jit_test_impl()
def _jit_check():
if not _jit_test():
warnings.warn('JIT requested, but numba is not available!')
## arcdist ##
def _arcdist(p0,p1):
a0,z0 = p0[...,0], p0[...,1]
a1,z1 = p1[...,0], p1[...,1]
return np.arccos(np.cos(z0)*np.cos(z1)+np.cos(a0-a1)*np.sin(z0)*np.sin(z1))
def _arcdist_deg(p0,p1):
return np.rad2deg(_arcdist(np.deg2rad(p0),np.deg2rad(p1)))
_arcdist_jit = njit(_arcdist)
_arcdist_deg_jit = njit(_arcdist_deg)
## Dates and times ##
# this application has unusual date/time requirements that are not supported by
# Python's time or datetime libraries. Specifically:
# 1. The subroutines use Julian days to represent time
# 2. The algorithm supports dates from year -2000 to 6000 (datetime supports years 1-9999)
# For that reason, we will use our own date & time codes
# The Julian Day conversion in Reda & Andreas's paper required the Gregorian
# (year, month, day), with the time of day specified as a fractional day, all in UTC.
# We can use the algorithms in "Euclidean affine functions and their application to calendar algorithms" C. Neri, L. Schneider (2022) https://doi.org/10.1002/spe.3172
# to convert rata die timestamps (day number since epoch) to Gregorian (year, month, day)
# We need to be able to convert the following to Julian days:
# datetime.datetime
# numpy.datetime64
# int/float POSIX timestamp
# ISO8601 string
@register_jitable
def _time_day_to_date(day_number):
'''convert integer day number to Gregorian (year, month, day)
year,month,day are int32, uint8, uint8
"Euclidean affine functions and their application to calendar algorithms" C. Neri, L. Schneider (2022) https://doi.org/10.1002/spe.3172
'''
# date32_t to_date(int32_t N_U) {
N_U = np.int32(day_number)
s = np.uint32(82) #static uint32_t constexpr s = 82;
K = np.uint32(719468 + 146097 * s) #static uint32_t constexpr K = 719468 + 146097 * s;
L = np.uint32(400*s) #static uint32_t constexpr L = 400 * s;
# Rata die shift.
N = np.uint32(N_U + K) # uint32_t const N = N_U + K;
# Century.
N_1 = np.uint32(4 * N + 3) # uint32_t const N_1 = 4 * N + 3;
# uint32_t const C = N_1 / 146097;
# uint32_t const N_C = N_1 % 146097 / 4;
C, N_C = divmod(N_1, 146097)
C, N_C = np.uint32(C), np.uint32(N_C // 4)
# Year.
N_2 = np.uint32(4 * N_C + 3) # uint32_t const N_2 = 4 * N_C + 3;
P_2 = 2939745 * np.uint64(N_2) # uint64_t const P_2 = uint64_t(2939745) * N_2;
# uint32_t const Z = uint32_t(P_2 / 4294967296);
# uint32_t const N_Y = uint32_t(P_2 % 4294967296) / 2939745 / 4;
Z, N_Y = divmod(P_2, 4294967296)
Z, N_Y = np.uint32(Z), np.uint32(N_Y)
N_Y = np.uint32((N_Y // 2939745) // 4)
Y = np.uint32(100 * C + Z) # uint32_t const Y = 100 * C + Z;
# Month and day.
N_3 = np.uint32(2141 * N_Y + 197913) # uint32_t const N_3 = 2141 * N_Y + 197913;
# uint32_t const M = N_3 / 65536;
# uint32_t const D = N_3 % 65536 / 2141;
M, D = divmod(N_3, 65536)
M, D = np.uint32(M), np.uint32(D // 2141)
# Map. (Notice the year correction, including type change.)
J = (N_Y >= 306) # uint32_t const J = N_Y >= 306;
Y_G = np.int32(Y) - np.int32(L) + np.int32(J) # int32_t const Y_G = (Y - L) + J;
# uint32_t const M_G = J ? M - 12 : M;
if J:
M_G = np.uint8(M - 12) #we use uint8 instead of uint32
else:
M_G = np.uint8(M)
D_G = np.uint8(D + 1) # uint32_t const D_G = D + 1;
# return { Y_G, M_G, D_G };
return (Y_G, M_G, D_G)
@register_jitable
def _time_i64_to_datetime(t_microseconds):
'''Convert int64 timestamp to (year,month,day,hour,minute,second,micros)
t_microseconds : microseconds since 1970-1-1
'''
# divide by number of microseconds in a day
day_number, micros = divmod(t_microseconds,86400000000)
# use Neri-Schneider calendar algorithm to get Gregorian date
year,month,day = _time_day_to_date(day_number)
# divide micros to get hour,minute,second
hour, micros = divmod(micros, 3600000000)
minute, micros = divmod(micros, 60000000)
second, micros = divmod(micros, 1000000)
hour, minute, second = np.uint8(hour), np.uint8(minute), np.uint8(second)
micros = np.uint32(micros)
return year,month,day,hour,minute,second,micros
@register_jitable
def _time_date_to_day(date_tuple):
'''convert Gregorian (year, month, day) to day number
date_tuple : (int32, uint8, uint8) : year,month,day
returns day_number : int32
"Euclidean affine functions and their application to calendar algorithms" C. Neri, L. Schneider (2022) https://doi.org/10.1002/spe.3172
'''
year,month,day = date_tuple[:3]
Y_G = np.int32(year)
M_G = np.uint32(month)
D_G = np.uint32(day)
s = np.uint32(82) #static uint32_t constexpr s = 82;
K = np.uint32(719468 + 146097 * s) #static uint32_t constexpr K = 719468 + 146097 * s;
L = np.uint32(400*s) #static uint32_t constexpr L = 400 * s;
# int32_t to_rata_die(int32_t Y_G, uint32_t M_G, uint32_t D_G) {
# Map. (Notice the year correction, including type change.)
J = (M_G <= 2) # uint32_t const J = M_G <= 2;
Y = np.uint32(Y_G + L - J) # uint32_t const Y = (uint32_t(Y_G) + L) - J;
# uint32_t const M = J ? M_G + 12 : M_G;
if J:
M = np.uint32(M_G + 12)
else:
M = M_G
D = np.uint32(D_G - 1) # uint32_t const D = D_G - 1;
C = np.uint32(Y // 100) # uint32_t const C = Y / 100;
# Rata die.
y_star = np.uint32(1461 * Y // 4 - C + C // 4) # uint32_t const y_star = 1461 * Y / 4 - C + C / 4;
m_star = np.uint32((979 * M - 2919) // 32) # uint32_t const m_star = (979 * M - 2919) / 32;
N = np.uint32(y_star + m_star + D) # uint32_t const N = y_star + m_star + D;
# Rata die shift.
N_U = np.int32(N) - np.int32(K) # uint32_t const N_U = N - K; -- the original casts to int32 at return, we do it here
return N_U
@register_jitable
def _time_datetime_to_i64(datetime_tuple):
'''Convert datetime_tuple to int64 timestamp
datetime_tuple : (int32, uint8, uint8, uint8, uint8, uint8, uint32)
year,month,day,hour,minute,second,microsecond
returns t : int64, microseconds since epoch 1970-01-01
Does not check for valid dates
'''
year,month,day,hour,minute,second,microsecond = datetime_tuple[:7]
# accumulate parts
t = np.int64(microsecond)
t += np.int64(second)*1000000
t += np.int64(minute)*60000000
t += np.int64(hour)*3600000000
t += np.int64(_time_date_to_day((year,month,day)))*86400000000
return t
@register_jitable
def _time_datetime_to_i64_checked(datetime_tuple):
'''Convert year,month,day,hour,minute second,microsecond to int64 timestamp, with validity check
year,month,day : int32, uint8, uint8
hour,minute,second,microsecond: uint8, uint8, uint8, uint32
Throws a ValueError if the date & time are not valid in the Gregorian calendar
return t : int64, microseconds since the epoch 1970-01-01
'''
t = _time_datetime_to_i64(datetime_tuple)
datetime_tuple_valid = _time_i64_to_datetime(t)
if datetime_tuple != datetime_tuple_valid:
raise ValueError('Invalid date')
return t
# ( (DATE, NO SEPARATORS )|(DATE, WITH SEPARATORS ))(TIME (TIMEZONE ) )
# (1:YEAR )(2:MONTH)(3:DAY ) (4: YEAR )(5) (6:MONTH) (7: DAY) (8: HOUR) (9: MIN ) (10: SECOND ) (11:TZHOUR) (12:TZMIN)
_iso8601_re = re.compile(r'(?:(?:([+-]?\d{4})([01]\d)([0-3]\d))|(?:([+-]?\d{1,4})([-/])([01]?\d)\5([0-3]?\d)))(?:[T ]([012]\d):?([0-5]\d)(?::?([0-6]\d(?:\.\d*)?))?(?:Z|(?:([+-]\d{2})(?::?(\d{2}))?))?)?')
def _time_string_to_i64(s):
'''parse timestamp string to integer microsecond timestamp, assumes UTC if timezone is not specified
strings may be:
- "now" -- which gets the current time
- POSIX timestamp string (seconds since epoch, including fractional part)
- ISO 8601 formatted string (including negative years, fractional seconds)
'''
if s == 'now':
return time.time_ns()//1000
try:
t = float(s)
#if np.abs(t - np.nextafter(t,0)) > 1e-6: warnings.warn('Timestamp resolution greater than 1 microsecond.')
return round(float(s)*1e6)
except ValueError: #
pass
match = _iso8601_re.fullmatch(s)
if not match:
raise ValueError('Could not parse timestamp string (must be "now" or float or ISO8601)')
year,month,day,year2,sep,month2,day2,hour,minute,second,tz_hour,tz_minute = match.groups()
if year is None: year,month,day = year2,month2,day2
if second is None: second = 0
if tz_hour is None: tz_hour = 0
if tz_minute is None: tz_minute = 0
if year is None or month is None or day is None or hour is None or minute is None:
raise ValueError('Could not parse timestamp string (must be "now" or float or ISO8601)')
year, month, day = int(year),int(month),int(day)
hour,minute,micros = int(hour), int(minute), round(float(second)*1e6)
second,micros = divmod(micros,1000000)
tz_hour, tz_minute = int(tz_hour), int(tz_minute)
if tz_minute < 0 or tz_minute >= 60:
raise ValueError(f'Invalid timezone: "{tz_hour:02}:{tz_minute:02}"')
if tz_hour < 0: tz_minute = -tz_minute
tz_micros = (tz_hour*3600 + tz_minute*60)*1000000 # timezone offset, in microseconds
# UTC offsets vary from -12:00 (US Minor Outlying Islands) to +14:00 (Kiribati)
tz_min, tz_max = -12*3600*1000000, 14*3600*1000000
if tz_micros < tz_min or tz_micros > tz_max:
raise ValueError(f'Invalid timezone: "{tz_hour:02}:{tz_minute:02}"')
#convert to timestamp
t = _time_datetime_to_i64_checked((year,month,day,hour,minute,second,micros))
#apply timezone offset and return
return t - tz_micros
_time_string_to_i64_vec = np.vectorize(_time_string_to_i64)
def _time_i64_to_string(t):
'''Format a microsecond timestamp as an ISO8601 string'''
year, month, day, hour, minute, sec, micros = _time_i64_to_datetime(t)
millis, micros = divmod(micros,1000)
if millis:
if micros: fsec = f'.{millis:03}{micros:03}'
else: fsec = f'.{millis:03}'
else:
if micros: fsec = f'.000{micros:03}'
else: fsec = ''
return f'{year:04}-{month:02}-{day:02}T{hour:02}:{minute:02}:{sec:02}{fsec}Z'
_time_i64_to_string_vec = np.vectorize(_time_i64_to_string)
#we can't use numba to accelerate datetime.datetime ops, so use np.vectorize here
@np.vectorize
def _time_datetime_to_datetime64(t : datetime.datetime):
'''get the i64 timestamp from a datetime.datetime object.
Converts the datetime to UTC first, datetime objects without tzinfo are treated as local time.
'''
return np.datetime64(t.astimezone(datetime.timezone.utc).replace(tzinfo=None))
## Procedure from Reda, Andreas (2004) ##
## 3.1. Calculate the Julian and Julian Ephemeris Day (JDE), centure, and millenium
@register_jitable
def _julian_day(t_microseconds):
"""Calculate the Julian Day from posix timestamp (seconds since epoch)"""
#TODO: check julian-gregorian calendar changeover for posix timestamps
# divide by number of microseconds in a day
day_number, micros = divmod(t_microseconds,86400000000)
# use Neri-Schneider calendar algorithm to get Gregorian date
year,month,day = _time_day_to_date(day_number)
day += np.float64(micros)/86400000000 #add back in fractional day
# eq 4
# 3.1.1) if M = 1 or 2, then Y = Y - 1 and M = M + 12
if month <= 2:
month += 12
year -= 1
jd = int(365.25 * (year + 4716)) + int(30.6001 * (month + 1)) + day - 1524.5
if jd > 2299160:
# 3.1.1) B is equal to 0 for the Julian calendar and is equal to
# (2-A+INT(A/4)), A = INT(Y/100), for the Gregorian calendar.
a = int(year / 100)
b = 2 - a + int(a / 4)
jd += b
return jd
_julian_day_vec = np.vectorize(_julian_day)
@njit
def _julian_day_vec_jit(t):
ts = np.asarray(t)
ts_flat = ts.flat
n = len(ts_flat)
jds = np.empty(n, dtype=np.float64)
for i, t in enumerate(ts_flat):
jds[i] = _julian_day(t)
jds = jds.reshape(ts.shape)
return jds[()]
@register_jitable
def _julian_ephemeris_day(jd, Delta_t):
"""Calculate the Julian Ephemeris Day from the Julian Day and delta-time = (terrestrial time - universal time) in seconds"""
# eq 5
return jd + Delta_t / 86400.0
@register_jitable
def _julian_century(jd):
"""Caluclate the Julian Century from Julian Day or Julian Ephemeris Day"""
# eq 6
# eq 7
return (jd - 2451545.0) / 36525.0
@register_jitable
def _julian_millennium(jc):
"""Calculate the Julian Millennium from Julian Ephemeris Century"""
# eq 8
return jc / 10.0
## 3.2. Calculate the Earth heliocentric longitude, latitude, and radius vector (L, B, and R)
@register_jitable
def _cos_sum(x, coeffs):
y = np.zeros(len(coeffs))
for i, abc in enumerate(coeffs):
for a,b,c in abc:
# eq 9
y[i] += a*np.cos(b + c*x)
return y
#implement np.polyval for numba using overload
@overload(np.polyval)
def _polyval_jit(p, x):
def _polyval_impl(p, x):
y = 0.0
for v in p:
y = y*x + v
return y
return _polyval_impl
# Table 1. Earth periodic terms, Earth Heliocentric Longitude coefficients (L0, L1, L2, L3, L4, and L5)
_EHL = (
#L5:
np.array([(1.0, 3.14, 0.0)]),
#L4:
np.array([(114.0, 3.142, 0.0), (8.0, 4.13, 6283.08), (1.0, 3.84, 12566.15)]),
#L3:
np.array([(289.0, 5.844, 6283.076), (35.0, 0.0, 0.0,), (17.0, 5.49, 12566.15),
(3.0, 5.2, 155.42), (1.0, 4.72, 3.52), (1.0, 5.3, 18849.23),
(1.0, 5.97, 242.73)]),
#L2:
np.array([(52919.0, 0.0, 0.0), (8720.0, 1.0721, 6283.0758), (309.0, 0.867, 12566.152),
(27.0, 0.05, 3.52), (16.0, 5.19, 26.3), (16.0, 3.68, 155.42),
(10.0, 0.76, 18849.23), (9.0, 2.06, 77713.77), (7.0, 0.83, 775.52),
(5.0, 4.66, 1577.34), (4.0, 1.03, 7.11), (4.0, 3.44, 5573.14),
(3.0, 5.14, 796.3), (3.0, 6.05, 5507.55), (3.0, 1.19, 242.73),
(3.0, 6.12, 529.69), (3.0, 0.31, 398.15), (3.0, 2.28, 553.57),
(2.0, 4.38, 5223.69), (2.0, 3.75, 0.98)]),
#L1:
np.array([(628331966747.0, 0.0, 0.0), (206059.0, 2.678235, 6283.07585), (4303.0, 2.6351, 12566.1517),
(425.0, 1.59, 3.523), (119.0, 5.796, 26.298), (109.0, 2.966, 1577.344),
(93.0, 2.59, 18849.23), (72.0, 1.14, 529.69), (68.0, 1.87, 398.15),
(67.0, 4.41, 5507.55), (59.0, 2.89, 5223.69), (56.0, 2.17, 155.42),
(45.0, 0.4, 796.3), (36.0, 0.47, 775.52), (29.0, 2.65, 7.11),
(21.0, 5.34, 0.98), (19.0, 1.85, 5486.78), (19.0, 4.97, 213.3),
(17.0, 2.99, 6275.96), (16.0, 0.03, 2544.31), (16.0, 1.43, 2146.17),
(15.0, 1.21, 10977.08), (12.0, 2.83, 1748.02), (12.0, 3.26, 5088.63),
(12.0, 5.27, 1194.45), (12.0, 2.08, 4694), (11.0, 0.77, 553.57),
(10.0, 1.3, 6286.6), (10.0, 4.24, 1349.87), (9.0, 2.7, 242.73),
(9.0, 5.64, 951.72), (8.0, 5.3, 2352.87), (6.0, 2.65, 9437.76),
(6.0, 4.67, 4690.48)]),
#L0:
np.array([(175347046.0, 0.0, 0.0), (3341656.0, 4.6692568, 6283.07585), (34894.0, 4.6261, 12566.1517),
(3497.0, 2.7441, 5753.3849), (3418.0, 2.8289, 3.5231), (3136.0, 3.6277, 77713.7715),
(2676.0, 4.4181, 7860.4194), (2343.0, 6.1352, 3930.2097), (1324.0, 0.7425, 11506.7698),
(1273.0, 2.0371, 529.691), (1199.0, 1.1096, 1577.3435), (990.0, 5.233, 5884.927),
(902.0, 2.045, 26.298), (857.0, 3.508, 398.149), (780.0, 1.179, 5223.694),
(753.0, 2.533, 5507.553), (505.0, 4.583, 18849.228), (492.0, 4.205, 775.523),
(357.0, 2.92, 0.067), (317.0, 5.849, 11790.629), (284.0, 1.899, 796.298),
(271.0, 0.315, 10977.079), (243.0, 0.345, 5486.778), (206.0, 4.806, 2544.314),
(205.0, 1.869, 5573.143), (202.0, 2.458, 6069.777), (156.0, 0.833, 213.299),
(132.0, 3.411, 2942.463), (126.0, 1.083, 20.775), (115.0, 0.645, 0.98),
(103.0, 0.636, 4694.003), (102.0, 0.976, 15720.839), (102.0, 4.267, 7.114),
(99.0, 6.21, 2146.17), (98.0, 0.68, 155.42), (86.0, 5.98, 161000.69),
(85.0, 1.3, 6275.96), (85.0, 3.67, 71430.7), (80.0, 1.81, 17260.15),
(79.0, 3.04, 12036.46), (75.0, 1.76, 5088.63), (74.0, 3.5, 3154.69),
(74.0, 4.68, 801.82), (70.0, 0.83, 9437.76), (62.0, 3.98, 8827.39),
(61.0, 1.82, 7084.9), (57.0, 2.78, 6286.6), (56.0, 4.39, 14143.5),
(56.0, 3.47, 6279.55), (52.0, 0.19, 12139.55), (52.0, 1.33, 1748.02),
(51.0, 0.28, 5856.48), (49.0, 0.49, 1194.45), (41.0, 5.37, 8429.24),
(41.0, 2.4, 19651.05), (39.0, 6.17, 10447.39), (37.0, 6.04, 10213.29),
(37.0, 2.57, 1059.38), (36.0, 1.71, 2352.87), (36.0, 1.78, 6812.77),
(33.0, 0.59, 17789.85), (30.0, 0.44, 83996.85), (30.0, 2.74, 1349.87),
(25.0, 3.16, 4690.48)])
)
@register_jitable
def _heliocentric_longitude(jme):
"""Compute the Earth Heliocentric Longitude (L) in degrees given the Julian Ephemeris Millennium"""
# eq 10
Li = _cos_sum(jme, _EHL) #L5, ..., L0
# eq 11
L = np.polyval(Li, jme) / 1e8
# eq 12
L = np.rad2deg(L) % 360
return L
# Table 1. Earth periodic terms, Earth Heliocentric Latitude coefficients (B0 and B1)
_EHB = (
#B1:
np.array([(9.0, 3.9, 5507.55), (6.0, 1.73, 5223.69)]),
#B0:
np.array([(280.0, 3.199, 84334.662), (102.0, 5.422, 5507.553), (80.0, 3.88, 5223.69),
(44.0, 3.7, 2352.87), (32.0, 4.0, 1577.34)])
)
@register_jitable
def _heliocentric_latitude(jme):
"""Compute the Earth Heliocentric Latitude (B) in degrees given the Julian Ephemeris Millennium"""
# section 3.2.7
Bi = _cos_sum(jme, _EHB)
B = np.polyval(Bi, jme) / 1e8
B = np.rad2deg(B) % 360
return B
#Table 1. Earth periodic terms, Earth Heliocentric Radius coefficients (R0, R1, R2, R3, R4)
_EHR = (
#R4:
np.array([(4.0, 2.56, 6283.08)]),
#R3:
np.array([(145.0, 4.273, 6283.076), (7.0, 3.92, 12566.15)]),
#R2:
np.array([(4359.0, 5.7846, 6283.0758), (124.0, 5.579, 12566.152), (12.0, 3.14, 0.0),
(9.0, 3.63, 77713.77), (6.0, 1.87, 5573.14), (3.0, 5.47, 18849.23)]),
#R1:
np.array([(103019.0, 1.10749, 6283.07585), (1721.0, 1.0644, 12566.1517), (702.0, 3.142, 0.0),
(32.0, 1.02, 18849.23), (31.0, 2.84, 5507.55), (25.0, 1.32, 5223.69),
(18.0, 1.42, 1577.34), (10.0, 5.91, 10977.08), (9.0, 1.42, 6275.96),
(9.0, 0.27, 5486.78)]),
#R0:
np.array([(100013989.0, 0.0, 0.0), (1670700.0, 3.0984635, 6283.07585), (13956.0, 3.05525, 12566.1517),
(3084.0, 5.1985, 77713.7715), (1628.0, 1.1739, 5753.3849), (1576.0, 2.8469, 7860.4194),
(925.0, 5.453, 11506.77), (542.0, 4.564, 3930.21), (472.0, 3.661, 5884.927),
(346.0, 0.964, 5507.553), (329.0, 5.9, 5223.694), (307.0, 0.299, 5573.143),
(243.0, 4.273, 11790.629), (212.0, 5.847, 1577.344), (186.0, 5.022, 10977.079),
(175.0, 3.012, 18849.228), (110.0, 5.055, 5486.778), (98.0, 0.89, 6069.78),
(86.0, 5.69, 15720.84), (86.0, 1.27, 161000.69), (65.0, 0.27, 17260.15),
(63.0, 0.92, 529.69), (57.0, 2.01, 83996.85), (56.0, 5.24, 71430.7),
(49.0, 3.25, 2544.31), (47.0, 2.58, 775.52), (45.0, 5.54, 9437.76),
(43.0, 6.01, 6275.96), (39.0, 5.36, 4694), (38.0, 2.39, 8827.39),
(37.0, 0.83, 19651.05), (37.0, 4.9, 12139.55), (36.0, 1.67, 12036.46),
(35.0, 1.84, 2942.46), (33.0, 0.24, 7084.9), (32.0, 0.18, 5088.63),
(32.0, 1.78, 398.15), (28.0, 1.21, 6286.6), (28.0, 1.9, 6279.55),
(26.0, 4.59, 10447.39)])
)
@register_jitable
def _heliocentric_radius(jme):
"""Compute the Earth Heliocentric Radius (R) in astronimical units given the Julian Ephemeris Millennium"""
# section 3.2.8
Ri = _cos_sum(jme, _EHR)
R = np.polyval(Ri, jme) / 1e8
return R
@register_jitable
def _heliocentric_position(jme):
"""Compute the Earth Heliocentric Longitude, Latitude, and Radius given the Julian Ephemeris Millennium
Returns (L, B, R) where L = longitude in degrees, B = latitude in degrees, and R = radius in astronimical units
"""
return _heliocentric_longitude(jme), _heliocentric_latitude(jme), _heliocentric_radius(jme)
## 3.3. Calculate the geocentric longitude and latitude (Θ [Theta] and β [beta])
@register_jitable
def _geocentric_position(heliocentric_position):
"""Compute the geocentric latitude (Θ [Theta]) and longitude (β [beta]) (in degrees) of the sun given the earth's heliocentric position (L, B, R)"""
L,B,R = heliocentric_position
# eq 13
Theta = L + 180
# eq 14
beta = -B
return (Theta, beta)
## 3.4. Calculate the nutation in longitude and obliquity (Δψ [Delta_psi] and Δε [Delta_epsilon])
#Table 2. Periodic terms for the nutation in longitude and obliquity, Coefficients for sin terms (Yi)
_NLO_Y = np.array([(0.0, 0.0, 0.0, 0.0, 1.0), (-2.0, 0.0, 0.0, 2.0, 2.0), (0.0, 0.0, 0.0, 2.0, 2.0),
(0.0, 0.0, 0.0, 0.0, 2.0), (0.0, 1.0, 0.0, 0.0, 0.0), (0.0, 0.0, 1.0, 0.0, 0.0),
(-2.0, 1.0, 0.0, 2.0, 2.0), (0.0, 0.0, 0.0, 2.0, 1.0), (0.0, 0.0, 1.0, 2.0, 2.0),
(-2.0, -1.0, 0.0, 2.0, 2.0), (-2.0, 0.0, 1.0, 0.0, 0.0), (-2.0, 0.0, 0.0, 2.0, 1.0),
(0.0, 0.0, -1.0, 2.0, 2.0), (2.0, 0.0, 0.0, 0.0, 0.0), (0.0, 0.0, 1.0, 0.0, 1.0),
(2.0, 0.0, -1.0, 2.0, 2.0), (0.0, 0.0, -1.0, 0.0, 1.0), (0.0, 0.0, 1.0, 2.0, 1.0),
(-2.0, 0.0, 2.0, 0.0, 0.0), (0.0, 0.0, -2.0, 2.0, 1.0), (2.0, 0.0, 0.0, 2.0, 2.0),
(0.0, 0.0, 2.0, 2.0, 2.0), (0.0, 0.0, 2.0, 0.0, 0.0), (-2.0, 0.0, 1.0, 2.0, 2.0),
(0.0, 0.0, 0.0, 2.0, 0.0), (-2.0, 0.0, 0.0, 2.0, 0.0), (0.0, 0.0, -1.0, 2.0, 1.0),
(0.0, 2.0, 0.0, 0.0, 0.0), (2.0, 0.0, -1.0, 0.0, 1.0), (-2.0, 2.0, 0.0, 2.0, 2.0),
(0.0, 1.0, 0.0, 0.0, 1.0), (-2.0, 0.0, 1.0, 0.0, 1.0), (0.0, -1.0, 0.0, 0.0, 1.0),
(0.0, 0.0, 2.0, -2.0, 0.0), (2.0, 0.0, -1.0, 2.0, 1.0), (2.0, 0.0, 1.0, 2.0, 2.0),
(0.0, 1.0, 0.0, 2.0, 2.0), (-2.0, 1.0, 1.0, 0.0, 0.0), (0.0, -1.0, 0.0, 2.0, 2.0),
(2.0, 0.0, 0.0, 2.0, 1.0), (2.0, 0.0, 1.0, 0.0, 0.0), (-2.0, 0.0, 2.0, 2.0, 2.0),
(-2.0, 0.0, 1.0, 2.0, 1.0), (2.0, 0.0, -2.0, 0.0, 1.0), (2.0, 0.0, 0.0, 0.0, 1.0),
(0.0, -1.0, 1.0, 0.0, 0.0), (-2.0, -1.0, 0.0, 2.0, 1.0), (-2.0, 0.0, 0.0, 0.0, 1.0),
(0.0, 0.0, 2.0, 2.0, 1.0), (-2.0, 0.0, 2.0, 0.0, 1.0), (-2.0, 1.0, 0.0, 2.0, 1.0),
(0.0, 0.0, 1.0, -2.0, 0.0), (-1.0, 0.0, 1.0, 0.0, 0.0), (-2.0, 1.0, 0.0, 0.0, 0.0),
(1.0, 0.0, 0.0, 0.0, 0.0), (0.0, 0.0, 1.0, 2.0, 0.0), (0.0, 0.0, -2.0, 2.0, 2.0),
(-1.0, -1.0, 1.0, 0.0, 0.0), (0.0, 1.0, 1.0, 0.0, 0.0), (0.0, -1.0, 1.0, 2.0, 2.0),
(2.0, -1.0, -1.0, 2.0, 2.0), (0.0, 0.0, 3.0, 2.0, 2.0), (2.0, -1.0, 0.0, 2.0, 2.0)])
#Table 2. Periodic terms for the nutation in longitude and obliquity, Coefficients for Δψ [Delta_psi] (a,b)
_NLO_AB = np.array([(-171996.0, -174.2), (-13187.0, -1.6), (-2274.0, -0.2), (2062.0, 0.2), (1426.0, -3.4), (712.0, 0.1),
(-517.0, 1.2), (-386.0, -0.4), (-301.0, 0.0), (217.0, -0.5), (-158.0, 0.0), (129.0, 0.1),
(123.0, 0.0), (63.0, 0.0), (63.0, 0.1), (-59.0, 0.0), (-58.0, -0.1), (-51.0, 0.0),
(48.0, 0.0), (46.0, 0.0), (-38.0, 0.0), (-31.0, 0.0), (29.0, 0.0), (29.0, 0.0),
(26.0, 0.0), (-22.0, 0.0), (21.0, 0.0), (17.0, -0.1), (16.0, 0.0), (-16.0, 0.1),
(-15.0, 0.0), (-13.0, 0.0), (-12.0, 0.0), (11.0, 0.0), (-10.0, 0.0), (-8.0, 0.0),
(7.0, 0.0), (-7.0, 0.0), (-7.0, 0.0), (-7.0, 0.0), (6.0, 0.0), (6.0, 0.0),
(6.0, 0.0), (-6.0, 0.0), (-6.0, 0.0), (5.0, 0.0), (-5.0, 0.0), (-5.0, 0.0),
(-5.0, 0.0), (4.0, 0.0), (4.0, 0.0), (4.0, 0.0), (-4.0, 0.0), (-4.0, 0.0),
(-4.0, 0.0), (3.0, 0.0), (-3.0, 0.0), (-3.0, 0.0), (-3.0, 0.0), (-3.0, 0.0),
(-3.0, 0.0), (-3.0, 0.0), (-3.0, 0.0)])
#Table 2. Periodic terms for the nutation in longitude and obliquity, Coefficients for Δε [Delta_epsilon] (c,d)
_NLO_CD = np.array([(92025.0, 8.9), (5736.0, -3.1), (977.0, -0.5), (-895.0, 0.5),
(54.0, -0.1), (-7.0, 0.0), (224.0, -0.6), (200.0, 0.0),
(129.0, -0.1), (-95.0, 0.3), (0.0, 0.0), (-70.0, 0.0),
(-53.0, 0.0), (0.0, 0.0), (-33.0, 0.0), (26.0, 0.0),
(32.0, 0.0), (27.0, 0.0), (0.0, 0.0), (-24.0, 0.0),
(16.0, 0.0), (13.0, 0.0), (0.0, 0.0), (-12.0, 0.0),
(0.0, 0.0), (0.0, 0.0), (-10.0, 0.0), (0.0, 0.0),
(-8.0, 0.0), (7.0, 0.0), (9.0, 0.0), (7.0, 0.0),
(6.0, 0.0), (0.0, 0.0), (5.0, 0.0), (3.0, 0.0),
(-3.0, 0.0), (0.0, 0.0), (3.0, 0.0), (3.0, 0.0),
(0.0, 0.0), (-3.0, 0.0), (-3.0, 0.0), (3.0, 0.0),
(3.0, 0.0), (0.0, 0.0), (3.0, 0.0), (3.0, 0.0),
(3.0, 0.0), (0.0, 0.0), (0.0, 0.0), (0.0, 0.0),
(0.0, 0.0), (0.0, 0.0), (0.0, 0.0), (0.0, 0.0),
(0.0, 0.0), (0.0, 0.0), (0.0, 0.0), (0.0, 0.0),
(0.0, 0.0), (0.0, 0.0), (0.0, 0.0)])
@register_jitable
def _eqs15_19(jce):
'''Compute eqs 15-18, in radians'''
#mean elongation of the moon from the sun, in radians:
# eq 15, mean elongation of the moon from the sun, in radians
eq15_coeffs = np.array([1./189474, -0.0019142, 445267.111480, 297.85036])
x0 = np.deg2rad(np.polyval(eq15_coeffs,jce))
# eq 16, mean anomaly of the sun (Earth), in radians:
eq16_coeffs = np.array([-1/3e5, -0.0001603, 35999.050340, 357.52772])
x1 = np.deg2rad(np.polyval(eq16_coeffs, jce))
# eq 17, mean anomaly of the moon, in radians:
eq17_coeffs = np.array([1./56250, 0.0086972, 477198.867398, 134.96298])
x2 = np.deg2rad(np.polyval(eq17_coeffs, jce))
# eq 18, moon's argument of latitude, in radians:
eq18_coeffs = np.array([1./327270, -0.0036825, 483202.017538, 93.27191])
x3 = np.deg2rad(np.polyval(eq18_coeffs, jce))
# eq 19, Longitude of the ascending node of the moon's mean orbit on the
# ecliptic measured from the mean equinox of the date, in radians:
eq19_coeffs = np.array([1./45e4, 0.0020708, -1934.136261, 125.04452])
x4 = np.deg2rad(np.polyval(eq19_coeffs, jce))
return np.array([x0, x1, x2, x3, x4])
@register_jitable
def _nutation_obliquity(jce):
"""Compute the nutation in longitude (Δψ, Delta_psi) and the true obliquity of the ecliptic (ε, epsilon) given the Julian Ephemeris Century"""
x = _eqs15_19(jce)
# eq 20
# eq 22
a,b = _NLO_AB.T
Delta_psi = np.sum((a + b*jce)*np.sin(np.dot(_NLO_Y, x)))/36e6
# eq 21
# eq 23
c,d = _NLO_CD.T
Delta_epsilon = np.sum((c + d*jce)*np.cos(np.dot(_NLO_Y, x)))/36e6
epsilon = _ecliptic_obliquity(_julian_millennium(jce), Delta_epsilon)
return Delta_psi, epsilon