166 lines
No EOL
5.9 KiB
Python
166 lines
No EOL
5.9 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Exemples d'utilisation du wrapper AtmoDataClient
|
|
"""
|
|
|
|
from atmo_data_wrapper import AtmoDataClient, AtmoDataException
|
|
from atmo_data_wrapper import AASQA_CODES, POLLUANTS
|
|
from datetime import datetime, timedelta
|
|
|
|
def main():
|
|
# Initialisation du client
|
|
client = AtmoDataClient()
|
|
|
|
# Authentification automatique avec credentials.json
|
|
try:
|
|
success = client.auto_login()
|
|
if not success:
|
|
print("Échec de l'authentification")
|
|
return
|
|
print("Connexion réussie avec credentials.json !")
|
|
except AtmoDataException as e:
|
|
print(f"Erreur d'authentification: {e}")
|
|
print("💡 Assurez-vous d'avoir créé le fichier credentials.json")
|
|
print(" Utilisez credentials.json.example comme modèle")
|
|
return
|
|
|
|
# Exemple 1: Récupération des indices ATMO d'aujourd'hui
|
|
print("\n=== Indices ATMO d'aujourd'hui ===")
|
|
try:
|
|
today = datetime.now().strftime("%Y-%m-%d")
|
|
indices = client.get_indices_atmo(
|
|
date=today,
|
|
aasqa="11", # Spécifier une région pour éviter erreur serveur
|
|
format="geojson"
|
|
)
|
|
print(f"Nombre d'indices récupérés: {len(indices)}")
|
|
print(f"Résumé: {indices.to_summary()}")
|
|
|
|
# Exemples avec objets typés
|
|
if len(indices) > 0:
|
|
first_indice = indices[0]
|
|
print(f"Premier indice - Zone: {first_indice.lib_zone}")
|
|
print(f"Qualité: {first_indice.get_qualificatif()}")
|
|
|
|
except AtmoDataException as e:
|
|
print(f"Erreur: {e}")
|
|
|
|
# Exemple 2: Récupération des épisodes de pollution en cours
|
|
print("\n=== Épisodes de pollution en cours ===")
|
|
try:
|
|
episodes = client.get_episodes_3jours(
|
|
format="geojson",
|
|
aasqa="11", # Île-de-France
|
|
polluant=POLLUANTS[2] # PM10
|
|
)
|
|
print(f"Nombre d'épisodes: {len(episodes)}")
|
|
|
|
# Analyser les alertes
|
|
alerts_actives = [ep for ep in episodes if ep.is_alert_active()]
|
|
print(f"Alertes actives: {len(alerts_actives)}")
|
|
|
|
if alerts_actives:
|
|
for alert in alerts_actives[:3]: # Max 3 exemples
|
|
print(f" - {alert.lib_zone}: {alert.get_alert_level()}")
|
|
|
|
except AtmoDataException as e:
|
|
print(f"Erreur: {e}")
|
|
|
|
# Exemple 3: Données d'émissions pour l'Île-de-France
|
|
print(f"\n=== Émissions {AASQA_CODES['11']} ===")
|
|
try:
|
|
emissions = client.get_emissions(
|
|
aasqa="11", # Île-de-France
|
|
echelle="region",
|
|
format="geojson"
|
|
)
|
|
print(f"Données d'émissions récupérées: {len(emissions)}")
|
|
|
|
# Analyser les émissions
|
|
if len(emissions) > 0:
|
|
em = emissions[0]
|
|
print(f"Territoire: {em.name}")
|
|
print(f"Population: {em.population:,.0f} habitants")
|
|
total_em = em.get_total_emissions()
|
|
print(f"Émissions NOx: {total_em['NOx']:,.1f} t/an")
|
|
except AtmoDataException as e:
|
|
print(f"Erreur: {e}")
|
|
|
|
# Exemple 4: Indices pollen avec alerte
|
|
print("\n=== Indices pollen avec alerte ===")
|
|
try:
|
|
pollens = client.get_indices_pollens(
|
|
format="geojson",
|
|
aasqa="11", # Île-de-France
|
|
alerte=True,
|
|
with_geom=True
|
|
)
|
|
print(f"Nombre d'alertes pollen: {len(pollens)}")
|
|
|
|
# Analyser les pollens dangereux
|
|
if len(pollens) > 0:
|
|
dangerous_pollens = []
|
|
for pollen in pollens[:5]: # Max 5 exemples
|
|
dangerous = pollen.get_dangerous_pollens()
|
|
if dangerous:
|
|
dangerous_pollens.extend(dangerous)
|
|
|
|
if dangerous_pollens:
|
|
print(f"Pollens à risque élevé: {', '.join(set(dangerous_pollens))}")
|
|
except AtmoDataException as e:
|
|
print(f"Erreur: {e}")
|
|
|
|
# Exemple 5: Recherche dans une zone géographique (bounding box)
|
|
print("\n=== Recherche dans une zone géographique ===")
|
|
try:
|
|
# Bounding box approximative de Paris
|
|
bbox = "2.2 48.8 2.4 48.9"
|
|
indices_paris = client.get_indices_atmo(
|
|
bounding_box=bbox,
|
|
date=today,
|
|
format="geojson"
|
|
)
|
|
print(f"Indices dans la zone Paris: {len(indices_paris)}")
|
|
|
|
# Analyser la qualité
|
|
if len(indices_paris) > 0:
|
|
stats = indices_paris.get_statistics()
|
|
qs = stats['quality_stats']
|
|
print(f"Qualité moyenne: {qs['moyenne']:.1f}/7")
|
|
print(f"Bonne qualité: {qs['bon_pourcentage']:.1f}%")
|
|
except AtmoDataException as e:
|
|
print(f"Erreur: {e}")
|
|
|
|
# Exemple 6: Données historiques
|
|
print("\n=== Données historiques ===")
|
|
try:
|
|
# Episodes d'il y a 30 jours
|
|
date_historique = (datetime.now() - timedelta(days=30)).strftime("%Y-%m-%d")
|
|
episodes_historiques = client.get_episodes_historique(
|
|
date=today,
|
|
date_historique=date_historique,
|
|
aasqa="11", # Île-de-France
|
|
format="geojson"
|
|
)
|
|
print(f"Épisodes sur 30 jours: {len(episodes_historiques)}")
|
|
|
|
# Analyser l'évolution
|
|
if len(episodes_historiques) > 0:
|
|
alerts_historiques = [ep for ep in episodes_historiques if ep.is_alert_active()]
|
|
print(f"Alertes historiques: {len(alerts_historiques)}")
|
|
|
|
# Compter par polluant
|
|
polluants_hist = {}
|
|
for ep in episodes_historiques:
|
|
pol = ep.lib_pol
|
|
polluants_hist[pol] = polluants_hist.get(pol, 0) + 1
|
|
|
|
print("Répartition par polluant:")
|
|
for pol, count in polluants_hist.items():
|
|
print(f" {pol}: {count} épisodes")
|
|
except AtmoDataException as e:
|
|
print(f"Erreur: {e}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |