|
26 | 26 |
|
27 | 27 | import geopandas as gpd |
28 | 28 | import numpy as np |
| 29 | +import pandas as pd |
29 | 30 | from scipy import sparse |
30 | 31 |
|
31 | 32 | from climada import CONFIG |
32 | 33 | from climada.engine import Impact, ImpactCalc |
33 | 34 | from climada.engine.impact_calc import LOGGER as ILOG |
| 35 | +from climada.engine.impact_forecast import ImpactForecast |
34 | 36 | from climada.entity import Exposures, ImpactFunc, ImpactFuncSet, ImpfTropCyclone |
35 | 37 | from climada.entity.entity_def import Entity |
36 | 38 | from climada.hazard.base import Centroids, Hazard |
| 39 | +from climada.hazard.forecast import HazardForecast |
37 | 40 | from climada.test import get_test_file |
38 | 41 | from climada.util.api_client import Client |
39 | 42 | from climada.util.config import Config |
|
47 | 50 |
|
48 | 51 |
|
49 | 52 | def check_impact(self, imp, haz, exp, aai_agg, eai_exp, at_event, imp_mat_array=None): |
50 | | - """Test properties of imapcts""" |
| 53 | + """Test properties of impacts""" |
51 | 54 | self.assertEqual(len(haz.event_id), len(imp.at_event)) |
52 | 55 | self.assertIsInstance(imp, Impact) |
53 | 56 | np.testing.assert_allclose(imp.coord_exp[:, 0], exp.latitude) |
@@ -302,6 +305,89 @@ def test_calc_impact_RF_pass(self): |
302 | 305 | # fmt: on |
303 | 306 | check_impact(self, impact, haz, exp, aai_agg, eai_exp, at_event, imp_mat_array) |
304 | 307 |
|
| 308 | + def test_impactForecast(self): |
| 309 | + """Test that ImpactForecast is returned correctly""" |
| 310 | + lead_time = pd.timedelta_range("1h", periods=6).to_numpy() |
| 311 | + member = np.arange(6) |
| 312 | + _haz = Hazard.from_hdf5(get_test_file("test_hazard_US_flood_random_locations")) |
| 313 | + haz_fc = HazardForecast.from_hazard(_haz, lead_time=lead_time, member=member) |
| 314 | + |
| 315 | + exp = Exposures.from_hdf5( |
| 316 | + get_test_file("test_exposure_US_flood_random_locations") |
| 317 | + ) |
| 318 | + impf_set = ImpactFuncSet.from_excel( |
| 319 | + Path(__file__).parent / "data" / "flood_imp_func_set.xls" |
| 320 | + ) |
| 321 | + icalc = ImpactCalc(exp, impf_set, haz_fc) |
| 322 | + impact = icalc.impact(assign_centroids=False) |
| 323 | + aai_agg = 161436.05112960344 |
| 324 | + eai_exp = np.array( |
| 325 | + [ |
| 326 | + 1.61159701e05, |
| 327 | + 1.33742847e02, |
| 328 | + 0.00000000e00, |
| 329 | + 4.21352988e-01, |
| 330 | + 1.42185609e02, |
| 331 | + 0.00000000e00, |
| 332 | + 0.00000000e00, |
| 333 | + 0.00000000e00, |
| 334 | + ] |
| 335 | + ) |
| 336 | + at_event = np.array( |
| 337 | + [ |
| 338 | + 0.00000000e00, |
| 339 | + 0.00000000e00, |
| 340 | + 9.85233619e04, |
| 341 | + 3.41245461e04, |
| 342 | + 7.73566566e07, |
| 343 | + 0.00000000e00, |
| 344 | + 0.00000000e00, |
| 345 | + ] |
| 346 | + ) |
| 347 | + # fmt: off |
| 348 | + imp_mat_array = np.array( |
| 349 | + [ |
| 350 | + [ |
| 351 | + 0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00, |
| 352 | + 0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00, |
| 353 | + ], |
| 354 | + [ |
| 355 | + 0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00, |
| 356 | + 0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00, |
| 357 | + ], |
| 358 | + [ |
| 359 | + 0.00000000e00, 6.41965663e04, 0.00000000e00, 2.02249434e02, |
| 360 | + 3.41245461e04, 0.00000000e00, 0.00000000e00, 0.00000000e00, |
| 361 | + ], |
| 362 | + [ |
| 363 | + 0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00, |
| 364 | + 3.41245461e04, 0.00000000e00, 0.00000000e00, 0.00000000e00, |
| 365 | + ], |
| 366 | + [ |
| 367 | + 7.73566566e07, 0.00000000e00, 0.00000000e00, 0.00000000e00, |
| 368 | + 0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00, |
| 369 | + ], |
| 370 | + [ |
| 371 | + 0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00, |
| 372 | + 0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00, |
| 373 | + ], |
| 374 | + [ |
| 375 | + 0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00, |
| 376 | + 0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00, |
| 377 | + ], |
| 378 | + ] |
| 379 | + ) |
| 380 | + # fmt: on |
| 381 | + check_impact( |
| 382 | + self, impact, haz_fc, exp, aai_agg, eai_exp, at_event, imp_mat_array |
| 383 | + ) |
| 384 | + |
| 385 | + # additional test to check that impact is indeed ImpactForecast |
| 386 | + self.assertIsInstance(impact, ImpactForecast) |
| 387 | + np.testing.assert_array_equal(impact.lead_time, lead_time) |
| 388 | + self.assertIs(impact.lead_time.dtype, lead_time.dtype) |
| 389 | + np.testing.assert_array_equal(impact.member, member) |
| 390 | + |
305 | 391 | def test_empty_impact(self): |
306 | 392 | """Check that empty impact is returned if no centroids match the exposures""" |
307 | 393 | exp = ENT.exposures.copy() |
|
0 commit comments