Files
CatoandClaude 59ccaa532c feat(coverage): mass-balance coverage ratio, inference rules, and "How complete is this picture" (#227)
- lib/coverage: capacity cells read without guessing (NA, estimates, per-material
  parts, two-figure cells left unreadable), exact unit factors only, basis
  matching per national series, a group capacity counted once.
- coverageFor/coverageOf: named capacity / national production (USGS MCS, WNA),
  with every row, both sources and the arithmetic, or the reason it cannot be
  computed; rows with a named operator, operators with a stated owner, flows.
- Three inference rules (co-ownership, same-parent, registry-bridge) write
  derived/lead proposals to research/derived-relations.json; the store holds
  them apart in `proposals`, never walked, counted or indexed.
- Country pages: "How complete is this picture" under the dossier; chat tool
  coverage_of(country, resource?).


Claude-Session: https://claude.ai/code/session_018WZnBnaLP6RhwFkLxRTKgE

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-24 09:26:12 +02:00

48 lines
2.2 KiB
TypeScript

/**
* Units a facility row and a national figure can be printed in, and the only
* conversions allowed between them: exact, definitional factors within one
* family. Tonnes become kilotonnes; tonnes never become carats, barrels or
* "tonnes of contained metal". Anything else is `incomparable`, with the reason.
*/
type Family = 'mass' | 'volume' | 'carats' | 'oil-rate' | 'energy';
/** Each unit as a multiple of its family's base: tonne, cubic metre, carat. */
const UNITS: Record<string, { family: Family; factor: number }> = {
kilograms: { family: 'mass', factor: 0.001 },
kg: { family: 'mass', factor: 0.001 },
'metric tons': { family: 'mass', factor: 1 },
t: { family: 'mass', factor: 1 },
'thousand metric tons': { family: 'mass', factor: 1_000 },
kt: { family: 'mass', factor: 1_000 },
'million metric tons': { family: 'mass', factor: 1_000_000 },
Mt: { family: 'mass', factor: 1_000_000 },
// One short ton is 907.18474 kg by definition.
Mst: { family: 'mass', factor: 907_184.74 },
// One troy ounce is 31.1034768 g by definition.
'troy ounces': { family: 'mass', factor: 0.0000311034768 },
'thousand cubic meters': { family: 'volume', factor: 1_000 },
'million cubic meters': { family: 'volume', factor: 1_000_000 },
Mcm: { family: 'volume', factor: 1_000_000 },
bcm: { family: 'volume', factor: 1_000_000_000 },
carats: { family: 'carats', factor: 1 },
'thousand carats': { family: 'carats', factor: 1_000 },
Mct: { family: 'carats', factor: 1_000_000 },
'kb/d': { family: 'oil-rate', factor: 1 },
TWh: { family: 'energy', factor: 1 },
};
export type Converted = { value: number; factor: number } | { incomparable: string };
/** `value` in `from`, expressed in `to`, or why it cannot be. */
export function convert(value: number, from: string, to: string): Converted {
const a = UNITS[from];
const b = UNITS[to];
if (!a) return { incomparable: `the unit "${from}" is not one this check can convert` };
if (!b) return { incomparable: `the unit "${to}" is not one this check can convert` };
if (a.family !== b.family)
return { incomparable: `"${from}" (${a.family}) and "${to}" (${b.family}) measure different things` };
const factor = a.factor / b.factor;
return { value: value * factor, factor };
}