Spaces:
Runtime error
Runtime error
File size: 4,732 Bytes
61e4fc2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
import pandas as pd # stress hydrique and rendement, besoin en eau
import plotly.graph_objects as go
from typing import List
import plotly.express as px
from fuzzywuzzy import process
from data_pipelines.historical_weather_data import (
download_historical_weather_data,
aggregate_hourly_weather_data,
)
def concatenate_historic_forecast(historic, forecast, cols_to_keep):
historic["period"] = "historique"
forecast["period"] = "futur"
historic = historic[cols_to_keep]
forecast = forecast[cols_to_keep]
full_data = pd.concat([historic, forecast])
return full_data
def visualize_climate(
moderate: pd.DataFrame,
historic: pd.DataFrame,
x_axis="year",
column: str = "Precipitation (mm)",
cols_to_keep: List[str] = [
"Precipitation (mm)",
"Near Surface Air Temperature (°C)",
"Surface Downwelling Shortwave Radiation (W/m²)",
"year",
"period",
],
):
concatenated_df = concatenate_historic_forecast(historic, moderate, cols_to_keep)
concatenated_df = concatenated_df.sort_values(by=x_axis) # Ensure order
fig = go.Figure()
# colors = {"historique": "blue", "forecast": "purple"}
for condition_value in concatenated_df["period"].unique():
segment = concatenated_df[concatenated_df["period"] == condition_value]
fig.add_trace(
go.Scatter(
x=segment[x_axis],
y=segment[column],
mode="lines",
name=f"{condition_value}",
line=dict(color="blue" if condition_value == "futur" else "purple"),
)
)
interpolation_df = concatenated_df[concatenated_df[x_axis] > 2023]
fig.add_trace(
go.Scatter(
x=interpolation_df[x_axis],
y=interpolation_df[column].interpolate(),
mode="lines",
line=dict(color="blue"),
showlegend=False,
),
)
fig.update_layout(
title=f"Historique et Forecast pour {column}",
xaxis_title="Date",
yaxis_title=column,
)
return fig
def aggregate_yearly(df, col_to_agg):
df[col_to_agg] = df.groupby("year")[col_to_agg].transform("mean")
return df
def generate_plots(
moderate: pd.DataFrame,
historic: pd.DataFrame,
x_axes: List[str],
cols_to_plot: List[str],
):
plots = []
for i, col in enumerate(cols_to_plot):
plots.append(visualize_climate(moderate, historic, x_axes[i], col))
return plots
if __name__ == "__main__":
cols_to_keep = [
"Precipitation (mm)",
"Near Surface Air Temperature (°C)",
"Surface Downwelling Shortwave Radiation (W/m²)",
"year",
"period",
]
cols_to_plot = [
"Precipitation (mm)",
"Near Surface Air Temperature (°C)",
"Surface Downwelling Shortwave Radiation (W/m²)",
]
x_axes = ["year"] * len(cols_to_plot)
latitude = 47
longitude = 5
start_year = 2000
end_year = 2025
cols_to_keep = [
"Precipitation (mm)",
"Near Surface Air Temperature (°C)",
"Surface Downwelling Shortwave Radiation (W/m²)",
"year",
"period",
]
cols_to_plot = [
"Precipitation (mm)",
"Near Surface Air Temperature (°C)",
"Surface Downwelling Shortwave Radiation (W/m²)",
]
x_axes = ["year"] * len(cols_to_plot)
df = download_historical_weather_data(latitude, longitude, start_year, end_year)
historic = aggregate_hourly_weather_data(df)
historic = historic.reset_index()
historic = historic.rename(
columns={
"precipitation": "Precipitation (mm)",
"air_temperature_mean": "Near Surface Air Temperature (°C)",
"irradiance": "Surface Downwelling Shortwave Radiation (W/m²)",
"index": "time",
}
)
historic["time"] = pd.to_datetime(historic["time"])
historic = historic.sort_values("time")
historic = historic[historic["time"] < "2025-01-01"]
climate_data_path = "data/data_climate_test/final_climate_data.csv" # will use a similar func to download_historical_weather_data
moderate = pd.read_csv(climate_data_path)
moderate["time"] = pd.to_datetime(moderate["time"])
moderate = moderate.sort_values("time")
moderate["year"] = moderate["time"].dt.year
historic["year"] = historic["time"].dt.year
moderate["Precipitation (mm)"] = moderate["Precipitation (mm)"] * 3600
for col in cols_to_plot:
moderate = aggregate_yearly(moderate, col)
historic = aggregate_yearly(historic, col)
plots = generate_plots(moderate, historic, x_axes, cols_to_plot) # List of 3 plots to show
|