Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/25_RADAR_RENDERING_QUALITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@ The above-radar set is deliberately small. Its lines remain recognizable when so
- Must not bridge missing radials, transparent/missing regions, or categorical range-folded regions as if valid reflectivity existed there.
- Must handle the 0/360-degree azimuth seam without inventing a discontinuity.

#### National `Smooth` edge behavior

The National grid renderer weights only the **valid** neighbors of a sample footprint and renormalizes them. A missing or no-coverage cell contributes no value at any weight, so `Smooth` still never bridges an invalid status; but a partially invalid footprint no longer collapses to a hard nearest-cell block, which previously drew every echo boundary as a row of 1 km squares.

The renormalized weight that survives is the fragment's **coverage**, and it scales palette opacity by its square root. A fully surrounded cell is unchanged at coverage 1; the outermost half-cell ring fades rather than terminating in a square; and a lone valid corner still renders at half palette opacity, so a measured cell never becomes invisible. This is display-only, `Smooth`-only, and applied after palette lookup:

- the painted footprint is identical in both modes, because an invalid nearest cell already paints nothing;
- no coverage is invented beyond the measured cells;
- `Native` remains exact nearest-cell sampling at full opacity;
- interrogation continues to report the exact backend value in both modes.

`src/national-radar/sampling.ts` mirrors this contract outside WebGL so it stays unit-tested; the shader in `src/national-radar/NationalGridLayer.ts` must match it.

Neither mode adds resolution. The MRMS CONUS mosaic is a 0.01-degree (roughly 1 km) grid, so beyond about zoom 9 each measured cell covers many screen pixels and remains individually visible. Close-range structural detail is the selected-site Level II product's job, not the national mosaic's.

### `Native`

- Uses the exact native polar gate selected by nearest sampling.
Expand Down
27 changes: 22 additions & 5 deletions src/national-radar/NationalGridLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,31 @@ void main() {
uint c10 = texelFetch(u_raw_codes, ivec2(upper.x, lower.y), 0).r;
uint c01 = texelFetch(u_raw_codes, ivec2(lower.x, upper.y), 0).r;
uint c11 = texelFetch(u_raw_codes, upper, 0).r;
if (!valid_code(c00) || !valid_code(c10) || !valid_code(c01) || !valid_code(c11)) {
vec4 codes = vec4(float(c00), float(c10), float(c01), float(c11));
// Weight only the valid neighbors and renormalize. A missing or no-coverage
// cell never contributes its value, but it no longer collapses the fragment
// to a hard nearest-cell block either, so echo edges stop rendering as
// squares. Coverage then fades the outermost half-cell ring; the painted
// footprint is unchanged because an invalid nearest cell already discarded.
vec2 fraction = clamp(fract(local), 0.0, 1.0);
vec4 weights = vec4(
(1.0 - fraction.x) * (1.0 - fraction.y),
fraction.x * (1.0 - fraction.y),
(1.0 - fraction.x) * fraction.y,
fraction.x * fraction.y
) * vec4(
valid_code(c00) ? 1.0 : 0.0,
valid_code(c10) ? 1.0 : 0.0,
valid_code(c01) ? 1.0 : 0.0,
valid_code(c11) ? 1.0 : 0.0
);
float coverage = dot(weights, vec4(1.0));
if (coverage <= 0.0) {
frag_color = palette_color(float(nearest_raw));
return;
}
vec2 fraction = clamp(fract(local), 0.0, 1.0);
float top = mix(float(c00), float(c10), fraction.x);
float bottom = mix(float(c01), float(c11), fraction.x);
frag_color = palette_color(mix(top, bottom, fraction.y));
// The palette is premultiplied, so scaling the whole color scales opacity.
frag_color = palette_color(dot(weights, codes) / coverage) * sqrt(coverage);
}`;

export interface NationalPaintReceipt {
Expand Down
56 changes: 43 additions & 13 deletions src/national-radar/sampling.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { decodeNationalRaw, sampleNationalGrid } from "./sampling";
import { decodeNationalRaw, nationalEdgeAlphaScale, sampleNationalGrid } from "./sampling";

describe("National numeric sampling", () => {
it("decodes the complete structural u16 domain by the GRIB formula", () => {
Expand All @@ -13,12 +13,13 @@ describe("National numeric sampling", () => {
expect(decodeNationalRaw(0).status).toBe("no_coverage");
});

it("keeps Native on the nearest selected-level cell", () => {
it("keeps Native on the nearest selected-level cell at full opacity", () => {
const raw = new Uint16Array([9_990, 10_000, 10_100, 10_200]);
expect(sampleNationalGrid(raw, 2, 2, 0.8, 0.2, "native")).toEqual({
status: "valid",
rawCode: 10_000,
valueDbz: 1,
coverage: 1,
});
});

Expand All @@ -28,20 +29,49 @@ describe("National numeric sampling", () => {
expect(sample.status).toBe("valid");
expect(sample.rawCode).toBe(10_020);
expect(sample.valueDbz).toBe(3);
expect(sample.coverage).toBe(1);
});

it("never interpolates across missing or no-coverage status", () => {
it("never mixes a missing or no-coverage value into the smoothed result", () => {
// Weights at (0.25, 0.25) are 0.5625 / 0.1875 / 0.1875 / 0.0625. The
// invalid neighbor drops out and the remaining three renormalize, so the
// sentinel's own code can never influence the displayed value.
const missing = new Uint16Array([9_990, 9_000, 10_030, 10_050]);
expect(sampleNationalGrid(missing, 2, 2, 0.25, 0.25, "smooth")).toEqual({
status: "valid",
rawCode: 9_990,
valueDbz: 0,
});
const smoothed = sampleNationalGrid(missing, 2, 2, 0.25, 0.25, "smooth");
const expectedCoverage = 0.5625 + 0.1875 + 0.0625;
const expectedRaw = (9_990 * 0.5625 + 10_030 * 0.1875 + 10_050 * 0.0625) / expectedCoverage;
expect(smoothed.status).toBe("valid");
expect(smoothed.coverage).toBeCloseTo(expectedCoverage, 10);
expect(smoothed.rawCode).toBeCloseTo(expectedRaw, 8);

// A no-coverage sentinel at the same position must produce the identical
// result: only validity matters, never the sentinel's numeric code.
const uncovered = new Uint16Array([9_990, 0, 10_030, 10_050]);
expect(sampleNationalGrid(uncovered, 2, 2, 0.25, 0.25, "smooth")).toEqual({
status: "valid",
rawCode: 9_990,
valueDbz: 0,
});
expect(sampleNationalGrid(uncovered, 2, 2, 0.25, 0.25, "smooth")).toEqual(smoothed);
});

it("softens an echo edge instead of collapsing it to a nearest-cell block", () => {
// Two valid cells above two no-coverage cells. Sampling just inside the
// measured row still blends both valid neighbors and reports partial
// coverage, where the old contract snapped to one flat nearest square.
const edge = new Uint16Array([10_200, 10_400, 0, 0]);
const sample = sampleNationalGrid(edge, 2, 2, 0.5, 0.25, "smooth");
expect(sample.rawCode).toBeCloseTo(10_300, 8);
expect(sample.coverage).toBeCloseTo(0.75, 10);
expect(nationalEdgeAlphaScale(sample.coverage)).toBeCloseTo(Math.sqrt(0.75), 10);
});

it("keeps a lone measured corner visible at half palette opacity", () => {
const isolated = new Uint16Array([0, 0, 9_000, 10_600]);
const sample = sampleNationalGrid(isolated, 2, 2, 0.5, 0.5, "smooth");
expect(sample.rawCode).toBeCloseTo(10_600, 8);
expect(sample.coverage).toBeCloseTo(0.25, 10);
expect(nationalEdgeAlphaScale(sample.coverage)).toBeCloseTo(0.5, 10);
});

it("bounds the display-only edge fade", () => {
expect(nationalEdgeAlphaScale(1)).toBe(1);
expect(nationalEdgeAlphaScale(0)).toBe(0);
expect(() => nationalEdgeAlphaScale(1.5)).toThrow("between 0 and 1");
});
});
68 changes: 51 additions & 17 deletions src/national-radar/sampling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,29 @@ export interface NationalDisplaySample {
valueDbz: number | null;
}

export interface NationalDisplayFragment extends NationalDisplaySample {
/**
* Fraction of the sample footprint backed by valid measured cells: 1 when
* every contributing neighbor is valid (and always 1 in `Native`), lower
* near a missing or no-coverage boundary.
*/
coverage: number;
}

/**
* Display-only opacity applied to a partially covered `Smooth` fragment. The
* square root keeps a measured cell clearly visible at its weakest footprint
* (a lone valid corner renders at half palette opacity) while the outermost
* half-cell ring fades instead of ending in a hard square. `Native` never
* applies it, and interrogation is unaffected in either mode.
*/
export function nationalEdgeAlphaScale(coverage: number): number {
if (!Number.isFinite(coverage) || coverage < 0 || coverage > 1) {
throw new RangeError("National coverage must be between 0 and 1");
}
return Math.sqrt(coverage);
}

export function decodeNationalRaw(
rawCode: number,
missingRaw = 9000,
Expand All @@ -21,6 +44,13 @@ export function decodeNationalRaw(
return { status: "valid", rawCode, valueDbz: (-9990 + rawCode) / 10 };
}

/**
* Mirrors the National fragment shader's sampling contract so it stays
* testable outside WebGL. `Smooth` weights only the valid neighbors of the
* footprint and renormalizes: a missing or no-coverage cell never contributes
* its value, but it no longer collapses the fragment to a hard nearest-cell
* block either. The returned `coverage` drives the display-only edge fade.
*/
export function sampleNationalGrid(
rawCodes: Uint16Array,
width: number,
Expand All @@ -30,7 +60,7 @@ export function sampleNationalGrid(
mode: RadarDisplayMode,
missingRaw = 9000,
noCoverageRaw = 0,
): NationalDisplaySample {
): NationalDisplayFragment {
if (
!Number.isSafeInteger(width)
|| !Number.isSafeInteger(height)
Expand All @@ -46,36 +76,40 @@ export function sampleNationalGrid(
const nearestY = clamp(Math.floor(y + 0.5), 0, height - 1);
const nearest = rawCodes[nearestY * width + nearestX];
const nearestSample = decodeNationalRaw(nearest, missingRaw, noCoverageRaw);
if (mode === "native" || nearestSample.status !== "valid") return nearestSample;
// An invalid nearest cell paints nothing at all, so the measured footprint
// is identical in both modes; only opacity inside it can soften.
if (mode === "native" || nearestSample.status !== "valid") {
return { ...nearestSample, coverage: 1 };
}
const lowerX = clamp(Math.floor(x), 0, width - 1);
const lowerY = clamp(Math.floor(y), 0, height - 1);
const upperX = Math.min(lowerX + 1, width - 1);
const upperY = Math.min(lowerY + 1, height - 1);
const fractionX = clamp(x - Math.floor(x), 0, 1);
const fractionY = clamp(y - Math.floor(y), 0, 1);
const corners = [
rawCodes[lowerY * width + lowerX],
rawCodes[lowerY * width + upperX],
rawCodes[upperY * width + lowerX],
rawCodes[upperY * width + upperX],
{ raw: rawCodes[lowerY * width + lowerX], weight: (1 - fractionX) * (1 - fractionY) },
{ raw: rawCodes[lowerY * width + upperX], weight: fractionX * (1 - fractionY) },
{ raw: rawCodes[upperY * width + lowerX], weight: (1 - fractionX) * fractionY },
{ raw: rawCodes[upperY * width + upperX], weight: fractionX * fractionY },
];
if (corners.some((raw) => decodeNationalRaw(raw, missingRaw, noCoverageRaw).status !== "valid")) {
return nearestSample;
let coverage = 0;
let weighted = 0;
for (const corner of corners) {
if (decodeNationalRaw(corner.raw, missingRaw, noCoverageRaw).status !== "valid") continue;
coverage += corner.weight;
weighted += corner.weight * corner.raw;
}
const fractionX = clamp(x - Math.floor(x), 0, 1);
const fractionY = clamp(y - Math.floor(y), 0, 1);
const top = mix(corners[0], corners[1], fractionX);
const bottom = mix(corners[2], corners[3], fractionX);
const rawCode = mix(top, bottom, fractionY);
if (coverage <= 0) return { ...nearestSample, coverage: 1 };
const rawCode = weighted / coverage;
return {
status: "valid",
rawCode,
valueDbz: (-9990 + rawCode) / 10,
coverage: clamp(coverage, 0, 1),
};
}

function clamp(value: number, minimum: number, maximum: number) {
return Math.max(minimum, Math.min(maximum, value));
}

function mix(lower: number, upper: number, fraction: number) {
return lower + (upper - lower) * fraction;
}