def irma_percen(irma_tc_s, irma_tc_n, exp_s, exp_n): """ Plot irma damage in % in lesser antilles and TCA. """ if_exp = ImpactFuncSet() if_em = IFTropCyclone() if_em.set_emanuel_usa() if_exp.append(if_em) fig, axs = plt.subplots(1, 2, figsize=(16, 20), subplot_kw=dict(projection=ccrs.PlateCarree()), squeeze=True, sharex=False, sharey=False) for idx, axis in enumerate(axs.flatten()): grid = axis.gridlines(draw_labels=True, alpha=0.2) grid.xlabels_top = grid.ylabels_right = False grid.xformatter = LONGITUDE_FORMATTER grid.yformatter = LATITUDE_FORMATTER # south plot_percen(irma_tc_s, exp_s, if_exp, axs[0]) # nord im = plot_percen(irma_tc_n, exp_n, if_exp, axs[1]) plt.subplots_adjust(wspace=0.14) fig.subplots_adjust(right=0.88) cbar_ax = fig.add_axes([0.885, 0.4285, 0.03, 0.148]) fig.colorbar(im, cax=cbar_ax, orientation='vertical', label='% Damage') return fig
def test_calc_sector_total_impact(self): """Test running total impact calculations.""" sup = SupplyChain() sup.read_wiod16(year='test', range_rows=(5, 117), range_cols=(4, 116), col_iso3=2, col_sectors=1) # Tropical cyclone over Florida and Caribbean hazard = Hazard('TC') hazard.read_mat(HAZ_TEST_MAT) # Read demo entity values # Set the entity default file to the demo one exp = Exposures() exp.read_hdf5(EXP_DEMO_H5) exp.check() exp.gdf.region_id = 840 #assign right id for USA exp.assign_centroids(hazard) impf_tc = IFTropCyclone() impf_tc.set_emanuel_usa() impf_set = ImpactFuncSet() impf_set.append(impf_tc) impf_set.check() sup.calc_sector_direct_impact(hazard, exp, impf_set) sup.calc_indirect_impact(io_approach='ghosh') sup.calc_total_impact() self.assertAlmostEqual((sup.years.shape[0], sup.mriot_data.shape[0]), sup.total_impact.shape) self.assertAlmostEqual((sup.mriot_data.shape[0], ), sup.total_aai_agg.shape)
def init_if(if_name_or_instance, param_dict, df_out = pd.DataFrame(index=[0])): """ create an ImpactFunc based on the parameters in param_dict using the method specified in if_parameterisation_name and document it in df_out. Parameters: if_name_or_instance (str or ImpactFunc): method of impact function parameterisation e.g. 'emanuel' or an instance of ImpactFunc param_dict: dict of parameter_names and values e.g. {'v_thresh': 25.7, 'v_half': 70, 'scale': 1} or e.g. {'mdd_shift': 1.05, 'mdd_scale': 0.8, 'paa_shift': 1, paa_scale': 1} Returns: ImpactFunc: The Impact function based on the parameterisation df_out: Output DataFrame with headers of columns defined and with first row (index=0) defined with values. The impact function parameters from param_dict are represented here. """ ImpactFunc_final = None if isinstance(if_name_or_instance,str): if if_name_or_instance == 'emanuel': ImpactFunc_final = IFTropCyclone() ImpactFunc_final.set_emanuel_usa(**param_dict) ImpactFunc_final.haz_type = 'TC' ImpactFunc_final.id = 1 df_out['impact_function'] = if_name_or_instance elif isinstance(if_name_or_instance,impact_funcs.ImpactFunc): ImpactFunc_final = change_if(if_name_or_instance, param_dict) df_out['impact_function'] = ('given_' + ImpactFunc_final.haz_type + str(ImpactFunc_final.id)) for key, val in param_dict.items(): df_out[key] = val return ImpactFunc_final, df_out
def calc_imp(expo_dict, tc_dict, data_dir): """ Compute impacts of TCs in every island group. """ try: abs_path = os.path.join(data_dir, 'imp_isl.p') with open(abs_path, 'rb') as f: imp_dict = pickle.load(f) print('Loaded imp_isl:', len(imp_dict)) except FileNotFoundError: if_exp = ImpactFuncSet() if_em = IFTropCyclone() if_em.set_emanuel_usa() if_exp.add_func(if_em) imp_dict = dict() for isl_iso in expo_dict: imp = Impact() imp.calc(expo_dict[isl_iso], if_exp, tc_dict[isl_iso]) imp_dict[isl_iso] = imp save(os.path.join(data_dir, 'imp_isl.p'), imp_dict) return imp_dict
def plot_right(irma_tc, exp, ax, scale_pos, plot_line=False): """ Plot irma damage in USD. """ if_exp = ImpactFuncSet() if_em = IFTropCyclone() if_em.set_emanuel_usa() if_exp.append(if_em) imp_irma = Impact() imp_irma.calc(exp, if_exp, irma_tc) extent = [ exp.longitude.min() - BUFFER_DEG, exp.longitude.max() + BUFFER_DEG, exp.latitude.min() - BUFFER_DEG, exp.latitude.max() + BUFFER_DEG ] ax.set_extent((extent)) u_plot.add_shapes(ax) sel_pos = np.argwhere(imp_irma.eai_exp > 0)[:, 0] hex_bin = ax.hexbin(imp_irma.coord_exp[sel_pos, 1], imp_irma.coord_exp[sel_pos, 0], C=imp_irma.eai_exp[sel_pos], reduce_C_function=np.average, transform=ccrs.PlateCarree(), gridsize=2000, norm=LogNorm(vmin=MIN_VAL, vmax=MAX_VAL), cmap='YlOrRd', vmin=MIN_VAL, vmax=MAX_VAL) ax.set_title('') ax.grid(False) add_cntry_names(ax, extent) scale_bar(ax, scale_pos, 10) if plot_line: x1, y1 = [-64.57, -64.82], [18.28, 18.47] ax.plot(x1, y1, linewidth=1.0, color='grey', linestyle='--') return hex_bin
def calc_sector_direct_impact(self): """Test running direct impact calculations.""" sup = SupplyChain() sup.read_wiod16(year='test', range_rows=(5, 117), range_cols=(4, 116), col_iso3=2, col_sectors=1) # Tropical cyclone over Florida and Caribbean hazard = Hazard('TC') hazard.read_mat(HAZ_TEST_MAT) # Read demo entity values # Set the entity default file to the demo one exp = Exposures() exp.read_hdf5(EXP_DEMO_H5) exp.check() exp.gdf.region_id = 840 #assign right id for USA exp.assign_centroids(hazard) impf_tc = IFTropCyclone() impf_tc.set_emanuel_usa() impf_set = ImpactFuncSet() impf_set.append(impf_tc) impf_set.check() subsecs = list(range(10)) + list(range(15, 25)) sup.calc_sector_direct_impact(hazard, exp, impf_set, selected_subsec=subsecs) self.assertAlmostEqual((sup.years.shape[0], sup.mriot_data.shape[0]), sup.direct_impact.shape) self.assertAlmostEqual(sup.direct_impact.sum(), sup.direct_impact[:, sup.reg_pos['USA']].sum(), places=3) self.assertAlmostEqual((sup.mriot_data.shape[0], ), sup.direct_aai_agg.shape) self.assertAlmostEqual(sup.direct_aai_agg.sum(), sup.direct_aai_agg[sup.reg_pos['USA']].sum(), places=3) self.assertAlmostEqual(sup.reg_dir_imp[0], 'USA') self.assertAlmostEqual(sup.direct_impact.sum(), sup.direct_impact[:, subsecs].sum(), places=3) self.assertAlmostEqual(sup.direct_aai_agg.sum(), sup.direct_aai_agg[subsecs].sum(), places=3) sup.calc_sector_direct_impact(hazard, exp, impf_set, selected_subsec='manufacturing') self.assertAlmostEqual((sup.years.shape[0], sup.mriot_data.shape[0]), sup.direct_impact.shape) self.assertAlmostEqual(sup.direct_impact.sum(), sup.direct_impact[:, sup.reg_pos['USA']].sum(), places=3) self.assertAlmostEqual((sup.mriot_data.shape[0], ), sup.direct_aai_agg.shape) self.assertAlmostEqual(sup.direct_aai_agg.sum(), sup.direct_aai_agg[sup.reg_pos['USA']].sum(), places=3) self.assertAlmostEqual(sup.reg_dir_imp[0], 'USA') self.assertAlmostEqual(sup.direct_impact.sum(), sup.direct_impact[:, range(4, 23)].sum(), places=3) self.assertAlmostEqual(sup.direct_aai_agg.sum(), sup.direct_aai_agg[range(4, 23)].sum(), places=3) sup.calc_sector_direct_impact(hazard, exp, impf_set, selected_subsec='agriculture') self.assertAlmostEqual((sup.years.shape[0], sup.mriot_data.shape[0]), sup.direct_impact.shape) self.assertAlmostEqual(sup.direct_impact.sum(), sup.direct_impact[:, sup.reg_pos['USA']].sum(), places=3) self.assertAlmostEqual((sup.mriot_data.shape[0], ), sup.direct_aai_agg.shape) self.assertAlmostEqual(sup.direct_aai_agg.sum(), sup.direct_aai_agg[sup.reg_pos['USA']].sum(), places=3) self.assertAlmostEqual(sup.direct_impact.sum(), sup.direct_impact[:, range(0, 1)].sum(), places=3) self.assertAlmostEqual(sup.direct_aai_agg.sum(), sup.direct_aai_agg[range(0, 1)].sum(), places=3) sup.calc_sector_direct_impact(hazard, exp, impf_set, selected_subsec='mining') self.assertAlmostEqual((sup.years.shape[0], sup.mriot_data.shape[0]), sup.direct_impact.shape) self.assertAlmostEqual(sup.direct_impact.sum(), sup.direct_impact[:, sup.reg_pos['USA']].sum(), places=3) self.assertAlmostEqual((sup.mriot_data.shape[0], ), sup.direct_aai_agg.shape) self.assertAlmostEqual(sup.direct_aai_agg.sum(), sup.direct_aai_agg[sup.reg_pos['USA']].sum(), places=3) self.assertAlmostEqual(sup.direct_impact.sum(), sup.direct_impact[:, range(3, 4)].sum(), places=3) self.assertAlmostEqual(sup.direct_aai_agg.sum(), sup.direct_aai_agg[range(3, 4)].sum(), places=3) sup.calc_sector_direct_impact(hazard, exp, impf_set, selected_subsec='service') self.assertAlmostEqual((sup.years.shape[0], sup.mriot_data.shape[0]), sup.direct_impact.shape) self.assertAlmostEqual(sup.direct_impact.sum(), sup.direct_impact[:, sup.reg_pos['USA']].sum(), places=3) self.assertAlmostEqual((sup.mriot_data.shape[0], ), sup.direct_aai_agg.shape) self.assertAlmostEqual(sup.direct_aai_agg.sum(), sup.direct_aai_agg[sup.reg_pos['USA']].sum(), places=3) self.assertAlmostEqual(sup.direct_impact.sum(), sup.direct_impact[:, range(26, 56)].sum(), places=3) self.assertAlmostEqual(sup.direct_aai_agg.sum(), sup.direct_aai_agg[range(26, 56)].sum(), places=3)