2929
3030from climada import CONFIG
3131from climada .engine .impact import Impact
32+ from climada .engine .impact_forecast import ImpactForecast
33+ from climada .hazard .forecast import HazardForecast
3234
3335LOGGER = logging .getLogger (__name__ )
3436
@@ -217,7 +219,7 @@ def _return_impact(self, imp_mat_gen, save_mat):
217219
218220 Returns
219221 -------
220- Impact
222+ Impact or ImpactForecast
221223 Impact Object initialize from the impact matrix
222224
223225 See Also
@@ -230,12 +232,31 @@ def _return_impact(self, imp_mat_gen, save_mat):
230232 at_event , eai_exp , aai_agg = self .risk_metrics (
231233 imp_mat , self .hazard .frequency
232234 )
235+ if isinstance (self .hazard , HazardForecast ):
236+ eai_exp = np .full_like (eai_exp , np .nan , dtype = eai_exp .dtype )
237+ aai_agg = np .full_like (aai_agg , np .nan , dtype = aai_agg .dtype )
238+ LOGGER .warning (
239+ "eai_exp and aai_agg are undefined with forecasts. "
240+ "Setting them to NaN arrays."
241+ )
242+
233243 else :
244+ if isinstance (self .hazard , HazardForecast ):
245+ raise ValueError (
246+ "Saving impact matrix is required when using HazardForecast."
247+ "Please set save_mat=True."
248+ )
234249 imp_mat = None
235250 at_event , eai_exp , aai_agg = self .stitch_risk_metrics (imp_mat_gen )
236- return Impact .from_eih (
251+
252+ impact = Impact .from_eih (
237253 self .exposures , self .hazard , at_event , eai_exp , aai_agg , imp_mat
238254 )
255+ if isinstance (self .hazard , HazardForecast ):
256+ return ImpactForecast .from_impact (
257+ impact , self .hazard .lead_time , self .hazard .member
258+ )
259+ return impact
239260
240261 def _return_empty (self , save_mat ):
241262 """
@@ -248,21 +269,37 @@ def _return_empty(self, save_mat):
248269
249270 Returns
250271 -------
251- Impact
272+ Impact or ImpactForecast
252273 Empty impact object with correct array sizes.
253274 """
254275 at_event = np .zeros (self .n_events )
255- eai_exp = np .zeros (self .n_exp_pnt )
256- aai_agg = 0.0
276+ if isinstance (self .hazard , HazardForecast ):
277+ eai_exp = np .full (self .n_exp_pnt , np .nan )
278+ aai_agg = np .nan
279+ else :
280+ eai_exp = np .zeros (self .n_exp_pnt )
281+ aai_agg = 0.0
282+
257283 if save_mat :
258284 imp_mat = sparse .csr_matrix (
259285 (self .n_events , self .n_exp_pnt ), dtype = np .float64
260286 )
261287 else :
288+ if isinstance (self .hazard , HazardForecast ):
289+ raise ValueError (
290+ "Saving impact matrix is required when using HazardForecast. "
291+ "Please set save_mat=True."
292+ )
262293 imp_mat = None
263- return Impact .from_eih (
294+
295+ impact = Impact .from_eih (
264296 self .exposures , self .hazard , at_event , eai_exp , aai_agg , imp_mat
265297 )
298+ if isinstance (self .hazard , HazardForecast ):
299+ return ImpactForecast .from_impact (
300+ impact , self .hazard .lead_time , self .hazard .member
301+ )
302+ return impact
266303
267304 def minimal_exp_gdf (
268305 self , impf_col , assign_centroids , ignore_cover , ignore_deductible
0 commit comments