def test_EU(self): """test with demo data containing France and Germany""" bbox = [-5, 42, 16, 55] haz = RelativeCropyield() haz.set_from_single_run(input_dir=INPUT_DIR, yearrange=(2001, 2005), bbox=bbox, ag_model='lpjml', cl_model='ipsl-cm5a-lr', scenario='historical', soc='2005soc', co2='co2', crop='whe', irr='noirr', fn_str_var=FN_STR_DEMO) hist_mean = haz.calc_mean(yearrange_mean=(2001, 2005)) haz.set_rel_yield_to_int(hist_mean) haz.centroids.set_region_id() exp = CropProduction() exp.set_from_single_run(input_dir=INPUT_DIR, filename=FILENAME_LU, hist_mean=FILENAME_MEAN, bbox=bbox, yearrange=(2001, 2005), scenario='flexible', unit='t', crop='whe', irr='firr') exp.set_to_usd(INPUT_DIR) exp.assign_centroids(haz, threshold=20) if_cp = ImpactFuncSet() if_def = IFRelativeCropyield() if_def.set_relativeyield() if_cp.append(if_def) if_cp.check() impact = Impact() impact.calc(exp.loc[exp.region_id == 276], if_cp, haz.select(['2002']), save_mat=True) exp_manual = exp.value.loc[exp.region_id == 276].values impact_manual = haz.select(event_names=['2002'], reg_id=276).intensity.multiply(exp_manual) dif = (impact_manual - impact.imp_mat).data self.assertEqual(haz.tag.haz_type, 'RC') self.assertEqual(haz.size, 5) self.assertEqual(haz.centroids.size, 1092) self.assertAlmostEqual(haz.intensity.mean(), -2.0489097e-08) self.assertAlmostEqual(exp.value.max(), 53074789.755290434) self.assertEqual(exp.latitude.values.size, 1092) self.assertAlmostEqual(exp.value[3], 0.0) self.assertAlmostEqual(exp.value[1077], 405026.6857207429) self.assertAlmostEqual(impact.imp_mat.data[3], -176102.5359452465) self.assertEqual(len(dif), 0)
def test_EU_nan(self): """Test whether setting the zeros in exp.value to NaN changes the impact""" bbox = [0, 42, 10, 52] haz = RelativeCropyield() haz.set_from_isimip_netcdf(input_dir=INPUT_DIR, yearrange=(2001, 2005), bbox=bbox, ag_model='lpjml', cl_model='ipsl-cm5a-lr', scenario='historical', soc='2005soc', co2='co2', crop='whe', irr='noirr', fn_str_var=FN_STR_DEMO) hist_mean = haz.calc_mean(yearrange_mean=(2001, 2005)) haz.set_rel_yield_to_int(hist_mean) haz.centroids.set_region_id() exp = CropProduction() exp.set_from_isimip_netcdf(input_dir=INPUT_DIR, filename=FILENAME_LU, hist_mean=FILENAME_MEAN, bbox=bbox, yearrange=(2001, 2005), scenario='flexible', unit='t/y', crop='whe', irr='firr') exp.assign_centroids(haz, threshold=20) impf_cp = ImpactFuncSet() impf_def = ImpfRelativeCropyield() impf_def.set_relativeyield() impf_cp.append(impf_def) impf_cp.check() impact = Impact() impact.calc(exp, impf_cp, haz, save_mat=True) exp_nan = CropProduction() exp_nan.set_from_isimip_netcdf(input_dir=INPUT_DIR, filename=FILENAME_LU, hist_mean=FILENAME_MEAN, bbox=[0, 42, 10, 52], yearrange=(2001, 2005), scenario='flexible', unit='t/y', crop='whe', irr='firr') exp_nan.gdf.value[exp_nan.gdf.value == 0] = np.nan exp_nan.assign_centroids(haz, threshold=20) impact_nan = Impact() impact_nan.calc(exp_nan, impf_cp, haz, save_mat=True) self.assertListEqual(list(impact.at_event), list(impact_nan.at_event)) self.assertAlmostEqual(12.056545220060798, impact_nan.aai_agg) self.assertAlmostEqual(12.056545220060798, impact.aai_agg)