Spaces:
Sleeping
Sleeping
Delete pages/1 vents.py
Browse files- pages/1 vents.py +0 -147
pages/1 vents.py
DELETED
|
@@ -1,147 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import folium
|
| 3 |
-
from streamlit_folium import folium_static
|
| 4 |
-
import cdsapi
|
| 5 |
-
import xarray as xr
|
| 6 |
-
import numpy as np
|
| 7 |
-
import tempfile
|
| 8 |
-
import os
|
| 9 |
-
import json
|
| 10 |
-
from datetime import datetime
|
| 11 |
-
|
| 12 |
-
# Clé API Copernicus (à remplacer par votre vraie clé)
|
| 13 |
-
COPERNICUS_API_KEY = '9e79c0bd-0655-496f-8241-62503112dd31'
|
| 14 |
-
|
| 15 |
-
# Fonction pour obtenir les données de vent depuis Copernicus
|
| 16 |
-
def get_copernicus_wind_data(date):
|
| 17 |
-
c = cdsapi.Client(url="https://cds.climate.copernicus.eu/api/v2", key=COPERNICUS_API_KEY)
|
| 18 |
-
|
| 19 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix='.nc') as tmp:
|
| 20 |
-
c.retrieve(
|
| 21 |
-
'reanalysis-era5-single-levels',
|
| 22 |
-
{
|
| 23 |
-
'product_type': 'reanalysis',
|
| 24 |
-
'variable': ['10m_u_component_of_wind', '10m_v_component_of_wind'],
|
| 25 |
-
'year': date.strftime('%Y'),
|
| 26 |
-
'month': date.strftime('%m'),
|
| 27 |
-
'day': date.strftime('%d'),
|
| 28 |
-
'time': '12:00',
|
| 29 |
-
'area': [
|
| 30 |
-
47, 6, 45,
|
| 31 |
-
7,
|
| 32 |
-
],
|
| 33 |
-
'format': 'netcdf',
|
| 34 |
-
},
|
| 35 |
-
tmp.name)
|
| 36 |
-
|
| 37 |
-
ds = xr.open_dataset(tmp.name)
|
| 38 |
-
os.unlink(tmp.name)
|
| 39 |
-
return ds
|
| 40 |
-
|
| 41 |
-
# Fonction pour formater les données de vent pour leaflet-velocity
|
| 42 |
-
def format_wind_data(ds):
|
| 43 |
-
u10 = ds['u10'].values[0]
|
| 44 |
-
v10 = ds['v10'].values[0]
|
| 45 |
-
|
| 46 |
-
formatted_data = {
|
| 47 |
-
"data": [
|
| 48 |
-
{
|
| 49 |
-
"header": {
|
| 50 |
-
"parameterNumberName": "eastward_wind",
|
| 51 |
-
"parameterUnit": "m.s-1",
|
| 52 |
-
"parameterNumber": 2,
|
| 53 |
-
"parameterCategory": 2,
|
| 54 |
-
"nx": u10.shape[1],
|
| 55 |
-
"ny": u10.shape[0],
|
| 56 |
-
"lo1": float(ds.longitude.min()),
|
| 57 |
-
"la1": float(ds.latitude.max()),
|
| 58 |
-
"lo2": float(ds.longitude.max()),
|
| 59 |
-
"la2": float(ds.latitude.min()),
|
| 60 |
-
"dx": float(ds.longitude[1] - ds.longitude[0]),
|
| 61 |
-
"dy": float(ds.latitude[0] - ds.latitude[1])
|
| 62 |
-
},
|
| 63 |
-
"data": u10.flatten().tolist()
|
| 64 |
-
},
|
| 65 |
-
{
|
| 66 |
-
"header": {
|
| 67 |
-
"parameterNumberName": "northward_wind",
|
| 68 |
-
"parameterUnit": "m.s-1",
|
| 69 |
-
"parameterNumber": 3,
|
| 70 |
-
"parameterCategory": 2,
|
| 71 |
-
"nx": v10.shape[1],
|
| 72 |
-
"ny": v10.shape[0],
|
| 73 |
-
"lo1": float(ds.longitude.min()),
|
| 74 |
-
"la1": float(ds.latitude.max()),
|
| 75 |
-
"lo2": float(ds.longitude.max()),
|
| 76 |
-
"la2": float(ds.latitude.min()),
|
| 77 |
-
"dx": float(ds.longitude[1] - ds.longitude[0]),
|
| 78 |
-
"dy": float(ds.latitude[0] - ds.latitude[1])
|
| 79 |
-
},
|
| 80 |
-
"data": v10.flatten().tolist()
|
| 81 |
-
}
|
| 82 |
-
]
|
| 83 |
-
}
|
| 84 |
-
|
| 85 |
-
return formatted_data
|
| 86 |
-
|
| 87 |
-
# Configuration de la page Streamlit
|
| 88 |
-
st.set_page_config(page_title="Vent sur la région lémanique", layout="wide")
|
| 89 |
-
|
| 90 |
-
# Titre de l'application
|
| 91 |
-
st.title("Visualisation du vent sur la région lémanique")
|
| 92 |
-
|
| 93 |
-
# Sélection de la date
|
| 94 |
-
selected_date = st.date_input("Sélectionnez une date", value=datetime.now())
|
| 95 |
-
|
| 96 |
-
# Obtention et formatage des données de vent
|
| 97 |
-
try:
|
| 98 |
-
wind_data = get_copernicus_wind_data(selected_date)
|
| 99 |
-
formatted_wind_data = format_wind_data(wind_data)
|
| 100 |
-
|
| 101 |
-
# Création de la carte avec folium
|
| 102 |
-
m = folium.Map(location=[46.4, 6.5], zoom_start=9)
|
| 103 |
-
|
| 104 |
-
# Ajout de la couche leaflet-velocity
|
| 105 |
-
folium.JavascriptLink("https://unpkg.com/leaflet-velocity@1.7.0/dist/leaflet-velocity.js").add_to(m)
|
| 106 |
-
folium.CssLink("https://unpkg.com/leaflet-velocity@1.7.0/dist/leaflet-velocity.css").add_to(m)
|
| 107 |
-
|
| 108 |
-
# Ajout du script pour initialiser leaflet-velocity
|
| 109 |
-
folium.Element(f"""
|
| 110 |
-
<script>
|
| 111 |
-
var windData = {json.dumps(formatted_wind_data)};
|
| 112 |
-
var velocityLayer = L.velocityLayer({{
|
| 113 |
-
displayValues: true,
|
| 114 |
-
displayOptions: {{
|
| 115 |
-
velocityType: "Vent",
|
| 116 |
-
position: "bottomleft",
|
| 117 |
-
emptyString: "Pas de données de vent",
|
| 118 |
-
angleConvention: "bearingCW",
|
| 119 |
-
speedUnit: "km/h"
|
| 120 |
-
}},
|
| 121 |
-
data: windData.data,
|
| 122 |
-
maxVelocity: 10,
|
| 123 |
-
velocityScale: 0.1
|
| 124 |
-
}});
|
| 125 |
-
velocityLayer.addTo(map);
|
| 126 |
-
</script>
|
| 127 |
-
""").add_to(m)
|
| 128 |
-
|
| 129 |
-
# Affichage de la carte dans Streamlit
|
| 130 |
-
folium_static(m, width=1000, height=600)
|
| 131 |
-
|
| 132 |
-
# Affichage des données brutes
|
| 133 |
-
if st.checkbox("Afficher les données brutes"):
|
| 134 |
-
st.write(wind_data)
|
| 135 |
-
|
| 136 |
-
except Exception as e:
|
| 137 |
-
st.error(f"Une erreur s'est produite lors de la récupération des données : {str(e)}")
|
| 138 |
-
|
| 139 |
-
# Instructions d'utilisation
|
| 140 |
-
st.markdown("""
|
| 141 |
-
## Instructions d'utilisation
|
| 142 |
-
1. Sélectionnez une date dans le calendrier ci-dessus.
|
| 143 |
-
2. La carte affichera automatiquement les données de vent pour la région lémanique à la date sélectionnée.
|
| 144 |
-
3. Vous pouvez zoomer et vous déplacer sur la carte pour explorer différentes zones.
|
| 145 |
-
4. Les flèches sur la carte indiquent la direction et la vitesse du vent.
|
| 146 |
-
5. Cochez la case "Afficher les données brutes" pour voir les données de vent non formatées.
|
| 147 |
-
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|