示例#1
0
    def set_countries(self, countries, ref_year=2016, res_km=None, from_hr=None,
                      admin_file='admin_0_countries', **kwargs):
        """ Model countries using values at reference year. If GDP or income
        group not available for that year, consider the value of the closest
        available year.

        Parameters:
            countries (list or dict): list of country names (admin0 or subunits)
                or dict with key = admin0 name and value = [admin1 names]
            ref_year (int, optional): reference year. Default: 2016
            res_km (float, optional): approx resolution in km. Default:
                nightlights resolution.
            from_hr (bool, optional): force to use higher resolution image,
                independently of its year of acquisition.
            admin_file (str): file name, admin_0_countries or admin_0_map_subunits
            kwargs (optional): 'gdp' and 'inc_grp' dictionaries with keys the
                country ISO_alpha3 code. 'poly_val' polynomial transformation
                [1,x,x^2,...] to apply to nightlight (DEF_POLY_VAL used if not
                provided). If provided, these are used.
        """
        admin_key_dict = {'admin_0_countries': ['ADMIN', 'ADM0_A3'],
                          'admin_0_map_subunits': ['SUBUNIT', 'SU_A3']}

        shp_file = shapereader.natural_earth(resolution='10m',
                                             category='cultural',
                                             name=admin_file)
        shp_file = shapereader.Reader(shp_file)

        cntry_info, cntry_admin1 = country_iso_geom(countries, shp_file,
                                                    admin_key_dict[admin_file])
        fill_econ_indicators(ref_year, cntry_info, shp_file, **kwargs)

        nightlight, coord_nl, fn_nl, res_fact, res_km = get_nightlight(
            ref_year, cntry_info, res_km, from_hr)

        tag = Tag()
        bkmrbl_list = []

        for cntry_iso, cntry_val in cntry_info.items():

            bkmrbl_list.append(
                self._set_one_country(cntry_val, nightlight, coord_nl, res_fact, res_km,
                                      cntry_admin1[cntry_iso], **kwargs))
            tag.description += ("{} {:d} GDP: {:.3e} income group: {:d} \n").\
                format(cntry_val[1], cntry_val[3], cntry_val[4], cntry_val[5])

        Exposures.__init__(self, gpd.GeoDataFrame(
            pd.concat(bkmrbl_list, ignore_index=True)), crs=DEF_CRS)

        # set metadata
        self.ref_year = ref_year
        self.tag = tag
        self.tag.file_name = fn_nl
        self.value_unit = 'USD'
        rows, cols, ras_trans = pts_to_raster_meta(
            (self.longitude.min(), self.latitude.min(),
             self.longitude.max(), self.latitude.max()),
            (coord_nl[0, 1], -coord_nl[0, 1]))
        self.meta = {'width': cols, 'height': rows, 'crs': self.crs, 'transform': ras_trans}
示例#2
0
    def set_countries(self, countries=[], reg=[], ref_year=2000,
                      path=None):
        """Model countries using values at reference year. If GDP or income
        group not available for that year, consider the value of the closest
        available year.

        Parameters:
            countries (list): list of country names ISO3
            ref_year (int, optional): reference year. Default: 2016
            path (string): path to exposure dataset (ISIMIP)
        """
        gdp2a_list = []
        tag = Tag()

        if path is None:
            LOGGER.error('No path for exposure data set')
            raise NameError

        if not os.path.exists(path):
            LOGGER.error('Invalid path %s', path)
            raise NameError
        try:

            if not countries:
                if reg:
                    natISO = region2isos(reg)
                    countries = np.array(natISO)
                else:
                    LOGGER.error('set_countries requires countries or reg')
                    raise ValueError

            for cntr_ind in range(len(countries)):
                gdp2a_list.append(self._set_one_country(countries[cntr_ind],
                                                        ref_year, path))
                tag.description += ("{} GDP2Asset \n").\
                    format(countries[cntr_ind])
            Exposures.__init__(self, gpd.GeoDataFrame(
                pd.concat(gdp2a_list, ignore_index=True)))
        except KeyError:
            LOGGER.error('Exposure countries: %s or reg %s could not be set, check ISO3 or'
                         ' reference year %s', countries, reg, ref_year)
            raise KeyError
        self.tag = tag
        self.ref_year = ref_year
        self.value_unit = 'USD'
        self.tag.description = 'GDP2Asset ' + str(self.ref_year)
        self.crs = DEF_CRS
        # set meta
        res = 0.0416666


        rows, cols, ras_trans = pts_to_raster_meta((self.longitude.min(),
                                                    self.latitude.min(),
                                                    self.longitude.max(),
                                                    self.latitude.max()), res)
        self.meta = {'width': cols, 'height': rows, 'crs': self.crs,
                     'transform': ras_trans}
示例#3
0
    def set_countries(self,
                      countries,
                      ref_year=2016,
                      res_km=None,
                      from_hr=None,
                      **kwargs):
        """ Model countries using values at reference year. If GDP or income
        group not available for that year, consider the value of the closest
        available year.

        Parameters:
            countries (list or dict): list of country names (admin0) or dict
                with key = admin0 name and value = [admin1 names]
            ref_year (int, optional): reference year. Default: 2016
            res_km (float, optional): approx resolution in km. Default:
                nightlights resolution.
            from_hr (bool, optional): force to use higher resolution image,
                independently of its year of acquisition.
            kwargs (optional): 'gdp' and 'inc_grp' dictionaries with keys the
                country ISO_alpha3 code. 'poly_val' polynomial transformation
                [1,x,x^2,...] to apply to nightlight (DEF_POLY_VAL used if not
                provided). If provided, these are used.
        """
        shp_file = shapereader.natural_earth(resolution='10m',
                                             category='cultural',
                                             name='admin_0_countries')
        shp_file = shapereader.Reader(shp_file)

        cntry_info, cntry_admin1 = country_iso_geom(countries, shp_file)
        fill_econ_indicators(ref_year, cntry_info, shp_file, **kwargs)

        nightlight, coord_nl, fn_nl, res_fact, res_km = get_nightlight(\
            ref_year, cntry_info, res_km, from_hr)

        tag = Tag()
        bkmrbl_list = []

        for cntry_iso, cntry_val in cntry_info.items():

            bkmrbl_list.append(self._set_one_country(cntry_val, nightlight, \
                coord_nl, res_fact, res_km, cntry_admin1[cntry_iso], **kwargs))
            tag.description += ("{} {:d} GDP: {:.3e} income group: {:d} \n").\
                format(cntry_val[1], cntry_val[3], cntry_val[4], cntry_val[5])

        Exposures.__init__(
            self, gpd.GeoDataFrame(pd.concat(bkmrbl_list, ignore_index=True)))

        # set metadata
        self.ref_year = ref_year
        self.tag = tag
        self.tag.file_name = fn_nl
        self.value_unit = 'USD'
        self.crs = {'init': 'epsg:4326'}
示例#4
0
    def set_countries(self, countries=[], reg=[], ref_year=2000,
                      path=None):
        """ Model countries using values at reference year. If GDP or income
        group not available for that year, consider the value of the closest
        available year.

        Parameters:
            countries (list): list of country names ISO3
            ref_year (int, optional): reference year. Default: 2016
            path (string): path to exposure dataset
        """
        gdp2a_list = []
        tag = Tag()

        if path is None:
            LOGGER.error('No path for exposure data set')
            raise NameError

        if not os.path.exists(path):
            LOGGER.error('Invalid path ' + path)
            raise NameError
        try:

            if not countries:
                if reg:
                    natID_info = pd.read_csv(NAT_REG_ID)
                    natISO = natID_info["ISO"][np.isin(natID_info["Reg_name"],
                                                       reg)]
                    countries = np.array(natISO)
                else:
                    LOGGER.error('set_countries requires countries or reg')
                    raise ValueError

            for cntr_ind in range(len(countries)):
                gdp2a_list.append(self._set_one_country(countries[cntr_ind],
                                                        ref_year, path))
                tag.description += ("{} GDP2Asset \n").\
                    format(countries[cntr_ind])
            Exposures.__init__(self, gpd.GeoDataFrame(
                        pd.concat(gdp2a_list, ignore_index=True)))
        except KeyError:
            LOGGER.error('Exposure countries: ' + str(countries) + ' or reg ' +
                         str(reg) + ' could not be set, check ISO3 or' +
                         ' reference year ' + str(ref_year))
            raise KeyError
        self.ref_year = ref_year
        self.value_unit = 'USD'
        self.tag = tag
        self.crs = DEF_CRS