Datasets:
License:
File size: 3,552 Bytes
74ab36d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | #!/usr/bin/env python3
"""Validate the standardized OneScience/CMEMS dataset package."""
from __future__ import annotations
import argparse
import hashlib
from pathlib import Path
import h5py
import numpy as np
import yaml
YEARS = [1993, 1994, 1995, 1996, 1997, 1998, 1999]
EXPECTED_FIELD_SHAPE = (3, 96, 2041, 4320)
EXPECTED_STATS_SHAPE = (1, 96, 1, 1)
def sha256(path: Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as handle:
for chunk in iter(lambda: handle.read(1024 * 1024 * 16), b""):
digest.update(chunk)
return digest.hexdigest()
def load_schema(path: Path) -> dict:
with path.open("r", encoding="utf-8") as handle:
return yaml.safe_load(handle)
def load_checksums(path: Path) -> dict[str, str]:
checksums = {}
with path.open("r", encoding="utf-8") as handle:
for line in handle:
if not line.strip():
continue
digest, relpath = line.strip().split(maxsplit=1)
checksums[relpath] = digest
return checksums
def normalize_attr_strings(values) -> list[str]:
return [value.decode() if isinstance(value, bytes) else str(value) for value in values]
def main() -> None:
parser = argparse.ArgumentParser(description="Check CMEMS HDF5 files, schema, attrs and optional checksums.")
parser.add_argument("--dataset-root", default=".")
parser.add_argument("--verify-checksums", action="store_true")
args = parser.parse_args()
root = Path(args.dataset_root).resolve()
schema = load_schema(root / "metadata" / "schema.yaml")
expected_variables = schema["variables"]
checksums = load_checksums(root / "metadata" / "file_checksums.sha256")
for year in YEARS:
relpath = f"data/{year}.h5"
path = root / relpath
if not path.exists():
raise FileNotFoundError(f"missing file: {path}")
if path.stat().st_size != 10157335296:
raise AssertionError(f"{path} size mismatch: {path.stat().st_size}")
with h5py.File(path, "r") as handle:
for key in ("fields", "global_means", "global_stds"):
if key not in handle:
raise KeyError(f"{path} missing dataset {key}")
fields = handle["fields"]
if tuple(fields.shape) != EXPECTED_FIELD_SHAPE:
raise AssertionError(f"{path} fields shape {fields.shape}")
if fields.dtype != np.dtype("float32"):
raise AssertionError(f"{path} fields dtype {fields.dtype}")
if int(fields.attrs["time_step"]) != 24:
raise AssertionError(f"{path} time_step attr mismatch")
variables = normalize_attr_strings(fields.attrs["variables"])
if variables != expected_variables:
raise AssertionError(f"{path} variables attr does not match schema")
for stat_key in ("global_means", "global_stds"):
stat = handle[stat_key]
if tuple(stat.shape) != EXPECTED_STATS_SHAPE:
raise AssertionError(f"{path} {stat_key} shape {stat.shape}")
if stat.dtype != np.dtype("float32"):
raise AssertionError(f"{path} {stat_key} dtype {stat.dtype}")
if args.verify_checksums:
actual = sha256(path)
if actual != checksums[relpath]:
raise AssertionError(f"{path} sha256 mismatch: {actual} != {checksums[relpath]}")
print("CMEMS dataset validation passed.")
if __name__ == "__main__":
main()
|