def emdat_impact_event(countries, hazard_name, emdat_file_csv, year_range, \
                       reference_year=0, imp_str="Total damage ('000 US$)"):
    """function to load EM-DAT data return impact per event

    Parameters:
        countries (list of str): country ISO3-codes or names, i.e. ['JAM'].
        hazard_name (str): Hazard name according to EMDAT terminology or
            CLIMADA abbreviation, i.e. 'TC'
        emdat_file_csv (str): Full path to EMDAT-file (CSV), i.e.:
            emdat_file_csv = os.path.join(SYSTEM_DIR, 'emdat_201810.csv')
        reference_year (int): reference year of exposures. Impact is scaled
            proportional to GDP to the value of the reference year. No scaling
            for 0 (default)
        imp_str (str): Column name of impact metric in EMDAT CSV,
            default = "Total damage ('000 US$)"

    Returns:
        out (pandas DataFrame): EMDAT DataFrame with new columns "year",
            "region_id", and scaled total impact per event with
            same unit as chosen impact,
            i.e. 1000 current US$ for imp_str="Total damage ('000 US$) scaled".
    """
    out = pd.DataFrame()
    for country in countries:
        data, _, country = emdat_df_load(country, hazard_name, \
                                                 emdat_file_csv, year_range)
        if data is None:
            continue
        if reference_year > 0:
            gdp_ref = gdp(country, reference_year)[1]
        else:
            gdp_ref = 0
        data['year'] = pd.Series(np.zeros(data.shape[0], dtype='int'), \
            index=data.index)
        data['region_id'] = pd.Series(int(iso_cntry.get(country).numeric) + \
            np.zeros(data.shape[0], dtype='int'), \
            index=data.index)
        data['reference_year'] = pd.Series(reference_year+np.zeros(\
            data.shape[0], dtype='int'), index=data.index)
        data[imp_str + " scaled"] = pd.Series(np.zeros(data.shape[0], dtype='int'), \
            index=data.index)
        for cnt in np.arange(data.shape[0]):
            data.loc[cnt, 'year'] = int(data.loc[cnt, 'Disaster No.'][0:4])
            data.loc[cnt, 'reference_year'] = int(reference_year)
            if data.loc[cnt][imp_str] > 0 and gdp_ref > 0:
                data.loc[cnt, imp_str + " scaled"] = \
                    data.loc[cnt, imp_str] * gdp_ref / \
                    gdp(country, int(data.loc[cnt, 'year']))[1]
        out = out.append(data)
        del data
    out = out.reset_index(drop=True)
    if '000 US' in imp_str and not out.empty:  # EM-DAT damages provided in '000 USD
        out[imp_str + " scaled"] = out[imp_str + " scaled"] * 1e3
        out[imp_str] = out[imp_str] * 1e3
    return out
def emdat_impact_yearlysum(countries, hazard_name, emdat_file_csv, year_range=None, \
                         reference_year=0, imp_str="Total damage ('000 US$)"):
    """function to load EM-DAT data and sum impact per year
    Parameters:
        countries (list of str): country ISO3-codes or names, i.e. ['JAM'].
        hazard_name (str): Hazard name according to EMDAT terminology or
            CLIMADA abbreviation
        emdat_file_csv (str): Full path to EMDAT-file (CSV), i.e.:
            emdat_file_csv = os.path.join(SYSTEM_DIR, 'emdat_201810.csv')
        reference_year (int): reference year of exposures. Impact is scaled
            proportional to GDP to the value of the reference year. No scaling
            for 0 (default)
        imp_str (str): Column name of impact metric in EMDAT CSV,
            default = "Total damage ('000 US$)"

    Returns:
        yearly_impact (dict, mapping years to impact):
            total impact per year, same unit as chosen impact,
            i.e. 1000 current US$ for imp_str="Total damage ('000 US$)".
        all_years (list of int): list of years
    """

    out = pd.DataFrame()
    for country in countries:
        data, all_years, country = emdat_df_load(country, hazard_name, \
                                            emdat_file_csv, year_range)
        if data is None:
            continue
        data_out = pd.DataFrame(index=np.arange(0, len(all_years)), \
                                columns=['ISO3', 'region_id', 'year', 'impact', \
                                'reference_year', 'impact_scaled'])
        if reference_year > 0:
            gdp_ref = gdp(country, reference_year)[1]
        for cnt, year in enumerate(all_years):
            data_out.loc[cnt, 'year'] = year
            data_out.loc[cnt, 'reference_year'] = reference_year
            data_out.loc[cnt, 'ISO3'] = country
            data_out.loc[cnt,
                         'region_id'] = int(iso_cntry.get(country).numeric)
            data_out.loc[cnt, 'impact'] = \
                sum(data.loc[data['Disaster No.'].str.contains(str(year))]\
                             [imp_str])
            if '000 US' in imp_str:  # EM-DAT damages provided in '000 USD
                data_out.loc[cnt,
                             'impact'] = data_out.loc[cnt, 'impact'] * 1000
            if reference_year > 0:
                data_out.loc[cnt, 'impact_scaled'] = data_out.loc[cnt, 'impact'] * \
                gdp_ref / gdp(country, year)[1]
        out = out.append(data_out)
    out = out.reset_index(drop=True)
    return out
Beispiel #3
0
    def test_switzerland300_pass(self):
        """Create LitPop entity for Switzerland on 300 arcsec:"""
        country_name = ['CHE']
        resolution = 300
        fin_mode = 'income_group'
        with self.assertLogs('climada.entity.exposures.litpop',
                             level='INFO') as cm:
            ent = lp.LitPop.from_countries(country_name,
                                           res_arcsec=resolution,
                                           fin_mode=fin_mode,
                                           reference_year=2016)

        self.assertIn('LitPop: Init Exposure for country: CHE', cm.output[0])
        self.assertEqual(ent.gdf.region_id.min(), 756)
        self.assertEqual(ent.gdf.region_id.max(), 756)
        # confirm that the total value is equal to GDP * (income_group+1):
        self.assertAlmostEqual(ent.gdf.value.sum() / gdp('CHE', 2016)[1],
                               (income_group('CHE', 2016)[1] + 1))
        self.assertIn("LitPop Exposure for ['CHE'] at 300 as, year: 2016",
                      ent.tag.description)
        self.assertIn('income_group', ent.tag.description)
        self.assertIn('1, 1', ent.tag.description)
        self.assertTrue(u_coord.equal_crs(ent.crs, 'epsg:4326'))
        self.assertEqual(ent.meta['width'], 54)
        self.assertEqual(ent.meta['height'], 23)
        self.assertTrue(u_coord.equal_crs(ent.meta['crs'], 'epsg:4326'))
        self.assertAlmostEqual(ent.meta['transform'][0], 0.08333333333333333)
        self.assertAlmostEqual(ent.meta['transform'][1], 0)
        self.assertAlmostEqual(ent.meta['transform'][2], 5.9166666666666)
        self.assertAlmostEqual(ent.meta['transform'][3], 0)
        self.assertAlmostEqual(ent.meta['transform'][4], -0.08333333333333333)
        self.assertAlmostEqual(ent.meta['transform'][5], 47.75)
Beispiel #4
0
def fill_econ_indicators(ref_year, cntry_info, shp_file, **kwargs):
    """Get GDP and income group per country in reference year, or it closest
    one. Source: world bank. Natural earth repository used when missing data.
    Modifies country info with values [country id, country name,
    country geometry, ref_year, gdp, income_group].

    Parameters:
        ref_year (int): reference year
        cntry_info (dict): key = ISO alpha_3 country, value = [country id,
            country name, country geometry]
        kwargs (optional): 'gdp' and 'inc_grp' dictionaries with keys the
            country ISO_alpha3 code. If provided, these are used
    """
    for cntry_iso, cntry_val in cntry_info.items():
        cntry_val.append(ref_year)
        if 'gdp' in kwargs and kwargs['gdp'][cntry_iso] != '':
            gdp_val = kwargs['gdp'][cntry_iso]
        else:
            _, gdp_val = gdp(cntry_iso, ref_year, shp_file)
        cntry_val.append(gdp_val)

        if 'inc_grp' in kwargs and kwargs['inc_grp'][cntry_iso] != '':
            inc_grp = kwargs['inc_grp'][cntry_iso]
        else:
            _, inc_grp = income_group(cntry_iso, ref_year, shp_file)
        cntry_val.append(inc_grp)
def scale_impact2refyear(impact_values,
                         year_values,
                         iso3a_values,
                         reference_year=None):
    """Scale give impact values proportional to GDP to the according value in a reference year
    (for normalization of monetary values)

    Parameters
    ----------
    impact_values : list or array
        Impact values to be scaled.
    year_values : list or array
        Year of each impact (same length as impact_values)
    iso3a_values : list or array
        ISO3alpha code of country for each impact (same length as impact_values)

    Optional Parameters
    -------------------
    reference_year : int
        Impact is scaled proportional to GDP to the value of the reference year.
        No scaling for reference_year=None (default)
        """
    impact_values = np.array(impact_values)
    year_values = np.array(year_values)
    iso3a_values = np.array(iso3a_values)
    if reference_year and isinstance(reference_year, (int, float)):
        reference_year = int(reference_year)
        gdp_ref = dict()
        gdp_years = dict()
        for country in np.unique(iso3a_values):
            # get reference GDP value for each country:
            gdp_ref[country] = gdp(country, reference_year)[1]
            # get GDP value for each country and year:
            gdp_years[country] = dict()
            years_country = np.unique(year_values[iso3a_values == country])
            print(years_country)
            for year in years_country:
                gdp_years[country][year] = gdp(country, year)[1]
        # loop through each value and apply scaling:
        for idx, val in enumerate(impact_values):
            impact_values[idx] = val * gdp_ref[iso3a_values[idx]] / \
                gdp_years[iso3a_values[idx]][year_values[idx]]
        return list(impact_values)
    if not reference_year:
        return impact_values
    raise ValueError('Invalid reference_year')
 def test_gdp_twn_2012_pass(self):
     """Test gdp function TWN."""
     ref_year = 2014
     res_year, res_val = gdp('TWN', ref_year)
     _, res_val_direct = _gdp_twn(ref_year)
     ref_val = 530515000000.0
     ref_year = 2014
     self.assertEqual(res_year, ref_year)
     self.assertEqual(res_val, ref_val)
     self.assertEqual(res_val_direct, ref_val)
Beispiel #7
0
    def test_gdp_sxm_2012_pass(self):
        """ Test gdp function Sint Maarten."""
        ref_year = 2012
        with self.assertLogs('climada.util.finance', level='INFO') as cm:
            res_year, res_val = gdp('SXM', ref_year)

        ref_val = 3.658e+08
        ref_year = 2014
        self.assertIn('GDP SXM 2014: 3.658e+08', cm.output[0])
        self.assertEqual(res_year, ref_year)
        self.assertEqual(res_val, ref_val)
    def test_gdp_sxm_2010_pass(self):
        """Test gdp function Sint Maarten."""
        # If World Bank input data changes, make sure to set ref_year to a year where
        # no data is available so that the next available data point has to be selected.
        ref_year = 2010
        with self.assertLogs('climada.util.finance', level='INFO') as cm:
            res_year, res_val = gdp('SXM', ref_year)

        ref_val = 936089385.47486  # reference GDP value
        ref_year = 2011  # nearest year with data available (might change)
        # GDP and years with data available might change if worldbank input
        # data changes, check magnitude and adjust ref_val and/or ref_year
        # if test fails:
        self.assertIn('GDP SXM %i: %1.3e' % (ref_year, ref_val), cm.output[0])
        self.assertEqual(res_year, ref_year)
        self.assertAlmostEqual(res_val, ref_val, places=0)