#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Quick integrity checks for the full SeismicX-Cont release folder.""" from __future__ import annotations import argparse import json import sqlite3 from pathlib import Path import h5py def count_h5_segments(path: Path) -> tuple[int, int, int]: segments = 0 stations = set() channels = set() with h5py.File(path, "r") as h5: for year_id in h5: year = h5[year_id] if not isinstance(year, h5py.Group): continue for day_id in year: day = year[day_id] if not isinstance(day, h5py.Group) or "stations" not in day: continue for station_id, station_grp in day["stations"].items(): stations.add(station_id) waveform = station_grp.get("waveform") if waveform is None: continue for channel, channel_grp in waveform.items(): if not isinstance(channel_grp, h5py.Group): continue channels.add(channel) segments += sum(1 for key in channel_grp if key.isdigit()) return segments, len(stations), len(channels) def main() -> None: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--root", type=Path, default=Path(__file__).resolve().parents[1]) parser.add_argument("--scan-hdf5", action="store_true", help="Open each HDF5 file and count waveform segments.") args = parser.parse_args() root = args.root h5_files = sorted((root / "data" / "hdf5").glob("continuous_waveform_usa_*.h5")) label_json = root / "data" / "label" / "annotations_for_continuous_hdf5.json" db_path = root / "data" / "index" / "waveform_index.sqlite" print(f"HDF5 files: {len(h5_files)}") print(f"HDF5 total size: {sum(p.stat().st_size for p in h5_files) / 1024**3:.2f} GiB") if args.scan_hdf5: total_segments = 0 for path in h5_files: segments, stations, channels = count_h5_segments(path) total_segments += segments print(f" {path.name}: segments={segments:,}, stations={stations:,}, channels={channels:,}") print(f"HDF5 scanned segments: {total_segments:,}") with label_json.open("r", encoding="utf-8") as f: labels = json.load(f) print("Annotation summary:") print(json.dumps(labels.get("summary", {}), ensure_ascii=False, indent=2)) conn = sqlite3.connect(db_path) cur = conn.cursor() n_files = cur.execute("SELECT COUNT(*) FROM hdf5_files").fetchone()[0] n_segments = cur.execute("SELECT COUNT(*) FROM waveform_segments").fetchone()[0] n_stations = cur.execute("SELECT COUNT(*) FROM stations").fetchone()[0] conn.close() print("SQLite index:") print(f" files={n_files}, waveform_segments={n_segments:,}, stations={n_stations:,}") if __name__ == "__main__": main()