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