chore(deps): update dependency suncalc to v2.1.3
This commit was merged in pull request #16.
This commit is contained in:
1 parent
5bd86a5325
commit
a1691f685b
3 files changed
+60
-25
No files matched your search
Vendored
+55
-20
@@ -139,22 +139,36 @@ function solarTransit(dt, lw) {
|
||||
return dt;
|
||||
}
|
||||
|
||||
// time the Sun reaches altitude h0 on the given side of transit (sign -1 = rise, +1 = set);
|
||||
// starts from the hour angle at transit and converges with Meeus' altitude correction (15.2).
|
||||
function getSetJ(h0, dt, sign, lw, phi, decT) {
|
||||
const cosH0 = (sin(h0) - sin(phi) * sin(decT)) / (cos(phi) * cos(decT));
|
||||
if (cosH0 < -1 || cosH0 > 1) return NaN; // sun stays above / below this altitude all day
|
||||
// time the Sun reaches altitude h0 within the half day [a, b] between transit and nadir, given the
|
||||
// sine of its altitude at both ends; NaN if it doesn't cross h0 there (altitude is monotonic over the
|
||||
// half day, so a sign change means exactly one crossing). Seeds from the hour angle at transit
|
||||
// declination, then Newton on sin(h) kept inside a shrinking bracket, bisecting whenever a step
|
||||
// leaves it — on near-grazing days the slope is tiny and plain Newton steps land days away.
|
||||
function getSetJ(h0, a, b, sa, sb, dt, lw, phi, decT) {
|
||||
const s0 = sin(h0);
|
||||
const rising = sa < sb;
|
||||
if ((sa - s0) * (sb - s0) > 0) return NaN; // sun stays above / below this altitude all half day
|
||||
|
||||
let d = dt + sign * acos(cosH0) / (2 * PI);
|
||||
for (let i = 0; i < 2; i++) {
|
||||
const cosH0 = (s0 - sin(phi) * sin(decT)) / (cos(phi) * cos(decT));
|
||||
const H0 = cosH0 <= -1 ? PI : cosH0 >= 1 ? 0 : acos(cosH0);
|
||||
let d = dt + (rising ? -H0 : H0) / (2 * PI);
|
||||
|
||||
for (;;) {
|
||||
const c = sunCoords(toDaysTT(d));
|
||||
const H = wrapPi(siderealTime(d, lw) - c.ra);
|
||||
const h = altitude(H, phi, c.dec);
|
||||
const sinH = cos(phi) * cos(c.dec) * sin(H);
|
||||
if (abs(sinH) < 1e-6) break; // grazing the horizon — correction is ill-conditioned
|
||||
d += (h - h0) / (2 * PI * sinH);
|
||||
const H = siderealTime(d, lw) - c.ra;
|
||||
const k = cos(phi) * cos(c.dec);
|
||||
const f = sin(phi) * sin(c.dec) + k * cos(H) - s0;
|
||||
if ((f < 0) === rising) a = d; else b = d;
|
||||
|
||||
const step = f / (2 * PI * k * sin(H));
|
||||
if (d + step > a && d + step < b) {
|
||||
d += step;
|
||||
if (abs(step) < 1e-5) return d; // ~1 s step, so quadratic convergence leaves ~µs error
|
||||
} else {
|
||||
d = (a + b) / 2;
|
||||
if (b - a < 1e-8) return d;
|
||||
}
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
// start of the civil day containing date in a zone utcOffset minutes ahead of UTC, as a ms timestamp
|
||||
@@ -182,24 +196,45 @@ export function getTimes(date, lat, lng, height = 0, utcOffset) {
|
||||
const dt = solarDayTransit(date, lw, utcOffset);
|
||||
const dec = sunCoords(toDaysTT(dt)).dec; // declination at transit, shared by every rise/set solve
|
||||
|
||||
// sine of the Sun's altitude at transit and at the nadirs either side (H = 0 and π), bracketing
|
||||
// every rise and set; the nadirs use their own declination, which matters on regime-boundary days
|
||||
const sNoon = cos(phi - dec);
|
||||
const sNadir1 = -cos(phi + sunCoords(toDaysTT(dt - 0.5)).dec);
|
||||
const sNadir2 = -cos(phi + sunCoords(toDaysTT(dt + 0.5)).dec);
|
||||
|
||||
// the window is the solar day [dt - 0.5, dt + 0.5), or with an offset the civil day, which is just as
|
||||
// long but shifted by up to half a day: it then also takes in the evening of the previous solar day
|
||||
// or the morning of the next one, where events cut off from this solar day are looked up instead
|
||||
const start = utcOffset === undefined ? dt - 0.5 : toDays(civilMidnight(date, utcOffset));
|
||||
const end = start + 1;
|
||||
const prev = start < dt - 0.5;
|
||||
const next = end > dt + 0.5;
|
||||
const dt2 = prev ? solarTransit(dt - 1, lw) : next ? solarTransit(dt + 1, lw) : dt;
|
||||
const dec2 = dt2 === dt ? dec : sunCoords(toDaysTT(dt2)).dec;
|
||||
const sNoon2 = cos(phi - dec2);
|
||||
|
||||
// solar days run up to ~30 s longer than 24 h, so a civil day can miss a nadir by seconds; keep
|
||||
// this solar day's one then
|
||||
const result = {
|
||||
solarNoon: fromJulian(dt + J2000),
|
||||
nadir: fromJulian(dt + J2000 - 0.5)
|
||||
nadir: fromJulian((next && dt2 - 0.5 < end ? dt2 : dt) - 0.5 + J2000)
|
||||
};
|
||||
|
||||
for (const [angle, riseName, setName] of times) {
|
||||
const h0 = (angle + dh) * rad;
|
||||
const jrise = getSetJ(h0, dt, -1, lw, phi, dec);
|
||||
const jset = getSetJ(h0, dt, 1, lw, phi, dec);
|
||||
let jrise = getSetJ(h0, dt - 0.5, dt, sNadir1, sNoon, dt, lw, phi, dec);
|
||||
let jset = getSetJ(h0, dt, dt + 0.5, sNoon, sNadir2, dt, lw, phi, dec);
|
||||
if (next && !(jrise >= start)) jrise = getSetJ(h0, dt + 0.5, dt2, sNadir2, sNoon2, dt2, lw, phi, dec2);
|
||||
if (prev && !(jset < end)) jset = getSetJ(h0, dt2, dt - 0.5, sNoon2, sNadir1, dt2, lw, phi, dec2);
|
||||
|
||||
// a NaN means the Sun never reaches this altitude on this day — report null, not Invalid Date
|
||||
result[riseName] = Number.isNaN(jrise) ? null : fromJulian(jrise + J2000);
|
||||
result[setName] = Number.isNaN(jset) ? null : fromJulian(jset + J2000);
|
||||
// NaN (the Sun never reaches this altitude) or outside the window: report null, not Invalid Date
|
||||
result[riseName] = jrise >= start && jrise < end ? fromJulian(jrise + J2000) : null;
|
||||
result[setName] = jset >= start && jset < end ? fromJulian(jset + J2000) : null;
|
||||
}
|
||||
|
||||
// polar day/night: when the Sun never crosses the standard rise/set altitude, flag which side it
|
||||
// stays on by comparing its altitude at solar noon (its daily maximum) against that threshold.
|
||||
if (result.sunrise === null) {
|
||||
if (result.sunrise === null && result.sunset === null) {
|
||||
const noonAlt = altitude(0, phi, dec);
|
||||
const riseSetAlt = (times[0][0] + dh) * rad;
|
||||
result.alwaysUp = noonAlt > riseSetAlt;
|
||||
|
||||
Reference in new issue
Block a user