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)
def test_load_EU_all(self): """Test defining crop potential hazard from complete demo file (Central Europe)""" haz = RelativeCropyield() haz.set_from_single_run(input_dir=INPUT_DIR, yearrange=(2001, 2005), ag_model='lpjml', cl_model='ipsl-cm5a-lr', scenario='historical', soc='2005soc', co2='co2', crop='whe', irr='noirr', fn_str_var=FN_STR_DEMO) self.assertEqual(haz.crop, 'whe') self.assertEqual(haz.tag.haz_type, 'RC') self.assertIn('lpjml', haz.tag.file_name) self.assertIn('ipsl-cm5a-lr', haz.tag.file_name) self.assertIn('hist', haz.tag.file_name) self.assertIn('2005soc', haz.tag.file_name) self.assertIn('noirr', haz.tag.file_name) self.assertEqual(haz.centroids.lon.min(), -4.75) self.assertEqual(haz.centroids.lon.max(), 15.75) self.assertEqual(haz.centroids.lat.min(), 42.25) self.assertEqual(haz.centroids.lat.max(), 54.75) self.assertEqual(haz.intensity.shape, (5, 1092)) self.assertEqual(haz.event_id.size, 5) self.assertAlmostEqual(haz.intensity.max(), 10.176164, places=5)
def test_set_percentile_to_int(self): """Test setting intensity to percentile of the yield""" haz = RelativeCropyield() haz.set_from_single_run(input_dir=INPUT_DIR, yearrange=(2001, 2005), ag_model='lpjml', cl_model='ipsl-cm5a-lr', scenario='historical', soc='2005soc', co2='co2', crop='whe', irr='noirr', fn_str_var=FN_STR_DEMO) haz.set_percentile_to_int() self.assertEqual(haz.intensity_def, 'Percentile') self.assertEqual(haz.intensity.shape, (5, 1092)) self.assertAlmostEqual(haz.intensity.max(), 1.0, places=5) self.assertAlmostEqual(haz.intensity.min(), 0.2, places=5) self.assertAlmostEqual(haz.intensity.data[10], 0.6, places=5)
def test_set_rel_yield(self): """Test setting intensity to relativ yield""" haz = RelativeCropyield() haz.set_from_single_run(input_dir=INPUT_DIR, yearrange=(2001, 2005), 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(np.array([2001, 2005])) self.assertEqual(haz.intensity_def, 'Yearly Yield') haz.set_rel_yield_to_int(hist_mean) self.assertEqual(haz.intensity_def, 'Relative Yield') self.assertEqual(np.shape(hist_mean), (1092,)) self.assertAlmostEqual(np.max(hist_mean), 8.397826, places=5) self.assertEqual(haz.intensity.shape, (5, 1092)) self.assertAlmostEqual(np.nanmax(haz.intensity.toarray()), 4.0, places=5) self.assertAlmostEqual(haz.intensity.max(), 4.0, places=5) self.assertAlmostEqual(haz.intensity.min(), -1.0, places=5)