"""The weather of each tracked station with a position, into data//weather/. Run by pump.sh after each pumping. One CSV per day that has data in the station's folder: data//weather/_weather_.csv, the hourly values of the MeteoSwiss station nearest to the position (weather/meteoswiss.py), UTC, columns: time_utc, position (the line of stations.txt), place (found), station, station_name, distance_km, height_m, then the measurements (meteoswiss.PARAMETERS). A day without MeteoSwiss values gets the header only. Fetched again: a missing day, a day written for another position, the last 2 days (incomplete, not yet quality-checked). A station without a position (an indoor site) or whose place is not found: no weather/ folder, a line in the log. Usage: python3 extraction/weather.py [--data DIR] [SERIAL...] (default: every tracked station, into data/; --data: another root with the tree of data/, e.g. data_diff/, which pump_diff.py passes) """ import csv import functools import re import sys from datetime import datetime, timedelta, timezone from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path[:0] = [str(ROOT / "extraction"), str(ROOT / "weather")] import meteoswiss # noqa: E402 import pumping # noqa: E402 DATA = ROOT / "data" DAY = re.compile(r"(? MeteoSwiss {where['station']} {where['name']}, " f"{where['distance_km']:.1f} km") def main(serials, data=DATA): tracked, now = pumping.tracked(), datetime.now(timezone.utc) for serial in serials or tracked: if not (Path(data) / serial).is_dir(): continue if not (position := tracked.get(serial)): print(f"{serial}: no position in stations.txt, no weather") continue try: station(serial, position, now, Path(data)) except (LookupError, OSError) as e: # the pumping goes on without the weather of this station print(f"{serial}: no weather: {e}") if __name__ == "__main__": args = sys.argv[1:] if args[:1] == ["--data"]: main(args[2:], Path(args[1])) else: main(args)