80 lignes
2.9 KiB
Python
80 lignes
2.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Importe l'historique Infoclimat des stations du Vercors depuis la collecte GWS (lecture seule) dans l'archive.
|
|
|
|
Source : /mnt/hdd3/gws-meteo/infoclimat/<id Infoclimat>/*.json.gz (clé "hourly" : {id: [relevés]}, valeurs en texte).
|
|
Cible : base SQLite de l'archive (table obs, pas de 10 min), via archiver_historique.ingerer :
|
|
une valeur vide n'écrase jamais une valeur connue, ré-import sans doublon.
|
|
Usage : python3 importer_infoclimat_hdd3.py --db ~/meteo-vercors/archive/vercors.db [--source /mnt/hdd3/gws-meteo/infoclimat]
|
|
"""
|
|
import argparse
|
|
import gzip
|
|
import json
|
|
import os
|
|
import sys
|
|
import time
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
import archiver_historique as arch # noqa: E402
|
|
from extraire_infoclimat import STATIONS # noqa: E402
|
|
|
|
|
|
def nombre(v):
|
|
try:
|
|
return float(v)
|
|
except (TypeError, ValueError):
|
|
return None
|
|
|
|
|
|
def releves(fichier, id_ic):
|
|
with gzip.open(fichier, "rt", encoding="utf-8") as f:
|
|
d = json.load(f)
|
|
lignes = (d.get("hourly") or {}).get(id_ic) or []
|
|
out = []
|
|
for r in lignes:
|
|
if not isinstance(r, dict) or not isinstance(r.get("dh_utc"), str):
|
|
continue
|
|
out.append({k: (v if k == "dh_utc" else nombre(v)) for k, v in r.items()})
|
|
return out
|
|
|
|
|
|
def main(argv=None):
|
|
ap = argparse.ArgumentParser()
|
|
ap.add_argument("--db", required=True)
|
|
ap.add_argument("--source", default="/mnt/hdd3/gws-meteo/infoclimat")
|
|
a = ap.parse_args(argv)
|
|
db = arch.ouvrir(a.db)
|
|
db.execute("PRAGMA busy_timeout = 60000") # le minuteur écrit aussi dans la base toutes les 6 min
|
|
total = 0
|
|
for site, id_ic in STATIONS.items():
|
|
dossier = os.path.join(a.source, id_ic)
|
|
if not os.path.isdir(dossier):
|
|
print("%-13s %-11s aucun dossier" % (site, id_ic))
|
|
continue
|
|
n_site, premier = 0, None
|
|
for nom in sorted(os.listdir(dossier)):
|
|
if not nom.endswith(".json.gz"):
|
|
continue
|
|
try:
|
|
rs = releves(os.path.join(dossier, nom), id_ic)
|
|
except (OSError, ValueError, EOFError) as e:
|
|
print(" illisible :", id_ic, nom, type(e).__name__)
|
|
continue
|
|
if rs and premier is None:
|
|
premier = rs[0]["dh_utc"][:10]
|
|
for essai in range(5):
|
|
try:
|
|
n_site += arch.ingerer(db, {"stations": {site: {"releves": rs}}})
|
|
break
|
|
except Exception as e: # base occupée par le minuteur : on réessaie
|
|
if "locked" not in str(e) or essai == 4:
|
|
raise
|
|
time.sleep(5)
|
|
total += n_site
|
|
print("%-13s %-11s %8d relevés, depuis %s" % (site, id_ic, n_site, premier or "—"), flush=True)
|
|
print("total :", total)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|