"""How much did it really rain? No one really knows. Any two of ERA5 (reanalysis), PARDIG and IMERG (satellite rain maps), side by side.""" from __future__ import annotations import logging from pathlib import Path import gradio as gr import numpy as np import pandas as pd import maps logging.basicConfig(level=logging.INFO) HERE = Path(__file__).parent N_DAYS = 2 # every view plays 48 hours FEATURED = "Tropical cyclone, Mozambique Channel" #: Dropdown label -> source key in maps.SOURCES. DATASETS = {"IMERG (NASA, satellites)": "imerg", "PARDIG (Google, satellites + AI)": "pardig", "ERA5 (ECMWF, reanalysis)": "era5"} DEFAULT_LEFT, DEFAULT_RIGHT = "IMERG (NASA, satellites)", "PARDIG (Google, satellites + AI)" REGIONS = { FEATURED: ((-24, -6, 31, 55), "2026-01-29"), "Europe": ((35, 72, -25, 45), None), "France": ((41, 51.5, -5.5, 10), None), "British Isles": ((49, 61, -11, 3), None), "Iberia": ((35, 44.5, -10, 4), None), "Contiguous US": ((24, 50, -125, -66), None), "US Southeast": ((25, 37, -95, -75), None), "Amazon": ((-15, 5, -75, -45), None), "West Africa": ((2, 22, -18, 15), None), "India": ((5, 35, 65, 95), None), "East Asia": ((20, 50, 100, 145), None), "Maritime Continent": ((-11, 8, 95, 150), None), "Australia": ((-45, -10, 110, 155), None), } INTRO = """

How much did it really rain? No one really knows.

Forecasting precipitation accurately is important to predict floods, droughts and water supply, which have a direct impact on people’s lives, agriculture, natural disasters, etc. But precipitation is harder to predict and evaluate than other variables such as temperature.

""" PROBLEM = """

There is no ground truth for rain. Evaluating a prediction requires comparing it to a reference. But since rain is not measured everywhere on the planet, the references we have are not perfect. Rain gauges are accurate but sparse, and each one measures only a single point. Radars cover larger areas but struggle over mountains and in heavy storms. Reanalyses (such as ERA5 or CERRA) and satellite-based estimates (such as IMERG or PARDIG) offer much wider coverage, but they infer rain from a physics model or from satellite measurements, so they come with their own biases.

I made this space to visualize different rain maps. The goal is to see how they differ, not to evaluate which one is better. It shows two of them side by side, hour by hour, with their difference, so you can see where and how they disagree.

""" STORY = """
Reanalysis

ERA5: a reconstruction of the past weather hour by hour since 1940

ERA5 is a reanalysis produced by the European Centre for Medium-Range Weather Forecasts (ECMWF). It reconstructs the past weather hour by hour since 1940, by combining a physics model of the atmosphere with observations from satellites, weather stations and weather balloons, on a 28 km grid. It is the dataset most AI weather models are trained on and evaluated against. Its rain comes from the physics model, so it looks smoother than the satellite maps: light rain tends to cover larger areas, and heavy rain is weaker and more spread out.

Satellites

IMERG: NASA's satellite rain map

IMERG (Integrated Multi-satellitE Retrievals for GPM) is NASA's standard global rain map, every 30 minutes on a 10 km grid. It merges the rain seen by a constellation of microwave satellites, anchored by the GPM Core Observatory's radar, and fills the gaps between their passes with infrared images from geostationary satellites. Shown here is the Late run, available about 14 hours after real time. It has no reliable rain over snow and ice, so high latitudes are often left blank.

Satellites + AI

PARDIG: a new satellite-based reconstruction

PARDIG (Precipitation AI Reanalysis, Densely Inferred GPM) is a precipitation dataset recently released by Google DeepMind and Google Research. It estimates the rain over the whole planet every 15 minutes, on a 10 km grid. An AI model converts geostationary satellite images and weather analyses into rain. The dataset is openly available under a CC BY 4.0 license.

""" FOOTER = ("Data: [`EmmaScharfmann/era5-pardig-scores`](https://huggingface.co/datasets/EmmaScharfmann/era5-pardig-scores) · " "IMERG: NASA GES DISC, doi:10.5067/GPM/IMERG/3B-HH-L/07 · " "Basemap © OpenStreetMap contributors © CARTO.") COMPONENT_HTML = """
IMERG · 10 km
PARDIG · 10 km
IMERG minus PARDIG, both averaged to the 28 km grid
""" COMPONENT_CSS = """ .rm { display: flex; flex-direction: column; gap: 8px; } .rm-maps { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px 10px; } .rm-legend-rain { grid-column: 1 / 3; } .rm-legend-diff { grid-column: 3 / 4; } .rm-panel { border: 1px solid var(--border-color-primary); border-radius: 10px; overflow: hidden; } .rm-title { padding: 7px 10px; font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } .rm-title span { font-weight: 400; font-size: 12px; color: var(--body-text-color-subdued); margin-left: 6px; } .rm-map { height: 470px; } @media (max-width: 900px) { .rm-maps { grid-template-columns: 1fr; } .rm-map { height: 320px; } .rm-legend-rain, .rm-legend-diff { grid-column: 1 / -1; } } .rm-legend { padding: 0 2px 6px; } .rm-leg-title { font-size: 12px; margin-bottom: 4px; color: var(--body-text-color); } .rm-leg-cells { display: flex; gap: 2px; } .rm-leg-cell { flex: 1; text-align: center; } .rm-leg-cell:first-child .rm-leg-swatch { border-radius: 4px 0 0 4px; } .rm-leg-cell:last-child .rm-leg-swatch { border-radius: 0 4px 4px 0; } .rm-leg-swatch { height: 14px; } .rm-leg-name { font-size: 13px; font-weight: 600; margin-top: 4px; color: var(--body-text-color); } .rm-leg-text { font-size: 11px; color: var(--body-text-color-subdued); white-space: nowrap; } .rm-bar-scale { display: flex; height: 14px; border-radius: 4px; overflow: hidden; } .rm-ticks { position: relative; height: 16px; } .rm-tick { position: absolute; transform: translateX(-50%); font-size: 11px; color: var(--body-text-color-subdued); top: 2px; } .rm-bar-ends { display: flex; justify-content: space-between; font-size: 12px; font-weight: 600; } .rm-bar-ends .wet { color: #542788; } .rm-bar-ends .dry { color: #b35806; } .rm-bar-ends .mid { color: var(--body-text-color-subdued); font-weight: 400; } .rm-chips { display: flex; flex-wrap: wrap; gap: 6px 14px; padding-top: 18px; } .rm-chip { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; font-weight: 600; } .rm-chip-note { font-weight: 400; color: var(--body-text-color-subdued); } .rm-chip-sw { width: 14px; height: 14px; border-radius: 3px; display: inline-block; } .rm-leg-ends { display: flex; justify-content: space-between; font-size: 12px; font-weight: 600; margin-top: 2px; } .rm-leg-ends span:first-child { color: #b35806; } .rm-leg-ends span:last-child { color: #542788; } .rm-bar { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; } .rm-play { width: 44px; height: 36px; border-radius: 8px; border: 1px solid var(--border-color-primary); background: var(--button-secondary-background-fill); cursor: pointer; font-size: 15px; } .rm-slider { flex: 1; min-width: 140px; accent-color: #2a78d6; } .rm-time { min-width: 240px; font-variant-numeric: tabular-nums; font-size: 14px; } .rm-speed { height: 36px; border-radius: 8px; border: 1px solid var(--border-color-primary); background: var(--background-fill-primary); color: var(--body-text-color); padding: 0 6px; } .rm-status { font-size: 13px; color: var(--body-text-color-subdued); min-height: 1em; } """ GLOBAL_STYLE = """""" HERO_CSS = """ h1 { font-size: 36px; line-height: 1.15; margin: 6px 0 10px; font-weight: 700; letter-spacing: -0.01em; color: var(--body-text-color); } p { font-size: 18px; line-height: 1.55; max-width: 1000px; color: var(--body-text-color); margin: 0 0 6px; } """ STORY_CSS = """ .story { display: grid; grid-template-columns: repeat(auto-fit, minmax(380px, 1fr)); gap: 14px; } section { padding: 16px 20px; border-radius: 12px; background: var(--background-fill-secondary); border: 1px solid var(--border-color-primary); } section.caveat { background: transparent; border-style: dashed; } .kicker { margin-bottom: 6px; } h3 { font-size: 21px; line-height: 1.25; margin: 0 0 8px; font-weight: 700; color: var(--body-text-color); } p { font-size: 16.5px; line-height: 1.6; margin: 0 0 10px; color: var(--body-text-color); } p:last-child { margin-bottom: 0; } b { font-weight: 700; } .tag { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 11px; font-weight: 700; letter-spacing: .05em; text-transform: uppercase; } .tag-map { background: #cde2fb; color: #0d366b; } .tag-diff { background: linear-gradient(90deg, #fdb863, #b2abd2); color: #2d004b; } .sw { display: inline-block; width: 12px; height: 12px; border-radius: 3px; margin: 0 5px -1px 2px; } .sw.wet { background: #8073ac; } .sw.dry { background: #e08214; } """ MAPLIBRE = ('' '') def _day(value) -> str | None: if value in (None, ""): return None try: return pd.Timestamp(value).strftime("%Y-%m-%d") except (TypeError, ValueError): return None def _pair(left, right) -> tuple[str, str]: return DATASETS.get(left, "imerg"), DATASETS.get(right, "pardig") def _nearest_day(value, days) -> str: wanted = _day(value) if wanted is None: return days[-1] if wanted in days: return wanted return days[int(np.abs((pd.DatetimeIndex(days) - pd.Timestamp(wanted)).days).argmin())] def coverage(days) -> str: return f"Maps from {days[0]} to {days[-1]}" if days else "No maps yet" def show(region, day, left, right): """Rain over a region for 48 hours from a day: two datasets side by side and their difference. Args: region: a region name from the list. day: first day, YYYY-MM-DD. left: dataset on the left map (IMERG, PARDIG or ERA5, as in the dropdown). right: dataset on the right map. """ maps.DAYS.refresh() a, b = _pair(left, right) if a == b: raise gr.Error("Pick two different datasets.") days = maps.DAYS.for_sources(a, b) if not days: raise gr.Error(f"No maps stored yet for {left} and {right}.") box = REGIONS.get(region, REGIONS[FEATURED])[0] picked, start = _day(day), _nearest_day(day, days) if picked and picked != start: gr.Info(f"No maps of both for {picked} yet: showing {start}. {coverage(days)}.") return maps.payload(start, N_DAYS, tuple(float(v) for v in box), a, b) def choose_region(region, day): """A region with a featured date jumps to it; others keep the chosen day.""" featured_day = REGIONS.get(region, (None, None))[1] return gr.update(value=featured_day) if featured_day else gr.update(value=day) def step_day(day, step, left, right): days = maps.DAYS.for_sources(*_pair(left, right)) if not days: return gr.update() i = days.index(_nearest_day(day, days)) return gr.update(value=days[min(max(i + step, 0), len(days) - 1)]) def on_load(): maps.DAYS.refresh() return gr.update() with gr.Blocks(title="How much did it really rain? No one really knows.") as demo: gr.HTML(INTRO, css_template=HERO_CSS, apply_default_css=False, head=GLOBAL_STYLE) gr.HTML(PROBLEM, css_template=STORY_CSS, apply_default_css=False) with gr.Row(equal_height=True): left = gr.Dropdown(list(DATASETS), value=DEFAULT_LEFT, label="Left map", scale=3, min_width=220) right = gr.Dropdown(list(DATASETS), value=DEFAULT_RIGHT, label="Right map", scale=3, min_width=220) region = gr.Dropdown(list(REGIONS), value=FEATURED, label="Region", scale=4, min_width=260) day = gr.DateTime(value=REGIONS[FEATURED][1], include_time=False, type="string", label="Start day · plays 48 hours", scale=3, min_width=200, elem_id="start-day") prev_day = gr.Button("◀", scale=0, min_width=48) next_day = gr.Button("▶", scale=0, min_width=48) rain = gr.HTML(value="", html_template=COMPONENT_HTML, css_template=COMPONENT_CSS, js_on_load=(HERE / "rainmaps.js").read_text(), apply_default_css=False, head=MAPLIBRE) totals = gr.Markdown(elem_classes="summary-line") gr.HTML(STORY, css_template=STORY_CSS, apply_default_css=False) with gr.Accordion("About the data", open=False): gr.Markdown((HERE / "ABOUT.md").read_text()) gr.Markdown(FOOTER) inputs = [region, day, left, right] region.input(choose_region, [region, day], day, api_name=False).then( show, inputs, [rain, totals], api_name=False) day.change(show, inputs, [rain, totals], api_name="maps") for pick in (left, right): pick.input(show, inputs, [rain, totals], api_name=False) prev_day.click(lambda d, a, b: step_day(d, -1, a, b), [day, left, right], day, api_name=False) next_day.click(lambda d, a, b: step_day(d, 1, a, b), [day, left, right], day, api_name=False) demo.load(on_load, None, day, api_name=False).then(show, inputs, [rain, totals], api_name=False) if __name__ == "__main__": demo.launch(mcp_server=True)