File size: 2,005 Bytes
6eff894
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os
from shutil import copyfile


RESULTS_DIR = "results"
ASSETS_DIR = "assets"
TEMPS_RESULTS = os.path.join(RESULTS_DIR, "temps.png")
PRECIP_RESULTS = os.path.join(RESULTS_DIR, "precip.png")
TEMPS_ASSET = os.path.join(ASSETS_DIR, "temps.png")
PRECIP_ASSET = os.path.join(ASSETS_DIR, "precip.png")

os.environ.setdefault("MPLCONFIGDIR", os.path.join(RESULTS_DIR, ".matplotlib"))
os.environ.setdefault("XDG_CACHE_HOME", os.path.join(RESULTS_DIR, ".cache"))

import matplotlib

matplotlib.use("Agg")
import matplotlib.pyplot as plt
import pandas as pd


def ensure_dirs():
    os.makedirs(RESULTS_DIR, exist_ok=True)
    os.makedirs(ASSETS_DIR, exist_ok=True)
    os.makedirs(os.environ["MPLCONFIGDIR"], exist_ok=True)
    os.makedirs(os.environ["XDG_CACHE_HOME"], exist_ok=True)


def mirror_asset(src: str, dest: str) -> None:
    copyfile(src, dest)


def main():
    with open("data/weather.json") as handle:
        data = json.load(handle)

    days = pd.to_datetime(data["daily"]["time"])
    tmax = pd.Series(data["daily"]["temperature_2m_max"])
    tmin = pd.Series(data["daily"]["temperature_2m_min"])
    prec = pd.Series(data["daily"].get("precipitation_sum", [0] * len(days)))

    ensure_dirs()

    plt.figure()
    plt.plot(days, tmax, marker="o", label="Max °C")
    plt.plot(days, tmin, marker="o", label="Min °C")
    plt.xticks(rotation=45, ha="right")
    plt.title("Daily Temperatures (°C)")
    plt.legend()
    plt.tight_layout()
    plt.savefig(TEMPS_RESULTS)
    plt.close()

    mirror_asset(TEMPS_RESULTS, TEMPS_ASSET)

    # Precipitation bar chart
    plt.figure()
    plt.bar(days, prec)
    plt.xticks(rotation=45, ha="right")
    plt.title("Daily Precipitation (mm)")
    plt.tight_layout()
    plt.savefig(PRECIP_RESULTS)
    plt.close()

    mirror_asset(PRECIP_RESULTS, PRECIP_ASSET)

    print(f"✅ Wrote {TEMPS_RESULTS} / {PRECIP_RESULTS}")
    print(f"✅ Updated assets at {TEMPS_ASSET} / {PRECIP_ASSET}")

if __name__ == "__main__":
    main()