File size: 2,094 Bytes
49f3de8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Probe Pangeo CMIP6 for DCPP dcppA-hindcast datasets; cache the subset."""
from __future__ import annotations

import csv
import io
import re
import urllib.request
from collections import defaultdict
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent
CACHE = ROOT / "data" / "external" / "pangeo_dcpp_subset.csv"
URL = "https://storage.googleapis.com/cmip6/pangeo-cmip6.csv"


def main() -> None:
    if not CACHE.exists():
        CACHE.parent.mkdir(parents=True, exist_ok=True)
        rows = []
        with urllib.request.urlopen(URL, timeout=300) as r:
            rd = csv.DictReader(io.TextIOWrapper(r, encoding="utf-8"))
            fields = rd.fieldnames
            for row in rd:
                if row.get("activity_id") == "DCPP" and row.get("experiment_id") == "dcppA-hindcast":
                    rows.append(row)
        with CACHE.open("w", newline="", encoding="utf-8") as f:
            w = csv.DictWriter(f, fieldnames=fields)
            w.writeheader()
            w.writerows(rows)
        print(f"cached {len(rows)} DCPP rows -> {CACHE}")
    starts = defaultdict(set)
    members = defaultdict(set)
    zstores = {}
    with CACHE.open(encoding="utf-8") as f:
        rd = csv.DictReader(f)
        print("catalog fields:", rd.fieldnames)
        for row in rd:
            if row["variable_id"] != "tas":
                continue
            year = row.get("dcpp_init_year") or ""
            m = re.search(r"/s(\d{4})-", row["zstore"])
            start = int(year) if year.isdigit() else (int(m.group(1)) if m else None)
            if start is None:
                continue
            starts[row["source_id"]].add(start)
            members[row["source_id"]].add(row["member_id"])
            zstores.setdefault(row["source_id"], row["zstore"])
    for src in sorted(starts):
        yrs = sorted(starts[src])
        print(f"{src:22s} starts {yrs[0]}-{yrs[-1]} n={len(yrs)} members={sorted(members[src])}")
        print(f"   {zstores[src]}")


if __name__ == "__main__":
    main()