- Python 94.7%
- Shell 5.3%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| .github | ||
| config | ||
| packages | ||
| scripts | ||
| src/lightning_anemoi_dataset | ||
| tests | ||
| .gitignore | ||
| .gitmodules | ||
| LICENSE | ||
| pyproject.toml | ||
| README.md | ||
| uv.lock | ||
lightning-anemoi-dataset
Anemoi dataset recipes and preprocessing code for MeteoSwiss THX lightning data on the INCA/RADAR 710×640 domain.
Getting started
Requires Python 3.12+. Install everything with uv:
git clone --recurse-submodules https://github.com/MeteoSwiss/lightning-anemoi-dataset
cd lightning-anemoi-dataset
uv sync
source .venv/bin/activate
The workspace installs three packages into .venv:
| Package | Source |
|---|---|
lightning-anemoi-dataset |
src/lightning_anemoi_dataset/ (this repo) |
anemoi-plugins-meteoswiss |
packages/anemoi-plugins-meteoswiss/ (submodule) |
anemoi-datasets |
packages/anemoi-datasets/ (submodule) |
Overview
Data source
Raw lightning data comes from the Vaisala THX network, archived at MeteoSwiss under:
/store_new/mch/msclim/hamann/database/lightning/THX/THX{YY}{JJJ}0000.prd[.zip]
Files are in a flat directory (no subdirectories). The filename encodes a 2-digit year and 3-digit day-of-year, e.g. THX25213 = 2025 day 213 = 2025-08-01.
Format: pipe-separated (|), one strike per row:
date time UTC | lon | lat | current | nS | mode | intra | Ax | Ki2 | Exc | Incl | Arc | ...
Key fields:
| Field | Description |
|---|---|
date time UTC |
Timestamp to millisecond precision, e.g. 01.01.2020 00:01:23.456 UTC |
lon / lat |
Geographic coordinates in decimal degrees (WGS84) |
current |
Peak current in kA (positive = positive stroke) |
intra |
Discharge type: 0 = cloud-to-ground (CG), 1 = intra-cloud (IC) |
Coverage: 2005-01-01 to present (~4,000 zipped + ~300 uncompressed files).
Network extent: approximately lat ∈ [42.5, 50.2], lon ∈ [0.0, 17.0] (central Europe, centred on Switzerland).
Grid variants
| Variant | Grid | Cells | CRS | Config suffix |
|---|---|---|---|---|
| ICON | ICON-CH1 R19B08 native unstructured | 1,147,980 | WGS84 | (none) |
| INCA/RADAR | INCA/RADAR 710×640 regular | 454,400 | CH1903+/LV95 (EPSG:2056) | -inca |
Preprocessing
Preprocessing converts raw .prd strike files into daily NetCDF files written to a shared output directory, then consumed by anemoi-datasets.
sbatch scripts/preprocess.sh icon # → lightning_icon_YYYYMMDD.nc
sbatch scripts/preprocess.sh inca # → lightning_inca_YYYYMMDD.nc
Output directories are set in config/preprocess/icon.yaml and config/preprocess/inca.yaml.
Configuration keys
| Key | Default | Description |
|---|---|---|
start / stop |
— | Date range (UTC, inclusive). Format: YYYYMMDD or YYYY-MM-DD. |
grid_type |
icon |
Target grid: icon (ICON-CH1 native) or inca (710×640 LV95 regular). |
icon_grid_file |
/store_new/mch/msclim/pronos/icon_grid_0001_R19B08_mch.nc |
ICON-CH1 R19B08 native grid file. Only used when grid_type: icon. |
granularity |
10min |
Accumulation window and timestep per output file. Options: 10min, 60min, 24h. |
discharge_type |
[CG, IC] |
Lightning types to include. CG = cloud-to-ground (intra=0), IC = intra-cloud (intra=1). |
radius_km |
8.0 |
Accumulation radius in km. Accepts a single value or a list (e.g. [4, 8, 16]). All radii are stored together under a radius dimension — the KD-tree is queried once per time step and thresholded per radius. 0 = nearest-neighbour only. |
network_bbox |
[42.5, 50.2, 0.0, 17.0] |
THX network extent as [lat_min, lat_max, lon_min, lon_max]. Cells outside this box are masked. |
archive_path |
/store_new/mch/msclim/hamann/database/lightning/THX |
Root of the THX archive tree. Both .prd and .prd.zip files are supported. |
output_dir |
. |
Shared directory where daily NetCDF files are written. Created if absent. |
Distance computation
ICON — chord distance on the unit sphere. Cell centres and strikes are converted to 3D Cartesian (x, y, z) coordinates; the KD-tree returns the Euclidean (chord) distance, which is a monotone proxy for great-circle distance. The radius cutoff converts km → chord length via 2 × sin(radius_km / 111.195°).
INCA/RADAR — Euclidean distance in projected metres. Strike coordinates are converted from WGS84 to LV95 (EPSG:2056) via pyproj; the KD-tree operates directly in metre space. Accurate because LV95 has very low metric distortion over the Alpine domain.
One query_ball_point call is made per radius per time step. Each strike increments all cells within the radius (neighbourhood count), not just the nearest cell.
Domain tessellation filter
Strikes that fall outside the grid's Voronoi tessellation are dropped before binning. Without this, boundary cells accumulate contributions from outside-domain strikes that are shared among fewer cells, inflating their counts relative to interior cells (most visible at 16 km radius).
- INCA: exact bounding box — strikes must lie within ±½ cell of the outermost cell centres in LV95 metres (half-diagonal ≈ 0.71 km for the 1 km grid).
- ICON: nearest-cell chord distance filter — the tessellation radius is estimated at construction time as half the maximum nearest-neighbour distance among a strided sample of cell centres.
Output format
ICON (grid_type: icon):
lightning_icon_YYYYMMDD.nc
dims: time=144 (unlimited), radius=N, ncells=1_147_980
vars: lightning_count(time, radius, ncells) [float32, fill=NaN]
coords: clat(ncells), clon(ncells), time(UTC), radius(km), time_bnds(time, bnds)
INCA/RADAR (grid_type: inca):
lightning_inca_YYYYMMDD.nc
dims: time=144 (unlimited), radius=N, y=640, x=710
vars: lightning_count(time, radius, y, x) [float32, fill=NaN]
coords: x(m, LV95), y(m, LV95), lat(y,x), lon(y,x), time(UTC), radius(km), time_bnds(time, bnds)
mapping: swiss_lv95_coordinates (oblique Mercator, EPSG:2056)
Both are written to the same directory (/store_new/mch/msclim/lightning_gridded).
Output follows the MeteoSwiss data model standard: CF-1.8, float32 encoding, NaN fill values, cell_method: "time: sum (bnds: end, interval: 10min)", MeteoSwiss global attributes (institution, history, references).
Select a specific radius with xarray: ds.sel(radius=8.0).
Typical wall-time estimates (10-min granularity, radius_km: [4, 8, 16])
| Period | Approx. wall time |
|---|---|
| 1 day | ~75 s |
| 1 month | ~40 min |
| 1 year | ~8 h |
The lightning-grid CLI is implemented in src/lightning_anemoi_dataset/:
src/lightning_anemoi_dataset/
config.py Config dataclass and YAML loader
extract.py Raw .prd / .prd.zip reader
grids/
icon.py ICON-CH1 KD-tree spatial binning
inca.py INCA/RADAR LV95 KD-tree spatial binning
accumulate.py Time-bin strikes into count arrays
output.py Write CF-1.8 compliant NetCDF
cli.py lightning-grid entry point
Anemoi plugin
The thx-lightning anemoi source plugin lives in packages/anemoi-plugins-meteoswiss/:
src/anemoi_plugins_meteoswiss/transform/sources/thx_lightning.py
It reads the preprocessed daily NetCDF files and exposes them as earthkit FieldList objects for anemoi-datasets.
Creating a dataset
- Run preprocessing (once, idempotent — skips existing files):
sbatch scripts/preprocess.sh icon # ICON native grid
sbatch scripts/preprocess.sh inca # INCA/RADAR grid
- Build the anemoi dataset:
sbatch scripts/build.sh config/varda-lightning-thxjoin-1km-2005-2025-10min-v1-inca.yaml
sbatch scripts/build.sh config/varda-lightning-thxjoin-1km-2005-2025-10min-v1.yaml
# with explicit output directory:
sbatch scripts/build.sh config/varda-lightning-thxjoin-1km-2005-2025-10min-v1-inca.yaml /store_new/mch/msclim/anemoi-datasets
The output zarr name is taken from the name: field in the config.
The preprocessed NetCDF files contain all three radii ([4, 8, 16] km) under a radius dimension, so no reprocessing is needed to build a different config.
Available configs:
| Config | Grid | Period | Channels |
|---|---|---|---|
config/varda-lightning-thxjoin-1km-2005-2025-10min-v1-inca.yaml |
INCA | 2005–2025 | lightning_count_4km, lightning_count_8km, lightning_count_16km |
config/varda-lightning-thxjoin-1km-2025-10min-v1-inca.yaml |
INCA | 2025 | lightning_count_4km, lightning_count_8km, lightning_count_16km |
config/varda-lightning-thxjoin-1km-2005-2025-10min-v1.yaml |
ICON | 2005–2025 | lightning_count_4km, lightning_count_8km, lightning_count_16km |
config/varda-lightning-thxjoin-1km-2025-10min-v1.yaml |
ICON | 2025 | lightning_count_4km, lightning_count_8km, lightning_count_16km |
Debug configs for a quick smoke test (6 hours, 2025-08-01 — requires preprocessed files):
limit coredumpsize 0
env MALLOC_CHECK_=0 anemoi-datasets create config/debug/lightning-inca.yaml /tmp/test-inca.zarr
env MALLOC_CHECK_=0 anemoi-datasets create config/debug/lightning-icon.yaml /tmp/test-icon.zarr
The process prints
munmap_chunk(): invalid pointerand exits with SIGABRT during Python shutdown — this is a C extension cleanup bug, not a dataset error. The zarr is fully written before the crash.limit coredumpsize 0prevents writing a core file. In tcsh,VAR=value cmdis not valid syntax — useenv VAR=value cmdorsetenv VAR valuefirst.
Inspecting a dataset
# Summary: shape, dates, frequency, variables, statistics
anemoi-datasets inspect /path/to/dataset.zarr
from anemoi.datasets import open_dataset
ds = open_dataset("/path/to/dataset.zarr")
print(ds) # summary
print(ds.dates) # all timestamps
print(ds.variables) # ['lightning_count_4km', 'lightning_count_8km', 'lightning_count_16km']
print(ds.latitudes.shape) # (454400,) for INCA, (1147980,) for ICON
# Raw zarr access
import zarr
z = zarr.open("/path/to/dataset.zarr")
print(z.tree()) # full array tree
print(z.attrs["resolution"]) # '1km'
print(z["data"].shape) # (time, variable, ensemble, cell)
Pre-generation checklist
Before generating a production dataset, verify:
- All changes are committed and pushed (ideally merged to
main) - Config filename follows the anemoi naming convention
- Submodules point to the right commits (ideally merged to
main) - An annotated Git tag has been pushed (e.g.
v1-inca) - A GitHub release has been created from the tag with a dataset description
Versioning
A dataset is a reproducible build artifact. Its exact content is determined by:
- The recipe — the YAML config under
config/passed toanemoi-datasets - The code — the pinned commits of the
anemoi-datasetsandanemoi-plugins-meteoswisssubmodules, plussrc/lightning_anemoi_dataset/
Both must be versioned together via a Git tag before running a production build.
varda-lightning-thx-1km-2005-2025-10min-v1 → tag: v1
varda-lightning-thx-1km-2005-2025-10min-v1-inca → tag: v1-inca
varda-lightning-thx-1km-2025-10min-v1 → tag: v1-2025
varda-lightning-thx-1km-2025-10min-v1-inca → tag: v1-inca-2025
Dataset naming
Follows the anemoi naming convention:
purpose-content-source-resolution-start-end-frequency-version[-extra-str]
| Component | Value |
|---|---|
| purpose | varda |
| content | lightning |
| source | thx (all three radii as channels) |
| resolution | 1km |
| start / end | 2005 / 2025 (or 2025 / 2025 for the single-year config) |
| frequency | 10min |
| version | v1 |
| extra-str | (none) for ICON; -inca for INCA/RADAR variant |