예제 #1
0
    def test_normalize_with_fao_cp(self):
        """ Test normalizing of two given exposures countrywise (usually firr + norr)
        with the mean crop production quantity"""
        exp = CropProduction()
        exp.set_from_single_run(input_dir=INPUT_DIR,
                                filename=FILENAME,
                                hist_mean=FILENAME_MEAN,
                                bbox=[-5, 42, 16, 55],
                                yearrange=np.array([2001, 2005]),
                                scenario='flexible',
                                crop='mai',
                                unit='t',
                                irr='firr')
        country_list, ratio, exp_firr_norm, exp_noirr_norm, fao_crop_production, exp_tot_production = \
             normalize_with_fao_cp(exp, exp, input_dir=INPUT_DIR,
                              yearrange=np.array([2009, 2018]), unit='t', return_data=True)
        self.assertAlmostEqual(ratio[2], 17.671166854032993)
        self.assertAlmostEqual(ratio[11], .86250775)
        self.assertAlmostEqual(fao_crop_production[2], 673416.4)
        self.assertAlmostEqual(fao_crop_production[11], 160328.7)
        self.assertAlmostEqual(np.nanmax(exp_firr_norm.value.values),
                               220735.69212710857)
        self.assertAlmostEqual(np.nanmax(exp_firr_norm.value.values),
                               np.nanmax(exp_noirr_norm.value.values))
        self.assertAlmostEqual(np.nanmax(exp.value.values), 284244.81023404596)
        self.assertAlmostEqual(np.nansum(exp_noirr_norm.value.values) +
                               np.nansum(exp_firr_norm.value.values),
                               np.nansum(fao_crop_production),
                               places=1)

        self.assertListEqual(list(country_list), [
            0, 40, 56, 70, 191, 203, 208, 250, 276, 380, 442, 528, 616, 705,
            724, 756, 826
        ])
예제 #2
0
    def test_EU(self):
        """test with demo data containing France and Germany"""
        bbox = [-5, 42, 16, 55]
        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.set_value_to_usd(INPUT_DIR, yearrange=(2000, 2018))
        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()
        reg_sel = exp.copy()
        reg_sel.gdf = reg_sel.gdf[reg_sel.gdf.region_id == 276]
        impact.calc(reg_sel, impf_cp, haz.select(['2002']), save_mat=True)

        exp_manual = reg_sel.gdf.value
        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.gdf.value.max(), 53074789.755290434)
        self.assertEqual(exp.gdf.latitude.values.size, 1092)
        self.assertAlmostEqual(exp.gdf.value[3], 0.0)
        self.assertAlmostEqual(exp.gdf.value[1077], 405026.6857207429)
        self.assertAlmostEqual(impact.imp_mat.data[3], -176102.5359452465)
        self.assertEqual(len(dif), 0)
예제 #3
0
 def set_value_to_usd(self):
     """Test calculating cropyield_isimip Exposure in [USD/y]"""
     exp = CropProduction()
     exp.set_from_isimip_netcdf(input_dir=INPUT_DIR, filename=FILENAME, hist_mean=FILENAME_MEAN,
                                   bbox=[-5, 42, 16, 55], yearrange=np.array([2001, 2005]),
                                   scenario='flexible', crop = 'mai', irr='firr', unit='USD/y')
     self.assertEqual(exp.gdf.longitude.min(), -4.75)
     self.assertEqual(exp.gdf.longitude.max(), 15.75)
     self.assertEqual(exp.gdf.latitude.min(), 42.25)
     self.assertEqual(exp.gdf.latitude.max(), 54.75)
     self.assertEqual(exp.gdf.value.shape, (1092,))
     self.assertEqual(exp.value_unit, 'USD/y')
     self.assertEqual(exp.crop, 'mai')
     self.assertAlmostEqual(exp.gdf.value.max(), 51603897.28533253, places=6)
예제 #4
0
    def test_load_central_EU(self):
        """Test defining crop_production Exposure from complete demo file (Central Europe)"""
        exp = CropProduction()
        exp.set_from_isimip_netcdf(input_dir=INPUT_DIR, filename=FILENAME, hist_mean=FILENAME_MEAN,
                                      bbox=[-5, 42, 16, 55], yearrange=np.array([2001, 2005]),
                                      scenario='flexible', unit='t/y', crop = 'mai', irr='firr')

        self.assertEqual(exp.gdf.longitude.min(), -4.75)
        self.assertEqual(exp.gdf.longitude.max(), 15.75)
        self.assertEqual(exp.gdf.latitude.min(), 42.25)
        self.assertEqual(exp.gdf.latitude.max(), 54.75)
        self.assertEqual(exp.gdf.value.shape, (1092,))
        self.assertEqual(exp.value_unit, 't/y')
        self.assertEqual(exp.crop, 'mai')
        self.assertAlmostEqual(exp.gdf.value.max(), 284244.81023404596, places=5)
예제 #5
0
    def test_set_from_area_and_yield_nc4(self):
        """Test defining crop_production Exposure from area and yield
        data extracted from netcdf test data for Switzerland"""
        exp = CropProduction()
        exp.set_from_area_and_yield_nc4('whe',
                                        2,
                                        2,
                                        FILENAME_YIELD,
                                        FILENAME_AREA,
                                        'yield.tot',
                                        'cultivated area all',
                                        input_dir=INPUT_DIR)

        self.assertEqual(exp.crop, 'whe')
        self.assertEqual(exp.gdf.shape[0], 55)
        self.assertEqual(exp.meta['width'] * exp.meta['height'], 55)
        self.assertIn(756, exp.gdf.region_id.values)
        self.assertIn(380, exp.gdf.region_id.values)
        self.assertAlmostEqual(exp.gdf['value'].max(), 253225.66611428373)
예제 #6
0
 def test_set_value_to_usd(self):
     """Test calculating crop_production Exposure in [USD/y]"""
     exp = CropProduction()
     exp.set_from_isimip_netcdf(input_dir=INPUT_DIR,
                                filename=FILENAME,
                                hist_mean=FILENAME_MEAN,
                                bbox=[-5, 42, 16, 55],
                                yearrange=np.array([2001, 2005]),
                                scenario='flexible',
                                unit='t/y',
                                crop='mai',
                                irr='firr')
     exp.set_value_to_usd(INPUT_DIR, yearrange=(2000, 2018))
     self.assertEqual(exp.gdf.longitude.min(), -4.75)
     self.assertEqual(exp.gdf.longitude.max(), 15.75)
     self.assertEqual(exp.gdf.latitude.min(), 42.25)
     self.assertEqual(exp.gdf.latitude.max(), 54.75)
     self.assertEqual(exp.gdf.value.shape, (1092, ))
     self.assertEqual(exp.value_unit, 'USD/y')
     self.assertEqual(exp.crop, 'mai')
     self.assertAlmostEqual(exp.gdf.tonnes_per_year[28], 1998.3634803238633)
     self.assertAlmostEqual(exp.gdf.value.max(),
                            51603897.28533253,
                            places=5)
     self.assertAlmostEqual(exp.gdf.value.mean(),
                            907401.9933073953,
                            places=5)
     self.assertEqual(exp.gdf.value.min(), 0.0)
예제 #7
0
    def test_set_value_to_kcal(self):
        """Test calculating crop_production Exposure in [kcal/y]"""
        exp = CropProduction()
        exp.set_from_isimip_netcdf(input_dir=INPUT_DIR,
                                   filename=FILENAME,
                                   hist_mean=FILENAME_MEAN,
                                   bbox=[-5, 45, 10, 50],
                                   yearrange=np.array([2001, 2005]),
                                   scenario='flexible',
                                   unit='t/y',
                                   crop='mai',
                                   irr='firr')
        max_tonnes = exp.gdf.value.max()
        exp.set_value_to_kcal()

        self.assertEqual(exp.gdf.latitude.min(), 45.25)
        self.assertEqual(exp.gdf.latitude.max(), 49.75)
        self.assertEqual(exp.gdf.value.shape, (300, ))
        self.assertAlmostEqual(exp.gdf.value.max(),
                               365 * 10 * 1000 * max_tonnes,
                               places=3)
        self.assertAlmostEqual(exp.gdf.value.max(),
                               874488976392.9623,
                               places=3)
        self.assertAlmostEqual(exp.gdf.value.mean(),
                               19910311383.52674,
                               places=4)
        self.assertEqual(exp.gdf.value.min(), 0.0)
예제 #8
0
    def test_set_value_to_kcal(self):
        """Test calculating crop_production Exposure in [kcal/y]"""

        # (1) biomass = True
        exp = CropProduction()
        exp.set_from_isimip_netcdf(input_dir=INPUT_DIR,
                                   filename=FILENAME,
                                   hist_mean=FILENAME_MEAN,
                                   bbox=[-5, 45, 10, 50],
                                   yearrange=np.array([2001, 2005]),
                                   scenario='flexible',
                                   unit='t/y',
                                   crop='mai',
                                   irr='firr')
        max_tonnes = exp.gdf.value.max()
        exp.set_value_to_kcal()

        self.assertEqual(exp.gdf.latitude.min(), 45.25)
        self.assertEqual(exp.gdf.latitude.max(), 49.75)
        self.assertEqual(exp.gdf.value.shape, (300, ))
        self.assertAlmostEqual(exp.gdf.value.max(),
                               3.56e6 * max_tonnes,
                               places=3)
        self.assertAlmostEqual(exp.gdf.value.max(),
                               852926234509.3002,
                               places=3)
        self.assertAlmostEqual(exp.gdf.value.mean(),
                               19419372198.727455,
                               places=4)
        self.assertEqual(exp.gdf.value.min(), 0.0)

        # (2) biomass = False
        exp = CropProduction()
        exp.set_from_isimip_netcdf(input_dir=INPUT_DIR,
                                   filename=FILENAME,
                                   hist_mean=FILENAME_MEAN,
                                   bbox=[-5, 45, 10, 50],
                                   yearrange=np.array([2001, 2005]),
                                   scenario='flexible',
                                   unit='t/y',
                                   crop='mai',
                                   irr='firr')
        max_tonnes = exp.gdf.value.max()
        exp.set_value_to_kcal(biomass=False)
        self.assertEqual(exp.gdf.latitude.min(), 45.25)
        self.assertEqual(exp.gdf.value.shape, (300, ))
        self.assertAlmostEqual(exp.gdf.value.max(),
                               3.56e6 * max_tonnes / (1 - .12),
                               places=3)
        self.assertAlmostEqual(exp.gdf.value.mean(),
                               22067468407.644833,
                               places=4)
예제 #9
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)