Skip to content

The OSINT investigator's workbench. One case, one folder, every tool. Local-first: your media and your investigations never leave your machine.

JavaScript
5.7 KB
Raw Blame
/**
 * Sentinel-2 passes over the areas a sweep would cover.
 *
 * A crosshair date picker asks "is there imagery here"; a sweep has to ask "is
 * there imagery over all of this", and the two have different answers. Sentinel-2
 * flies 290 km-wide swaths on a five-day revisit, so an area can sit across two
 * of them and have no single day that covers it at all. Pinned to one date
 * anyway, half the sweep reads nodata and reports nothing found over ground it
 * never saw.
 *
 * The lookup itself is `POST /api/satellite/sentinel/acquisitions`; everything
 * here is the pure part around it, so the panel stays a view.
 */

import { daysBefore, isoDay } from '../sentinel.js';

/**
 * How far back the pass list looks. A calendar asks for one chosen month instead.
 */
export const LOOKBACK_WINDOWS = Object.freeze([
  { id: 30, label: '30 days' },
  { id: 90, label: '90 days' },
  { id: 365, label: '1 year' },
]);

/** The day each collection's first satellite was launched: no pass is older. */
export const MISSION_START = Object.freeze({ sentinel2: '2015-06-23', sentinel1: '2014-04-03' });

/** The days a lookback asks about: a number of days up to today, or two picked days. */
export function lookupSpan(lookback, now = new Date()) {
  return typeof lookback === 'number'
    ? { start: daysBefore(lookback, now), end: isoDay(now) }
    : { start: lookback?.start ?? '', end: lookback?.end ?? '' };
}

/**
 * Where to look for what a list stopped short of, or null when nothing is left.
 *
 * The catalogue answers newest first, so what a cut list lacks is older than its
 * last row. The same start, then, up to that row's day, which is asked again
 * because the cut may have fallen among its granules.
 */
export function olderSpan(lookback, list, now = new Date()) {
  const oldest = list?.at(-1)?.date;
  const { start } = lookupSpan(lookback, now);
  return oldest && start && oldest > start ? { start, end: oldest } : null;
}

/**
 * A list with the older passes found after it, newest first and each pass once.
 * The older lookup's copy of the day they share wins: it read all of its granules.
 */
export function withOlder(list, older) {
  const passes = new Map((list ?? []).map((entry) => [passKey(entry), entry]));
  for (const entry of older ?? []) passes.set(passKey(entry), entry);
  return [...passes.values()].sort((x, y) => passKey(y).localeCompare(passKey(x)));
}

/**
 * Coverage at or above this counts as covering the whole area. Not 1.0 because
 * the backend measures it by sampling, and a ring's own edge lands a point or
 * two outside a granule that in truth reaches it. Mirrors `FULL_COVER` in
 * `engine/analyzers.py`.
 */
export const FULL_COVER = 0.98;

/** The request body for a lookup over the areas currently drawn. */
export function acquisitionQuery(zones, lookback, now = new Date(), collection = 'sentinel2') {
  return {
    zones: zones.map(({ id, name, kind, points }) => ({ id, name, kind, points })),
    ...lookupSpan(lookback, now),
    collection,
  };
}

/** One pass as a key: a radar day can hold two passes, morning and evening. */
export function passKey(entry) {
  return entry?.time ? `${entry.date}T${entry.time}` : entry?.date ?? '';
}

/**
 * What a coverage share was measured over, as a comparable key.
 *
 * A share describes one set of shapes. Redraw them and every number in the list
 * is about ground that is no longer being asked about, so the list has to go
 * rather than quietly age into a lie. Geometry only: renaming an area changes
 * nothing a satellite swath cares about, and dropping the list for it would
 * charge a request for a rename.
 */
export function areaKey(zones) {
  return JSON.stringify((zones ?? []).map((zone) => [zone.kind, zone.points]));
}

/** How much of the drawn areas a pass reaches, as a badge. */
export function coverLabel(coverage) {
  if (!Number.isFinite(coverage)) return '';
  if (coverage >= FULL_COVER) return 'Full cover';
  return `${Math.round(coverage * 100)}% of the areas`;
}

/** Whole, partial or barely there — what colours the badge. */
export function coverClass(coverage) {
  if (!Number.isFinite(coverage)) return 'unknown';
  if (coverage >= FULL_COVER) return 'full';
  return coverage >= 0.5 ? 'part' : 'thin';
}

/**
 * Passes the cloud ceiling still allows, newest first. A pass the service gave
 * no cloud figure for is kept: an unknown is not a reason to hide real imagery.
 */
export function allowed(list, maxcc) {
  return (list ?? []).filter(
    (entry) => entry.cloud == null || entry.cloud <= maxcc
  );
}

/**
 * What to warn about once a date is chosen, or '' when there is nothing to say.
 * Stated before the run, this is a row in a list; discovered after it, it is a
 * sweep paid for in tiles that found nothing.
 */
export function coverageWarning(entry) {
  if (!entry || !Number.isFinite(entry.coverage) || entry.coverage >= FULL_COVER) return '';
  return `${entry.date} reaches ${Math.round(entry.coverage * 100)}% of the areas. The rest has no imagery that day and will be reported as not swept.`;
}

/**
 * What to say about a chosen Sentinel-2 pass over the cloud ceiling, or ''.
 * It is read all the same, since a chosen day is the analyst's call; the
 * clouds are left out pixel by pixel, which is worth knowing before the run.
 */
export function cloudWarning(entry, maxcc = 100) {
  if (!entry || entry.cloud == null || !(entry.cloud > maxcc)) return '';
  return `${entry.date} is ${Math.round(entry.cloud)}% cloud over its tile. It is read anyway, and ground under cloud is left out.`;
}

/** The share of a finished run's areas that had imagery on both dates. */
export function sweptNote(swept) {
  if (!Number.isFinite(swept) || swept >= FULL_COVER) return '';
  return `Swept ${Math.round(swept * 100)}% of the areas; the rest had no imagery on these dates.`;
}