예제 #1
0
 def test_cosmoe_read(self):
     """test reading from cosmo-e netcdf"""
     haz = StormEurope.from_cosmoe_file(
         DATA_DIR.joinpath('storm_europe_cosmoe_forecast_vmax_testfile.nc'),
         run_datetime=dt.datetime(2018,1,1),
         event_date=dt.datetime(2018,1,3))
     self.assertEqual(haz.tag.haz_type, 'WS')
     self.assertEqual(haz.units, 'm/s')
     self.assertEqual(haz.event_id.size, 21)
     self.assertEqual(haz.date.size, 21)
     self.assertEqual(dt.datetime.fromordinal(haz.date[0]).year, 2018)
     self.assertEqual(dt.datetime.fromordinal(haz.date[0]).month, 1)
     self.assertEqual(dt.datetime.fromordinal(haz.date[0]).day, 3)
     self.assertEqual(haz.event_id[-1], 21)
     self.assertEqual(haz.event_name[-1], '2018-01-03_ens21')
     self.assertIsInstance(haz.intensity,
                           sparse.csr.csr_matrix)
     self.assertIsInstance(haz.fraction,
                           sparse.csr.csr_matrix)
     self.assertEqual(haz.intensity.shape, (21, 25))
     self.assertAlmostEqual(haz.intensity.max(), 36.426735,places=3)
     self.assertEqual(haz.fraction.shape, (21, 25))
 def test_Forecast_calc_properties(self):
     """Test calc and propety functions from the Forecast class"""
     #hazard
     haz = StormEurope.from_cosmoe_file(
         HAZ_DIR.joinpath('storm_europe_cosmoe_forecast_vmax_testfile.nc'),
         run_datetime=dt.datetime(2018, 1, 1),
         event_date=dt.datetime(2018, 1, 3))
     #exposure
     data = {}
     data['latitude'] = haz.centroids.lat
     data['longitude'] = haz.centroids.lon
     data['value'] = np.ones_like(data['latitude']) * 100000
     data['deductible'] = np.zeros_like(data['latitude'])
     data[INDICATOR_IMPF + 'WS'] = np.ones_like(data['latitude'])
     data['region_id'] = np.ones_like(data['latitude'], dtype=int) * 756
     expo = Exposures(gpd.GeoDataFrame(data=data))
     #vulnerability
     #generate vulnerability
     impact_function = ImpfStormEurope.from_welker()
     impact_function_set = ImpactFuncSet()
     impact_function_set.append(impact_function)
     #create and calculate Forecast
     forecast = Forecast({dt.datetime(2018, 1, 1): haz}, expo,
                         impact_function_set)
     forecast.calc()
     # test
     self.assertEqual(len(forecast.run_datetime), 1)
     self.assertEqual(forecast.run_datetime[0], dt.datetime(2018, 1, 1))
     self.assertEqual(forecast.event_date, dt.datetime(2018, 1, 3))
     self.assertEqual(forecast.lead_time().days, 2)
     self.assertEqual(forecast.summary_str(),
                      'WS_NWP_run2018010100_event20180103_Switzerland')
     self.assertAlmostEqual(forecast.ai_agg(), 26.347, places=1)
     self.assertAlmostEqual(forecast.ei_exp()[1], 7.941, places=1)
     self.assertEqual(len(forecast.hazard), 1)
     self.assertIsInstance(forecast.hazard[0], StormEurope)
     self.assertIsInstance(forecast.exposure, Exposures)
     self.assertIsInstance(forecast.vulnerability, ImpactFuncSet)
    def test_Forecast_plot(self):
        """Test cplotting functions from the Forecast class"""
        #hazard
        haz1 = StormEurope.from_cosmoe_file(
            HAZ_DIR.joinpath('storm_europe_cosmoe_forecast_vmax_testfile.nc'),
            run_datetime=dt.datetime(2018, 1, 1),
            event_date=dt.datetime(2018, 1, 3))
        haz1.centroids.lat += 0.6
        haz1.centroids.lon -= 1.2
        haz2 = StormEurope.from_cosmoe_file(
            HAZ_DIR.joinpath('storm_europe_cosmoe_forecast_vmax_testfile.nc'),
            run_datetime=dt.datetime(2018, 1, 1),
            event_date=dt.datetime(2018, 1, 3))
        haz2.centroids.lat += 0.6
        haz2.centroids.lon -= 1.2
        #exposure
        data = {}
        data['latitude'] = haz1.centroids.lat
        data['longitude'] = haz1.centroids.lon
        data['value'] = np.ones_like(data['latitude']) * 100000
        data['deductible'] = np.zeros_like(data['latitude'])
        data[INDICATOR_IMPF + 'WS'] = np.ones_like(data['latitude'])
        data['region_id'] = np.ones_like(data['latitude'], dtype=int) * 756
        expo = Exposures(gpd.GeoDataFrame(data=data))
        #vulnerability
        #generate vulnerability
        impact_function = ImpfStormEurope.from_welker()
        impact_function_set = ImpactFuncSet()
        impact_function_set.append(impact_function)
        #create and calculate Forecast
        forecast = Forecast(
            {
                dt.datetime(2018, 1, 2): haz1,
                dt.datetime(2017, 12, 31): haz2
            }, expo, impact_function_set)
        forecast.calc()
        #create a file containing the polygons of Swiss cantons using natural earth
        cantons_file = CONFIG.local_data.save_dir.dir() / 'CHE_cantons.shp'
        adm1_shape_file = shapereader.natural_earth(
            resolution='10m',
            category='cultural',
            name='admin_1_states_provinces')
        if not cantons_file.exists():
            with fiona.open(adm1_shape_file, 'r') as source:
                with fiona.open(cantons_file, 'w', **source.meta) as sink:
                    for f in source:
                        if f['properties']['adm0_a3'] == 'CHE':
                            sink.write(f)
        #test plotting functions
        forecast.plot_imp_map(run_datetime=dt.datetime(2017, 12, 31),
                              polygon_file=str(cantons_file),
                              save_fig=True,
                              close_fig=True)
        map_file_name = (forecast.summary_str(dt.datetime(2017, 12, 31)) +
                         '_impact_map' + '.jpeg')
        map_file_name_full = Path(FORECAST_PLOT_DIR) / map_file_name
        map_file_name_full.absolute().unlink(missing_ok=False)
        forecast.plot_hist(run_datetime=dt.datetime(2017, 12, 31),
                           save_fig=False,
                           close_fig=True)
        forecast.plot_exceedence_prob(run_datetime=dt.datetime(2017, 12, 31),
                                      threshold=5000,
                                      save_fig=False,
                                      close_fig=True)

        forecast.plot_warn_map(
            str(cantons_file),
            decision_level='polygon',
            thresholds=[100000, 500000, 1000000, 5000000],
            probability_aggregation='mean',
            area_aggregation='sum',
            title="Building damage warning",
            explain_text="warn level based on aggregated damages",
            save_fig=False,
            close_fig=True)
        forecast.plot_warn_map(
            str(cantons_file),
            decision_level='exposure_point',
            thresholds=[1, 1000, 5000, 5000000],
            probability_aggregation=0.2,
            area_aggregation=0.2,
            title="Building damage warning",
            explain_text="warn level based on aggregated damages",
            run_datetime=dt.datetime(2017, 12, 31),
            save_fig=False,
            close_fig=True)
        forecast.plot_hexbin_ei_exposure()
        plt.close()
        with self.assertRaises(ValueError):
            forecast.plot_warn_map(
                str(cantons_file),
                decision_level='test_fail',
                probability_aggregation=0.2,
                area_aggregation=0.2,
                title="Building damage warning",
                explain_text="warn level based on aggregated damages",
                save_fig=False,
                close_fig=True)
        plt.close()
        with self.assertRaises(ValueError):
            forecast.plot_warn_map(
                str(cantons_file),
                decision_level='exposure_point',
                probability_aggregation='test_fail',
                area_aggregation=0.2,
                title="Building damage warning",
                explain_text="warn level based on aggregated damages",
                save_fig=False,
                close_fig=True)
        plt.close()
        with self.assertRaises(ValueError):
            forecast.plot_warn_map(
                str(cantons_file),
                decision_level='exposure_point',
                probability_aggregation=0.2,
                area_aggregation='test_fail',
                title="Building damage warning",
                explain_text="warn level based on aggregated damages",
                save_fig=False,
                close_fig=True)
        plt.close()