File size: 4,346 Bytes
a5899e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Push Central Asia outputs to HF.

    python scripts/50_push_ca.py cube        # cells.parquet, monthly/, manifest.json, aoi -> TrendeX90/ca-cube
    python scripts/50_push_ca.py formula     # backend outputs -> TrendeX90/ca-cube/formula/
    python scripts/50_push_ca.py products    # runs/v2_ca/products -> TrendeX90/ca-cube/products/
    python scripts/50_push_ca.py model       # runs/v2_ca/hf_model -> TrendeX90/expected-greenness-ca (tag v2-ca)
"""
from __future__ import annotations

import argparse
import json
from pathlib import Path

from huggingface_hub import HfApi

ROOT = Path("/root/trendex-ca")
CUBE, RUN, BACKEND = ROOT / "data" / "cube", ROOT / "runs" / "v2_ca", Path("/root/trendex/backend")
CUBE_REPO, MODEL_REPO = "TrendeX90/ca-cube", "TrendeX90/expected-greenness-ca"


def card(manifest: dict) -> str:
    status = "PARTIAL - NDVI only" if manifest.get("partial") else "full"
    units = "\n".join(f"- `{k}`: {v}" for k, v in manifest.get("units", {}).items())
    data = "\n".join(f"- {k} v{v}" for k, v in manifest.get("datasets", {}).items())
    notes = "\n".join(f"- {n}" for n in manifest.get("notes", []))
    cells = manifest.get("cells", {})
    return f"""---
license: other
pretty_name: TrendeX Central Asia cube
tags: [nasa, modis, ndvi, imerg, gldas, central-asia, pastures]
---
# TrendeX Central Asia cube ({status})

Kazakhstan, Kyrgyzstan, Tajikistan, Turkmenistan, Uzbekistan on the global MODIS sinusoidal 1 km grid
({cells.get('n', '?'):,} cells). Same layout as TrendeX90/naryn-cube; **synthetic = false**.

- `cells.parquet`: cell_id, row, col, x, y, lon, lat, country (ISO3), region (ADM1 shapeName), block_id / block_row / block_col,
  terrain (NASADEM 30 m -> 1 km), land cover (MCD12Q1 2020) and fractions, mask_reason.
- `monthly/year=YYYY/part.parquet`: Apr-Oct rows, `pd.read_parquet("monthly/")`.
- `products/`, `formula/`: model and formula outputs (when present).

## Units
{units}

## Data
{data}

## Notes
{notes}
"""


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("what", choices=["cube", "formula", "products", "model"])
    a = ap.parse_args()
    api = HfApi()
    if a.what == "cube":
        m = json.loads((CUBE / "manifest.json").read_text())
        (CUBE / "README.md").write_text(card(m))
        api.upload_folder(repo_id=CUBE_REPO, repo_type="dataset", folder_path=str(CUBE),
                          allow_patterns=["cells.parquet", "manifest.json", "README.md", "monthly/**"],
                          commit_message=f"Central Asia cube ({'partial' if m.get('partial') else 'full'})")
        api.upload_folder(repo_id=CUBE_REPO, repo_type="dataset", folder_path=str(ROOT / "data" / "aoi"),
                          path_in_repo="aoi", allow_patterns=["aoi_request.geojson", "aoi_summary.json"],
                          commit_message="area of interest")
    elif a.what == "formula":
        for local, remote in ((BACKEND / "export" / "ca" / "v1" / "fe", "formula/export/ca/v1/fe"),
                              (BACKEND / "export_ca" / "v1", "formula/export_ca/v1"),
                              (BACKEND / "artifacts_ca", "formula/artifacts_ca")):
            if local.exists():
                api.upload_folder(repo_id=CUBE_REPO, repo_type="dataset", folder_path=str(local), path_in_repo=remote,
                                  ignore_patterns=["*.npy", "*.npz"], commit_message=f"formula outputs: {remote}")
                print("pushed", remote, flush=True)
            else:
                print("missing", local, flush=True)
    elif a.what == "products":
        api.upload_folder(repo_id=CUBE_REPO, repo_type="dataset", folder_path=str(RUN / "products"),
                          path_in_repo="products", commit_message="model v2-ca products")
    else:
        if not api.repo_exists(MODEL_REPO):
            api.create_repo(MODEL_REPO, private=False)
        api.upload_folder(repo_id=MODEL_REPO, folder_path=str(RUN / "hf_model"),
                          ignore_patterns=["__pycache__/*", "*.pyc", ".pytest_cache/*"], commit_message="expected greenness v2-ca")
        if "v2-ca" not in [t.name for t in api.list_repo_refs(MODEL_REPO).tags]:
            api.create_tag(MODEL_REPO, tag="v2-ca", tag_message="protocol model_v2_ca")
    print(f"PUSHED_{a.what.upper()}", flush=True)


if __name__ == "__main__":
    main()