File size: 1,858 Bytes
1aeffbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Generate a small synthetic dataset description for local smoke tests."""

import argparse
import json
from pathlib import Path

import numpy as np
import yaml

try:
    import h5py
except ImportError as exc:
    raise SystemExit("fake_data.py requires h5py; install it in the active OneScience environment") from exc

ROOT = Path(__file__).resolve().parents[1]


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--config", default=str(ROOT / "conf/config.yaml"))
    parser.add_argument("--output", default=None)
    args = parser.parse_args()
    with open(args.config, encoding="utf-8") as handle:
        config = yaml.safe_load(handle)
    metadata = {
        "variables": config["data"]["channels"],
        "grid": config["data"]["grid"],
        "input_steps": config["data"]["input_steps"],
        "output_steps": config["data"]["output_steps"],
        "time_resolution": "1 day",
        "source": "synthetic; not GLORYS12 values",
    }
    data_root = ROOT / config["data"]["data_dir"] / "data"
    data_root.mkdir(parents=True, exist_ok=True)
    fields = np.random.default_rng(config["project"]["seed"]).standard_normal(
        (config["data"]["synthetic_samples"] + config["data"]["input_steps"],
         len(config["data"]["channels"]), *config["data"]["grid"]), dtype=np.float32
    )
    with h5py.File(data_root / "2000.h5", "w") as handle:
        dataset = handle.create_dataset("fields", data=fields)
        dataset.attrs["variables"] = config["data"]["channels"]
        dataset.attrs["time_step"] = 24
    output = Path(args.output or ROOT / config["data"]["data_dir"] / "synthetic_metadata.json")
    output.parent.mkdir(parents=True, exist_ok=True)
    output.write_text(json.dumps(metadata, indent=2) + "\n", encoding="utf-8")
    print(f"saved={output}")


if __name__ == "__main__":
    main()