Akram Sanad commited on
Commit
787f4ea
·
1 Parent(s): 319e1da

added stress hydrique as 4th plot

Browse files
Files changed (1) hide show
  1. visualize/visualize.py +168 -49
visualize/visualize.py CHANGED
@@ -7,7 +7,64 @@ from data_pipelines.historical_weather_data import (
7
  aggregate_hourly_weather_data,
8
  )
9
  import os
10
- from forecast import get_forecast_datasets, get_forecast_data
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
 
13
  def concatenate_historic_forecast(
@@ -31,57 +88,74 @@ def visualize_climate(
31
  "Precipitation (mm)",
32
  "Near Surface Air Temperature (°C)",
33
  "Surface Downwelling Shortwave Radiation (W/m²)",
 
34
  "year",
35
  "period",
36
  ],
37
  ):
38
- concatenated_moderate = concatenate_historic_forecast(historic, moderate, cols_to_keep)
 
 
39
  concatenated_moderate = concatenated_moderate.sort_values(by=x_axis) # Ensure order
40
 
41
  fig = go.Figure()
42
 
43
  if column == "Precipitation (mm)":
44
  for condition_value in concatenated_moderate["period"].unique():
45
- segment = concatenated_moderate[concatenated_moderate["period"] == condition_value]
46
- avg_precipitation = segment.groupby(x_axis)[column].mean().reset_index()
47
-
 
 
48
  fig.add_trace(
49
  go.Bar(
50
- x=avg_precipitation[x_axis],
51
- y=avg_precipitation[column],
52
  name=f"{condition_value}",
53
- marker=dict(color="blue" if condition_value == "historique" else "purple"),
 
 
54
  )
55
  )
56
-
57
- concatenated_pessimist = concatenate_historic_forecast(historic, pessimist, cols_to_keep, "forecast scénario pessimiste")
 
 
58
  concatenated_pessimist = concatenated_pessimist.sort_values(by=x_axis)
59
- concatenated_pessimist = concatenated_pessimist[concatenated_pessimist["period"]!="historique"]
 
 
60
  for condition_value in concatenated_pessimist["period"].unique():
61
- segment = concatenated_pessimist[concatenated_pessimist["period"] == condition_value]
62
- avg_precipitation = segment.groupby(x_axis)[column].mean().reset_index()
63
-
 
 
64
  fig.add_trace(
65
  go.Bar(
66
- x=avg_precipitation[x_axis],
67
- y=avg_precipitation[column],
68
  name=f"{condition_value}",
69
- marker=dict(color="orange" if condition_value != "historique" else "blue"),
 
 
70
  )
71
  )
72
-
73
  # Update layout for bar chart
74
  fig.update_layout(
75
  title=f"Moyenne de {column} par année",
76
  xaxis_title="Année", # Set the x-axis title to Year
77
  yaxis_title="Précipitation (mm)", # Set the y-axis title to Precipitation
78
- barmode='group' # Group bars for different conditions
79
  )
80
-
81
  else:
82
  # For other columns, continue with the line plot as before
83
  for condition_value in concatenated_moderate["period"].unique():
84
- segment = concatenated_moderate[concatenated_moderate["period"] == condition_value]
 
 
85
  if condition_value == "historique":
86
  fig.add_trace(
87
  go.Scatter(
@@ -89,10 +163,13 @@ def visualize_climate(
89
  y=segment[column], # Precipitation values on y-axis
90
  mode="lines",
91
  name=f"{condition_value}",
92
- legendgroup='group1',
93
  showlegend=False,
94
- line=dict(color="blue" if condition_value == "historique" else "purple"),
95
-
 
 
 
96
  )
97
  )
98
  else:
@@ -102,17 +179,26 @@ def visualize_climate(
102
  y=segment[column], # Precipitation values on y-axis
103
  mode="lines",
104
  name=f"{condition_value}",
105
- legendgroup='group2',
106
  showlegend=False,
107
- line=dict(color="blue" if condition_value == "historique" else "purple", dash='dot'),
 
 
 
 
 
108
  )
109
  )
110
-
111
  # Continue with pessimistic data as in the original function...
112
- concatenated_pessimist = concatenate_historic_forecast(historic, pessimist, cols_to_keep, "forecast scénario pessimiste")
 
 
113
  concatenated_pessimist = concatenated_pessimist.sort_values(by=x_axis)
114
  for condition_value in concatenated_pessimist["period"].unique():
115
- segment = concatenated_pessimist[concatenated_pessimist["period"] == condition_value]
 
 
116
  if condition_value == "historique":
117
  fig.add_trace(
118
  go.Scatter(
@@ -120,8 +206,13 @@ def visualize_climate(
120
  y=segment[column], # Precipitation values on y-axis
121
  mode="lines",
122
  name=f"{condition_value}",
123
- legendgroup='group1',
124
- line=dict(color="blue" if condition_value == "historique" else "orange", dash='dot' if condition_value != "historique" else None),
 
 
 
 
 
125
  )
126
  )
127
  else:
@@ -131,37 +222,47 @@ def visualize_climate(
131
  y=segment[column], # Precipitation values on y-axis
132
  mode="lines",
133
  name=f"{condition_value}",
134
- legendgroup='group3',
135
- line=dict(color="blue" if condition_value == "historique" else "orange", dash='dot' if condition_value != "historique" else None),
 
 
 
 
 
136
  )
137
  )
138
  # Interpolation for the pessimistic scenario...
139
- interpolation_pessimist = concatenated_pessimist[concatenated_pessimist[x_axis] > 2023]
140
- interpolation_pessimist = interpolation_pessimist[interpolation_pessimist[x_axis] <= 2025]
 
 
 
 
141
  fig.add_trace(
142
  go.Scatter(
143
  x=interpolation_pessimist[x_axis],
144
  y=interpolation_pessimist[column].interpolate(),
145
  mode="lines",
146
- name = "forecast scénario pessimiste",
147
- legendgroup='group3',
148
  showlegend=False,
149
- line=dict(color="orange", dash='dot'),
150
-
151
  ),
152
  )
153
- interpolation_moderate = concatenated_moderate[concatenated_moderate[x_axis] > 2023]
154
- interpolation_moderate = interpolation_moderate[interpolation_moderate[x_axis] <= 2025]
 
 
 
 
155
  fig.add_trace(
156
  go.Scatter(
157
  x=interpolation_moderate[x_axis],
158
  y=interpolation_moderate[column].interpolate(),
159
  mode="lines",
160
- name = "forecast scénario modéré",
161
- legendgroup='group2',
162
- line=dict(color="purple", dash='dot'),
163
-
164
-
165
  ),
166
  )
167
  fig.update_layout(
@@ -196,11 +297,13 @@ def get_plots():
196
  "Precipitation (mm)",
197
  "Near Surface Air Temperature (°C)",
198
  "Surface Downwelling Shortwave Radiation (W/m²)",
 
199
  ]
200
- cols_to_keep = [
201
  "Precipitation (mm)",
202
  "Near Surface Air Temperature (°C)",
203
  "Surface Downwelling Shortwave Radiation (W/m²)",
 
204
  "year",
205
  "period",
206
  ]
@@ -212,15 +315,31 @@ def get_plots():
212
 
213
  df = download_historical_weather_data(latitude, longitude, start_year, end_year)
214
  historic = aggregate_hourly_weather_data(df)
215
- historic = historic.reset_index()
216
  historic = historic.rename(
217
  columns={
218
  "precipitation": "Precipitation (mm)",
219
  "air_temperature_mean": "Near Surface Air Temperature (°C)",
220
  "irradiance": "Surface Downwelling Shortwave Radiation (W/m²)",
221
- "index": "time",
222
  }
223
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
224
 
225
  moderate = get_forecast_data(latitude, longitude, "moderate")
226
  pessimist = get_forecast_data(latitude, longitude, "pessimist")
 
7
  aggregate_hourly_weather_data,
8
  )
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()
16
+ preprocessed_data["irradiance"] = preprocessed_data[
17
+ "Surface Downwelling Shortwave Radiation (W/m²)"
18
+ ] * (1 - shading_coef)
19
+ preprocessed_data["air_temperature_min"] = preprocessed_data[
20
+ "Daily Minimum Near Surface Air Temperature (°C)"
21
+ ]
22
+ preprocessed_data["air_temperature_max"] = preprocessed_data[
23
+ "Daily Maximum Near Surface Air Temperature (°C)"
24
+ ]
25
+ if historic == True:
26
+ preprocessed_data["relative_humidity_min"] = preprocessed_data[
27
+ "Relative Humidity_min"
28
+ ]
29
+ preprocessed_data["relative_humidity_max"] = preprocessed_data[
30
+ "Relative Humidity_max"
31
+ ]
32
+ else:
33
+ preprocessed_data["relative_humidity_min"] = preprocessed_data[
34
+ "Relative Humidity (%)"
35
+ ]
36
+ preprocessed_data["relative_humidity_max"] = preprocessed_data[
37
+ "Relative Humidity (%)"
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
 
70
  def concatenate_historic_forecast(
 
88
  "Precipitation (mm)",
89
  "Near Surface Air Temperature (°C)",
90
  "Surface Downwelling Shortwave Radiation (W/m²)",
91
+ "Water Deficit (mm/day)",
92
  "year",
93
  "period",
94
  ],
95
  ):
96
+ concatenated_moderate = concatenate_historic_forecast(
97
+ historic, moderate, cols_to_keep
98
+ )
99
  concatenated_moderate = concatenated_moderate.sort_values(by=x_axis) # Ensure order
100
 
101
  fig = go.Figure()
102
 
103
  if column == "Precipitation (mm)":
104
  for condition_value in concatenated_moderate["period"].unique():
105
+ segment = concatenated_moderate[
106
+ concatenated_moderate["period"] == condition_value
107
+ ]
108
+ avg_precipitation = segment.groupby(x_axis)[column].mean().reset_index()
109
+
110
  fig.add_trace(
111
  go.Bar(
112
+ x=avg_precipitation[x_axis],
113
+ y=avg_precipitation[column],
114
  name=f"{condition_value}",
115
+ marker=dict(
116
+ color="blue" if condition_value == "historique" else "purple"
117
+ ),
118
  )
119
  )
120
+
121
+ concatenated_pessimist = concatenate_historic_forecast(
122
+ historic, pessimist, cols_to_keep, "forecast scénario pessimiste"
123
+ )
124
  concatenated_pessimist = concatenated_pessimist.sort_values(by=x_axis)
125
+ concatenated_pessimist = concatenated_pessimist[
126
+ concatenated_pessimist["period"] != "historique"
127
+ ]
128
  for condition_value in concatenated_pessimist["period"].unique():
129
+ segment = concatenated_pessimist[
130
+ concatenated_pessimist["period"] == condition_value
131
+ ]
132
+ avg_precipitation = segment.groupby(x_axis)[column].mean().reset_index()
133
+
134
  fig.add_trace(
135
  go.Bar(
136
+ x=avg_precipitation[x_axis],
137
+ y=avg_precipitation[column],
138
  name=f"{condition_value}",
139
+ marker=dict(
140
+ color="orange" if condition_value != "historique" else "blue"
141
+ ),
142
  )
143
  )
144
+
145
  # Update layout for bar chart
146
  fig.update_layout(
147
  title=f"Moyenne de {column} par année",
148
  xaxis_title="Année", # Set the x-axis title to Year
149
  yaxis_title="Précipitation (mm)", # Set the y-axis title to Precipitation
150
+ barmode="group", # Group bars for different conditions
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
158
+ ]
159
  if condition_value == "historique":
160
  fig.add_trace(
161
  go.Scatter(
 
163
  y=segment[column], # Precipitation values on y-axis
164
  mode="lines",
165
  name=f"{condition_value}",
166
+ legendgroup="group1",
167
  showlegend=False,
168
+ line=dict(
169
+ color=(
170
+ "blue" if condition_value == "historique" else "purple"
171
+ )
172
+ ),
173
  )
174
  )
175
  else:
 
179
  y=segment[column], # Precipitation values on y-axis
180
  mode="lines",
181
  name=f"{condition_value}",
182
+ legendgroup="group2",
183
  showlegend=False,
184
+ line=dict(
185
+ color=(
186
+ "blue" if condition_value == "historique" else "purple"
187
+ ),
188
+ dash="dot",
189
+ ),
190
  )
191
  )
192
+
193
  # Continue with pessimistic data as in the original function...
194
+ concatenated_pessimist = concatenate_historic_forecast(
195
+ historic, pessimist, cols_to_keep, "forecast scénario pessimiste"
196
+ )
197
  concatenated_pessimist = concatenated_pessimist.sort_values(by=x_axis)
198
  for condition_value in concatenated_pessimist["period"].unique():
199
+ segment = concatenated_pessimist[
200
+ concatenated_pessimist["period"] == condition_value
201
+ ]
202
  if condition_value == "historique":
203
  fig.add_trace(
204
  go.Scatter(
 
206
  y=segment[column], # Precipitation values on y-axis
207
  mode="lines",
208
  name=f"{condition_value}",
209
+ legendgroup="group1",
210
+ line=dict(
211
+ color=(
212
+ "blue" if condition_value == "historique" else "orange"
213
+ ),
214
+ dash="dot" if condition_value != "historique" else None,
215
+ ),
216
  )
217
  )
218
  else:
 
222
  y=segment[column], # Precipitation values on y-axis
223
  mode="lines",
224
  name=f"{condition_value}",
225
+ legendgroup="group3",
226
+ line=dict(
227
+ color=(
228
+ "blue" if condition_value == "historique" else "orange"
229
+ ),
230
+ dash="dot" if condition_value != "historique" else None,
231
+ ),
232
  )
233
  )
234
  # Interpolation for the pessimistic scenario...
235
+ interpolation_pessimist = concatenated_pessimist[
236
+ concatenated_pessimist[x_axis] > 2023
237
+ ]
238
+ interpolation_pessimist = interpolation_pessimist[
239
+ interpolation_pessimist[x_axis] <= 2025
240
+ ]
241
  fig.add_trace(
242
  go.Scatter(
243
  x=interpolation_pessimist[x_axis],
244
  y=interpolation_pessimist[column].interpolate(),
245
  mode="lines",
246
+ name="forecast scénario pessimiste",
247
+ legendgroup="group3",
248
  showlegend=False,
249
+ line=dict(color="orange", dash="dot"),
 
250
  ),
251
  )
252
+ interpolation_moderate = concatenated_moderate[
253
+ concatenated_moderate[x_axis] > 2023
254
+ ]
255
+ interpolation_moderate = interpolation_moderate[
256
+ interpolation_moderate[x_axis] <= 2025
257
+ ]
258
  fig.add_trace(
259
  go.Scatter(
260
  x=interpolation_moderate[x_axis],
261
  y=interpolation_moderate[column].interpolate(),
262
  mode="lines",
263
+ name="forecast scénario modéré",
264
+ legendgroup="group2",
265
+ line=dict(color="purple", dash="dot"),
 
 
266
  ),
267
  )
268
  fig.update_layout(
 
297
  "Precipitation (mm)",
298
  "Near Surface Air Temperature (°C)",
299
  "Surface Downwelling Shortwave Radiation (W/m²)",
300
+ 'Water Deficit (mm/day)'
301
  ]
302
+ cols_to_keep: List[str] = [
303
  "Precipitation (mm)",
304
  "Near Surface Air Temperature (°C)",
305
  "Surface Downwelling Shortwave Radiation (W/m²)",
306
+ "Water Deficit (mm/day)",
307
  "year",
308
  "period",
309
  ]
 
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
+ 'index': 'time'
325
  }
326
  )
327
+ historic["time"] = pd.to_datetime(historic["time"])
328
+ historic = historic.sort_values('time')
329
+ historic = historic[historic["time"]<"2025-01-01"]
330
+ historic = historic.rename(columns={"air_temperature_min":"Daily Minimum Near Surface Air Temperature (°C)",
331
+ "air_temperature_max":"Daily Maximum Near Surface Air Temperature (°C)",
332
+ "relative_humidity_min": 'Relative Humidity_min',
333
+ "relative_humidity_max": 'Relative Humidity_max',
334
+ "wind_speed":"Near Surface Wind Speed (m/s)",
335
+ 'Precipitation (mm)':'Precipitation (kg m-2 s-1)'
336
+ })
337
+ historic["Precipitation (kg m-2 s-1)"] = historic["Precipitation (kg m-2 s-1)"]/3600
338
+
339
+ historic = water_deficit(historic,latitude,longitude)
340
+ historic = historic.rename(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")