@typescriptify/sweph 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +422 -0
- package/ephe/semo_18.se1 +0 -0
- package/ephe/sepl_18.se1 +0 -0
- package/originalCode/.eslintrc.json +124 -0
- package/originalCode/.gitattributes +2 -0
- package/originalCode/.github/FUNDING.yml +5 -0
- package/originalCode/.github/workflows/test.yml +35 -0
- package/originalCode/LICENSE +840 -0
- package/originalCode/README.md +91 -0
- package/originalCode/binding.gyp +41 -0
- package/originalCode/constants.js +366 -0
- package/originalCode/docs.gif +0 -0
- package/originalCode/index.d.ts +5115 -0
- package/originalCode/index.js +7 -0
- package/originalCode/index.mjs +109 -0
- package/originalCode/package.json +55 -0
- package/originalCode/src/functions/azalt.cpp +39 -0
- package/originalCode/src/functions/azalt_rev.cpp +35 -0
- package/originalCode/src/functions/calc.cpp +29 -0
- package/originalCode/src/functions/calc_pctr.cpp +31 -0
- package/originalCode/src/functions/calc_ut.cpp +29 -0
- package/originalCode/src/functions/close.cpp +6 -0
- package/originalCode/src/functions/cotrans.cpp +26 -0
- package/originalCode/src/functions/cotrans_sp.cpp +26 -0
- package/originalCode/src/functions/cs2degstr.cpp +19 -0
- package/originalCode/src/functions/cs2lonlatstr.cpp +23 -0
- package/originalCode/src/functions/cs2timestr.cpp +23 -0
- package/originalCode/src/functions/csnorm.cpp +15 -0
- package/originalCode/src/functions/csroundsec.cpp +15 -0
- package/originalCode/src/functions/d2l.cpp +15 -0
- package/originalCode/src/functions/date_conversion.cpp +30 -0
- package/originalCode/src/functions/day_of_week.cpp +15 -0
- package/originalCode/src/functions/degnorm.cpp +15 -0
- package/originalCode/src/functions/deltat.cpp +15 -0
- package/originalCode/src/functions/deltat_ex.cpp +24 -0
- package/originalCode/src/functions/difcs2n.cpp +19 -0
- package/originalCode/src/functions/difcsn.cpp +19 -0
- package/originalCode/src/functions/difdeg2n.cpp +19 -0
- package/originalCode/src/functions/difdegn.cpp +19 -0
- package/originalCode/src/functions/fixstar.cpp +32 -0
- package/originalCode/src/functions/fixstar2.cpp +32 -0
- package/originalCode/src/functions/fixstar2_mag.cpp +28 -0
- package/originalCode/src/functions/fixstar2_ut.cpp +32 -0
- package/originalCode/src/functions/fixstar_mag.cpp +28 -0
- package/originalCode/src/functions/fixstar_ut.cpp +32 -0
- package/originalCode/src/functions/gauquelin_sector.cpp +44 -0
- package/originalCode/src/functions/get_ayanamsa.cpp +15 -0
- package/originalCode/src/functions/get_ayanamsa_ex.cpp +27 -0
- package/originalCode/src/functions/get_ayanamsa_ex_ut.cpp +27 -0
- package/originalCode/src/functions/get_ayanamsa_name.cpp +19 -0
- package/originalCode/src/functions/get_ayanamsa_ut.cpp +15 -0
- package/originalCode/src/functions/get_current_file_data.cpp +28 -0
- package/originalCode/src/functions/get_library_path.cpp +8 -0
- package/originalCode/src/functions/get_orbital_elements.cpp +29 -0
- package/originalCode/src/functions/get_planet_name.cpp +19 -0
- package/originalCode/src/functions/get_tid_acc.cpp +7 -0
- package/originalCode/src/functions/heliacal_pheno_ut.cpp +52 -0
- package/originalCode/src/functions/heliacal_ut.cpp +52 -0
- package/originalCode/src/functions/helio_cross.cpp +33 -0
- package/originalCode/src/functions/helio_cross_ut.cpp +33 -0
- package/originalCode/src/functions/house_name.cpp +20 -0
- package/originalCode/src/functions/house_pos.cpp +36 -0
- package/originalCode/src/functions/houses.cpp +35 -0
- package/originalCode/src/functions/houses_armc.cpp +38 -0
- package/originalCode/src/functions/houses_armc_ex2.cpp +47 -0
- package/originalCode/src/functions/houses_ex.cpp +37 -0
- package/originalCode/src/functions/houses_ex2.cpp +46 -0
- package/originalCode/src/functions/jdet_to_utc.cpp +38 -0
- package/originalCode/src/functions/jdut1_to_utc.cpp +38 -0
- package/originalCode/src/functions/julday.cpp +25 -0
- package/originalCode/src/functions/lat_to_lmt.cpp +27 -0
- package/originalCode/src/functions/lmt_to_lat.cpp +27 -0
- package/originalCode/src/functions/lun_eclipse_how.cpp +34 -0
- package/originalCode/src/functions/lun_eclipse_when.cpp +31 -0
- package/originalCode/src/functions/lun_eclipse_when_loc.cpp +39 -0
- package/originalCode/src/functions/lun_occult_when_glob.cpp +35 -0
- package/originalCode/src/functions/lun_occult_when_loc.cpp +43 -0
- package/originalCode/src/functions/lun_occult_where.cpp +34 -0
- package/originalCode/src/functions/mooncross.cpp +26 -0
- package/originalCode/src/functions/mooncross_node.cpp +30 -0
- package/originalCode/src/functions/mooncross_node_ut.cpp +30 -0
- package/originalCode/src/functions/mooncross_ut.cpp +26 -0
- package/originalCode/src/functions/nod_aps.cpp +42 -0
- package/originalCode/src/functions/nod_aps_ut.cpp +42 -0
- package/originalCode/src/functions/orbit_max_min_true_distance.cpp +37 -0
- package/originalCode/src/functions/pheno.cpp +29 -0
- package/originalCode/src/functions/pheno_ut.cpp +29 -0
- package/originalCode/src/functions/radnorm.cpp +15 -0
- package/originalCode/src/functions/refrac.cpp +23 -0
- package/originalCode/src/functions/refrac_extended.cpp +32 -0
- package/originalCode/src/functions/revjul.cpp +33 -0
- package/originalCode/src/functions/rise_trans.cpp +44 -0
- package/originalCode/src/functions/rise_trans_true_hor.cpp +46 -0
- package/originalCode/src/functions/set_delta_t_userdef.cpp +14 -0
- package/originalCode/src/functions/set_ephe_path.cpp +14 -0
- package/originalCode/src/functions/set_jpl_file.cpp +14 -0
- package/originalCode/src/functions/set_sid_mode.cpp +20 -0
- package/originalCode/src/functions/set_tid_acc.cpp +14 -0
- package/originalCode/src/functions/set_topo.cpp +20 -0
- package/originalCode/src/functions/sidtime.cpp +15 -0
- package/originalCode/src/functions/sidtime0.cpp +21 -0
- package/originalCode/src/functions/sol_eclipse_how.cpp +34 -0
- package/originalCode/src/functions/sol_eclipse_when_glob.cpp +31 -0
- package/originalCode/src/functions/sol_eclipse_when_loc.cpp +39 -0
- package/originalCode/src/functions/sol_eclipse_where.cpp +30 -0
- package/originalCode/src/functions/solcross.cpp +26 -0
- package/originalCode/src/functions/solcross_ut.cpp +26 -0
- package/originalCode/src/functions/split_deg.cpp +35 -0
- package/originalCode/src/functions/time_equ.cpp +25 -0
- package/originalCode/src/functions/utc_time_zone.cpp +48 -0
- package/originalCode/src/functions/utc_to_jd.cpp +37 -0
- package/originalCode/src/functions/version.cpp +8 -0
- package/originalCode/src/functions/vis_limit_mag.cpp +50 -0
- package/originalCode/src/sweph.cpp +150 -0
- package/originalCode/src/sweph.h +119 -0
- package/originalCode/swisseph/swecl.c +6428 -0
- package/originalCode/swisseph/swedate.c +588 -0
- package/originalCode/swisseph/swedate.h +81 -0
- package/originalCode/swisseph/swehel.c +3511 -0
- package/originalCode/swisseph/swehouse.c +3143 -0
- package/originalCode/swisseph/swehouse.h +98 -0
- package/originalCode/swisseph/swejpl.c +958 -0
- package/originalCode/swisseph/swejpl.h +103 -0
- package/originalCode/swisseph/swemmoon.c +1930 -0
- package/originalCode/swisseph/swemplan.c +967 -0
- package/originalCode/swisseph/swemptab.h +10640 -0
- package/originalCode/swisseph/swenut2000a.h +2819 -0
- package/originalCode/swisseph/sweodef.h +326 -0
- package/originalCode/swisseph/sweph.c +8614 -0
- package/originalCode/swisseph/sweph.h +849 -0
- package/originalCode/swisseph/swephexp.h +1020 -0
- package/originalCode/swisseph/swephlib.c +4634 -0
- package/originalCode/swisseph/swephlib.h +189 -0
- package/package.json +28 -0
- package/scripts/gen-swemptab.js +177 -0
- package/scripts/gen-swenut2000a.js +106 -0
- package/src/SwissEph/README.md +268 -0
- package/src/SwissEph/UseCases/Ayanamsa.md +363 -0
- package/src/SwissEph/UseCases/AzimuthAltitude.md +408 -0
- package/src/SwissEph/UseCases/CoordinateSystems.md +337 -0
- package/src/SwissEph/UseCases/DateAndTime.md +368 -0
- package/src/SwissEph/UseCases/DeltaT.md +258 -0
- package/src/SwissEph/UseCases/EphemerisFiles.md +338 -0
- package/src/SwissEph/UseCases/FixedStars.md +300 -0
- package/src/SwissEph/UseCases/GauquelinSectors.md +304 -0
- package/src/SwissEph/UseCases/HeliacalEvents.md +396 -0
- package/src/SwissEph/UseCases/HelioCrossings.md +325 -0
- package/src/SwissEph/UseCases/HousePosition.md +254 -0
- package/src/SwissEph/UseCases/HouseSystems.md +279 -0
- package/src/SwissEph/UseCases/LunarEclipse.md +326 -0
- package/src/SwissEph/UseCases/MeridianTransit.md +279 -0
- package/src/SwissEph/UseCases/MoonCrossings.md +373 -0
- package/src/SwissEph/UseCases/NodesAndApsides.md +307 -0
- package/src/SwissEph/UseCases/Occultation.md +352 -0
- package/src/SwissEph/UseCases/OrbitalElements.md +469 -0
- package/src/SwissEph/UseCases/Phenomena.md +328 -0
- package/src/SwissEph/UseCases/PlanetPositions.md +366 -0
- package/src/SwissEph/UseCases/Planetocentric.md +278 -0
- package/src/SwissEph/UseCases/Refraction.md +314 -0
- package/src/SwissEph/UseCases/RiseAndSet.md +433 -0
- package/src/SwissEph/UseCases/SiderealTime.md +302 -0
- package/src/SwissEph/UseCases/SolarEclipse.md +379 -0
- package/src/SwissEph/UseCases/SunCrossings.md +275 -0
- package/src/SwissEph/UseCases/TopocentricCorrection.md +335 -0
- package/src/SwissEph/errors.ts +10 -0
- package/src/SwissEph/index.ts +823 -0
- package/src/SwissEph/types.ts +291 -0
- package/src/constants.ts +762 -0
- package/src/file-reader.ts +147 -0
- package/src/index.ts +10 -0
- package/src/swecl.ts +4526 -0
- package/src/swedate.ts +376 -0
- package/src/swehel.ts +1939 -0
- package/src/swehouse.ts +2167 -0
- package/src/swejpl.ts +470 -0
- package/src/swemmoon.ts +1318 -0
- package/src/swemplan.ts +585 -0
- package/src/swemptab.ts +4448 -0
- package/src/swenut2000a.ts +2763 -0
- package/src/sweph.ts +3993 -0
- package/src/swephlib.ts +2720 -0
- package/src/types.ts +490 -0
- package/tests/c-style/ayanamsa.test.ts +63 -0
- package/tests/c-style/config.test.ts +96 -0
- package/tests/c-style/crossings.test.ts +81 -0
- package/tests/c-style/date-time.test.ts +114 -0
- package/tests/c-style/eclipses.test.ts +84 -0
- package/tests/c-style/fixed-stars.test.ts +66 -0
- package/tests/c-style/heliacal.test.ts +34 -0
- package/tests/c-style/houses.test.ts +135 -0
- package/tests/c-style/math-utils.test.ts +160 -0
- package/tests/c-style/orbital.test.ts +78 -0
- package/tests/c-style/phenomena.test.ts +42 -0
- package/tests/c-style/planetocentric.test.ts +26 -0
- package/tests/c-style/planets.test.ts +117 -0
- package/tests/c-style/rise-set.test.ts +71 -0
- package/tests/helpers.ts +21 -0
- package/tests/modern/ayanamsa.test.ts +47 -0
- package/tests/modern/calc.test.ts +113 -0
- package/tests/modern/config.test.ts +46 -0
- package/tests/modern/crossings.test.ts +45 -0
- package/tests/modern/eclipses.test.ts +81 -0
- package/tests/modern/errors.test.ts +71 -0
- package/tests/modern/heliacal.test.ts +30 -0
- package/tests/modern/houses.test.ts +87 -0
- package/tests/modern/orbital.test.ts +79 -0
- package/tests/modern/phenomena.test.ts +41 -0
- package/tests/modern/rise-set.test.ts +60 -0
- package/tests/modern/statics.test.ts +99 -0
- package/tests/modern/utilities.test.ts +70 -0
- package/tsconfig.json +20 -0
package/src/swedate.ts
ADDED
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
/*************************************************************
|
|
2
|
+
* swedate.ts — Julian day / calendar date conversions
|
|
3
|
+
* Translated from swedate.c
|
|
4
|
+
*
|
|
5
|
+
* swe_julday() -> julDay()
|
|
6
|
+
* swe_revjul() -> revJul()
|
|
7
|
+
* swe_date_conversion() -> dateConversion()
|
|
8
|
+
* swe_utc_time_zone() -> utcTimeZone()
|
|
9
|
+
* swe_utc_to_jd() -> utcToJd()
|
|
10
|
+
* swe_jdet_to_utc() -> jdetToUtc()
|
|
11
|
+
* swe_jdut1_to_utc() -> jdut1ToUtc()
|
|
12
|
+
* swe_day_of_week() -> dayOfWeek()
|
|
13
|
+
*
|
|
14
|
+
* Copyright (C) 1997 - 2021 Astrodienst AG, Switzerland. (AGPL)
|
|
15
|
+
*
|
|
16
|
+
* Authors: Dieter Koch and Alois Treindl, Astrodienst Zurich
|
|
17
|
+
*************************************************************/
|
|
18
|
+
|
|
19
|
+
import { SE_GREG_CAL, SE_JUL_CAL, OK, ERR } from './constants';
|
|
20
|
+
import type { DateResult, JdToUtcResult, UtcToJdResult } from './types';
|
|
21
|
+
|
|
22
|
+
/* Leap seconds were inserted at the end of the following days */
|
|
23
|
+
const NLEAP_SECONDS = 27;
|
|
24
|
+
const NLEAP_INIT = 10; // initial difference between UTC and TAI in 1972
|
|
25
|
+
const J1972 = 2441317.5;
|
|
26
|
+
|
|
27
|
+
const LEAP_SECONDS: number[] = [
|
|
28
|
+
19720630,
|
|
29
|
+
19721231,
|
|
30
|
+
19731231,
|
|
31
|
+
19741231,
|
|
32
|
+
19751231,
|
|
33
|
+
19761231,
|
|
34
|
+
19771231,
|
|
35
|
+
19781231,
|
|
36
|
+
19791231,
|
|
37
|
+
19810630,
|
|
38
|
+
19820630,
|
|
39
|
+
19830630,
|
|
40
|
+
19850630,
|
|
41
|
+
19871231,
|
|
42
|
+
19891231,
|
|
43
|
+
19901231,
|
|
44
|
+
19920630,
|
|
45
|
+
19930630,
|
|
46
|
+
19940630,
|
|
47
|
+
19951231,
|
|
48
|
+
19970630,
|
|
49
|
+
19981231,
|
|
50
|
+
20051231,
|
|
51
|
+
20081231,
|
|
52
|
+
20120630,
|
|
53
|
+
20150630,
|
|
54
|
+
20161231,
|
|
55
|
+
];
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Compute the Julian day number for a given calendar date.
|
|
59
|
+
*
|
|
60
|
+
* If gregflag = SE_GREG_CAL (1), Gregorian calendar is assumed.
|
|
61
|
+
* If gregflag = SE_JUL_CAL (0), Julian calendar is assumed.
|
|
62
|
+
*
|
|
63
|
+
* The Julian day number is a double representing the number of days since
|
|
64
|
+
* JD = 0.0 on 1 Jan -4712, 12:00 noon (Julian calendar).
|
|
65
|
+
*
|
|
66
|
+
* Original author: Marc Pottenger, Los Angeles.
|
|
67
|
+
* Bug fix for year < -4711: Alois Treindl, 15-aug-88.
|
|
68
|
+
*/
|
|
69
|
+
export function julDay(year: number, month: number, day: number, hour: number, gregflag: number = SE_GREG_CAL): number {
|
|
70
|
+
let u = year;
|
|
71
|
+
if (month < 3) u -= 1;
|
|
72
|
+
const u0 = u + 4712.0;
|
|
73
|
+
let u1 = month + 1.0;
|
|
74
|
+
if (u1 < 4) u1 += 12.0;
|
|
75
|
+
let jd = Math.floor(u0 * 365.25)
|
|
76
|
+
+ Math.floor(30.6 * u1 + 0.000001)
|
|
77
|
+
+ day + hour / 24.0 - 63.5;
|
|
78
|
+
if (gregflag === SE_GREG_CAL) {
|
|
79
|
+
let u2 = Math.floor(Math.abs(u) / 100) - Math.floor(Math.abs(u) / 400);
|
|
80
|
+
if (u < 0.0) u2 = -u2;
|
|
81
|
+
jd = jd - u2 + 2;
|
|
82
|
+
if ((u < 0.0) && (u / 100 === Math.floor(u / 100)) && (u / 400 !== Math.floor(u / 400))) {
|
|
83
|
+
jd -= 1;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return jd;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Reverse Julian day: compute calendar date from a Julian day number.
|
|
91
|
+
*
|
|
92
|
+
* Returns { year, month, day, hour } where hour is a decimal fraction (0..23.999).
|
|
93
|
+
*
|
|
94
|
+
* Original author: Mark Pottenger, Los Angeles.
|
|
95
|
+
* Bug fix for year < -4711: Alois Treindl, 16-aug-88.
|
|
96
|
+
*/
|
|
97
|
+
export function revJul(jd: number, gregflag: number = SE_GREG_CAL): DateResult {
|
|
98
|
+
let u0 = jd + 32082.5;
|
|
99
|
+
if (gregflag === SE_GREG_CAL) {
|
|
100
|
+
let u1 = u0 + Math.floor(u0 / 36525.0) - Math.floor(u0 / 146100.0) - 38.0;
|
|
101
|
+
if (jd >= 1830691.5) u1 += 1;
|
|
102
|
+
u0 = u0 + Math.floor(u1 / 36525.0) - Math.floor(u1 / 146100.0) - 38.0;
|
|
103
|
+
}
|
|
104
|
+
const u2 = Math.floor(u0 + 123.0);
|
|
105
|
+
const u3 = Math.floor((u2 - 122.2) / 365.25);
|
|
106
|
+
const u4 = Math.floor((u2 - Math.floor(365.25 * u3)) / 30.6001);
|
|
107
|
+
let month = Math.trunc(u4 - 1.0);
|
|
108
|
+
if (month > 12) month -= 12;
|
|
109
|
+
const day = Math.trunc(u2 - Math.floor(365.25 * u3) - Math.floor(30.6001 * u4));
|
|
110
|
+
const year = Math.trunc(u3 + Math.floor((u4 - 2.0) / 12.0) - 4800);
|
|
111
|
+
const hour = (jd - Math.floor(jd + 0.5) + 0.5) * 24.0;
|
|
112
|
+
return { year, month, day, hour };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Validate and convert a date+time to Julian day number.
|
|
117
|
+
*
|
|
118
|
+
* Returns OK (0) for valid dates, ERR (-1) for illegal dates.
|
|
119
|
+
* The Julian day is always computed, even for illegal dates (e.g. 32 Jan → 1 Feb).
|
|
120
|
+
*
|
|
121
|
+
* @param c Calendar: 'g' for Gregorian, 'j' for Julian
|
|
122
|
+
*/
|
|
123
|
+
export function dateConversion(
|
|
124
|
+
y: number, m: number, d: number,
|
|
125
|
+
uttime: number, c: string
|
|
126
|
+
): { ok: boolean; jd: number } {
|
|
127
|
+
const gregflag = c === 'g' ? SE_GREG_CAL : SE_JUL_CAL;
|
|
128
|
+
const jd = julDay(y, m, d, uttime, gregflag);
|
|
129
|
+
const rev = revJul(jd, gregflag);
|
|
130
|
+
const ok = (rev.month === m && rev.day === d && rev.year === y);
|
|
131
|
+
return { ok, jd };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Convert local time to UTC or UTC to local time.
|
|
136
|
+
*
|
|
137
|
+
* For time zones east of Greenwich, d_timezone is positive.
|
|
138
|
+
* For time zones west of Greenwich, d_timezone is negative.
|
|
139
|
+
*
|
|
140
|
+
* For conversion from local time to UTC, use +d_timezone.
|
|
141
|
+
* For conversion from UTC to local time, use -d_timezone.
|
|
142
|
+
*/
|
|
143
|
+
export function utcTimeZone(
|
|
144
|
+
iyear: number, imonth: number, iday: number,
|
|
145
|
+
ihour: number, imin: number, dsec: number,
|
|
146
|
+
dTimezone: number
|
|
147
|
+
): JdToUtcResult {
|
|
148
|
+
let haveLeapsec = false;
|
|
149
|
+
if (dsec >= 60.0) {
|
|
150
|
+
haveLeapsec = true;
|
|
151
|
+
dsec -= 1.0;
|
|
152
|
+
}
|
|
153
|
+
let dhour = ihour + imin / 60.0 + dsec / 3600.0;
|
|
154
|
+
let tjd = julDay(iyear, imonth, iday, 0, SE_GREG_CAL);
|
|
155
|
+
dhour -= dTimezone;
|
|
156
|
+
if (dhour < 0.0) {
|
|
157
|
+
tjd -= 1.0;
|
|
158
|
+
dhour += 24.0;
|
|
159
|
+
}
|
|
160
|
+
if (dhour >= 24.0) {
|
|
161
|
+
tjd += 1.0;
|
|
162
|
+
dhour -= 24.0;
|
|
163
|
+
}
|
|
164
|
+
const rev = revJul(tjd + 0.001, SE_GREG_CAL);
|
|
165
|
+
const hourOut = Math.trunc(dhour);
|
|
166
|
+
let d = (dhour - hourOut) * 60;
|
|
167
|
+
const minOut = Math.trunc(d);
|
|
168
|
+
let secOut = (d - minOut) * 60;
|
|
169
|
+
if (haveLeapsec) {
|
|
170
|
+
secOut += 1.0;
|
|
171
|
+
}
|
|
172
|
+
return {
|
|
173
|
+
year: rev.year,
|
|
174
|
+
month: rev.month,
|
|
175
|
+
day: rev.day,
|
|
176
|
+
hour: hourOut,
|
|
177
|
+
minute: minOut,
|
|
178
|
+
second: secOut,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* monday = 0, ... sunday = 6
|
|
184
|
+
*/
|
|
185
|
+
export function dayOfWeek(jd: number): number {
|
|
186
|
+
return (Math.trunc(jd - 2433282 - 1.5) % 7 + 7) % 7;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Type for a delta-T function that will be provided by swephlib.
|
|
191
|
+
* Signature matches swe_deltat_ex(tjd, iflag, serr).
|
|
192
|
+
*/
|
|
193
|
+
export type DeltaTFn = (tjd: number, iflag: number) => number;
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Convert UTC to Julian day numbers ET and UT1.
|
|
197
|
+
*
|
|
198
|
+
* - Before 1972, input time is treated as UT1.
|
|
199
|
+
* - From 1972 on, input time is treated as UTC.
|
|
200
|
+
* - If delta_t - nleap - 32.184 > 1, input is treated as UT1
|
|
201
|
+
* (to avoid errors when the leap seconds table is outdated).
|
|
202
|
+
*
|
|
203
|
+
* @param deltatEx delta T function (injected from swephlib)
|
|
204
|
+
*/
|
|
205
|
+
export function utcToJd(
|
|
206
|
+
iyear: number, imonth: number, iday: number,
|
|
207
|
+
ihour: number, imin: number, dsec: number,
|
|
208
|
+
gregflag: number,
|
|
209
|
+
deltatEx: DeltaTFn
|
|
210
|
+
): UtcToJdResult {
|
|
211
|
+
let tjdUt1 = julDay(iyear, imonth, iday, 0, gregflag);
|
|
212
|
+
const rev = revJul(tjdUt1, gregflag);
|
|
213
|
+
if (iyear !== rev.year || imonth !== rev.month || iday !== rev.day) {
|
|
214
|
+
return {
|
|
215
|
+
tjdEt: 0, tjdUt: 0,
|
|
216
|
+
error: `invalid date: year = ${iyear}, month = ${imonth}, day = ${iday}`,
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
if (ihour < 0 || ihour > 23
|
|
220
|
+
|| imin < 0 || imin > 59
|
|
221
|
+
|| dsec < 0 || dsec >= 61
|
|
222
|
+
|| (dsec >= 60 && (imin < 59 || ihour < 23 || tjdUt1 < J1972))) {
|
|
223
|
+
return {
|
|
224
|
+
tjdEt: 0, tjdUt: 0,
|
|
225
|
+
error: `invalid time: ${ihour}:${imin}:${dsec.toFixed(2)}`,
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
const dhour = ihour + imin / 60.0 + dsec / 3600.0;
|
|
229
|
+
/* before 1972, treat input as UT1 */
|
|
230
|
+
if (tjdUt1 < J1972) {
|
|
231
|
+
const ut = julDay(iyear, imonth, iday, dhour, gregflag);
|
|
232
|
+
return { tjdEt: ut + deltatEx(ut, -1), tjdUt: ut };
|
|
233
|
+
}
|
|
234
|
+
/* if Julian calendar, convert to Gregorian for leap second lookup */
|
|
235
|
+
let iy = iyear, im = imonth, id = iday;
|
|
236
|
+
let gflag = gregflag;
|
|
237
|
+
if (gflag === SE_JUL_CAL) {
|
|
238
|
+
gflag = SE_GREG_CAL;
|
|
239
|
+
const r = revJul(tjdUt1, gflag);
|
|
240
|
+
iy = r.year; im = r.month; id = r.day;
|
|
241
|
+
}
|
|
242
|
+
/* number of leap seconds since 1972 */
|
|
243
|
+
const tabsiz = LEAP_SECONDS.length;
|
|
244
|
+
let nleap = NLEAP_INIT;
|
|
245
|
+
const ndat = iy * 10000 + im * 100 + id;
|
|
246
|
+
let i: number;
|
|
247
|
+
for (i = 0; i < tabsiz; i++) {
|
|
248
|
+
if (ndat <= LEAP_SECONDS[i]) break;
|
|
249
|
+
nleap++;
|
|
250
|
+
}
|
|
251
|
+
/* If delta_t - nleap - 32.184 >= 1.0, treat as UT1 (table outdated) */
|
|
252
|
+
let d = deltatEx(tjdUt1, -1) * 86400.0;
|
|
253
|
+
if (d - nleap - 32.184 >= 1.0) {
|
|
254
|
+
const ut = tjdUt1 + dhour / 24.0;
|
|
255
|
+
return { tjdEt: ut + deltatEx(ut, -1), tjdUt: ut };
|
|
256
|
+
}
|
|
257
|
+
/* validate leap second input */
|
|
258
|
+
if (dsec >= 60) {
|
|
259
|
+
let found = false;
|
|
260
|
+
for (i = 0; i < tabsiz; i++) {
|
|
261
|
+
if (ndat === LEAP_SECONDS[i]) { found = true; break; }
|
|
262
|
+
}
|
|
263
|
+
if (!found) {
|
|
264
|
+
return {
|
|
265
|
+
tjdEt: 0, tjdUt: 0,
|
|
266
|
+
error: `invalid time (no leap second!): ${ihour}:${imin}:${dsec.toFixed(2)}`,
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
/* convert UTC to ET and UT1 */
|
|
271
|
+
d = tjdUt1 - J1972;
|
|
272
|
+
d += ihour / 24.0 + imin / 1440.0 + dsec / 86400.0;
|
|
273
|
+
const tjdEt1972 = J1972 + (32.184 + NLEAP_INIT) / 86400.0;
|
|
274
|
+
const tjdEt = tjdEt1972 + d + (nleap - NLEAP_INIT) / 86400.0;
|
|
275
|
+
d = deltatEx(tjdEt, -1);
|
|
276
|
+
let tjdUt = tjdEt - deltatEx(tjdEt - d, -1);
|
|
277
|
+
tjdUt = tjdEt - deltatEx(tjdUt, -1);
|
|
278
|
+
return { tjdEt, tjdUt };
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Convert Julian day (TT/ET) to UTC date components.
|
|
283
|
+
*
|
|
284
|
+
* - Before 1 Jan 1972 UTC → output is UT1
|
|
285
|
+
* - From 1972 on → output is UTC
|
|
286
|
+
*
|
|
287
|
+
* @param deltatEx delta T function (injected from swephlib)
|
|
288
|
+
*/
|
|
289
|
+
export function jdetToUtc(
|
|
290
|
+
tjdEt: number, gregflag: number,
|
|
291
|
+
deltatEx: DeltaTFn
|
|
292
|
+
): JdToUtcResult {
|
|
293
|
+
const tjdEt1972 = J1972 + (32.184 + NLEAP_INIT) / 86400.0;
|
|
294
|
+
let d = deltatEx(tjdEt, -1);
|
|
295
|
+
let tjdUt = tjdEt - deltatEx(tjdEt - d, -1);
|
|
296
|
+
tjdUt = tjdEt - deltatEx(tjdUt, -1);
|
|
297
|
+
|
|
298
|
+
/* before 1972, return UT1 */
|
|
299
|
+
if (tjdEt < tjdEt1972) {
|
|
300
|
+
const r = revJul(tjdUt, gregflag);
|
|
301
|
+
const hourInt = Math.trunc(r.hour);
|
|
302
|
+
d = (r.hour - hourInt) * 60;
|
|
303
|
+
const minInt = Math.trunc(d);
|
|
304
|
+
const sec = (d - minInt) * 60.0;
|
|
305
|
+
return { year: r.year, month: r.month, day: r.day, hour: hourInt, minute: minInt, second: sec };
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/* count leap seconds */
|
|
309
|
+
const tabsiz = LEAP_SECONDS.length;
|
|
310
|
+
const rPrev = revJul(tjdUt - 1, SE_GREG_CAL);
|
|
311
|
+
const ndat = rPrev.year * 10000 + rPrev.month * 100 + rPrev.day;
|
|
312
|
+
let nleap = 0;
|
|
313
|
+
let i: number;
|
|
314
|
+
for (i = 0; i < tabsiz; i++) {
|
|
315
|
+
if (ndat <= LEAP_SECONDS[i]) break;
|
|
316
|
+
nleap++;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/* date of potentially missing leap second */
|
|
320
|
+
let second60 = 0;
|
|
321
|
+
if (nleap < tabsiz) {
|
|
322
|
+
const ls = LEAP_SECONDS[nleap];
|
|
323
|
+
const iyear2 = Math.trunc(ls / 10000);
|
|
324
|
+
const imonth2 = Math.trunc((ls % 10000) / 100);
|
|
325
|
+
const iday2 = ls % 100;
|
|
326
|
+
const tjd = julDay(iyear2, imonth2, iday2, 0, SE_GREG_CAL);
|
|
327
|
+
const rNext = revJul(tjd + 1, SE_GREG_CAL);
|
|
328
|
+
const utcRes = utcToJd(rNext.year, rNext.month, rNext.day, 0, 0, 0, SE_GREG_CAL, deltatEx);
|
|
329
|
+
d = tjdEt - utcRes.tjdEt;
|
|
330
|
+
if (d >= 0) {
|
|
331
|
+
nleap++;
|
|
332
|
+
} else if (d < 0 && d > -1.0 / 86400.0) {
|
|
333
|
+
second60 = 1;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/* UTC, still unsure about one leap second */
|
|
338
|
+
let tjd2 = J1972 + (tjdEt - tjdEt1972) - (nleap + second60) / 86400.0;
|
|
339
|
+
let r = revJul(tjd2, SE_GREG_CAL);
|
|
340
|
+
let yearOut = r.year, monthOut = r.month, dayOut = r.day;
|
|
341
|
+
let hourOut = Math.trunc(r.hour);
|
|
342
|
+
d = (r.hour - hourOut) * 60;
|
|
343
|
+
let minOut = Math.trunc(d);
|
|
344
|
+
let secOut = (d - minOut) * 60.0 + second60;
|
|
345
|
+
|
|
346
|
+
/* If leap seconds table is outdated, treat as UT1 */
|
|
347
|
+
d = deltatEx(tjdEt, -1);
|
|
348
|
+
d = deltatEx(tjdEt - d, -1);
|
|
349
|
+
if (d * 86400.0 - (nleap + NLEAP_INIT) - 32.184 >= 1.0) {
|
|
350
|
+
r = revJul(tjdEt - d, SE_GREG_CAL);
|
|
351
|
+
yearOut = r.year; monthOut = r.month; dayOut = r.day;
|
|
352
|
+
hourOut = Math.trunc(r.hour);
|
|
353
|
+
d = (r.hour - hourOut) * 60;
|
|
354
|
+
minOut = Math.trunc(d);
|
|
355
|
+
secOut = (d - minOut) * 60.0;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
if (gregflag === SE_JUL_CAL) {
|
|
359
|
+
const tjdG = julDay(yearOut, monthOut, dayOut, 0, SE_GREG_CAL);
|
|
360
|
+
r = revJul(tjdG, gregflag);
|
|
361
|
+
yearOut = r.year; monthOut = r.month; dayOut = r.day;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
return { year: yearOut, month: monthOut, day: dayOut, hour: hourOut, minute: minOut, second: secOut };
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Convert Julian day (UT1) to UTC date components.
|
|
369
|
+
*/
|
|
370
|
+
export function jdut1ToUtc(
|
|
371
|
+
tjdUt: number, gregflag: number,
|
|
372
|
+
deltatEx: DeltaTFn
|
|
373
|
+
): JdToUtcResult {
|
|
374
|
+
const tjdEt = tjdUt + deltatEx(tjdUt, -1);
|
|
375
|
+
return jdetToUtc(tjdEt, gregflag, deltatEx);
|
|
376
|
+
}
|