Beispiel #1
0
 def test_switzerland300_pc2016_pass(self):
     """Create LitPop entity for Switzerland 2016 with admin1 and produced capital:"""
     country_name = ['CHE']
     fin_mode = 'pc'
     resolution = 300
     ref_year = 2016
     adm1 = True
     cons = True
     comparison_total_val = world_bank_wealth_account(country_name[0],
                                                      ref_year,
                                                      no_land=1)[1]
     ent = LitPop()
     with self.assertLogs('climada.entity.exposures.litpop',
                          level='INFO') as cm:
         ent.set_country(country_name,
                         res_arcsec=resolution,
                         reference_year=ref_year,
                         fin_mode=fin_mode,
                         conserve_cntrytotal=cons,
                         calc_admin1=adm1)
     # print(cm)
     self.assertIn('Generating LitPop data at a resolution of 300 arcsec',
                   cm.output[0])
     self.assertEqual(np.around(ent.gdf.value.sum(), 0),
                      np.around(comparison_total_val, 0))
     self.assertAlmostEqual(ent.gdf.value.sum() / 2225854927260, 1.0)
Beispiel #2
0
 def test_switzerland300_pass(self):
     """Create LitPop entity for Switzerland on 300 arcsec:"""
     country_name = ['CHE']
     resolution = 300
     fin_mode = 'income_group'
     ent = LitPop()
     with self.assertLogs('climada.entity.exposures.litpop',
                          level='INFO') as cm:
         ent.set_country(country_name,
                         res_arcsec=resolution,
                         fin_mode=fin_mode)
     # print(cm)
     self.assertIn('Generating LitPop data at a resolution of 300 arcsec',
                   cm.output[0])
     self.assertTrue(ent.region_id.min() == 756)
     self.assertTrue(ent.region_id.max() == 756)
     self.assertTrue(np.int(ent.value.sum().round()) == 3350905328146)
     self.assertIn('LitPop for Switzerland at 300 as, year=2016',
                   ent.tag.description)
     self.assertIn('financial mode=income_group', ent.tag.description)
     self.assertIn('GPW-year=2015', ent.tag.description)
     self.assertIn('BM-year=2016', ent.tag.description)
     self.assertIn('exp=[1, 1]', ent.tag.description)
     self.assertTrue(equal_crs(ent.crs['init'], {'init': 'epsg:4326'}))
     self.assertEqual(ent.meta['width'], 54)
     self.assertEqual(ent.meta['height'], 23)
     self.assertTrue(equal_crs(ent.meta['crs'], {'init': '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.7499999999999)
Beispiel #3
0
 def test_exposure_set_admin1_BGD(self):
     """Test function exposure_set_admin1 of litpop for Bangladesh"""
     country_name = ['BGD']
     resolution = 600
     ent = LitPop()
     ent.set_country(country_name, res_arcsec=resolution)
     ent = lp.exposure_set_admin1(ent, 600)
     self.assertIn(5492, ent.admin1_ID.values)
     self.assertIn('Chittagong', ent.admin1.values)
     self.assertEqual(len(np.unique(ent.admin1_ID)), 7)
     self.assertEqual(np.min(np.unique(ent.admin1_ID)), 1806.0)
Beispiel #4
0
 def test_suriname30_nfw_pass(self):
     """Create LitPop entity for Suriname for non-finanical wealth:"""
     country_name = ['SUR']
     fin_mode = 'nfw'
     ent = LitPop()
     with self.assertLogs('climada.entity.exposures.litpop',
                          level='INFO') as cm:
         ent.set_country(country_name,
                         reference_year=2016,
                         fin_mode=fin_mode)
     # print(cm)
     self.assertIn('Generating LitPop data at a resolution of 30.0 arcsec',
                   cm.output[0])
     self.assertTrue(ent.region_id.min() == 740)
     self.assertTrue(ent.region_id.max() == 740)
     self.assertTrue(np.int(ent.value.sum().round()) == 2332806589)
Beispiel #5
0
 def test_switzerland30normPop_pass(self):
     """Create LitPop entity for Switzerland on 30 arcsec:"""
     country_name = ['CHE']
     resolution = 30
     exp = [0, 1]
     fin_mode = 'norm'
     ent = LitPop()
     with self.assertLogs('climada.entity.exposures.litpop',
                          level='INFO') as cm:
         ent.set_country(country_name, res_arcsec=resolution, exponent=exp,\
                         fin_mode=fin_mode, reference_year=2015)
     # print(cm)
     self.assertIn('Generating LitPop data at a resolution of 30 arcsec',
                   cm.output[0])
     self.assertTrue(ent.region_id.min() == 756)
     self.assertTrue(ent.region_id.max() == 756)
     self.assertTrue(np.int((1000 * ent.value.sum()).round()) == 1000)
Beispiel #6
0
 def test_switzerland300_pc2013_pass(self):
     """Create LitPop entity for Switzerland 2013 for produced capital:"""
     country_name = ['CHE']
     fin_mode = 'pc'
     resolution = 300
     ref_year = 2013
     comparison_total_val = world_bank_wealth_account(country_name[0], \
                                                      ref_year, no_land=1)[1]
     ent = LitPop()
     with self.assertLogs('climada.entity.exposures.litpop',
                          level='INFO') as cm:
         ent.set_country(country_name, res_arcsec=resolution, \
                         reference_year=ref_year, fin_mode=fin_mode)
     # print(cm)
     self.assertIn('Generating LitPop data at a resolution of 300 arcsec',
                   cm.output[0])
     self.assertTrue(ent.value.sum() == comparison_total_val)
     self.assertTrue(np.int(ent.value.sum().round()) == 2296358085749)
Beispiel #7
0
 def test_switzerland300_pass(self):
     """Create LitPop entity for Switzerland on 300 arcsec:"""
     country_name = ['CHE']
     resolution = 300
     fin_mode = 'income_group'
     ent = LitPop()
     with self.assertLogs('climada.entity.exposures.litpop',
                          level='INFO') as cm:
         ent.set_country(country_name,
                         res_arcsec=resolution,
                         fin_mode=fin_mode)
     # print(cm)
     self.assertIn('Generating LitPop data at a resolution of 300 arcsec',
                   cm.output[0])
     self.assertTrue(ent.region_id.min() == 756)
     self.assertTrue(ent.region_id.max() == 756)
     self.assertTrue(np.int(ent.value.sum().round()) == 3350905328146)
     self.assertIn('LitPop for Switzerland at 300 as, year=2016',
                   ent.tag.description)
     self.assertIn('financial mode=income_group', ent.tag.description)
     self.assertIn('GPW-year=2015', ent.tag.description)
     self.assertIn('BM-year=2016', ent.tag.description)
     self.assertIn('exp=[1, 1]', ent.tag.description)
Beispiel #8
0
def _get_litpop_bbox(country, highValueArea, **kwargs):
    """get litpop exposure for the bbox area of the queried OSM features
    Parameters:
        country (str)
        highValueArea (GeoDataFrame)
        bbox (array)
        kwargs (dict): arguments for LitPop set_country method
    Returns:
        exp_sub (LitPop)
    """
    # Load LitPop Exposure for whole country, and High Value Area
    exp = LitPop()
    exp.set_country(country, **kwargs)
    exp.set_geometry_points()

    # Crop bbox of High Value Area from Country Exposure
    exp.gdf = exp.gdf.cx[min(highValueArea.bounds.minx):max(highValueArea.bounds.maxx),
                         min(highValueArea.bounds.miny):max(highValueArea.bounds.maxy)]

    return exp
Beispiel #9
0
def get_entity(country, exponents, description_str, date_str):

    file_name = (country + '_litpop_' + description_str + '_' + date_str +
                 '.hdf5')
    exposure_file = os.path.abspath(os.path.join( \
                CONFIG['local_data']['save_dir'], file_name))
    if os.path.isfile(exposure_file):
        exposure = LitPop()
        exposure.read_hdf5(exposure_file)


#        with open(exposure_file, 'rb') as f:
#            exposure = pickle.load(f)
    else:
        exposure = LitPop()
        # The arguments 'exponent' is used to set the power with which Lit and Pop go into LitPop:
        exposure.set_country(country, exponents=exponents, reference_year=2014)
        exposure.set_geometry_points()
        exposure.check()
        exposure.tag.description = (exposure.tag.description + '_' +
                                    description_str)
        exposure.set_lat_lon()
        exposure.write_hdf5(exposure_file)
        #pickle.dump(exposure,open(exposure_file, 'wb'))
    return exposure
filename_start = 'LitPop_pc_%iarcsec_' % (RES_ARCSEC)
RES_DIR = output_path  # '/Users/eberenzs/Documents/Projects/climada_papers/201903_litpop_exposure_data_model/climada_v1.3.1/results'

write_to_hdf5 = True
try_read_from_hdf5 = True

if not os.path.exists(RES_DIR):
    os.makedirs(RES_DIR)

files = [i for i in os.listdir(ENTITY_DIR) if os.path.isfile(os.path.join(ENTITY_DIR,i)) and \
         filename_start in i]
files = np.unique(files)
print('Number of country exposure files: %i' % (len(files)))

#
exposure_data = LitPop()

if try_read_from_hdf5 and os.path.exists(
        os.path.join(ENTITY_DIR, 'LitPop_pc_%iarcsec_000_all.hdf5' %
                     (RES_ARCSEC))):
    exposure_data.read_hdf5(
        os.path.join(ENTITY_DIR,
                     'LitPop_pc_%iarcsec_000_all.hdf5' % (RES_ARCSEC)))
else:
    grid_stats = pd.DataFrame(
        index=np.arange(0, len(files)),
        columns=['country', 'grid_count', 'sum', 'max', 'mean', 'median'])
    for idx, fi in enumerate(files):
        print('Loading: %s ...' % (fi))
        exposure_tmp = LitPop()
        exposure_tmp = exposure_tmp.from_csv(os.path.join(ENTITY_DIR, fi),
Beispiel #11
0
#    x0 = list(param_dict.values())
#    res = minimize(specific_calib, x0,
#                   constraints=cons,
#                   method='SLSQP',
#                   options={'xtol': 1e-5, 'disp': True, 'maxiter': 50})
#    
#    return param_dict_result


if __name__ == "__main__":
# yearly
 
    ## tryout calib_all
    hazard = TropCyclone()
    hazard.read_hdf5('C:/Users/ThomasRoosli/tc_NA_hazard.hdf5')
    exposure = LitPop()
    exposure.read_hdf5('C:/Users/ThomasRoosli/DOM_LitPop.hdf5')
    if_name_or_instance = 'emanuel'
    param_full_dict = {'v_thresh': [25.7, 20], 'v_half': [70], 'scale': [1, 0.8]}
    
    impact_data_source = {'emdat':('D:/Documents_DATA/EM-DAT/'
                                   '20181031_disaster_list_all_non-technological/'
                                   'ThomasRoosli_2018-10-31.csv')}
    year_range = [2004, 2017]
    yearly_impact = True
    df_result = calib_all(hazard,exposure,if_name_or_instance,param_full_dict,
                  impact_data_source, year_range, yearly_impact)
    
                                    reference_year=REF_YEAR)
#    ## tryout calib_optimize
#    hazard = TropCyclone()