feat(scheduler): observability engine and an ACP-compatible dispatcher
The email said the two hard parts are "various hardware devices" and "priorities". This is the priorities half. ACP is what small observatories have run for twenty years and DC-3 Dreams no longer supports it, so the scheduler is replicated rather than invented: an operator moving across should meet the behaviour they already know. The source is Denny, "Dispatch Scheduling of Automated Telescopes", Proc. Society for Astronomical Sciences 23, p.35 (2004), which adapts Steele & Carter, Proc. SPIE 3112, p.222 (1997). services/observability.js is the geometry: sidereal time, precession, alt/az, Kasten-Young airmass, Sun and Moon, twilight bands, visibility windows, and constraint evaluation that gives a reason for every refusal. Low precision on purpose, and the header says where it must not be used: pointing needs refraction and nutation, timing needs the barycentric correction the sidecar already does with astropy. services/dispatch.js is the scheduler. The six-term efficiency function with the coefficients the paper reports as working (priority 1.0, transit 0.8, slew 0.7), per-user priority normalisation to a mean of 0.5, the Lorentzian moon-avoidance distance, the four-value sky quality scale, and the rule that a running plan is never abandoned for a better unstarted one. Two of those terms are worth the words they cost. Nearness to transit is altitude over the target's OWN transit altitude, not over 90, because dividing by 90 rewards a target for the accident of having a declination near the observatory's latitude. Slew overhead is a fraction of observing time rather than a distance penalty, because thirty seconds of slew is nothing before an hour of integration and everything before a ten-second exposure. The tests assert the paper's own findings: weighting priority alone picks the most important target wherever it is, transit alone tracks the meridian, slew alone walks the sky in short hops. If those do not reproduce, it is not the same scheduler however plausible the output. One real bug, found only because the ephemeris is checked against astropy at three sites and four epochs rather than against itself: the first version fed J2000 catalogue coordinates into an of-date hour angle. That is a frame mismatch, and it put every altitude 0.37 degrees out in the same direction, which is precisely the error a self- consistent test suite cannot see. Precession to the equinox of date fixed it, and the module now takes and returns J2000 throughout. Two test expectations of mine were also wrong and are corrected rather than accommodated: a target 70 degrees east at declination 10 is below the altitude limit and never competed at all, and with the published weights a nine-fold priority edge is SUPPOSED to carry a 25-degree slew, which is what the paper describes. 319 server tests. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
380ca6583a
commit
71febaf14f
@@ -498,6 +498,32 @@ existing request-group state machine in
|
||||
`services/lifecycle.js` is roughly the right shape for what a scheduler
|
||||
consumes.
|
||||
|
||||
**Progress (2026-09-12):** The engine exists.
|
||||
[services/observability.js](server/src/services/observability.js) does the
|
||||
geometry: sidereal time, precession, alt/az, Kasten-Young airmass, Sun and Moon
|
||||
positions, twilight bands, visibility windows and per-target constraint
|
||||
evaluation with a stated reason for every refusal. Verified against astropy at
|
||||
three sites and four epochs, which is how the J2000-versus-of-date frame bug
|
||||
was caught: it put every altitude 0.37 degrees out, always in the same
|
||||
direction, and nothing but an independent ephemeris would have found it.
|
||||
|
||||
[services/dispatch.js](server/src/services/dispatch.js) replicates the ACP
|
||||
Scheduler design from Denny, *Dispatch Scheduling of Automated Telescopes*,
|
||||
Proc. Society for Astronomical Sciences **23**, p.35 (2004), which adapts
|
||||
Steele & Carter (Proc. SPIE 3112, p.222, 1997). Replicated rather than invented
|
||||
because ACP is what small observatories have run for twenty years and DC-3
|
||||
Dreams no longer supports it: an operator moving across should meet the same
|
||||
behaviour. That means the six-term efficiency function with its published
|
||||
coefficients (priority 1.0, transit 0.8, slew 0.7), per-user priority
|
||||
normalisation to a mean of 0.5, the Lorentzian moon-avoidance distance, the
|
||||
four-value sky quality scale, and the rule that a running plan is never
|
||||
abandoned for a better unstarted one.
|
||||
|
||||
Not yet done: linked observations (a plan of several targets with timing
|
||||
tolerances, which is what asteroid follow-up and variable-star work need), the
|
||||
forward time projection that checks a whole plan can complete before it starts,
|
||||
the "rising plan delay" refinement from later ACPS versions, and any UI.
|
||||
|
||||
## V. Executable methods
|
||||
|
||||
**What:** Make a published observation reproducible as an artifact: the exact Nox
|
||||
|
||||
@@ -0,0 +1,351 @@
|
||||
// A dispatch scheduler: which of the eligible plans to start next.
|
||||
//
|
||||
// This is a replication of the ACP Scheduler (ACPS) design published by Robert
|
||||
// B. Denny in "Dispatch Scheduling of Automated Telescopes", Proceedings of the
|
||||
// Society for Astronomical Sciences 23, p.35 (2004), which in turn adapts the
|
||||
// Liverpool Telescope work of Steele & Carter, Proc. SPIE 3112, p.222 (1997).
|
||||
// The efficiency function, its terms and the published weighting coefficients
|
||||
// below are theirs. It is replicated here rather than invented because ACP is
|
||||
// what small observatories have run for twenty years, DC-3 Dreams no longer
|
||||
// supports it, and an operator moving off it should find the same behaviour
|
||||
// rather than have to learn a new set of scheduling habits.
|
||||
//
|
||||
// Why dispatch rather than a pre-computed night
|
||||
// ---------------------------------------------
|
||||
// A static schedule optimised in advance assumes error-free execution, exact
|
||||
// knowledge of how long each observation takes, and foreknowledge of the
|
||||
// weather. Miss any of those and the schedule breaks: a failed observation
|
||||
// leaves a hole, an unplanned refocus shifts everything after it, and a cirrus
|
||||
// layer eliminates a whole class of observations while other work that could
|
||||
// have run sits unscheduled. A dispatcher decides at each cycle, so bad skies
|
||||
// cost the time they actually cost and nothing else.
|
||||
//
|
||||
// What this module does NOT do
|
||||
// ----------------------------
|
||||
// It does not talk to hardware, and it does not decide when a night starts or
|
||||
// whether it is safe to open. It answers one question: given what is eligible
|
||||
// right now, what should run next. Eligibility is services/observability.js.
|
||||
|
||||
import { evaluateTarget, angularSeparation, julianDate, skyState } from './observability.js';
|
||||
|
||||
/**
|
||||
* Sky quality, as ACPS numbers it. Higher is better.
|
||||
*
|
||||
* Four values rather than a continuous scale because an operator can tell
|
||||
* these apart by eye and cannot tell 0.62 from 0.68.
|
||||
*/
|
||||
export const SKY_QUALITY = { excellent: 4, good: 3, fair: 2, poor: 1 };
|
||||
|
||||
/**
|
||||
* Weights for the efficiency function's terms.
|
||||
*
|
||||
* The first three are the values Denny reports as producing "quite reasonable
|
||||
* behavior and efficiency" (61 of 66 plans completed in the paper's
|
||||
* simulation): priority first, then nearness to transit, then slew overhead.
|
||||
*
|
||||
* `retry` is deliberately small. The paper warns that a large value can trap a
|
||||
* failing plan in a retry loop, where it keeps winning the dispatch and keeps
|
||||
* failing.
|
||||
*
|
||||
* `meridian` is zero unless the mount is a German equatorial, because there is
|
||||
* no flip to penalise on a fork or an alt-az.
|
||||
*/
|
||||
export const DEFAULT_WEIGHTS = {
|
||||
priority: 1.0,
|
||||
transit: 0.8,
|
||||
slew: 0.7,
|
||||
retry: 0.1,
|
||||
meridian: 0.0,
|
||||
conditions: 0.0,
|
||||
};
|
||||
|
||||
/** Slew rate and overheads, used to estimate how long a move costs. */
|
||||
export const DEFAULT_TIMING = {
|
||||
slew_rate_deg_s: 3.5,
|
||||
slew_settle_s: 15,
|
||||
german_flip_s: 90,
|
||||
filter_change_s: 45,
|
||||
autofocus_s: 60,
|
||||
readout_s: 20,
|
||||
};
|
||||
|
||||
/**
|
||||
* Normalise each user's priorities so their mean is 0.5.
|
||||
*
|
||||
* The fairness mechanism, and the reason users are not made to agree on what
|
||||
* "priority 7" means. Everyone rates their own plans on whatever scale they
|
||||
* like; normalising per user means a user who marks everything urgent gains
|
||||
* nothing over one who ranks honestly, because it is the SHAPE of their
|
||||
* ranking that survives, not its magnitude.
|
||||
*
|
||||
* Recomputed every cycle rather than cached, so a plan submitted mid-night
|
||||
* lands in a correctly normalised set.
|
||||
*
|
||||
* @param {Array<{user_id?:string, priority?:number}>} plans
|
||||
* @returns {Map<object, number>} plan to its normalised priority in [0,1]
|
||||
*/
|
||||
export function normalizePriorities(plans) {
|
||||
const byUser = new Map();
|
||||
for (const p of plans) {
|
||||
const u = p.user_id ?? '__anonymous__';
|
||||
if (!byUser.has(u)) byUser.set(u, []);
|
||||
byUser.get(u).push(p);
|
||||
}
|
||||
|
||||
const out = new Map();
|
||||
for (const group of byUser.values()) {
|
||||
const raw = group.map(p => (Number.isFinite(p.priority) ? p.priority : 1));
|
||||
const mean = raw.reduce((a, b) => a + b, 0) / raw.length;
|
||||
|
||||
group.forEach((p, i) => {
|
||||
// A user whose plans all share one priority has expressed no preference
|
||||
// between them, so they all sit at the mean.
|
||||
if (mean === 0) { out.set(p, 0.5); return; }
|
||||
const scaled = (raw[i] / mean) * 0.5;
|
||||
out.set(p, Math.max(0, Math.min(1, scaled)));
|
||||
});
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Seconds to slew between two sky positions, including settle and flip.
|
||||
*
|
||||
* The great-circle distance rather than the larger of the two axis moves: it
|
||||
* is within a factor the estimate can carry, and it needs no knowledge of the
|
||||
* mount's geometry.
|
||||
*/
|
||||
export function slewSeconds(fromRaDec, toRaDec, timing = {}, flip = false) {
|
||||
const t = { ...DEFAULT_TIMING, ...timing };
|
||||
if (!fromRaDec) return t.slew_settle_s;
|
||||
const d = angularSeparation(fromRaDec.ra_deg, fromRaDec.dec_deg, toRaDec.ra_deg, toRaDec.dec_deg);
|
||||
return d / t.slew_rate_deg_s + t.slew_settle_s + (flip ? t.german_flip_s : 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* The ACPS efficiency function, term by term.
|
||||
*
|
||||
* e(n) = b1*E1 + b2*E2 + ... + b6*E6
|
||||
*
|
||||
* Each term is on [0,1] so the weights alone decide the balance. Returned as a
|
||||
* breakdown rather than a bare number, because the first question anyone asks
|
||||
* a scheduler is why it chose what it chose.
|
||||
*
|
||||
* @param {object} candidate plan with geometry already evaluated
|
||||
* @param {object} context { from, sky_quality_actual, timing, weights }
|
||||
*/
|
||||
export function efficiency(candidate, context = {}) {
|
||||
const w = { ...DEFAULT_WEIGHTS, ...(context.weights || {}) };
|
||||
const timing = { ...DEFAULT_TIMING, ...(context.timing || {}) };
|
||||
const g = candidate.geometry;
|
||||
|
||||
// E1: normalised scientific priority.
|
||||
const E1 = candidate.normalized_priority ?? 0.5;
|
||||
|
||||
// E2: nearness to transit altitude, NOT altitude over 90. Dividing by 90
|
||||
// would systematically favour targets whose declination sits near the
|
||||
// observatory's latitude, because those are the only ones that ever get
|
||||
// near the zenith. Dividing by the target's own transit altitude asks the
|
||||
// fair question: is this target as high as it is ever going to get?
|
||||
const transitAlt = g?.max_alt_deg;
|
||||
const E2 = (g && transitAlt > 0)
|
||||
? Math.max(0, Math.min(1, g.alt_deg / transitAlt))
|
||||
: 0;
|
||||
|
||||
// E3: slew overhead as a fraction of the time actually spent observing. A
|
||||
// thirty-second slew is nothing before an hour of integration and dominant
|
||||
// before a ten-second exposure, which is why it is a ratio rather than a
|
||||
// penalty on distance.
|
||||
const tO = Math.max(1, candidate.duration_s ?? 300);
|
||||
const tS = slewSeconds(context.from, candidate, timing, candidate.needs_flip);
|
||||
const E3 = tO / (tS + tO);
|
||||
|
||||
// E4: retry count. A plan that failed and was re-queued gets a small boost
|
||||
// so it is not starved by plans that have never been tried.
|
||||
const E4 = Math.min(1, (candidate.retry_count ?? 0) / 3);
|
||||
|
||||
// E5: meridian crossing penalty for a German equatorial. Zero-weighted
|
||||
// unless the mount actually flips.
|
||||
const E5 = candidate.needs_flip ? 0 : 1;
|
||||
|
||||
// E6: how well the conditions match what was asked for. Excellent skies
|
||||
// spent on a plan that only needed fair are excellent skies wasted, so a
|
||||
// close match scores higher than an extravagant one. Requires BOTH numbers;
|
||||
// with no measured sky quality there is nothing to match against.
|
||||
const required = SKY_QUALITY[candidate.sky_quality_required] ?? null;
|
||||
const actual = SKY_QUALITY[context.sky_quality_actual] ?? null;
|
||||
const E6 = (required !== null && actual !== null)
|
||||
? 1 / (Math.abs(required - actual) + 1)
|
||||
: 0;
|
||||
|
||||
const terms = { E1, E2, E3, E4, E5, E6 };
|
||||
const score =
|
||||
w.priority * E1 + w.transit * E2 + w.slew * E3
|
||||
+ w.retry * E4 + w.meridian * E5 + w.conditions * E6;
|
||||
|
||||
return { score, terms, weights: w, slew_s: tS };
|
||||
}
|
||||
|
||||
/**
|
||||
* ACP's moon avoidance: a required separation that shrinks as the Moon wanes.
|
||||
*
|
||||
* ACPS expresses this as a Lorentzian in the Moon's phase:
|
||||
*
|
||||
* required = distance / (1 + (0.5 - age/width)^2)
|
||||
*
|
||||
* `age` is days since new moon and `width` the synodic month the operator is
|
||||
* working against. At half a width, which is full Moon, the denominator is 1
|
||||
* and the full separation is demanded. Either side of that it relaxes, so a
|
||||
* thin crescent costs a few degrees and a full Moon costs all of them.
|
||||
*
|
||||
* A flat separation would be either too strict for three weeks a month or too
|
||||
* lax for one.
|
||||
*/
|
||||
export function moonAvoidanceDistance(baseDistanceDeg, moonAgeDays, widthDays = 29.53) {
|
||||
if (!Number.isFinite(baseDistanceDeg) || baseDistanceDeg <= 0) return 0;
|
||||
const x = 0.5 - (moonAgeDays / widthDays);
|
||||
return baseDistanceDeg / (1 + x * x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Moon age in days since new, derived from illumination and whether it is
|
||||
* waxing, so it stays consistent with the ephemeris rather than with a
|
||||
* calendar.
|
||||
*/
|
||||
export function moonAge(jd) {
|
||||
const { illuminated, waxing } = skyState(0, 0, jd).moon;
|
||||
// illuminated = (1 + cos(phase))/2, so phase = acos(2k - 1), and the age is
|
||||
// the fraction of the synodic month that phase corresponds to.
|
||||
const phase = Math.acos(Math.max(-1, Math.min(1, 2 * illuminated - 1)));
|
||||
const frac = phase / Math.PI; // 0 at full, 1 at new
|
||||
const sinceNew = waxing ? (1 - frac) / 2 : (1 + frac) / 2;
|
||||
return sinceNew * 29.53;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decide what to run next.
|
||||
*
|
||||
* The rules are ACPS's, and each one is there because the alternative
|
||||
* misbehaves:
|
||||
*
|
||||
* A plan already running wins over starting a new one. A plan is the unit
|
||||
* that must complete within a night, so abandoning one halfway to start
|
||||
* something better wastes everything already spent on it.
|
||||
*
|
||||
* A higher priority plan that has not started cannot force a running plan to
|
||||
* fail. It can only stop a lower priority plan from starting. Without this,
|
||||
* a busy night is one long sequence of interrupted half-observations.
|
||||
*
|
||||
* A plan whose estimated start is in the future is skipped this cycle rather
|
||||
* than held, because the dispatcher will be back before then and conditions
|
||||
* may have changed.
|
||||
*
|
||||
* @param {Array<object>} plans candidate plans
|
||||
* @param {object} context { site, when, from, sky_quality_actual,
|
||||
* weights, timing, constraints }
|
||||
* @returns {{selected:object|null, considered:Array<object>, reason:string}}
|
||||
*/
|
||||
export function selectNext(plans, context = {}) {
|
||||
const { site, when, constraints = {} } = context;
|
||||
const jd = julianDate(when);
|
||||
const normalized = normalizePriorities(plans);
|
||||
|
||||
const considered = plans.map(plan => {
|
||||
const evaluation = evaluateTarget(plan, site, when, { ...constraints, ...(plan.constraints || {}) });
|
||||
|
||||
// Moon avoidance on top of the flat separation the observability engine
|
||||
// applies, using ACP's phase-scaled distance where the plan asks for it.
|
||||
const extraReasons = [];
|
||||
if (Number.isFinite(plan.moon_avoid_deg) && evaluation.geometry) {
|
||||
const required = moonAvoidanceDistance(plan.moon_avoid_deg, moonAge(jd));
|
||||
if (evaluation.geometry.moon_up && evaluation.geometry.moon_separation_deg < required) {
|
||||
extraReasons.push(
|
||||
`${evaluation.geometry.moon_separation_deg.toFixed(0)} from the Moon,`
|
||||
+ ` which needs ${required.toFixed(0)} at this phase`);
|
||||
}
|
||||
}
|
||||
|
||||
const eligible = evaluation.ok && extraReasons.length === 0;
|
||||
const candidate = {
|
||||
...plan,
|
||||
normalized_priority: normalized.get(plan) ?? 0.5,
|
||||
geometry: evaluation.geometry,
|
||||
eligible,
|
||||
reasons: [...evaluation.reasons, ...extraReasons],
|
||||
};
|
||||
|
||||
const eff = eligible
|
||||
? efficiency(candidate, context)
|
||||
: { score: 0, terms: null, slew_s: null };
|
||||
|
||||
return { ...candidate, score: eff.score, terms: eff.terms, slew_s: eff.slew_s };
|
||||
});
|
||||
|
||||
// A plan already under way continues, whatever an unstarted plan scores.
|
||||
const running = considered.filter(c => c.state === 'running' && c.eligible);
|
||||
if (running.length) {
|
||||
const pick = running.sort((a, b) => b.score - a.score)[0];
|
||||
return {
|
||||
selected: pick,
|
||||
considered: rank(considered),
|
||||
reason: 'continuing a plan that is already under way',
|
||||
};
|
||||
}
|
||||
|
||||
const startable = considered.filter(c => c.eligible && c.state !== 'running');
|
||||
if (!startable.length) {
|
||||
return {
|
||||
selected: null,
|
||||
considered: rank(considered),
|
||||
reason: considered.length
|
||||
? 'nothing is eligible right now'
|
||||
: 'no candidate plans',
|
||||
};
|
||||
}
|
||||
|
||||
const pick = startable.sort((a, b) => b.score - a.score)[0];
|
||||
return {
|
||||
selected: pick,
|
||||
considered: rank(considered),
|
||||
reason: `highest efficiency of ${startable.length} eligible plan(s)`,
|
||||
};
|
||||
}
|
||||
|
||||
/** Eligible first, then by score. A declined plan keeps its reasons. */
|
||||
function rank(rows) {
|
||||
return [...rows].sort((a, b) =>
|
||||
Number(b.eligible) - Number(a.eligible)
|
||||
|| b.score - a.score
|
||||
|| String(a.name || '').localeCompare(String(b.name || '')));
|
||||
}
|
||||
|
||||
/**
|
||||
* Explain a choice in the terms that produced it.
|
||||
*
|
||||
* Denny's paper measures the scheduler's behaviour by which single term is
|
||||
* weighted: priority alone picks the most important target regardless of where
|
||||
* the telescope is pointing, transit alone tracks the meridian, slew alone
|
||||
* walks the sky in short hops. Being able to see the same breakdown per
|
||||
* decision is what makes the weights adjustable by someone other than the
|
||||
* author.
|
||||
*/
|
||||
export function explain(candidate) {
|
||||
if (!candidate?.terms) return candidate?.reasons?.join('; ') || 'not eligible';
|
||||
const w = { ...DEFAULT_WEIGHTS, ...(candidate.weights || {}) };
|
||||
const LABELS = {
|
||||
E1: 'priority',
|
||||
E2: 'nearness to transit',
|
||||
E3: 'slew overhead',
|
||||
E4: 'retry boost',
|
||||
E5: 'no meridian flip',
|
||||
E6: 'conditions match',
|
||||
};
|
||||
const WEIGHT_KEYS = {
|
||||
E1: 'priority', E2: 'transit', E3: 'slew',
|
||||
E4: 'retry', E5: 'meridian', E6: 'conditions',
|
||||
};
|
||||
return Object.entries(candidate.terms)
|
||||
.filter(([k]) => w[WEIGHT_KEYS[k]] > 0)
|
||||
.map(([k, v]) => `${LABELS[k]} ${(v * w[WEIGHT_KEYS[k]]).toFixed(3)}`)
|
||||
.join(' + ');
|
||||
}
|
||||
@@ -0,0 +1,538 @@
|
||||
// Where a target is, and whether it is worth pointing at right now.
|
||||
//
|
||||
// This is the half of an observatory control system that decides WHAT to
|
||||
// observe, as distinct from the drivers that decide HOW. A queue of targets
|
||||
// with priorities is useless without it: the operator ends up picking by hand,
|
||||
// which is what a calendar already does.
|
||||
//
|
||||
// Accuracy, and why these algorithms
|
||||
// ----------------------------------
|
||||
// Everything here is low-precision by design. Sun position is good to about
|
||||
// 0.01 degrees and Moon position to about 0.3, which is far better than any
|
||||
// decision this module makes. Altitude limits are set in whole degrees, moon
|
||||
// avoidance in tens of degrees, and a 0.3 degree error in the Moon changes no
|
||||
// scheduling outcome. Pulling in a full ephemeris (VSOP87, ELP2000) to decide
|
||||
// whether a target is 41 or 41.3 degrees up would be precision spent where
|
||||
// nothing reads it.
|
||||
//
|
||||
// This is NOT the right tool for pointing a telescope or for timing. Pointing
|
||||
// needs refraction, proper motion, precession to date, nutation and aberration;
|
||||
// timing needs the barycentric correction the sidecar already does with astropy.
|
||||
// Those two jobs have their own code, deliberately.
|
||||
//
|
||||
// Verified against astropy in test/observability.test.js: alt/az agree within
|
||||
// 0.1 degrees and the Moon within 0.5, across three sites from the equator to
|
||||
// mid-latitude north and south, at solstices and an equinox. Agreement with an
|
||||
// independent ephemeris is the only reason to trust hand-rolled astronomy.
|
||||
|
||||
const DEG = Math.PI / 180;
|
||||
const RAD = 180 / Math.PI;
|
||||
|
||||
/** Unix epoch in Julian Days. 1970-01-01T00:00:00Z. */
|
||||
const JD_UNIX_EPOCH = 2440587.5;
|
||||
|
||||
/** J2000.0: 2000-01-01T12:00:00 TT, the origin every series here counts from. */
|
||||
export const JD_J2000 = 2451545.0;
|
||||
|
||||
const norm360 = d => ((d % 360) + 360) % 360;
|
||||
/** Wrap to (-180, 180]. Used for hour angles, where sign means east or west. */
|
||||
const norm180 = d => { const x = norm360(d); return x > 180 ? x - 360 : x; };
|
||||
|
||||
/**
|
||||
* Julian Date for an instant.
|
||||
*
|
||||
* @param {Date|string|number} when
|
||||
* @returns {number}
|
||||
*/
|
||||
export function julianDate(when) {
|
||||
const ms = when instanceof Date ? when.getTime() : new Date(when).getTime();
|
||||
if (!Number.isFinite(ms)) throw new TypeError(`not a usable time: ${String(when)}`);
|
||||
return ms / 86400000 + JD_UNIX_EPOCH;
|
||||
}
|
||||
|
||||
/**
|
||||
* Greenwich Mean Sidereal Time, in degrees.
|
||||
*
|
||||
* The IAU 1982 series. UT1 and UTC are treated as the same thing here: they
|
||||
* differ by under 0.9 seconds by definition, which is 0.004 degrees of
|
||||
* rotation, and nothing downstream can see that.
|
||||
*/
|
||||
export function gmst(jd) {
|
||||
const d = jd - JD_J2000;
|
||||
const t = d / 36525;
|
||||
return norm360(
|
||||
280.46061837 + 360.98564736629 * d + 0.000387933 * t * t - (t * t * t) / 38710000,
|
||||
);
|
||||
}
|
||||
|
||||
/** Local Mean Sidereal Time in degrees. East longitude positive. */
|
||||
export function lmst(jd, longitudeDeg) {
|
||||
return norm360(gmst(jd) + longitudeDeg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Precess equatorial coordinates between J2000 and the equinox of date.
|
||||
*
|
||||
* This module takes and returns J2000 (ICRS) coordinates, because that is what
|
||||
* catalogues publish, what `archive_frames.ra` holds and what astropy returns.
|
||||
* Sidereal time, however, is an angle about the equinox OF DATE. Feeding a
|
||||
* J2000 right ascension into an of-date hour angle silently mixes two frames.
|
||||
*
|
||||
* The error is not subtle and it is not random: precession is about 50 arcsec
|
||||
* a year, so by 2026 it is 0.37 degrees, always in the same direction. Every
|
||||
* altitude comes out consistently wrong, which is exactly the kind of error
|
||||
* that survives testing against yourself. It was caught by comparing to
|
||||
* astropy and by nothing else.
|
||||
*
|
||||
* Meeus, Astronomical Algorithms, chapter 21. The rigorous reduction, not the
|
||||
* first-order approximation, because the latter degrades near the pole and
|
||||
* Polaris is in the test set.
|
||||
*
|
||||
* @param {boolean} toDate true for J2000 to date, false for the inverse
|
||||
*/
|
||||
function precess(raDeg, decDeg, jd, toDate = true) {
|
||||
const T = (jd - JD_J2000) / 36525;
|
||||
// Arcseconds, converted to degrees.
|
||||
const zeta = (2306.2181 * T + 0.30188 * T * T + 0.017998 * T ** 3) / 3600;
|
||||
const z = (2306.2181 * T + 1.09468 * T * T + 0.018203 * T ** 3) / 3600;
|
||||
const theta = (2004.3109 * T - 0.42665 * T * T - 0.041833 * T ** 3) / 3600;
|
||||
|
||||
// Going back to J2000 is the same rotation with zeta and z swapped and every
|
||||
// angle negated.
|
||||
const [a, b, th] = toDate ? [zeta, z, theta] : [-z, -zeta, -theta];
|
||||
|
||||
const ra0 = (raDeg + a) * DEG;
|
||||
const dec0 = decDeg * DEG;
|
||||
const thr = th * DEG;
|
||||
|
||||
const A = Math.cos(dec0) * Math.sin(ra0);
|
||||
const B = Math.cos(thr) * Math.cos(dec0) * Math.cos(ra0) - Math.sin(thr) * Math.sin(dec0);
|
||||
const C = Math.sin(thr) * Math.cos(dec0) * Math.cos(ra0) + Math.cos(thr) * Math.sin(dec0);
|
||||
|
||||
return {
|
||||
ra_deg: norm360(Math.atan2(A, B) * RAD + b),
|
||||
dec_deg: Math.asin(Math.max(-1, Math.min(1, C))) * RAD,
|
||||
};
|
||||
}
|
||||
|
||||
/** J2000 (ICRS) to the mean equinox of date. */
|
||||
export const precessToDate = (ra, dec, jd) => precess(ra, dec, jd, true);
|
||||
|
||||
/** Mean equinox of date back to J2000 (ICRS). */
|
||||
export const precessToJ2000 = (ra, dec, jd) => precess(ra, dec, jd, false);
|
||||
|
||||
/**
|
||||
* Equatorial to horizontal.
|
||||
*
|
||||
* Takes J2000 coordinates, which is what every caller has, and precesses them
|
||||
* to the equinox of date internally so the hour angle is computed in the same
|
||||
* frame the sidereal time belongs to.
|
||||
*
|
||||
* Written as a rotation into north/east/up components rather than as the
|
||||
* textbook tangent formula, because the tangent form needs quadrant fixing
|
||||
* that is easy to get subtly wrong near the meridian and the poles.
|
||||
*
|
||||
* Azimuth is measured from north through east, which is the convention every
|
||||
* mount and every FITS header uses.
|
||||
*
|
||||
* @returns {{alt_deg:number, az_deg:number, ha_deg:number}} hour angle is
|
||||
* negative east of the meridian, positive west, which is how an
|
||||
* observer states "two hours before transit".
|
||||
*/
|
||||
export function equatorialToHorizontal(raDeg, decDeg, latDeg, lonDeg, jd) {
|
||||
const ofDate = precessToDate(raDeg, decDeg, jd);
|
||||
const ha = norm180(lmst(jd, lonDeg) - ofDate.ra_deg);
|
||||
const H = ha * DEG;
|
||||
const dec = ofDate.dec_deg * DEG;
|
||||
const lat = latDeg * DEG;
|
||||
|
||||
const sinDec = Math.sin(dec), cosDec = Math.cos(dec);
|
||||
const sinLat = Math.sin(lat), cosLat = Math.cos(lat);
|
||||
const sinH = Math.sin(H), cosH = Math.cos(H);
|
||||
|
||||
const north = -cosH * cosDec * sinLat + sinDec * cosLat;
|
||||
const east = -sinH * cosDec;
|
||||
const up = cosH * cosDec * cosLat + sinDec * sinLat;
|
||||
|
||||
return {
|
||||
alt_deg: Math.asin(Math.max(-1, Math.min(1, up))) * RAD,
|
||||
az_deg: norm360(Math.atan2(east, north) * RAD),
|
||||
ha_deg: ha,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Airmass at a given altitude, by Kasten & Young (1989).
|
||||
*
|
||||
* Not sec(z): that diverges at the horizon and is already 2% wrong at 70
|
||||
* degrees zenith distance, which is exactly where an airmass limit gets set.
|
||||
*
|
||||
* Returns null below the horizon, because an airmass for a set target is not a
|
||||
* large number, it is a meaningless one.
|
||||
*/
|
||||
export function airmass(altDeg) {
|
||||
if (!Number.isFinite(altDeg) || altDeg <= 0) return null;
|
||||
const a = altDeg * DEG;
|
||||
return 1 / (Math.sin(a) + 0.50572 * Math.pow(altDeg + 6.07995, -1.6364));
|
||||
}
|
||||
|
||||
/**
|
||||
* Apparent geocentric position of the Sun.
|
||||
*
|
||||
* The low-precision series from the Astronomical Almanac, good to about 0.01
|
||||
* degrees over a century either side of J2000.
|
||||
*
|
||||
* @returns {{ra_deg:number, dec_deg:number, ecliptic_lon_deg:number}}
|
||||
*/
|
||||
export function sunPosition(jd) {
|
||||
const n = jd - JD_J2000;
|
||||
const L = norm360(280.460 + 0.9856474 * n);
|
||||
const g = norm360(357.528 + 0.9856003 * n) * DEG;
|
||||
const lambda = (L + 1.915 * Math.sin(g) + 0.020 * Math.sin(2 * g)) * DEG;
|
||||
const eps = (23.439 - 0.0000004 * n) * DEG;
|
||||
|
||||
// The series produces coordinates of date. Everything this module exposes is
|
||||
// J2000, so it is precessed back rather than handed over in a second frame.
|
||||
const ofDate = {
|
||||
ra: norm360(Math.atan2(Math.cos(eps) * Math.sin(lambda), Math.cos(lambda)) * RAD),
|
||||
dec: Math.asin(Math.sin(eps) * Math.sin(lambda)) * RAD,
|
||||
};
|
||||
const j2000 = precessToJ2000(ofDate.ra, ofDate.dec, jd);
|
||||
|
||||
return {
|
||||
ra_deg: j2000.ra_deg,
|
||||
dec_deg: j2000.dec_deg,
|
||||
ecliptic_lon_deg: norm360(lambda * RAD),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Apparent geocentric position of the Moon.
|
||||
*
|
||||
* The leading periodic terms of the lunar series, good to roughly 0.2 degrees
|
||||
* against astropy. That is coarse for an ephemeris and ample for moon
|
||||
* avoidance, which is set in multiples of ten degrees.
|
||||
*
|
||||
* Geocentric, not topocentric: the Moon's parallax moves it by up to a degree
|
||||
* for an observer on the surface. Correcting for it would change no avoidance
|
||||
* decision, and pretending to a precision this series does not have would be
|
||||
* worse than the error.
|
||||
*/
|
||||
export function moonPosition(jd) {
|
||||
const t = (jd - JD_J2000) / 36525;
|
||||
|
||||
// The five fundamental arguments of the lunar theory.
|
||||
const Lp = norm360(218.3165 + 481267.8813 * t); // mean longitude
|
||||
const D = norm360(297.8502 + 445267.1115 * t) * DEG; // mean elongation from the Sun
|
||||
const M = norm360(134.9634 + 477198.8676 * t) * DEG; // Moon's mean anomaly
|
||||
const Ms = norm360(357.5291 + 35999.0503 * t) * DEG; // Sun's mean anomaly
|
||||
const F = norm360(93.2721 + 483202.0175 * t) * DEG; // argument of latitude
|
||||
|
||||
// The largest periodic terms. Two terms (the equation of the centre alone)
|
||||
// leaves the Moon almost a degree out, because evection and variation are
|
||||
// both above a quarter degree and neither is negligible at this scale.
|
||||
// These nine bring it inside 0.2, which is past the point where any
|
||||
// scheduling decision changes.
|
||||
const lambda = (Lp
|
||||
+ 6.289 * Math.sin(M) // equation of the centre
|
||||
+ 1.274 * Math.sin(2 * D - M) // evection
|
||||
+ 0.658 * Math.sin(2 * D) // variation
|
||||
+ 0.214 * Math.sin(2 * M)
|
||||
- 0.186 * Math.sin(Ms) // annual equation
|
||||
- 0.114 * Math.sin(2 * F)
|
||||
+ 0.059 * Math.sin(2 * D - 2 * M)
|
||||
+ 0.057 * Math.sin(2 * D - M - Ms)
|
||||
+ 0.053 * Math.sin(2 * D + M)
|
||||
) * DEG;
|
||||
|
||||
const beta = (
|
||||
5.128 * Math.sin(F)
|
||||
+ 0.281 * Math.sin(M + F)
|
||||
- 0.278 * Math.sin(F - M)
|
||||
- 0.173 * Math.sin(F - 2 * D)
|
||||
) * DEG;
|
||||
|
||||
const eps = (23.439 - 0.0000004 * (jd - JD_J2000)) * DEG;
|
||||
|
||||
const sinB = Math.sin(beta), cosB = Math.cos(beta);
|
||||
const sinL = Math.sin(lambda), cosL = Math.cos(lambda);
|
||||
const sinE = Math.sin(eps), cosE = Math.cos(eps);
|
||||
|
||||
const ofDate = {
|
||||
ra: norm360(Math.atan2(sinL * cosE - (sinB / cosB) * sinE, cosL) * RAD),
|
||||
dec: Math.asin(sinB * cosE + cosB * sinE * sinL) * RAD,
|
||||
};
|
||||
return precessToJ2000(ofDate.ra, ofDate.dec, jd);
|
||||
}
|
||||
|
||||
/**
|
||||
* Great-circle separation between two equatorial positions, in degrees.
|
||||
*
|
||||
* The haversine form rather than the cosine rule: the cosine rule loses all its
|
||||
* precision at small separations, which is the case that matters when asking
|
||||
* whether a target is too close to the Moon.
|
||||
*/
|
||||
export function angularSeparation(ra1, dec1, ra2, dec2) {
|
||||
const d1 = dec1 * DEG, d2 = dec2 * DEG;
|
||||
const dDec = d2 - d1;
|
||||
const dRa = (ra2 - ra1) * DEG;
|
||||
const a = Math.sin(dDec / 2) ** 2
|
||||
+ Math.cos(d1) * Math.cos(d2) * Math.sin(dRa / 2) ** 2;
|
||||
return 2 * Math.asin(Math.min(1, Math.sqrt(a))) * RAD;
|
||||
}
|
||||
|
||||
/**
|
||||
* Illuminated fraction of the Moon's disk, 0 at new and 1 at full.
|
||||
*
|
||||
* Derived from the Sun-Moon elongation rather than from a calendar age, so it
|
||||
* is correct rather than approximately periodic.
|
||||
*/
|
||||
export function moonIllumination(jd) {
|
||||
const m = moonPosition(jd);
|
||||
const s = sunPosition(jd);
|
||||
const elongation = angularSeparation(m.ra_deg, m.dec_deg, s.ra_deg, s.dec_deg);
|
||||
// Phase angle, treating the Sun as infinitely distant. The error from that
|
||||
// is under a degree, and illumination is a cosine of it.
|
||||
const phaseAngle = 180 - elongation;
|
||||
return {
|
||||
elongation_deg: elongation,
|
||||
illuminated: (1 + Math.cos(phaseAngle * DEG)) / 2,
|
||||
// Waxing while the Moon leads the Sun in right ascension.
|
||||
waxing: norm360(m.ra_deg - s.ra_deg) < 180,
|
||||
};
|
||||
}
|
||||
|
||||
/** Sun altitude thresholds, in degrees. The standard definitions. */
|
||||
export const TWILIGHT = {
|
||||
day: 0,
|
||||
civil: -6,
|
||||
nautical: -12,
|
||||
astronomical: -18,
|
||||
};
|
||||
|
||||
/**
|
||||
* What the sky is doing at a site.
|
||||
*
|
||||
* `dark` means astronomical twilight has ended, which is the only state most
|
||||
* photometry is worth attempting in. Everything brighter is reported by name
|
||||
* rather than as a boolean, because a bright-star program can work in nautical
|
||||
* twilight and should not be told the sky is unusable.
|
||||
*/
|
||||
export function skyState(latDeg, lonDeg, jd) {
|
||||
const sun = sunPosition(jd);
|
||||
const h = equatorialToHorizontal(sun.ra_deg, sun.dec_deg, latDeg, lonDeg, jd);
|
||||
const alt = h.alt_deg;
|
||||
|
||||
let phase;
|
||||
if (alt > TWILIGHT.day) phase = 'day';
|
||||
else if (alt > TWILIGHT.civil) phase = 'civil';
|
||||
else if (alt > TWILIGHT.nautical) phase = 'nautical';
|
||||
else if (alt > TWILIGHT.astronomical) phase = 'astronomical';
|
||||
else phase = 'night';
|
||||
|
||||
const moon = moonPosition(jd);
|
||||
const moonH = equatorialToHorizontal(moon.ra_deg, moon.dec_deg, latDeg, lonDeg, jd);
|
||||
const illum = moonIllumination(jd);
|
||||
|
||||
return {
|
||||
sun: { ...sun, alt_deg: alt, az_deg: h.az_deg },
|
||||
moon: { ...moon, alt_deg: moonH.alt_deg, az_deg: moonH.az_deg, ...illum },
|
||||
twilight: phase,
|
||||
dark: phase === 'night',
|
||||
// A set Moon contributes no sky brightness however full it is, which is the
|
||||
// distinction a bare phase number loses.
|
||||
moon_up: moonH.alt_deg > 0,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a target ever clears an altitude from a site, and when it transits.
|
||||
*
|
||||
* Answers the question that stops a program before it starts: a target at
|
||||
* declination -40 is never usable from +40 latitude, and no amount of
|
||||
* scheduling changes that.
|
||||
*
|
||||
* @returns {{circumpolar:boolean, never_rises:boolean, max_alt_deg:number,
|
||||
* transit_lst_deg:number, hours_above:number|null}}
|
||||
*/
|
||||
export function visibilityWindow(decDeg, latDeg, minAltDeg = 30, raDeg = 0) {
|
||||
const dec = decDeg * DEG;
|
||||
const lat = latDeg * DEG;
|
||||
|
||||
// Altitude at upper culmination, which is the best this target will ever do.
|
||||
const maxAlt = 90 - Math.abs(latDeg - decDeg);
|
||||
|
||||
const cosH = (Math.sin(minAltDeg * DEG) - Math.sin(lat) * Math.sin(dec))
|
||||
/ (Math.cos(lat) * Math.cos(dec));
|
||||
|
||||
let hours = null;
|
||||
let circumpolar = false;
|
||||
let neverRises = false;
|
||||
if (cosH < -1) { circumpolar = true; hours = 24; }
|
||||
else if (cosH > 1) { neverRises = true; hours = 0; }
|
||||
else hours = (2 * Math.acos(cosH) * RAD) / 15;
|
||||
|
||||
return {
|
||||
circumpolar,
|
||||
never_rises: neverRises,
|
||||
max_alt_deg: maxAlt,
|
||||
transit_lst_deg: norm360(raDeg),
|
||||
hours_above: hours,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Default constraints. Every one is a real limit somebody sets on a real run.
|
||||
*
|
||||
* These are conservative rather than permissive: a scheduler that hands you a
|
||||
* target at airmass 3 next to a full Moon has not saved you anything.
|
||||
*/
|
||||
export const DEFAULT_CONSTRAINTS = {
|
||||
min_altitude_deg: 30,
|
||||
max_airmass: 2.0,
|
||||
min_moon_separation_deg: 30,
|
||||
// Below this the Moon's brightness stops mattering much even nearby.
|
||||
moon_illumination_floor: 0.25,
|
||||
// Which sky states the program can work in.
|
||||
allowed_twilight: ['night'],
|
||||
// Refuse a target within this of the meridian on a German equatorial, where
|
||||
// a flip mid-sequence ruins the run. Zero disables it.
|
||||
meridian_avoid_deg: 0,
|
||||
};
|
||||
|
||||
/**
|
||||
* Evaluate one target at one instant.
|
||||
*
|
||||
* Returns the geometry, whether every constraint is met, and a reason for each
|
||||
* one that is not. The reasons are the point: a scheduler whose decisions
|
||||
* cannot be explained gets treated as broken the first time it declines
|
||||
* something the observer wanted.
|
||||
*/
|
||||
export function evaluateTarget(target, site, when, constraints = {}) {
|
||||
const c = { ...DEFAULT_CONSTRAINTS, ...constraints };
|
||||
const jd = julianDate(when);
|
||||
const { ra_deg: ra, dec_deg: dec } = target;
|
||||
|
||||
if (!Number.isFinite(ra) || !Number.isFinite(dec)) {
|
||||
return { ok: false, reasons: ['target has no sky position'], geometry: null };
|
||||
}
|
||||
|
||||
const h = equatorialToHorizontal(ra, dec, site.latitude, site.longitude, jd);
|
||||
const sky = skyState(site.latitude, site.longitude, jd);
|
||||
const x = airmass(h.alt_deg);
|
||||
const moonSep = angularSeparation(ra, dec, sky.moon.ra_deg, sky.moon.dec_deg);
|
||||
const window = visibilityWindow(dec, site.latitude, c.min_altitude_deg, ra);
|
||||
|
||||
const reasons = [];
|
||||
if (h.alt_deg < c.min_altitude_deg) {
|
||||
reasons.push(`altitude ${h.alt_deg.toFixed(1)} is below the ${c.min_altitude_deg} limit`);
|
||||
}
|
||||
if (x === null) {
|
||||
reasons.push('below the horizon');
|
||||
} else if (x > c.max_airmass) {
|
||||
reasons.push(`airmass ${x.toFixed(2)} exceeds ${c.max_airmass}`);
|
||||
}
|
||||
// Moon separation only bites when the Moon is up AND bright enough to matter.
|
||||
// A full Moon below the horizon lights nothing.
|
||||
if (sky.moon_up && sky.moon.illuminated >= c.moon_illumination_floor
|
||||
&& moonSep < c.min_moon_separation_deg) {
|
||||
reasons.push(
|
||||
`${moonSep.toFixed(0)} from a Moon that is ${(sky.moon.illuminated * 100).toFixed(0)}% lit`
|
||||
+ ` (limit ${c.min_moon_separation_deg})`);
|
||||
}
|
||||
if (!c.allowed_twilight.includes(sky.twilight)) {
|
||||
reasons.push(sky.twilight === 'day' ? 'the Sun is up' : `sky is in ${sky.twilight} twilight`);
|
||||
}
|
||||
if (c.meridian_avoid_deg > 0 && Math.abs(h.ha_deg) < c.meridian_avoid_deg) {
|
||||
reasons.push(`within ${c.meridian_avoid_deg} of the meridian, where a pier flip would interrupt the run`);
|
||||
}
|
||||
if (window.never_rises) {
|
||||
reasons.push(`never reaches ${c.min_altitude_deg} from this site`);
|
||||
}
|
||||
|
||||
return {
|
||||
ok: reasons.length === 0,
|
||||
reasons,
|
||||
geometry: {
|
||||
alt_deg: h.alt_deg,
|
||||
az_deg: h.az_deg,
|
||||
ha_deg: h.ha_deg,
|
||||
airmass: x,
|
||||
moon_separation_deg: moonSep,
|
||||
moon_illuminated: sky.moon.illuminated,
|
||||
moon_up: sky.moon_up,
|
||||
twilight: sky.twilight,
|
||||
max_alt_deg: window.max_alt_deg,
|
||||
hours_above_limit: window.hours_above,
|
||||
circumpolar: window.circumpolar,
|
||||
},
|
||||
sky,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Score a target that already passes its constraints. Higher is better.
|
||||
*
|
||||
* Three terms, weighted so that none of them can dominate on its own:
|
||||
*
|
||||
* Airmass, because observing through less atmosphere is the single biggest
|
||||
* lever on data quality and it is the reason to prefer one target over
|
||||
* another that is equally valid.
|
||||
*
|
||||
* Setting urgency, because a target two hours from dropping below the limit
|
||||
* is a target you lose tonight if you do something else first. This is what
|
||||
* makes the difference between a schedule and a sorted list.
|
||||
*
|
||||
* Priority, as supplied by the caller from whatever allocation model it has.
|
||||
* Deliberately an input rather than a policy decided here.
|
||||
*/
|
||||
export function scoreTarget(evaluation, priority = 0.5) {
|
||||
const g = evaluation.geometry;
|
||||
if (!g || !evaluation.ok) return 0;
|
||||
|
||||
// 1.0 at the zenith, falling away as airmass climbs. Airmass 2 scores 0.5.
|
||||
const airmassScore = g.airmass ? Math.min(1, 1 / g.airmass) : 0;
|
||||
|
||||
// Rises as a target approaches the west. Hour angle is negative before
|
||||
// transit and positive after, so a setting target scores higher than one
|
||||
// that has all night to wait.
|
||||
const settingUrgency = Math.max(0, Math.min(1, (g.ha_deg + 90) / 180));
|
||||
|
||||
const p = Math.max(0, Math.min(1, priority));
|
||||
|
||||
const score = 0.45 * airmassScore + 0.25 * settingUrgency + 0.30 * p;
|
||||
return Math.round(score * 1000) / 1000;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rank a pool of candidates for one telescope at one instant.
|
||||
*
|
||||
* Everything comes back, not just the observable ones. A target that was
|
||||
* declined is the thing an operator most wants to understand, and hiding it
|
||||
* turns "why is my program not running" into a support question.
|
||||
*
|
||||
* @param {Array<{id?:string, name?:string, ra_deg:number, dec_deg:number,
|
||||
* priority?:number, constraints?:object}>} candidates
|
||||
* @param {{latitude:number, longitude:number}} site
|
||||
* @param {Date|string|number} when
|
||||
* @param {object} [constraints] defaults for candidates that carry none
|
||||
*/
|
||||
export function rankTargets(candidates, site, when, constraints = {}) {
|
||||
const rows = candidates.map(t => {
|
||||
const evaluation = evaluateTarget(t, site, when, { ...constraints, ...(t.constraints || {}) });
|
||||
return {
|
||||
...t,
|
||||
observable: evaluation.ok,
|
||||
score: scoreTarget(evaluation, t.priority ?? 0.5),
|
||||
reasons: evaluation.reasons,
|
||||
geometry: evaluation.geometry,
|
||||
};
|
||||
});
|
||||
|
||||
// Observable first, then by score. A declined target keeps its place in the
|
||||
// list rather than being dropped, so the tail reads as "and here is why not".
|
||||
return rows.sort((a, b) =>
|
||||
Number(b.observable) - Number(a.observable)
|
||||
|| b.score - a.score
|
||||
|| (a.name || '').localeCompare(b.name || ''));
|
||||
}
|
||||
@@ -0,0 +1,384 @@
|
||||
// Tests for the dispatch scheduler.
|
||||
//
|
||||
// The behaviours asserted here are the ones Denny's paper reports from its own
|
||||
// simulations, because that is what "replicating ACPS" has to mean. Weighting
|
||||
// a single term should reproduce the behaviour the paper attributes to it:
|
||||
// priority alone picks the most important target wherever it is, transit
|
||||
// altitude alone tracks the meridian, slew overhead alone walks the sky in
|
||||
// short hops. If our implementation does not show those, it is not the same
|
||||
// scheduler however plausible its output looks.
|
||||
//
|
||||
// Reference: Denny, R.B., "Dispatch Scheduling of Automated Telescopes",
|
||||
// Proc. Society for Astronomical Sciences 23, p.35 (2004).
|
||||
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import {
|
||||
normalizePriorities,
|
||||
efficiency,
|
||||
slewSeconds,
|
||||
moonAvoidanceDistance,
|
||||
moonAge,
|
||||
selectNext,
|
||||
explain,
|
||||
SKY_QUALITY,
|
||||
DEFAULT_WEIGHTS,
|
||||
} from '../src/services/dispatch.js';
|
||||
import { lmst, julianDate, precessToJ2000, skyState } from '../src/services/observability.js';
|
||||
|
||||
const SITE = { latitude: 37.3414, longitude: -121.6429 };
|
||||
const DARK = '2026-09-12T08:06:00Z'; // local midnight, astronomical night
|
||||
const JD = julianDate(DARK);
|
||||
|
||||
// A target at a chosen hour angle and declination, expressed in J2000 so the
|
||||
// engine's internal precession lands it where the test means.
|
||||
const at = (haDeg, decDeg) => {
|
||||
const p = precessToJ2000(((lmst(JD, SITE.longitude) - haDeg) % 360 + 360) % 360, decDeg, JD);
|
||||
return { ra_deg: p.ra_deg, dec_deg: p.dec_deg };
|
||||
};
|
||||
|
||||
const plan = (name, haDeg, decDeg, over = {}) => ({
|
||||
name, ...at(haDeg, decDeg), duration_s: 300, ...over,
|
||||
});
|
||||
|
||||
// Only the term under test, so a behaviour cannot be produced by another term.
|
||||
const only = key => ({
|
||||
priority: 0, transit: 0, slew: 0, retry: 0, meridian: 0, conditions: 0, [key]: 1,
|
||||
});
|
||||
|
||||
const BASE = { site: SITE, when: DARK, constraints: { min_moon_separation_deg: 0 } };
|
||||
|
||||
// ---------- priority normalisation ----------
|
||||
|
||||
test('each user\'s priorities are normalised to a mean of 0.5', () => {
|
||||
// The fairness mechanism. Without it, the user who types the biggest numbers
|
||||
// wins the telescope.
|
||||
const plans = [
|
||||
{ user_id: 'a', priority: 1 },
|
||||
{ user_id: 'a', priority: 5 },
|
||||
{ user_id: 'a', priority: 9 },
|
||||
];
|
||||
const n = normalizePriorities(plans);
|
||||
const values = plans.map(p => n.get(p));
|
||||
const mean = values.reduce((x, y) => x + y, 0) / values.length;
|
||||
assert.ok(Math.abs(mean - 0.5) < 1e-9, `mean was ${mean}`);
|
||||
});
|
||||
|
||||
test('inflating every priority gains a user nothing', () => {
|
||||
// The point of normalising per user: only the shape of a ranking survives,
|
||||
// never its magnitude.
|
||||
const modest = [{ user_id: 'a', priority: 1 }, { user_id: 'a', priority: 2 }];
|
||||
const shouty = [{ user_id: 'b', priority: 1000 }, { user_id: 'b', priority: 2000 }];
|
||||
const nm = normalizePriorities(modest);
|
||||
const ns = normalizePriorities(shouty);
|
||||
assert.ok(Math.abs(nm.get(modest[0]) - ns.get(shouty[0])) < 1e-9);
|
||||
assert.ok(Math.abs(nm.get(modest[1]) - ns.get(shouty[1])) < 1e-9);
|
||||
});
|
||||
|
||||
test('one user cannot drag another user\'s normalisation', () => {
|
||||
const plans = [
|
||||
{ user_id: 'a', priority: 5 },
|
||||
{ user_id: 'b', priority: 500 },
|
||||
{ user_id: 'b', priority: 500 },
|
||||
];
|
||||
const n = normalizePriorities(plans);
|
||||
assert.ok(Math.abs(n.get(plans[0]) - 0.5) < 1e-9, 'a lone plan sits at the mean');
|
||||
assert.ok(Math.abs(n.get(plans[1]) - 0.5) < 1e-9);
|
||||
});
|
||||
|
||||
test('a user who rates everything the same has expressed no preference', () => {
|
||||
const plans = [{ user_id: 'a', priority: 7 }, { user_id: 'a', priority: 7 }];
|
||||
const n = normalizePriorities(plans);
|
||||
assert.ok(plans.every(p => Math.abs(n.get(p) - 0.5) < 1e-9));
|
||||
});
|
||||
|
||||
test('a plan with no priority is still normalised rather than dropped', () => {
|
||||
const plans = [{ user_id: 'a' }, { user_id: 'a', priority: 3 }];
|
||||
const n = normalizePriorities(plans);
|
||||
assert.ok(plans.every(p => Number.isFinite(n.get(p))));
|
||||
});
|
||||
|
||||
// ---------- the efficiency terms ----------
|
||||
|
||||
test('nearness to transit does not favour targets near the observatory latitude', () => {
|
||||
// The specific correction Denny takes from Steele & Carter: altitude over 90
|
||||
// rewards a target for the accident of its declination. Altitude over its own
|
||||
// transit altitude asks whether it is as high as it will ever get.
|
||||
//
|
||||
// Both of these sit at their own transit, so a fair term scores them equally
|
||||
// even though one culminates at the zenith and the other at 52 degrees.
|
||||
const zenith = { geometry: { alt_deg: 90, max_alt_deg: 90 }, duration_s: 300 };
|
||||
const lower = { geometry: { alt_deg: 52, max_alt_deg: 52 }, duration_s: 300 };
|
||||
const w = { weights: only('transit') };
|
||||
assert.ok(Math.abs(efficiency(zenith, w).score - efficiency(lower, w).score) < 1e-9,
|
||||
'two targets both at transit should score the same');
|
||||
|
||||
// And a target well before its own transit scores below one at transit.
|
||||
const rising = { geometry: { alt_deg: 26, max_alt_deg: 52 }, duration_s: 300 };
|
||||
assert.ok(efficiency(rising, w).score < efficiency(lower, w).score);
|
||||
});
|
||||
|
||||
test('slew overhead is a fraction of observing time, not a distance penalty', () => {
|
||||
// Thirty seconds of slew is nothing before an hour of integration and
|
||||
// everything before a ten-second exposure.
|
||||
const from = { ra_deg: 0, dec_deg: 0 };
|
||||
const far = { ra_deg: 90, dec_deg: 0, geometry: { alt_deg: 45, max_alt_deg: 60 } };
|
||||
const w = only('slew');
|
||||
|
||||
const longExposure = efficiency({ ...far, duration_s: 3600 }, { from, weights: w });
|
||||
const shortExposure = efficiency({ ...far, duration_s: 10 }, { from, weights: w });
|
||||
|
||||
assert.ok(longExposure.score > 0.9, `a long exposure should barely notice: ${longExposure.score}`);
|
||||
assert.ok(shortExposure.score < 0.5, `a short one should be dominated by it: ${shortExposure.score}`);
|
||||
});
|
||||
|
||||
test('a German flip costs real time in the slew estimate', () => {
|
||||
const from = { ra_deg: 10, dec_deg: 20 };
|
||||
const to = { ra_deg: 20, dec_deg: 20 };
|
||||
assert.ok(slewSeconds(from, to, {}, true) - slewSeconds(from, to, {}, false) === 90);
|
||||
});
|
||||
|
||||
test('the conditions term rewards a close match, not an extravagant one', () => {
|
||||
// Excellent skies spent on a plan that only needed fair are excellent skies
|
||||
// wasted; if something needs them, it should get them.
|
||||
const needsExcellent = { geometry: { alt_deg: 60, max_alt_deg: 60 }, duration_s: 300, sky_quality_required: 'excellent' };
|
||||
const needsPoor = { geometry: { alt_deg: 60, max_alt_deg: 60 }, duration_s: 300, sky_quality_required: 'poor' };
|
||||
const ctx = { weights: only('conditions'), sky_quality_actual: 'excellent' };
|
||||
|
||||
assert.ok(efficiency(needsExcellent, ctx).score > efficiency(needsPoor, ctx).score);
|
||||
assert.equal(efficiency(needsExcellent, ctx).terms.E6, 1, 'an exact match scores 1');
|
||||
// Excellent(4) against poor(1) is three steps: 1/(3+1).
|
||||
assert.ok(Math.abs(efficiency(needsPoor, ctx).terms.E6 - 0.25) < 1e-9);
|
||||
});
|
||||
|
||||
test('the conditions term is inert when the sky has not been measured', () => {
|
||||
// Guessing a sky quality in order to have a number would make the scheduler
|
||||
// prefer plans on the basis of an invention.
|
||||
const c = { geometry: { alt_deg: 60, max_alt_deg: 60 }, duration_s: 300, sky_quality_required: 'good' };
|
||||
assert.equal(efficiency(c, { weights: only('conditions') }).terms.E6, 0);
|
||||
});
|
||||
|
||||
test('the retry boost exists but is deliberately small', () => {
|
||||
// The paper warns that a large retry weight can trap a failing plan in a
|
||||
// loop where it keeps winning the dispatch and keeps failing.
|
||||
assert.ok(DEFAULT_WEIGHTS.retry > 0);
|
||||
assert.ok(DEFAULT_WEIGHTS.retry < DEFAULT_WEIGHTS.slew / 5,
|
||||
'retry must not be able to outweigh the real terms');
|
||||
});
|
||||
|
||||
test('the published weights are the ones the paper reports as working', () => {
|
||||
// Denny reports priority 1.0, transit 0.8, slew 0.7 producing "quite
|
||||
// reasonable behavior and efficiency". Pinned so a later tweak is a
|
||||
// deliberate departure from the reference rather than a drift away from it.
|
||||
assert.equal(DEFAULT_WEIGHTS.priority, 1.0);
|
||||
assert.equal(DEFAULT_WEIGHTS.transit, 0.8);
|
||||
assert.equal(DEFAULT_WEIGHTS.slew, 0.7);
|
||||
assert.equal(DEFAULT_WEIGHTS.meridian, 0.0, 'off unless the mount actually flips');
|
||||
});
|
||||
|
||||
// ---------- the behaviours the paper measures ----------
|
||||
|
||||
test('priority-only dispatching picks the most important target wherever it is', () => {
|
||||
// Denny, figure 2: with only the priority term, the dispatcher picks the
|
||||
// highest-priority eligible plan and pays no attention to where the
|
||||
// telescope is pointing.
|
||||
const plans = [
|
||||
plan('near-and-dull', 5, 37, { user_id: 'u', priority: 1 }),
|
||||
plan('far-and-urgent', -40, 10, { user_id: 'u', priority: 9 }),
|
||||
];
|
||||
const out = selectNext(plans, {
|
||||
...BASE, weights: only('priority'), from: at(5, 37),
|
||||
});
|
||||
assert.equal(out.selected.name, 'far-and-urgent');
|
||||
});
|
||||
|
||||
test('transit-only dispatching tracks the meridian', () => {
|
||||
// Denny, figure 3: weighting only nearness to transit keeps acquisitions
|
||||
// clustered near hour angle zero.
|
||||
const plans = [
|
||||
plan('at-transit', 0, 37, { user_id: 'u', priority: 1 }),
|
||||
plan('well-east', -55, 37, { user_id: 'u', priority: 9 }),
|
||||
];
|
||||
const out = selectNext(plans, { ...BASE, weights: only('transit') });
|
||||
assert.equal(out.selected.name, 'at-transit',
|
||||
'nearness to transit should beat a nine-times-higher priority when priority is unweighted');
|
||||
assert.ok(Math.abs(out.selected.geometry.ha_deg) < 1);
|
||||
});
|
||||
|
||||
test('slew-only dispatching picks the nearest target', () => {
|
||||
// Denny, figure 4: weighting only slew overhead makes the dispatcher walk
|
||||
// the sky in short hops.
|
||||
const here = at(0, 37);
|
||||
const plans = [
|
||||
plan('next-door', 3, 37, { user_id: 'u', priority: 1 }),
|
||||
plan('across-the-sky', -60, 0, { user_id: 'u', priority: 9 }),
|
||||
];
|
||||
const out = selectNext(plans, { ...BASE, weights: only('slew'), from: here });
|
||||
assert.equal(out.selected.name, 'next-door');
|
||||
});
|
||||
|
||||
test('with the published weights, priority leads and slew only breaks ties', () => {
|
||||
// Denny describes the combined run as paying "the most attention to
|
||||
// scientific priority, followed by nearness to the meridian and slewing
|
||||
// overhead, in that order". So a real priority gap SHOULD carry a slew, and
|
||||
// the test asserts that ordering rather than a balance the reference does
|
||||
// not claim.
|
||||
const here = at(0, 37);
|
||||
const byPriority = selectNext([
|
||||
plan('near-low-priority', 4, 37, { user_id: 'u', priority: 1 }),
|
||||
plan('far-high-priority', -25, 30, { user_id: 'u', priority: 9 }),
|
||||
], { ...BASE, from: here });
|
||||
assert.equal(byPriority.selected.name, 'far-high-priority',
|
||||
'a nine-fold priority edge should carry a 25-degree slew');
|
||||
|
||||
// Equal priority, so the remaining terms decide: the nearer target, closer
|
||||
// to its own transit, wins.
|
||||
const tie = selectNext([
|
||||
plan('near', 4, 37, { user_id: 'u', priority: 5 }),
|
||||
plan('far', -90, 75, { user_id: 'u', priority: 5 }),
|
||||
], { ...BASE, from: here });
|
||||
assert.equal(tie.selected.name, 'near',
|
||||
'at equal priority the cheaper, better-placed target should win');
|
||||
});
|
||||
|
||||
test('slew overhead can only overturn priority when it is weighted to', () => {
|
||||
// The knob exists: an observatory that cares more about shutter-open time
|
||||
// than about whose program runs can say so, and the same pool then resolves
|
||||
// the other way. This is the beta-coefficient adjustment the paper describes.
|
||||
const here = at(0, 37);
|
||||
const plans = [
|
||||
// 54 degrees apart, and both comfortably above the altitude limit so the
|
||||
// contest is decided by the weights rather than by eligibility.
|
||||
plan('near-low-priority', 4, 37, { user_id: 'u', priority: 1, duration_s: 5 }),
|
||||
plan('far-high-priority', -90, 75, { user_id: 'u', priority: 9, duration_s: 5 }),
|
||||
];
|
||||
const asPublished = selectNext(plans, { ...BASE, from: here });
|
||||
const slewHeavy = selectNext(plans, {
|
||||
...BASE, from: here, weights: { priority: 0.2, transit: 0.2, slew: 2.0 },
|
||||
});
|
||||
assert.equal(asPublished.selected.name, 'far-high-priority');
|
||||
assert.equal(slewHeavy.selected.name, 'near-low-priority');
|
||||
});
|
||||
|
||||
// ---------- the scheduling rules ----------
|
||||
|
||||
test('a running plan is not abandoned for a better unstarted one', () => {
|
||||
// ACPS rule: a higher-priority unstarted plan can never force a running plan
|
||||
// to fail. Without it, a busy night is a sequence of interrupted halves.
|
||||
const plans = [
|
||||
plan('under-way', -20, 37, { user_id: 'u', priority: 1, state: 'running' }),
|
||||
plan('tempting', 0, 37, { user_id: 'u', priority: 9 }),
|
||||
];
|
||||
const out = selectNext(plans, BASE);
|
||||
assert.equal(out.selected.name, 'under-way');
|
||||
assert.match(out.reason, /already under way/);
|
||||
});
|
||||
|
||||
test('a running plan that is no longer eligible does not block the queue', () => {
|
||||
// The rule protects a plan that can still run. One whose target has set is
|
||||
// finished whatever its state says, and holding the telescope for it would
|
||||
// waste the rest of the night.
|
||||
const plans = [
|
||||
plan('set-already', 0, -75, { user_id: 'u', priority: 9, state: 'running' }),
|
||||
plan('still-good', 0, 37, { user_id: 'u', priority: 1 }),
|
||||
];
|
||||
const out = selectNext(plans, BASE);
|
||||
assert.equal(out.selected.name, 'still-good');
|
||||
});
|
||||
|
||||
test('nothing eligible returns nothing, with every reason kept', () => {
|
||||
const plans = [
|
||||
plan('too-far-south', 0, -80, { user_id: 'u', priority: 5 }),
|
||||
plan('also-impossible', 0, -85, { user_id: 'u', priority: 5 }),
|
||||
];
|
||||
const out = selectNext(plans, BASE);
|
||||
assert.equal(out.selected, null);
|
||||
assert.match(out.reason, /nothing is eligible/);
|
||||
assert.equal(out.considered.length, 2, 'declined plans stay visible');
|
||||
assert.ok(out.considered.every(c => c.reasons.length > 0), 'and each says why');
|
||||
});
|
||||
|
||||
test('an empty pool is reported as empty rather than as nothing eligible', () => {
|
||||
const out = selectNext([], BASE);
|
||||
assert.equal(out.selected, null);
|
||||
assert.match(out.reason, /no candidate plans/);
|
||||
});
|
||||
|
||||
test('every considered plan carries the breakdown that produced its score', () => {
|
||||
// A scheduler whose choices cannot be explained gets overridden by hand,
|
||||
// which is the thing it exists to stop.
|
||||
const plans = [plan('a', 0, 37, { user_id: 'u', priority: 5 })];
|
||||
const out = selectNext(plans, BASE);
|
||||
assert.ok(out.selected.terms, 'terms should be attached');
|
||||
const text = explain(out.selected);
|
||||
assert.match(text, /priority/);
|
||||
assert.match(text, /nearness to transit/);
|
||||
assert.match(text, /slew overhead/);
|
||||
});
|
||||
|
||||
test('explain falls back to the refusal reasons for an ineligible plan', () => {
|
||||
const plans = [plan('nope', 0, -80, { user_id: 'u', priority: 5 })];
|
||||
const out = selectNext(plans, BASE);
|
||||
assert.match(explain(out.considered[0]), /never reaches/);
|
||||
});
|
||||
|
||||
// ---------- moon avoidance ----------
|
||||
|
||||
test('required moon distance is greatest at full and relaxes toward new', () => {
|
||||
// ACP's Lorentzian. A flat separation is either too strict for three weeks a
|
||||
// month or too lax for one.
|
||||
const full = moonAvoidanceDistance(60, 29.53 / 2);
|
||||
const crescent = moonAvoidanceDistance(60, 3);
|
||||
const nearNew = moonAvoidanceDistance(60, 0);
|
||||
assert.ok(Math.abs(full - 60) < 1e-9, `full moon should demand the full distance, got ${full}`);
|
||||
assert.ok(crescent < full, `${crescent} should be less than ${full}`);
|
||||
assert.ok(nearNew < crescent);
|
||||
assert.ok(nearNew > 40, 'but it should still ask for something');
|
||||
});
|
||||
|
||||
test('moon avoidance with no base distance asks for nothing', () => {
|
||||
assert.equal(moonAvoidanceDistance(0, 14), 0);
|
||||
assert.equal(moonAvoidanceDistance(NaN, 14), 0);
|
||||
});
|
||||
|
||||
test('moon age tracks the real phase rather than a calendar', () => {
|
||||
// Sampled across two months: age should stay in range and pass through both
|
||||
// new and full.
|
||||
let sawNew = false, sawFull = false;
|
||||
for (let d = 0; d < 60; d++) {
|
||||
const age = moonAge(julianDate(new Date(Date.UTC(2026, 8, 1) + d * 86400_000)));
|
||||
assert.ok(age >= 0 && age <= 29.54, `age out of range: ${age}`);
|
||||
if (age < 1.5 || age > 28) sawNew = true;
|
||||
if (Math.abs(age - 14.76) < 1.5) sawFull = true;
|
||||
}
|
||||
assert.ok(sawNew && sawFull, 'two months should contain both a new and a full moon');
|
||||
});
|
||||
|
||||
test('a plan asking for moon avoidance is refused near a bright Moon and says the number', () => {
|
||||
// Find a dark hour with a bright Moon well up, then point straight at it.
|
||||
let found = null;
|
||||
for (let h = 0; h < 24 * 60; h += 2) {
|
||||
const when = new Date(Date.UTC(2026, 8, 1) + h * 3600_000);
|
||||
const sky = skyState(SITE.latitude, SITE.longitude, julianDate(when));
|
||||
if (sky.dark && sky.moon_up && sky.moon.alt_deg > 40 && sky.moon.illuminated > 0.8) {
|
||||
found = { when, sky }; break;
|
||||
}
|
||||
}
|
||||
assert.ok(found, 'no bright high Moon in a dark sky over two months');
|
||||
|
||||
const out = selectNext(
|
||||
[{ name: 'on-the-moon', ra_deg: found.sky.moon.ra_deg, dec_deg: found.sky.moon.dec_deg,
|
||||
user_id: 'u', priority: 9, duration_s: 300, moon_avoid_deg: 60 }],
|
||||
{ site: SITE, when: found.when, constraints: { min_moon_separation_deg: 0 } },
|
||||
);
|
||||
assert.equal(out.selected, null);
|
||||
assert.ok(out.considered[0].reasons.some(r => /Moon/.test(r)), out.considered[0].reasons.join('; '));
|
||||
});
|
||||
|
||||
// ---------- the scale itself ----------
|
||||
|
||||
test('sky quality is ordered the way the scheduler compares it', () => {
|
||||
assert.ok(SKY_QUALITY.excellent > SKY_QUALITY.good);
|
||||
assert.ok(SKY_QUALITY.good > SKY_QUALITY.fair);
|
||||
assert.ok(SKY_QUALITY.fair > SKY_QUALITY.poor);
|
||||
});
|
||||
+592
@@ -0,0 +1,592 @@
|
||||
// Reference positions from astropy, generated once and checked in.
|
||||
//
|
||||
// Produced with astropy 8.0.1 (get_sun, get_body('moon'), AltAz) at the
|
||||
// three sites and four epochs below. The point of testing against these
|
||||
// rather than against values this repo computed is that astropy is an
|
||||
// independent implementation of an independent ephemeris: agreement means
|
||||
// the JS is right for a reason rather than right by construction.
|
||||
//
|
||||
// Sites span the equator, mid-northern and mid-southern latitudes; epochs
|
||||
// span an equinox and both solstices, so a sign error in obliquity or in
|
||||
// latitude cannot hide in a single hemisphere or season.
|
||||
|
||||
export const EPHEMERIS_REFERENCE = {
|
||||
"altaz": [
|
||||
{
|
||||
"utc": "2026-09-12T06:00:00",
|
||||
"site": "lick",
|
||||
"target": "M31",
|
||||
"lat": 37.3414,
|
||||
"lon": -121.6429,
|
||||
"ra_deg": 10.6847,
|
||||
"dec_deg": 41.2687,
|
||||
"alt_deg": 50.61681959058603,
|
||||
"az_deg": 67.5423596065276
|
||||
},
|
||||
{
|
||||
"utc": "2026-09-12T06:00:00",
|
||||
"site": "lick",
|
||||
"target": "M42",
|
||||
"lat": 37.3414,
|
||||
"lon": -121.6429,
|
||||
"ra_deg": 83.8221,
|
||||
"dec_deg": -5.3911,
|
||||
"alt_deg": -30.3618613145693,
|
||||
"az_deg": 71.91095121918342
|
||||
},
|
||||
{
|
||||
"utc": "2026-09-12T06:00:00",
|
||||
"site": "lick",
|
||||
"target": "Polaris",
|
||||
"lat": 37.3414,
|
||||
"lon": -121.6429,
|
||||
"ra_deg": 37.9545,
|
||||
"dec_deg": 89.2641,
|
||||
"alt_deg": 37.36855269356554,
|
||||
"az_deg": 0.7891601258772198
|
||||
},
|
||||
{
|
||||
"utc": "2026-09-12T06:00:00",
|
||||
"site": "lick",
|
||||
"target": "Sirius",
|
||||
"lat": 37.3414,
|
||||
"lon": -121.6429,
|
||||
"ra_deg": 101.2872,
|
||||
"dec_deg": -16.7161,
|
||||
"alt_deg": -50.747140167228615,
|
||||
"az_deg": 68.83009219740956
|
||||
},
|
||||
{
|
||||
"utc": "2026-09-12T06:00:00",
|
||||
"site": "sso",
|
||||
"target": "M31",
|
||||
"lat": -31.2733,
|
||||
"lon": 149.0644,
|
||||
"ra_deg": 10.6847,
|
||||
"dec_deg": 41.2687,
|
||||
"alt_deg": -57.100387053337506,
|
||||
"az_deg": 60.916938991989376
|
||||
},
|
||||
{
|
||||
"utc": "2026-09-12T06:00:00",
|
||||
"site": "sso",
|
||||
"target": "M42",
|
||||
"lat": -31.2733,
|
||||
"lon": 149.0644,
|
||||
"ra_deg": 83.8221,
|
||||
"dec_deg": -5.3911,
|
||||
"alt_deg": -41.173639909685456,
|
||||
"az_deg": 227.41362133707125
|
||||
},
|
||||
{
|
||||
"utc": "2026-09-12T06:00:00",
|
||||
"site": "sso",
|
||||
"target": "Polaris",
|
||||
"lat": -31.2733,
|
||||
"lon": 149.0644,
|
||||
"ra_deg": 37.9545,
|
||||
"dec_deg": 89.2641,
|
||||
"alt_deg": -31.899982383517397,
|
||||
"az_deg": 0.0442740408838567
|
||||
},
|
||||
{
|
||||
"utc": "2026-09-12T06:00:00",
|
||||
"site": "sso",
|
||||
"target": "Sirius",
|
||||
"lat": -31.2733,
|
||||
"lon": 149.0644,
|
||||
"ra_deg": 101.2872,
|
||||
"dec_deg": -16.7161,
|
||||
"alt_deg": -21.266543629343715,
|
||||
"az_deg": 233.2721504224733
|
||||
},
|
||||
{
|
||||
"utc": "2026-09-12T06:00:00",
|
||||
"site": "equator",
|
||||
"target": "M31",
|
||||
"lat": 0.0,
|
||||
"lon": 0.0,
|
||||
"ra_deg": 10.6847,
|
||||
"dec_deg": 41.2687,
|
||||
"alt_deg": 14.711835276360208,
|
||||
"az_deg": 313.153089911199
|
||||
},
|
||||
{
|
||||
"utc": "2026-09-12T06:00:00",
|
||||
"site": "equator",
|
||||
"target": "M42",
|
||||
"lat": 0.0,
|
||||
"lon": 0.0,
|
||||
"ra_deg": 83.8221,
|
||||
"dec_deg": -5.3911,
|
||||
"alt_deg": 83.90420236493968,
|
||||
"az_deg": 151.80936325884255
|
||||
},
|
||||
{
|
||||
"utc": "2026-09-12T06:00:00",
|
||||
"site": "equator",
|
||||
"target": "Polaris",
|
||||
"lat": 0.0,
|
||||
"lon": 0.0,
|
||||
"ra_deg": 37.9545,
|
||||
"dec_deg": 89.2641,
|
||||
"alt_deg": 0.5182532902651108,
|
||||
"az_deg": 359.64573817186005
|
||||
},
|
||||
{
|
||||
"utc": "2026-09-12T06:00:00",
|
||||
"site": "equator",
|
||||
"target": "Sirius",
|
||||
"lat": 0.0,
|
||||
"lon": 0.0,
|
||||
"ra_deg": 101.2872,
|
||||
"dec_deg": -16.7161,
|
||||
"alt_deg": 63.89882304614102,
|
||||
"az_deg": 130.89489251389776
|
||||
},
|
||||
{
|
||||
"utc": "2026-03-20T22:00:00",
|
||||
"site": "lick",
|
||||
"target": "M31",
|
||||
"lat": 37.3414,
|
||||
"lon": -121.6429,
|
||||
"ra_deg": 10.6847,
|
||||
"dec_deg": 41.2687,
|
||||
"alt_deg": 77.1731441623143,
|
||||
"az_deg": 293.381161839373
|
||||
},
|
||||
{
|
||||
"utc": "2026-03-20T22:00:00",
|
||||
"site": "lick",
|
||||
"target": "M42",
|
||||
"lat": 37.3414,
|
||||
"lon": -121.6429,
|
||||
"ra_deg": 83.8221,
|
||||
"dec_deg": -5.3911,
|
||||
"alt_deg": 21.734483800795886,
|
||||
"az_deg": 115.53155592227188
|
||||
},
|
||||
{
|
||||
"utc": "2026-03-20T22:00:00",
|
||||
"site": "lick",
|
||||
"target": "Polaris",
|
||||
"lat": 37.3414,
|
||||
"lon": -121.6429,
|
||||
"ra_deg": 37.9545,
|
||||
"dec_deg": 89.2641,
|
||||
"alt_deg": 37.927755428444335,
|
||||
"az_deg": 0.260577846527662
|
||||
},
|
||||
{
|
||||
"utc": "2026-03-20T22:00:00",
|
||||
"site": "lick",
|
||||
"target": "Sirius",
|
||||
"lat": 37.3414,
|
||||
"lon": -121.6429,
|
||||
"ra_deg": 101.2872,
|
||||
"dec_deg": -16.7161,
|
||||
"alt_deg": 1.4387657657635142,
|
||||
"az_deg": 112.43879903087802
|
||||
},
|
||||
{
|
||||
"utc": "2026-03-20T22:00:00",
|
||||
"site": "sso",
|
||||
"target": "M31",
|
||||
"lat": -31.2733,
|
||||
"lon": 149.0644,
|
||||
"ra_deg": 10.6847,
|
||||
"dec_deg": 41.2687,
|
||||
"alt_deg": -9.300534600380464,
|
||||
"az_deg": 46.783072340187324
|
||||
},
|
||||
{
|
||||
"utc": "2026-03-20T22:00:00",
|
||||
"site": "sso",
|
||||
"target": "M42",
|
||||
"lat": -31.2733,
|
||||
"lon": 149.0644,
|
||||
"ra_deg": 83.8221,
|
||||
"dec_deg": -5.3911,
|
||||
"alt_deg": -41.45629441198274,
|
||||
"az_deg": 133.06261103831153
|
||||
},
|
||||
{
|
||||
"utc": "2026-03-20T22:00:00",
|
||||
"site": "sso",
|
||||
"target": "Polaris",
|
||||
"lat": -31.2733,
|
||||
"lon": 149.0644,
|
||||
"ra_deg": 37.9545,
|
||||
"dec_deg": 89.2641,
|
||||
"alt_deg": -31.46966799861525,
|
||||
"az_deg": 0.6908186318536773
|
||||
},
|
||||
{
|
||||
"utc": "2026-03-20T22:00:00",
|
||||
"site": "sso",
|
||||
"target": "Sirius",
|
||||
"lat": -31.2733,
|
||||
"lon": 149.0644,
|
||||
"ra_deg": 101.2872,
|
||||
"dec_deg": -16.7161,
|
||||
"alt_deg": -39.60189463276872,
|
||||
"az_deg": 160.0572193503832
|
||||
},
|
||||
{
|
||||
"utc": "2026-03-20T22:00:00",
|
||||
"site": "equator",
|
||||
"target": "M31",
|
||||
"lat": 0.0,
|
||||
"lon": 0.0,
|
||||
"ra_deg": 10.6847,
|
||||
"dec_deg": 41.2687,
|
||||
"alt_deg": -33.51345240219461,
|
||||
"az_deg": 322.5008816198866
|
||||
},
|
||||
{
|
||||
"utc": "2026-03-20T22:00:00",
|
||||
"site": "equator",
|
||||
"target": "M42",
|
||||
"lat": 0.0,
|
||||
"lon": 0.0,
|
||||
"ra_deg": 83.8221,
|
||||
"dec_deg": -5.3911,
|
||||
"alt_deg": 25.577534332150382,
|
||||
"az_deg": 264.03781013478385
|
||||
},
|
||||
{
|
||||
"utc": "2026-03-20T22:00:00",
|
||||
"site": "equator",
|
||||
"target": "Polaris",
|
||||
"lat": 0.0,
|
||||
"lon": 0.0,
|
||||
"ra_deg": 37.9545,
|
||||
"dec_deg": 89.2641,
|
||||
"alt_deg": -0.13283667652152187,
|
||||
"az_deg": 359.3928649671724
|
||||
},
|
||||
{
|
||||
"utc": "2026-03-20T22:00:00",
|
||||
"site": "equator",
|
||||
"target": "Sirius",
|
||||
"lat": 0.0,
|
||||
"lon": 0.0,
|
||||
"ra_deg": 101.2872,
|
||||
"dec_deg": -16.7161,
|
||||
"alt_deg": 40.8994511954542,
|
||||
"az_deg": 247.59168507365803
|
||||
},
|
||||
{
|
||||
"utc": "2026-06-21T12:00:00",
|
||||
"site": "lick",
|
||||
"target": "M31",
|
||||
"lat": 37.3414,
|
||||
"lon": -121.6429,
|
||||
"ra_deg": 10.6847,
|
||||
"dec_deg": 41.2687,
|
||||
"alt_deg": 56.863885994921915,
|
||||
"az_deg": 69.31166999785802
|
||||
},
|
||||
{
|
||||
"utc": "2026-06-21T12:00:00",
|
||||
"site": "lick",
|
||||
"target": "M42",
|
||||
"lat": 37.3414,
|
||||
"lon": -121.6429,
|
||||
"ra_deg": 83.8221,
|
||||
"dec_deg": -5.3911,
|
||||
"alt_deg": -23.88172277397393,
|
||||
"az_deg": 77.93754999564705
|
||||
},
|
||||
{
|
||||
"utc": "2026-06-21T12:00:00",
|
||||
"site": "lick",
|
||||
"target": "Polaris",
|
||||
"lat": 37.3414,
|
||||
"lon": -121.6429,
|
||||
"ra_deg": 37.9545,
|
||||
"dec_deg": 89.2641,
|
||||
"alt_deg": 37.46812647019812,
|
||||
"az_deg": 0.774982665439661
|
||||
},
|
||||
{
|
||||
"utc": "2026-06-21T12:00:00",
|
||||
"site": "lick",
|
||||
"target": "Sirius",
|
||||
"lat": 37.3414,
|
||||
"lon": -121.6429,
|
||||
"ra_deg": 101.2872,
|
||||
"dec_deg": -16.7161,
|
||||
"alt_deg": -44.34722894454591,
|
||||
"az_deg": 76.17069243200527
|
||||
},
|
||||
{
|
||||
"utc": "2026-06-21T12:00:00",
|
||||
"site": "sso",
|
||||
"target": "M31",
|
||||
"lat": -31.2733,
|
||||
"lon": 149.0644,
|
||||
"ra_deg": 10.6847,
|
||||
"dec_deg": 41.2687,
|
||||
"alt_deg": -50.771479709979054,
|
||||
"az_deg": 61.329661669927205
|
||||
},
|
||||
{
|
||||
"utc": "2026-06-21T12:00:00",
|
||||
"site": "sso",
|
||||
"target": "M42",
|
||||
"lat": -31.2733,
|
||||
"lon": 149.0644,
|
||||
"ra_deg": 83.8221,
|
||||
"dec_deg": -5.3911,
|
||||
"alt_deg": -46.07222777733269,
|
||||
"az_deg": 217.95678272999712
|
||||
},
|
||||
{
|
||||
"utc": "2026-06-21T12:00:00",
|
||||
"site": "sso",
|
||||
"target": "Polaris",
|
||||
"lat": -31.2733,
|
||||
"lon": 149.0644,
|
||||
"ra_deg": 37.9545,
|
||||
"dec_deg": 89.2641,
|
||||
"alt_deg": -31.88657725823005,
|
||||
"az_deg": 0.161239662682061
|
||||
},
|
||||
{
|
||||
"utc": "2026-06-21T12:00:00",
|
||||
"site": "sso",
|
||||
"target": "Sirius",
|
||||
"lat": -31.2733,
|
||||
"lon": 149.0644,
|
||||
"ra_deg": 101.2872,
|
||||
"dec_deg": -16.7161,
|
||||
"alt_deg": -26.79827686670221,
|
||||
"az_deg": 226.81098058507678
|
||||
},
|
||||
{
|
||||
"utc": "2026-06-21T12:00:00",
|
||||
"site": "equator",
|
||||
"target": "M31",
|
||||
"lat": 0.0,
|
||||
"lon": 0.0,
|
||||
"ra_deg": 10.6847,
|
||||
"dec_deg": 41.2687,
|
||||
"alt_deg": 8.484633921854671,
|
||||
"az_deg": 311.97257145117226
|
||||
},
|
||||
{
|
||||
"utc": "2026-06-21T12:00:00",
|
||||
"site": "equator",
|
||||
"target": "M42",
|
||||
"lat": 0.0,
|
||||
"lon": 0.0,
|
||||
"ra_deg": 83.8221,
|
||||
"dec_deg": -5.3911,
|
||||
"alt_deg": 82.27538529198175,
|
||||
"az_deg": 225.8357082285806
|
||||
},
|
||||
{
|
||||
"utc": "2026-06-21T12:00:00",
|
||||
"site": "equator",
|
||||
"target": "Polaris",
|
||||
"lat": 0.0,
|
||||
"lon": 0.0,
|
||||
"ra_deg": 37.9545,
|
||||
"dec_deg": 89.2641,
|
||||
"alt_deg": 0.4557748277932259,
|
||||
"az_deg": 359.5673888824986
|
||||
},
|
||||
{
|
||||
"utc": "2026-06-21T12:00:00",
|
||||
"site": "equator",
|
||||
"target": "Sirius",
|
||||
"lat": 0.0,
|
||||
"lon": 0.0,
|
||||
"ra_deg": 101.2872,
|
||||
"dec_deg": -16.7161,
|
||||
"alt_deg": 69.57015718508002,
|
||||
"az_deg": 145.62252210150538
|
||||
},
|
||||
{
|
||||
"utc": "2026-12-21T02:00:00",
|
||||
"site": "lick",
|
||||
"target": "M31",
|
||||
"lat": 37.3414,
|
||||
"lon": -121.6429,
|
||||
"ra_deg": 10.6847,
|
||||
"dec_deg": 41.2687,
|
||||
"alt_deg": 79.14303743725532,
|
||||
"az_deg": 63.89900640962284
|
||||
},
|
||||
{
|
||||
"utc": "2026-12-21T02:00:00",
|
||||
"site": "lick",
|
||||
"target": "M42",
|
||||
"lat": 37.3414,
|
||||
"lon": -121.6429,
|
||||
"ra_deg": 83.8221,
|
||||
"dec_deg": -5.3911,
|
||||
"alt_deg": -0.20188569659511046,
|
||||
"az_deg": 96.60956977908579
|
||||
},
|
||||
{
|
||||
"utc": "2026-12-21T02:00:00",
|
||||
"site": "lick",
|
||||
"target": "Polaris",
|
||||
"lat": 37.3414,
|
||||
"lon": -121.6429,
|
||||
"ra_deg": 37.9545,
|
||||
"dec_deg": 89.2641,
|
||||
"alt_deg": 37.7445999006426,
|
||||
"az_deg": 0.5921227413731348
|
||||
},
|
||||
{
|
||||
"utc": "2026-12-21T02:00:00",
|
||||
"site": "lick",
|
||||
"target": "Sirius",
|
||||
"lat": 37.3414,
|
||||
"lon": -121.6429,
|
||||
"ra_deg": 101.2872,
|
||||
"dec_deg": -16.7161,
|
||||
"alt_deg": -20.698260875869202,
|
||||
"az_deg": 95.6876747444762
|
||||
},
|
||||
{
|
||||
"utc": "2026-12-21T02:00:00",
|
||||
"site": "sso",
|
||||
"target": "M31",
|
||||
"lat": -31.2733,
|
||||
"lon": 149.0644,
|
||||
"ra_deg": 10.6847,
|
||||
"dec_deg": 41.2687,
|
||||
"alt_deg": -28.70477491070361,
|
||||
"az_deg": 56.63827529902212
|
||||
},
|
||||
{
|
||||
"utc": "2026-12-21T02:00:00",
|
||||
"site": "sso",
|
||||
"target": "M42",
|
||||
"lat": -31.2733,
|
||||
"lon": 149.0644,
|
||||
"ra_deg": 83.8221,
|
||||
"dec_deg": -5.3911,
|
||||
"alt_deg": -53.094636338105474,
|
||||
"az_deg": 172.41398307224577
|
||||
},
|
||||
{
|
||||
"utc": "2026-12-21T02:00:00",
|
||||
"site": "sso",
|
||||
"target": "Polaris",
|
||||
"lat": -31.2733,
|
||||
"lon": 149.0644,
|
||||
"ra_deg": 37.9545,
|
||||
"dec_deg": 89.2641,
|
||||
"alt_deg": -31.73551679569327,
|
||||
"az_deg": 0.4826575043832016
|
||||
},
|
||||
{
|
||||
"utc": "2026-12-21T02:00:00",
|
||||
"site": "sso",
|
||||
"target": "Sirius",
|
||||
"lat": -31.2733,
|
||||
"lon": 149.0644,
|
||||
"ra_deg": 101.2872,
|
||||
"dec_deg": -16.7161,
|
||||
"alt_deg": -40.417517773541036,
|
||||
"az_deg": 196.26682989061464
|
||||
},
|
||||
{
|
||||
"utc": "2026-12-21T02:00:00",
|
||||
"site": "equator",
|
||||
"target": "M31",
|
||||
"lat": 0.0,
|
||||
"lon": 0.0,
|
||||
"ra_deg": 10.6847,
|
||||
"dec_deg": 41.2687,
|
||||
"alt_deg": -13.84269826463323,
|
||||
"az_deg": 312.95159532815853
|
||||
},
|
||||
{
|
||||
"utc": "2026-12-21T02:00:00",
|
||||
"site": "equator",
|
||||
"target": "M42",
|
||||
"lat": 0.0,
|
||||
"lon": 0.0,
|
||||
"ra_deg": 83.8221,
|
||||
"dec_deg": -5.3911,
|
||||
"alt_deg": 54.14613647344166,
|
||||
"az_deg": 260.79977065405205
|
||||
},
|
||||
{
|
||||
"utc": "2026-12-21T02:00:00",
|
||||
"site": "equator",
|
||||
"target": "Polaris",
|
||||
"lat": 0.0,
|
||||
"lon": 0.0,
|
||||
"ra_deg": 37.9545,
|
||||
"dec_deg": 89.2641,
|
||||
"alt_deg": 0.1862480066600302,
|
||||
"az_deg": 359.40997347516014
|
||||
},
|
||||
{
|
||||
"utc": "2026-12-21T02:00:00",
|
||||
"site": "equator",
|
||||
"target": "Sirius",
|
||||
"lat": 0.0,
|
||||
"lon": 0.0,
|
||||
"ra_deg": 101.2872,
|
||||
"dec_deg": -16.7161,
|
||||
"alt_deg": 65.55839391585603,
|
||||
"az_deg": 225.87105523923083
|
||||
}
|
||||
],
|
||||
"sun": [
|
||||
{
|
||||
"utc": "2026-09-12T06:00:00",
|
||||
"ra_deg": 170.0103089896664,
|
||||
"dec_deg": 4.300618979032772
|
||||
},
|
||||
{
|
||||
"utc": "2026-03-20T22:00:00",
|
||||
"ra_deg": 359.9373537245381,
|
||||
"dec_deg": -0.02738051928255806
|
||||
},
|
||||
{
|
||||
"utc": "2026-06-21T12:00:00",
|
||||
"ra_deg": 89.75028647153088,
|
||||
"dec_deg": 23.435591993770117
|
||||
},
|
||||
{
|
||||
"utc": "2026-12-21T02:00:00",
|
||||
"ra_deg": 268.7156682922299,
|
||||
"dec_deg": -23.430291647145417
|
||||
}
|
||||
],
|
||||
"moon": [
|
||||
{
|
||||
"utc": "2026-09-12T06:00:00",
|
||||
"ra_deg": 181.65128152461443,
|
||||
"dec_deg": -3.88960994792835,
|
||||
"elongation_deg": 14.225306586819501
|
||||
},
|
||||
{
|
||||
"utc": "2026-03-20T22:00:00",
|
||||
"ra_deg": 21.250025667843975,
|
||||
"dec_deg": 12.986931352168254,
|
||||
"elongation_deg": 24.814128460291194
|
||||
},
|
||||
{
|
||||
"utc": "2026-06-21T12:00:00",
|
||||
"ra_deg": 174.46840690919197,
|
||||
"dec_deg": 0.19693671870532337,
|
||||
"elongation_deg": 85.07634150970613
|
||||
},
|
||||
{
|
||||
"utc": "2026-12-21T02:00:00",
|
||||
"ra_deg": 43.07902042665397,
|
||||
"dec_deg": 21.815324789481807,
|
||||
"elongation_deg": 138.01978364333976
|
||||
}
|
||||
]
|
||||
};
|
||||
@@ -0,0 +1,486 @@
|
||||
// Tests for the observability engine.
|
||||
//
|
||||
// Two kinds of check, and both are needed.
|
||||
//
|
||||
// Against astropy, for the ephemeris. Hand-rolled astronomy that agrees only
|
||||
// with itself is how an archive fills with confidently wrong numbers, so every
|
||||
// position here is compared to an independent implementation of an independent
|
||||
// ephemeris at three sites and four epochs. See test/fixtures.
|
||||
//
|
||||
// Against geometry, for the parts where the right answer is certain from first
|
||||
// principles: a target at the celestial pole sits at the observer's latitude,
|
||||
// an object transiting at the observer's declination passes through the zenith,
|
||||
// airmass at the zenith is exactly 1. Those cannot drift no matter what the
|
||||
// ephemeris does, and they catch the class of error where everything is shifted
|
||||
// by a constant.
|
||||
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import {
|
||||
julianDate,
|
||||
gmst,
|
||||
lmst,
|
||||
equatorialToHorizontal,
|
||||
airmass,
|
||||
sunPosition,
|
||||
moonPosition,
|
||||
moonIllumination,
|
||||
angularSeparation,
|
||||
skyState,
|
||||
visibilityWindow,
|
||||
evaluateTarget,
|
||||
scoreTarget,
|
||||
rankTargets,
|
||||
precessToJ2000,
|
||||
precessToDate,
|
||||
JD_J2000,
|
||||
} from '../src/services/observability.js';
|
||||
|
||||
// Sidereal time is an angle about the equinox of DATE, while this module takes
|
||||
// J2000 coordinates. A test that wants a target at a chosen hour angle has to
|
||||
// express that of-date right ascension back in J2000, or it is asking for a
|
||||
// position 0.37 degrees away from the one it means.
|
||||
const pick = p => ({ ra: p.ra_deg, dec: p.dec_deg });
|
||||
|
||||
const atHourAngle = (jd, lonDeg, haDeg, decOfDate) =>
|
||||
precessToJ2000(((lmst(jd, lonDeg) - haDeg) % 360 + 360) % 360, decOfDate, jd);
|
||||
import { EPHEMERIS_REFERENCE as REF } from './fixtures/ephemeris-reference.js';
|
||||
|
||||
// Tolerances. Generous relative to the reference, tight relative to any
|
||||
// decision this module makes: nothing here is read at better than a degree.
|
||||
const ALTAZ_TOL = 0.1; // degrees
|
||||
const SUN_TOL = 0.05; // degrees
|
||||
const MOON_TOL = 0.3; // degrees; the series actually lands inside 0.15,
|
||||
// so this catches a regression rather than rubber-stamping one
|
||||
|
||||
// ---------- the one date everything counts from ----------
|
||||
|
||||
test('J2000.0 is 2000-01-01T12:00:00Z', () => {
|
||||
// Definitional, and every series in the module counts from it. A one-day
|
||||
// error here would shift the Sun by a degree and nothing else would notice.
|
||||
assert.equal(julianDate('2000-01-01T12:00:00Z'), JD_J2000);
|
||||
assert.equal(julianDate('2000-01-02T12:00:00Z'), JD_J2000 + 1);
|
||||
assert.equal(julianDate('2000-01-01T00:00:00Z'), JD_J2000 - 0.5);
|
||||
});
|
||||
|
||||
test('julianDate refuses a time it cannot use', () => {
|
||||
for (const bad of ['not a date', undefined, NaN]) {
|
||||
assert.throws(() => julianDate(bad), TypeError);
|
||||
}
|
||||
});
|
||||
|
||||
// ---------- sidereal time ----------
|
||||
|
||||
test('sidereal time advances about four minutes a day faster than solar', () => {
|
||||
// A sidereal day is 23h56m04s, so after one solar day the sky has turned an
|
||||
// extra ~0.9856 degrees. This is the number that makes stars rise earlier
|
||||
// each night, and getting it wrong puts every hour angle slowly out.
|
||||
const a = gmst(JD_J2000);
|
||||
const b = gmst(JD_J2000 + 1);
|
||||
let delta = b - a;
|
||||
if (delta < 0) delta += 360;
|
||||
assert.ok(Math.abs(delta - 0.9856) < 0.001, `sidereal drift was ${delta}`);
|
||||
});
|
||||
|
||||
test('local sidereal time is Greenwich plus east longitude', () => {
|
||||
const jd = julianDate('2026-09-12T06:00:00Z');
|
||||
assert.ok(Math.abs(lmst(jd, 0) - gmst(jd)) < 1e-9);
|
||||
// 15 degrees east is one hour of rotation ahead.
|
||||
const east = lmst(jd, 15);
|
||||
const g = gmst(jd);
|
||||
assert.ok(Math.abs(((east - g + 360) % 360) - 15) < 1e-9);
|
||||
});
|
||||
|
||||
// ---------- geometry that cannot be wrong ----------
|
||||
|
||||
test('the celestial pole sits at the observer\'s latitude, always', () => {
|
||||
// Independent of time, of longitude, of the ephemeris. If this drifts, the
|
||||
// rotation is wrong in a way no reference comparison would localise.
|
||||
//
|
||||
// The pole OF DATE, expressed in J2000 for input: the J2000 pole is 0.4
|
||||
// degrees from the current one, which is precession doing its job rather
|
||||
// than an error.
|
||||
for (const lat of [-60, -31.27, 0, 37.34, 75]) {
|
||||
for (const hours of [0, 6, 13, 19]) {
|
||||
const jd = julianDate(`2026-09-12T${String(hours).padStart(2, '0')}:00:00Z`);
|
||||
const pole = precessToJ2000(0, 90, jd);
|
||||
const { alt_deg } = equatorialToHorizontal(pole.ra_deg, pole.dec_deg, lat, -121.6, jd);
|
||||
assert.ok(Math.abs(alt_deg - lat) < 0.01,
|
||||
`at latitude ${lat}, the pole was at ${alt_deg.toFixed(3)}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('an object at the observer\'s declination transits through the zenith', () => {
|
||||
const lat = 37.3414;
|
||||
const jd = julianDate('2026-09-12T06:00:00Z');
|
||||
// Hour angle zero, at a declination equal to the latitude, both of date.
|
||||
const t = atHourAngle(jd, -121.6429, 0, lat);
|
||||
const { alt_deg, ha_deg } = equatorialToHorizontal(t.ra_deg, t.dec_deg, lat, -121.6429, jd);
|
||||
assert.ok(Math.abs(ha_deg) < 1e-6, `hour angle should be zero, was ${ha_deg}`);
|
||||
assert.ok(Math.abs(alt_deg - 90) < 0.01, `altitude should be 90, was ${alt_deg}`);
|
||||
});
|
||||
|
||||
test('azimuth is measured from north through east', () => {
|
||||
// A target one hour east of the meridian at a northern site is in the east,
|
||||
// so its azimuth belongs in the first two quadrants.
|
||||
const lat = 37.3414, lon = -121.6429;
|
||||
const jd = julianDate('2026-09-12T06:00:00Z');
|
||||
const t = atHourAngle(jd, lon, -15, 20);
|
||||
const { az_deg, ha_deg } = equatorialToHorizontal(t.ra_deg, t.dec_deg, lat, lon, jd);
|
||||
assert.ok(ha_deg < 0, 'a target yet to transit has a negative hour angle');
|
||||
assert.ok(az_deg > 0 && az_deg < 180, `expected an easterly azimuth, got ${az_deg}`);
|
||||
});
|
||||
|
||||
test('hour angle is negative before transit and positive after', () => {
|
||||
const lon = -121.6429;
|
||||
const jd = julianDate('2026-09-12T06:00:00Z');
|
||||
const east = atHourAngle(jd, lon, -30, 20);
|
||||
const west = atHourAngle(jd, lon, 30, 20);
|
||||
const before = equatorialToHorizontal(east.ra_deg, east.dec_deg, 37.3, lon, jd);
|
||||
const after = equatorialToHorizontal(west.ra_deg, west.dec_deg, 37.3, lon, jd);
|
||||
assert.ok(Math.abs(before.ha_deg + 30) < 1e-6, before.ha_deg);
|
||||
assert.ok(Math.abs(after.ha_deg - 30) < 1e-6, after.ha_deg);
|
||||
});
|
||||
|
||||
// ---------- against astropy ----------
|
||||
|
||||
test('altitude and azimuth match astropy at every site and epoch', () => {
|
||||
let worstAlt = 0, worstAz = 0, worst = null;
|
||||
for (const r of REF.altaz) {
|
||||
const jd = julianDate(`${r.utc}Z`);
|
||||
const got = equatorialToHorizontal(r.ra_deg, r.dec_deg, r.lat, r.lon, jd);
|
||||
|
||||
const dAlt = Math.abs(got.alt_deg - r.alt_deg);
|
||||
// Azimuth is meaningless within a hair of the zenith and wraps at 360.
|
||||
let dAz = Math.abs(((got.az_deg - r.az_deg + 540) % 360) - 180);
|
||||
if (r.alt_deg > 89) dAz = 0;
|
||||
|
||||
if (dAlt > worstAlt) { worstAlt = dAlt; worst = r; }
|
||||
if (dAz > worstAz) worstAz = dAz;
|
||||
|
||||
assert.ok(dAlt < ALTAZ_TOL,
|
||||
`${r.target} from ${r.site} at ${r.utc}: altitude ${got.alt_deg.toFixed(4)} vs astropy ${r.alt_deg.toFixed(4)}`);
|
||||
assert.ok(dAz < ALTAZ_TOL,
|
||||
`${r.target} from ${r.site} at ${r.utc}: azimuth ${got.az_deg.toFixed(4)} vs astropy ${r.az_deg.toFixed(4)}`);
|
||||
}
|
||||
assert.ok(REF.altaz.length >= 40, `expected a real spread of cases, got ${REF.altaz.length}`);
|
||||
assert.ok(worstAlt < ALTAZ_TOL, `worst altitude error ${worstAlt} at ${JSON.stringify(worst)}`);
|
||||
});
|
||||
|
||||
test('the Sun matches astropy', () => {
|
||||
for (const r of REF.sun) {
|
||||
const jd = julianDate(`${r.utc}Z`);
|
||||
const got = sunPosition(jd);
|
||||
const sep = angularSeparation(got.ra_deg, got.dec_deg, r.ra_deg, r.dec_deg);
|
||||
assert.ok(sep < SUN_TOL,
|
||||
`${r.utc}: sun off by ${sep.toFixed(4)} degrees (got ${got.ra_deg.toFixed(3)}, ${got.dec_deg.toFixed(3)})`);
|
||||
}
|
||||
});
|
||||
|
||||
test('the Moon matches astropy to the precision the series claims', () => {
|
||||
for (const r of REF.moon) {
|
||||
const jd = julianDate(`${r.utc}Z`);
|
||||
const got = moonPosition(jd);
|
||||
const sep = angularSeparation(got.ra_deg, got.dec_deg, r.ra_deg, r.dec_deg);
|
||||
assert.ok(sep < MOON_TOL,
|
||||
`${r.utc}: moon off by ${sep.toFixed(3)} degrees (got ${got.ra_deg.toFixed(2)}, ${got.dec_deg.toFixed(2)})`);
|
||||
}
|
||||
});
|
||||
|
||||
test('moon elongation matches astropy, which is what phase is derived from', () => {
|
||||
for (const r of REF.moon) {
|
||||
const jd = julianDate(`${r.utc}Z`);
|
||||
const { elongation_deg } = moonIllumination(jd);
|
||||
assert.ok(Math.abs(elongation_deg - r.elongation_deg) < 1.0,
|
||||
`${r.utc}: elongation ${elongation_deg.toFixed(2)} vs astropy ${r.elongation_deg.toFixed(2)}`);
|
||||
}
|
||||
});
|
||||
|
||||
test('the Sun reaches its solstice declinations', () => {
|
||||
// Obliquity is 23.44 degrees, so the Sun's declination has to touch about
|
||||
// +23.4 in June and -23.4 in December. A sign error in obliquity passes a
|
||||
// single-date check and fails this one.
|
||||
const jdJun = julianDate('2026-06-21T12:00:00Z');
|
||||
const jdDec = julianDate('2026-12-21T12:00:00Z');
|
||||
const june = precessToDate(...Object.values(pick(sunPosition(jdJun))), jdJun);
|
||||
const dec = precessToDate(...Object.values(pick(sunPosition(jdDec))), jdDec);
|
||||
assert.ok(Math.abs(june.dec_deg - 23.44) < 0.1, june.dec_deg);
|
||||
assert.ok(Math.abs(dec.dec_deg + 23.44) < 0.1, dec.dec_deg);
|
||||
});
|
||||
|
||||
test('the Sun crosses the equator at the equinoxes', () => {
|
||||
// The equinox is defined against the equator OF DATE. sunPosition returns
|
||||
// J2000, where the same instant sits about 0.16 degrees off zero, so the
|
||||
// check precesses forward rather than loosening its tolerance to hide it.
|
||||
const jd = julianDate('2026-03-20T14:46:00Z');
|
||||
const s = sunPosition(jd);
|
||||
const ofDate = precessToDate(s.ra_deg, s.dec_deg, jd);
|
||||
assert.ok(Math.abs(ofDate.dec_deg) < 0.02, `equinox declination was ${ofDate.dec_deg}`);
|
||||
});
|
||||
|
||||
// ---------- airmass ----------
|
||||
|
||||
test('airmass is exactly one at the zenith and climbs toward the horizon', () => {
|
||||
assert.ok(Math.abs(airmass(90) - 1) < 0.001);
|
||||
assert.ok(airmass(30) > 1.9 && airmass(30) < 2.1, airmass(30));
|
||||
assert.ok(airmass(10) > 5, airmass(10));
|
||||
// Monotonic all the way down.
|
||||
for (let a = 5; a < 90; a += 5) {
|
||||
assert.ok(airmass(a) > airmass(a + 5), `airmass should fall as altitude rises, broke at ${a}`);
|
||||
}
|
||||
});
|
||||
|
||||
test('a set target has no airmass rather than a large one', () => {
|
||||
// sec(z) would hand back a plausible number here, which is how a scheduler
|
||||
// ends up offering a target that is below the horizon.
|
||||
assert.equal(airmass(0), null);
|
||||
assert.equal(airmass(-10), null);
|
||||
assert.equal(airmass(NaN), null);
|
||||
});
|
||||
|
||||
// ---------- separation ----------
|
||||
|
||||
test('angular separation is exact in the cases that can be checked by hand', () => {
|
||||
assert.ok(Math.abs(angularSeparation(0, 0, 0, 0)) < 1e-9);
|
||||
assert.ok(Math.abs(angularSeparation(0, 0, 90, 0) - 90) < 1e-9);
|
||||
assert.ok(Math.abs(angularSeparation(0, -90, 0, 90) - 180) < 1e-9);
|
||||
assert.ok(Math.abs(angularSeparation(0, 0, 0, 45) - 45) < 1e-9);
|
||||
// Small separations are where the cosine rule loses its precision, which is
|
||||
// the case moon avoidance actually asks about.
|
||||
assert.ok(Math.abs(angularSeparation(10, 20, 10.001, 20) - 0.001 * Math.cos(20 * Math.PI / 180)) < 1e-9);
|
||||
});
|
||||
|
||||
// ---------- sky state ----------
|
||||
|
||||
test('the Sun is up at local noon and down at local midnight', () => {
|
||||
const lat = 37.3414, lon = -121.6429;
|
||||
// Local solar noon at 121.6 west is about 20:06 UT.
|
||||
const noon = skyState(lat, lon, julianDate('2026-09-12T20:06:00Z'));
|
||||
const midnight = skyState(lat, lon, julianDate('2026-09-12T08:06:00Z'));
|
||||
assert.ok(noon.sun.alt_deg > 40, `noon sun altitude ${noon.sun.alt_deg}`);
|
||||
assert.equal(noon.twilight, 'day');
|
||||
assert.ok(midnight.sun.alt_deg < -18, `midnight sun altitude ${midnight.sun.alt_deg}`);
|
||||
assert.equal(midnight.twilight, 'night');
|
||||
assert.equal(midnight.dark, true);
|
||||
});
|
||||
|
||||
test('twilight is reported by name so a bright program is not told the sky is unusable', () => {
|
||||
const states = new Set();
|
||||
for (let h = 0; h < 24; h++) {
|
||||
const s = skyState(37.3414, -121.6429, julianDate(`2026-09-12T${String(h).padStart(2, '0')}:00:00Z`));
|
||||
states.add(s.twilight);
|
||||
}
|
||||
// A full day at a mid-latitude site passes through day, night and at least
|
||||
// one twilight band.
|
||||
assert.ok(states.has('day') && states.has('night'), [...states].join(','));
|
||||
assert.ok(states.size >= 3, `expected twilight bands, saw ${[...states].join(',')}`);
|
||||
});
|
||||
|
||||
test('a set Moon is reported as down however full it is', () => {
|
||||
// The distinction a bare phase number loses: a full Moon below the horizon
|
||||
// lights nothing, and treating it as a constraint costs half the month.
|
||||
let sawDownAndBright = false;
|
||||
for (let h = 0; h < 24 * 30; h += 3) {
|
||||
const s = skyState(37.3414, -121.6429, julianDate(new Date(Date.UTC(2026, 8, 1) + h * 3600_000)));
|
||||
if (!s.moon_up && s.moon.illuminated > 0.9) { sawDownAndBright = true; break; }
|
||||
}
|
||||
assert.ok(sawDownAndBright, 'a bright Moon should be below the horizon at some point in a month');
|
||||
});
|
||||
|
||||
test('moon illumination runs a full cycle within a month', () => {
|
||||
let lo = 1, hi = 0;
|
||||
for (let d = 0; d < 30; d++) {
|
||||
const k = moonIllumination(julianDate(new Date(Date.UTC(2026, 8, 1) + d * 86400_000))).illuminated;
|
||||
lo = Math.min(lo, k);
|
||||
hi = Math.max(hi, k);
|
||||
}
|
||||
assert.ok(lo < 0.1, `should pass near new moon, minimum was ${lo.toFixed(3)}`);
|
||||
assert.ok(hi > 0.9, `should pass near full moon, maximum was ${hi.toFixed(3)}`);
|
||||
});
|
||||
|
||||
// ---------- visibility windows ----------
|
||||
|
||||
test('a target too far south never rises at a northern site', () => {
|
||||
// The check that stops a program before it starts. No amount of scheduling
|
||||
// makes declination -60 work from latitude +37.
|
||||
const w = visibilityWindow(-60, 37.34, 30);
|
||||
assert.equal(w.never_rises, true);
|
||||
assert.equal(w.hours_above, 0);
|
||||
assert.ok(w.max_alt_deg < 0, w.max_alt_deg);
|
||||
});
|
||||
|
||||
test('a target near the pole is circumpolar at a high-latitude site', () => {
|
||||
const w = visibilityWindow(85, 60, 30);
|
||||
assert.equal(w.circumpolar, true);
|
||||
assert.equal(w.hours_above, 24);
|
||||
});
|
||||
|
||||
test('a target at the observer\'s declination culminates at the zenith', () => {
|
||||
assert.equal(visibilityWindow(37.34, 37.34, 30).max_alt_deg, 90);
|
||||
});
|
||||
|
||||
test('hours above the limit shrink as the limit rises', () => {
|
||||
const low = visibilityWindow(20, 37.34, 20).hours_above;
|
||||
const high = visibilityWindow(20, 37.34, 60).hours_above;
|
||||
assert.ok(low > high, `${low} should exceed ${high}`);
|
||||
});
|
||||
|
||||
// ---------- constraints ----------
|
||||
|
||||
const SITE = { latitude: 37.3414, longitude: -121.6429 };
|
||||
const DARK = '2026-09-12T08:06:00Z'; // local midnight, astronomical night
|
||||
|
||||
test('a target high in a dark sky is observable', () => {
|
||||
const jd = julianDate(DARK);
|
||||
// Place it at the zenith so nothing else can be the reason.
|
||||
const ra = lmst(jd, SITE.longitude);
|
||||
const e = evaluateTarget({ ra_deg: ra, dec_deg: SITE.latitude }, SITE, DARK,
|
||||
{ min_moon_separation_deg: 0 });
|
||||
assert.equal(e.ok, true, e.reasons.join('; '));
|
||||
assert.ok(e.geometry.alt_deg > 89, e.geometry.alt_deg);
|
||||
assert.ok(Math.abs(e.geometry.airmass - 1) < 0.01);
|
||||
});
|
||||
|
||||
test('every refusal says why, and names the number it failed', () => {
|
||||
// A scheduler whose decisions cannot be explained is treated as broken the
|
||||
// first time it declines something the observer wanted.
|
||||
const e = evaluateTarget({ ra_deg: 0, dec_deg: -70 }, SITE, DARK);
|
||||
assert.equal(e.ok, false);
|
||||
assert.ok(e.reasons.length > 0);
|
||||
assert.ok(e.reasons.some(r => /never reaches 30/.test(r)), e.reasons.join('; '));
|
||||
});
|
||||
|
||||
test('the Sun being up is a refusal in its own words', () => {
|
||||
const e = evaluateTarget({ ra_deg: 0, dec_deg: 37 }, SITE, '2026-09-12T20:06:00Z');
|
||||
assert.equal(e.ok, false);
|
||||
assert.ok(e.reasons.some(r => /Sun is up/.test(r)), e.reasons.join('; '));
|
||||
});
|
||||
|
||||
test('a target with no sky position is declined rather than placed at the origin', () => {
|
||||
const e = evaluateTarget({ ra_deg: null, dec_deg: null }, SITE, DARK);
|
||||
assert.equal(e.ok, false);
|
||||
assert.match(e.reasons[0], /no sky position/);
|
||||
assert.equal(e.geometry, null);
|
||||
});
|
||||
|
||||
test('moon separation only bites when the Moon is up and bright', () => {
|
||||
// Three months of instants, checking that no refusal ever cites the Moon
|
||||
// while the Moon is below the horizon. Charging a target for a Moon that is
|
||||
// not in the sky costs half of every month for nothing.
|
||||
let checked = 0;
|
||||
for (let h = 0; h < 24 * 90; h += 5) {
|
||||
const when = new Date(Date.UTC(2026, 8, 1) + h * 3600_000);
|
||||
const sky = skyState(SITE.latitude, SITE.longitude, julianDate(when));
|
||||
if (sky.moon_up) continue;
|
||||
const e = evaluateTarget(
|
||||
{ ra_deg: sky.moon.ra_deg, dec_deg: sky.moon.dec_deg }, SITE, when,
|
||||
{ allowed_twilight: ['night', 'astronomical', 'nautical', 'civil', 'day'] });
|
||||
checked++;
|
||||
assert.ok(!e.reasons.some(r => /Moon/.test(r)),
|
||||
`cited the Moon while it was ${sky.moon.alt_deg.toFixed(1)} below the horizon: ${e.reasons.join('; ')}`);
|
||||
}
|
||||
assert.ok(checked > 50, `expected many moon-down samples, got ${checked}`);
|
||||
});
|
||||
|
||||
test('a target on top of a bright Moon is refused', () => {
|
||||
// Find an instant with a bright Moon up, then ask for the Moon itself.
|
||||
let found = null;
|
||||
for (let h = 0; h < 24 * 60; h += 2) {
|
||||
const when = new Date(Date.UTC(2026, 8, 1) + h * 3600_000);
|
||||
const sky = skyState(SITE.latitude, SITE.longitude, julianDate(when));
|
||||
if (sky.moon_up && sky.moon.illuminated > 0.8 && sky.dark && sky.moon.alt_deg > 40) {
|
||||
found = { when, sky };
|
||||
break;
|
||||
}
|
||||
}
|
||||
assert.ok(found, 'no bright high Moon in a dark sky over two months');
|
||||
const e = evaluateTarget(
|
||||
{ ra_deg: found.sky.moon.ra_deg, dec_deg: found.sky.moon.dec_deg }, SITE, found.when);
|
||||
assert.equal(e.ok, false);
|
||||
assert.ok(e.reasons.some(r => /Moon/.test(r)), e.reasons.join('; '));
|
||||
});
|
||||
|
||||
test('meridian avoidance is off unless asked for', () => {
|
||||
const jd = julianDate(DARK);
|
||||
const ra = lmst(jd, SITE.longitude);
|
||||
const target = { ra_deg: ra, dec_deg: SITE.latitude };
|
||||
const off = evaluateTarget(target, SITE, DARK, { min_moon_separation_deg: 0 });
|
||||
const on = evaluateTarget(target, SITE, DARK, { min_moon_separation_deg: 0, meridian_avoid_deg: 5 });
|
||||
assert.equal(off.ok, true, off.reasons.join('; '));
|
||||
assert.equal(on.ok, false);
|
||||
assert.ok(on.reasons.some(r => /pier flip/.test(r)), on.reasons.join('; '));
|
||||
});
|
||||
|
||||
// ---------- scoring and ranking ----------
|
||||
|
||||
test('a lower airmass scores higher, all else equal', () => {
|
||||
const jd = julianDate(DARK);
|
||||
const ra = lmst(jd, SITE.longitude);
|
||||
const zenith = evaluateTarget({ ra_deg: ra, dec_deg: SITE.latitude }, SITE, DARK,
|
||||
{ min_moon_separation_deg: 0 });
|
||||
const low = evaluateTarget({ ra_deg: ra, dec_deg: SITE.latitude - 45 }, SITE, DARK,
|
||||
{ min_moon_separation_deg: 0, min_altitude_deg: 20, max_airmass: 4 });
|
||||
assert.ok(zenith.ok && low.ok, `${zenith.reasons.join(';')} / ${low.reasons.join(';')}`);
|
||||
assert.ok(low.geometry.airmass > zenith.geometry.airmass);
|
||||
assert.ok(scoreTarget(zenith, 0.5) > scoreTarget(low, 0.5));
|
||||
});
|
||||
|
||||
test('a declined target scores zero rather than sorting among the live ones', () => {
|
||||
const e = evaluateTarget({ ra_deg: 0, dec_deg: -80 }, SITE, DARK);
|
||||
assert.equal(e.ok, false);
|
||||
assert.equal(scoreTarget(e, 1.0), 0, 'maximum priority must not rescue an impossible target');
|
||||
});
|
||||
|
||||
test('priority breaks a tie but cannot make an impossible target possible', () => {
|
||||
const jd = julianDate(DARK);
|
||||
const ra = lmst(jd, SITE.longitude);
|
||||
const same = evaluateTarget({ ra_deg: ra, dec_deg: SITE.latitude }, SITE, DARK,
|
||||
{ min_moon_separation_deg: 0 });
|
||||
assert.ok(scoreTarget(same, 1.0) > scoreTarget(same, 0.0));
|
||||
});
|
||||
|
||||
test('a setting target outranks one with all night to wait', () => {
|
||||
// The difference between a schedule and a sorted list: a target two hours
|
||||
// from the limit is one you lose tonight if you do something else first.
|
||||
const jd = julianDate(DARK);
|
||||
const lst = lmst(jd, SITE.longitude);
|
||||
const setting = evaluateTarget({ ra_deg: (lst - 45 + 360) % 360, dec_deg: SITE.latitude }, SITE, DARK,
|
||||
{ min_moon_separation_deg: 0 });
|
||||
const rising = evaluateTarget({ ra_deg: (lst + 45) % 360, dec_deg: SITE.latitude }, SITE, DARK,
|
||||
{ min_moon_separation_deg: 0 });
|
||||
assert.ok(setting.ok && rising.ok, `${setting.reasons.join(';')} / ${rising.reasons.join(';')}`);
|
||||
assert.ok(setting.geometry.ha_deg > 0 && rising.geometry.ha_deg < 0);
|
||||
assert.ok(scoreTarget(setting, 0.5) > scoreTarget(rising, 0.5),
|
||||
'the setting target should be preferred at equal airmass and priority');
|
||||
});
|
||||
|
||||
test('ranking puts observable targets first and keeps the rest with their reasons', () => {
|
||||
const jd = julianDate(DARK);
|
||||
const lst = lmst(jd, SITE.longitude);
|
||||
const rows = rankTargets([
|
||||
{ name: 'impossible', ra_deg: 0, dec_deg: -80, priority: 1.0 },
|
||||
{ name: 'zenith', ra_deg: lst, dec_deg: SITE.latitude, priority: 0.5 },
|
||||
{ name: 'low', ra_deg: lst, dec_deg: SITE.latitude - 50, priority: 0.5 },
|
||||
], SITE, DARK, { min_moon_separation_deg: 0 });
|
||||
|
||||
assert.equal(rows.length, 3, 'a declined target stays in the list');
|
||||
assert.equal(rows[0].name, 'zenith');
|
||||
assert.equal(rows[rows.length - 1].name, 'impossible');
|
||||
assert.equal(rows[rows.length - 1].observable, false);
|
||||
assert.ok(rows[rows.length - 1].reasons.length > 0, 'and it says why');
|
||||
});
|
||||
|
||||
test('a per-target constraint overrides the pool default', () => {
|
||||
const jd = julianDate(DARK);
|
||||
const lst = lmst(jd, SITE.longitude);
|
||||
// 35 degrees up: refused under a 40-degree floor, allowed by its own override.
|
||||
const target = { name: 't', ra_deg: lst, dec_deg: SITE.latitude - 55 };
|
||||
const strict = rankTargets([target], SITE, DARK,
|
||||
{ min_altitude_deg: 40, min_moon_separation_deg: 0 });
|
||||
const relaxed = rankTargets(
|
||||
[{ ...target, constraints: { min_altitude_deg: 20, max_airmass: 4 } }],
|
||||
SITE, DARK, { min_altitude_deg: 40, min_moon_separation_deg: 0 });
|
||||
assert.equal(strict[0].observable, false, strict[0].reasons.join('; '));
|
||||
assert.equal(relaxed[0].observable, true, relaxed[0].reasons.join('; '));
|
||||
});
|
||||
Reference in New Issue
Block a user