114 lines
3.5 KiB
Python
114 lines
3.5 KiB
Python
# scripts/plot_basic_variables.py
|
|
"""Génère des séries temporelles simples (7 jours) pour chaque variable météo."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
from pathlib import Path
|
|
import sys
|
|
|
|
import matplotlib.pyplot as plt
|
|
import pandas as pd
|
|
|
|
|
|
PROJECT_ROOT = Path(__file__).resolve().parents[3]
|
|
if str(PROJECT_ROOT) not in sys.path:
|
|
sys.path.insert(0, str(PROJECT_ROOT))
|
|
|
|
from meteo.dataset import load_raw_csv
|
|
from meteo.plots import export_plot_dataset
|
|
from meteo.variables import Variable, VARIABLES
|
|
|
|
|
|
CSV_PATH = Path("data/weather_minutely.csv")
|
|
DOC_DIR = Path(__file__).resolve().parent.parent
|
|
DEFAULT_OUTPUT_DIR = DOC_DIR / "figures"
|
|
|
|
|
|
def _prepare_slice(df: pd.DataFrame, *, last_days: int) -> pd.DataFrame:
|
|
"""Extrait la fenêtre temporelle souhaitée et applique une moyenne horaire pour lisser la courbe."""
|
|
|
|
end = df.index.max()
|
|
start = end - pd.Timedelta(days=last_days)
|
|
df_slice = df.loc[start:end]
|
|
numeric_slice = df_slice.select_dtypes(include="number")
|
|
if numeric_slice.empty:
|
|
raise RuntimeError("Aucune colonne numérique disponible pour les moyennes horaires.")
|
|
return numeric_slice.resample("1h").mean()
|
|
|
|
|
|
def _plot_variable(df_hourly: pd.DataFrame, var: Variable, output_dir: Path) -> Path | None:
|
|
"""Trace la série pour une variable et retourne le chemin de l'image générée."""
|
|
|
|
if var.column not in df_hourly.columns:
|
|
print(f"⚠ Colonne absente pour {var.key} ({var.column}).")
|
|
return None
|
|
|
|
series = df_hourly[var.column].dropna()
|
|
if series.empty:
|
|
print(f"⚠ Aucun point valide pour {var.key} dans l'intervalle choisi.")
|
|
return None
|
|
|
|
output_dir.mkdir(parents=True, exist_ok=True)
|
|
output_path = output_dir / f"{var.key}_last_7_days.png"
|
|
|
|
export_plot_dataset(series.to_frame(name=var.column), output_path)
|
|
|
|
plt.figure()
|
|
plt.plot(series.index, series)
|
|
plt.xlabel("Temps (UTC)")
|
|
unit_text = f" ({var.unit})" if var.unit else ""
|
|
plt.ylabel(f"{var.label}{unit_text}")
|
|
plt.title(f"{var.label} - Moyenne horaire sur les 7 derniers jours")
|
|
plt.grid(True)
|
|
plt.tight_layout()
|
|
plt.savefig(output_path, dpi=150)
|
|
plt.close()
|
|
print(f"✔ Graphique généré : {output_path}")
|
|
return output_path
|
|
|
|
|
|
def main(argv: list[str] | None = None) -> None:
|
|
parser = argparse.ArgumentParser(description="Trace les séries simples pour chaque variable météo.")
|
|
parser.add_argument(
|
|
"--only",
|
|
nargs="*",
|
|
help="Clés de variables à tracer (par défaut : toutes).",
|
|
)
|
|
parser.add_argument(
|
|
"--days",
|
|
type=int,
|
|
default=7,
|
|
help="Nombre de jours à afficher (par défaut : 7).",
|
|
)
|
|
parser.add_argument(
|
|
"--output-dir",
|
|
type=Path,
|
|
default=DEFAULT_OUTPUT_DIR,
|
|
help="Dossier où stocker les figures.",
|
|
)
|
|
args = parser.parse_args(argv)
|
|
|
|
if not CSV_PATH.exists():
|
|
raise FileNotFoundError(f"Dataset introuvable : {CSV_PATH}")
|
|
|
|
df = load_raw_csv(CSV_PATH)
|
|
df_hourly = _prepare_slice(df, last_days=args.days)
|
|
|
|
selected: list[Variable]
|
|
if args.only:
|
|
keys = set(args.only)
|
|
selected = [var for var in VARIABLES if var.key in keys]
|
|
missing = keys - {var.key for var in selected}
|
|
if missing:
|
|
raise KeyError(f"Variables inconnues : {sorted(missing)}")
|
|
else:
|
|
selected = list(VARIABLES)
|
|
|
|
for variable in selected:
|
|
_plot_variable(df_hourly, variable, args.output_dir)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|