Spaces:
Runtime error
Runtime error
Akram Sanad
commited on
Commit
·
3d05be5
1
Parent(s):
d93b3a4
added ombrage
Browse files- visualize/visualize.py +45 -26
visualize/visualize.py
CHANGED
|
@@ -9,7 +9,7 @@ from data_pipelines.historical_weather_data import (
|
|
| 9 |
import os
|
| 10 |
from forecast import get_forecast_data
|
| 11 |
from compute_et0_adjusted import compute_et0
|
| 12 |
-
|
| 13 |
|
| 14 |
def water_deficit(df, latitude, longitude, shading_coef=0, historic=True):
|
| 15 |
preprocessed_data = df.copy()
|
|
@@ -38,32 +38,26 @@ def water_deficit(df, latitude, longitude, shading_coef=0, historic=True):
|
|
| 38 |
]
|
| 39 |
preprocessed_data["wind_speed"] = preprocessed_data["Near Surface Wind Speed (m/s)"]
|
| 40 |
|
| 41 |
-
# Convert 'time' to datetime and calculate Julian day
|
| 42 |
preprocessed_data["time"] = pd.to_datetime(
|
| 43 |
preprocessed_data["time"], errors="coerce"
|
| 44 |
)
|
| 45 |
preprocessed_data["month"] = preprocessed_data["time"].dt.month
|
| 46 |
preprocessed_data["day_of_year"] = preprocessed_data["time"].dt.dayofyear
|
| 47 |
|
| 48 |
-
# Compute ET0
|
| 49 |
et0 = compute_et0(preprocessed_data, latitude, longitude)
|
| 50 |
preprocessed_data["Evaporation (mm/day)"] = et0
|
| 51 |
preprocessed_data["Evaporation (mm/day)"] = preprocessed_data[
|
| 52 |
"Evaporation (mm/day)"
|
| 53 |
].clip(lower=0)
|
| 54 |
-
# Convert Precipitation from kg/m²/s to mm/day
|
| 55 |
|
| 56 |
preprocessed_data["Precipitation (mm/day)"] = (
|
| 57 |
86400 * preprocessed_data["Precipitation (kg m-2 s-1)"]
|
| 58 |
)
|
| 59 |
|
| 60 |
-
# Calculate Water Deficit: Water Deficit = ET0 - P + M
|
| 61 |
preprocessed_data["Water Deficit (mm/day)"] = (
|
| 62 |
preprocessed_data["Evaporation (mm/day)"]
|
| 63 |
- preprocessed_data["Precipitation (mm/day)"]
|
| 64 |
-
+ 4.5
|
| 65 |
)
|
| 66 |
-
|
| 67 |
return preprocessed_data
|
| 68 |
|
| 69 |
|
|
@@ -151,7 +145,6 @@ def visualize_climate(
|
|
| 151 |
)
|
| 152 |
|
| 153 |
else:
|
| 154 |
-
# For other columns, continue with the line plot as before
|
| 155 |
for condition_value in concatenated_moderate["period"].unique():
|
| 156 |
segment = concatenated_moderate[
|
| 157 |
concatenated_moderate["period"] == condition_value
|
|
@@ -285,6 +278,7 @@ def generate_plots(
|
|
| 285 |
pessimist: pd.DataFrame,
|
| 286 |
x_axes: List[str],
|
| 287 |
cols_to_plot: List[str],
|
|
|
|
| 288 |
):
|
| 289 |
plots = []
|
| 290 |
for i, col in enumerate(cols_to_plot):
|
|
@@ -297,7 +291,7 @@ def get_plots():
|
|
| 297 |
"Precipitation (mm)",
|
| 298 |
"Near Surface Air Temperature (°C)",
|
| 299 |
"Surface Downwelling Shortwave Radiation (W/m²)",
|
| 300 |
-
|
| 301 |
]
|
| 302 |
cols_to_keep: List[str] = [
|
| 303 |
"Precipitation (mm)",
|
|
@@ -315,31 +309,37 @@ def get_plots():
|
|
| 315 |
|
| 316 |
df = download_historical_weather_data(latitude, longitude, start_year, end_year)
|
| 317 |
historic = aggregate_hourly_weather_data(df)
|
| 318 |
-
historic= historic.reset_index()
|
| 319 |
historic = historic.rename(
|
| 320 |
columns={
|
| 321 |
"precipitation": "Precipitation (mm)",
|
| 322 |
"air_temperature_mean": "Near Surface Air Temperature (°C)",
|
| 323 |
"irradiance": "Surface Downwelling Shortwave Radiation (W/m²)",
|
| 324 |
-
|
| 325 |
}
|
| 326 |
)
|
| 327 |
historic["time"] = pd.to_datetime(historic["time"])
|
| 328 |
-
historic = historic.sort_values(
|
| 329 |
-
historic = historic[historic["time"]<"2025-01-01"]
|
| 330 |
-
historic = historic.rename(
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 338 |
|
| 339 |
-
historic = water_deficit(historic,latitude,longitude)
|
| 340 |
-
historic = historic.rename(
|
| 341 |
-
|
| 342 |
-
|
|
|
|
| 343 |
|
| 344 |
moderate = get_forecast_data(latitude, longitude, "moderate")
|
| 345 |
pessimist = get_forecast_data(latitude, longitude, "pessimist")
|
|
@@ -365,4 +365,23 @@ def get_plots():
|
|
| 365 |
historic = aggregate_yearly(historic, col)
|
| 366 |
pessimist = aggregate_yearly(pessimist, col)
|
| 367 |
plots = generate_plots(moderate, historic, pessimist, x_axes, cols_to_plot)
|
| 368 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
import os
|
| 10 |
from forecast import get_forecast_data
|
| 11 |
from compute_et0_adjusted import compute_et0
|
| 12 |
+
import copy
|
| 13 |
|
| 14 |
def water_deficit(df, latitude, longitude, shading_coef=0, historic=True):
|
| 15 |
preprocessed_data = df.copy()
|
|
|
|
| 38 |
]
|
| 39 |
preprocessed_data["wind_speed"] = preprocessed_data["Near Surface Wind Speed (m/s)"]
|
| 40 |
|
|
|
|
| 41 |
preprocessed_data["time"] = pd.to_datetime(
|
| 42 |
preprocessed_data["time"], errors="coerce"
|
| 43 |
)
|
| 44 |
preprocessed_data["month"] = preprocessed_data["time"].dt.month
|
| 45 |
preprocessed_data["day_of_year"] = preprocessed_data["time"].dt.dayofyear
|
| 46 |
|
|
|
|
| 47 |
et0 = compute_et0(preprocessed_data, latitude, longitude)
|
| 48 |
preprocessed_data["Evaporation (mm/day)"] = et0
|
| 49 |
preprocessed_data["Evaporation (mm/day)"] = preprocessed_data[
|
| 50 |
"Evaporation (mm/day)"
|
| 51 |
].clip(lower=0)
|
|
|
|
| 52 |
|
| 53 |
preprocessed_data["Precipitation (mm/day)"] = (
|
| 54 |
86400 * preprocessed_data["Precipitation (kg m-2 s-1)"]
|
| 55 |
)
|
| 56 |
|
|
|
|
| 57 |
preprocessed_data["Water Deficit (mm/day)"] = (
|
| 58 |
preprocessed_data["Evaporation (mm/day)"]
|
| 59 |
- preprocessed_data["Precipitation (mm/day)"]
|
|
|
|
| 60 |
)
|
|
|
|
| 61 |
return preprocessed_data
|
| 62 |
|
| 63 |
|
|
|
|
| 145 |
)
|
| 146 |
|
| 147 |
else:
|
|
|
|
| 148 |
for condition_value in concatenated_moderate["period"].unique():
|
| 149 |
segment = concatenated_moderate[
|
| 150 |
concatenated_moderate["period"] == condition_value
|
|
|
|
| 278 |
pessimist: pd.DataFrame,
|
| 279 |
x_axes: List[str],
|
| 280 |
cols_to_plot: List[str],
|
| 281 |
+
is_shaded: str = "",
|
| 282 |
):
|
| 283 |
plots = []
|
| 284 |
for i, col in enumerate(cols_to_plot):
|
|
|
|
| 291 |
"Precipitation (mm)",
|
| 292 |
"Near Surface Air Temperature (°C)",
|
| 293 |
"Surface Downwelling Shortwave Radiation (W/m²)",
|
| 294 |
+
"Water Deficit (mm/day)",
|
| 295 |
]
|
| 296 |
cols_to_keep: List[str] = [
|
| 297 |
"Precipitation (mm)",
|
|
|
|
| 309 |
|
| 310 |
df = download_historical_weather_data(latitude, longitude, start_year, end_year)
|
| 311 |
historic = aggregate_hourly_weather_data(df)
|
| 312 |
+
historic = historic.reset_index()
|
| 313 |
historic = historic.rename(
|
| 314 |
columns={
|
| 315 |
"precipitation": "Precipitation (mm)",
|
| 316 |
"air_temperature_mean": "Near Surface Air Temperature (°C)",
|
| 317 |
"irradiance": "Surface Downwelling Shortwave Radiation (W/m²)",
|
| 318 |
+
"index": "time",
|
| 319 |
}
|
| 320 |
)
|
| 321 |
historic["time"] = pd.to_datetime(historic["time"])
|
| 322 |
+
historic = historic.sort_values("time")
|
| 323 |
+
historic = historic[historic["time"] < "2025-01-01"]
|
| 324 |
+
historic = historic.rename(
|
| 325 |
+
columns={
|
| 326 |
+
"air_temperature_min": "Daily Minimum Near Surface Air Temperature (°C)",
|
| 327 |
+
"air_temperature_max": "Daily Maximum Near Surface Air Temperature (°C)",
|
| 328 |
+
"relative_humidity_min": "Relative Humidity_min",
|
| 329 |
+
"relative_humidity_max": "Relative Humidity_max",
|
| 330 |
+
"wind_speed": "Near Surface Wind Speed (m/s)",
|
| 331 |
+
"Precipitation (mm)": "Precipitation (kg m-2 s-1)",
|
| 332 |
+
}
|
| 333 |
+
)
|
| 334 |
+
historic["Precipitation (kg m-2 s-1)"] = (
|
| 335 |
+
historic["Precipitation (kg m-2 s-1)"] / 3600
|
| 336 |
+
)
|
| 337 |
|
| 338 |
+
historic = water_deficit(historic, latitude, longitude)
|
| 339 |
+
historic = historic.rename(
|
| 340 |
+
columns={"Precipitation (kg m-2 s-1)": "Precipitation (mm)"}
|
| 341 |
+
)
|
| 342 |
+
historic["Precipitation (mm)"] = historic["Precipitation (mm)"] * 3600
|
| 343 |
|
| 344 |
moderate = get_forecast_data(latitude, longitude, "moderate")
|
| 345 |
pessimist = get_forecast_data(latitude, longitude, "pessimist")
|
|
|
|
| 365 |
historic = aggregate_yearly(historic, col)
|
| 366 |
pessimist = aggregate_yearly(pessimist, col)
|
| 367 |
plots = generate_plots(moderate, historic, pessimist, x_axes, cols_to_plot)
|
| 368 |
+
moderate = get_forecast_data(latitude, longitude, "moderate", shading_coef=0.2)
|
| 369 |
+
pessimist = get_forecast_data(latitude, longitude, "pessimist", shading_coef=0.2)
|
| 370 |
+
pessimist["year"] = pessimist["time"].dt.year
|
| 371 |
+
pessimist = pessimist[["year", "Water Deficit (mm/day)"]]
|
| 372 |
+
pessimist = aggregate_yearly(pessimist, 'Water Deficit (mm/day)')
|
| 373 |
+
plot_ombrage = copy.deepcopy(plots[-1])
|
| 374 |
+
plot_ombrage.add_trace(
|
| 375 |
+
go.Scatter(
|
| 376 |
+
x=pessimist["year"],
|
| 377 |
+
y=pessimist['Water Deficit (mm/day)'],
|
| 378 |
+
mode="lines",
|
| 379 |
+
name="forecast scénario pessimisste ombrage de 20%",
|
| 380 |
+
line=dict(
|
| 381 |
+
color="green",
|
| 382 |
+
dash="dot",
|
| 383 |
+
),
|
| 384 |
+
)
|
| 385 |
+
)
|
| 386 |
+
plots.append(plot_ombrage)
|
| 387 |
+
return plots, pessimist
|