Merge branch 'feature/add-missing-jrc-flood-impact-functions' into fe… #123
4 fail, 2 skipped, 183 pass in 4m 37s
189 tests 183 ✅ 4m 37s ⏱️
1 suites 2 💤
1 files 4 ❌
Results for commit 4a66319.
Annotations
Check warning on line 0 in climada_petals.hazard.test.test_flood.TestRiverFlood
github-actions / Petals / Unit Test Results (3.11)
test_centroids_flood (climada_petals.hazard.test.test_flood.TestRiverFlood) failed
climada_petals/tests_xml/tests.xml [took 0s]
Raw output
AttributeError: 'Centroids' object has no attribute 'meta'
self = <climada_petals.hazard.test.test_flood.TestRiverFlood testMethod=test_centroids_flood>
def test_centroids_flood(self):
# this is going to go through the meta part
lat = np.arange(47, 56, 0.2)
lon = np.arange(5, 15, 0.2)
lon, lat = np.meshgrid(lon, lat)
rand_centroids = Centroids.from_lat_lon(lat.flatten(), lon.flatten())
> rf = RiverFlood.from_isimip_nc(dph_path=HAZ_DEMO_FLDDPH, frc_path=HAZ_DEMO_FLDFRC,
centroids=rand_centroids, ISINatIDGrid=False)
climada_petals/hazard/test/test_flood.py:184:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
cls = <class 'climada_petals.hazard.river_flood.RiverFlood'>
dph_path = PosixPath('/home/runner/climada/demo/data/flddph_2000_DEMO.nc')
frc_path = PosixPath('/home/runner/climada/demo/data/fldfrc_2000_DEMO.nc')
origin = False
centroids = <climada.hazard.centroids.centr.Centroids object at 0x7f458ab9da10>
countries = None, reg = None, shape = None, ISINatIDGrid = False, years = [2000]
@classmethod
def from_isimip_nc(cls, dph_path=None, frc_path=None, origin=False,
centroids=None, countries=None, reg=None, shape=None,
ISINatIDGrid=False, years=None):
"""Wrapper to fill hazard from nc_flood file
Parameters
----------
dph_path : str, optional
Flood file to read (depth)
frc_path : str, optional
Flood file to read (fraction)
origin : bool, optional
Historical or probabilistic event. Default: False
centroids : Centroids, optional
centroids to extract
countries : list of str, optional
If `reg` is None, use this selection of countries (ISO3). Default: None
reg : list of str, optional
Use region code to consider whole areas. If not None, countries and centroids
are ignored. Default: None
shape : str or Path, optional
If `reg` and `countries` are None, use the first geometry in this shape file to cut
out the area of interest. Default: None
ISINatIDGrid : bool, optional
Indicates whether ISIMIP_NatIDGrid is used. Default: False
years : list of int
Years that are considered. Default: None
Returns
-------
haz : RiverFlood instance
Raises
------
NameError
"""
if years is None:
years = [2000]
if dph_path is None:
raise NameError('No flood-depth-path set')
if frc_path is None:
raise NameError('No flood-fraction-path set')
if not Path(dph_path).exists():
raise NameError('Invalid flood-file path %s' % dph_path)
if not Path(frc_path).exists():
raise NameError('Invalid flood-file path %s' % frc_path)
with xr.open_dataset(dph_path) as flood_dph:
time = pd.to_datetime(flood_dph["time"].values)
event_index = np.where(np.isin(time.year, years))[0]
if len(event_index) == 0:
raise AttributeError(f'No events found for selected {years}')
bands = event_index + 1
if countries or reg:
# centroids as points
if ISINatIDGrid:
dest_centroids = RiverFlood._select_exact_area(countries, reg)[0]
meta_centroids = copy.copy(dest_centroids)
meta_centroids.set_lat_lon_to_meta()
haz = cls.from_raster(files_intensity=[dph_path],
files_fraction=[frc_path], band=bands.tolist(),
transform=meta_centroids.meta['transform'],
width=meta_centroids.meta['width'],
height=meta_centroids.meta['height'],
resampling=Resampling.nearest)
x_i = ((dest_centroids.lon - haz.centroids.meta['transform'][2]) /
haz.centroids.meta['transform'][0]).astype(int)
y_i = ((dest_centroids.lat - haz.centroids.meta['transform'][5]) /
haz.centroids.meta['transform'][4]).astype(int)
fraction = haz.fraction[:, y_i * haz.centroids.meta['width'] + x_i]
intensity = haz.intensity[:, y_i * haz.centroids.meta['width'] + x_i]
haz.centroids = dest_centroids
haz.intensity = sp.sparse.csr_matrix(intensity)
haz.fraction = sp.sparse.csr_matrix(fraction)
else:
if reg:
iso_codes = u_coord.region2isos(reg)
# envelope containing counties
cntry_geom = u_coord.get_land_geometry(iso_codes)
haz = cls.from_raster(files_intensity=[dph_path],
files_fraction=[frc_path],
band=bands.tolist(),
geometry=cntry_geom.geoms)
# self.centroids.set_meta_to_lat_lon()
else:
cntry_geom = u_coord.get_land_geometry(countries)
haz = cls.from_raster(files_intensity=[dph_path],
files_fraction=[frc_path],
band=bands.tolist(),
geometry=cntry_geom.geoms)
# self.centroids.set_meta_to_lat_lon()
elif shape:
shapes = gpd.read_file(shape)
rand_geom = shapes.geometry[0]
if isinstance(rand_geom, MultiPolygon):
rand_geom = rand_geom.geoms
elif isinstance(rand_geom, Polygon) or not isinstance(rand_geom, Iterable):
rand_geom = [rand_geom]
haz = cls.from_raster(files_intensity=[dph_path],
files_fraction=[frc_path],
band=bands.tolist(),
geometry=rand_geom)
elif not centroids:
# centroids as raster
haz = cls.from_raster(files_intensity=[dph_path],
files_fraction=[frc_path],
band=bands.tolist())
# self.centroids.set_meta_to_lat_lon()
else: # use given centroids
# if centroids.meta or grid_is_regular(centroids)[0]:
#TODO: implement case when meta or regulargrid is defined
# centroids.meta or grid_is_regular(centroidsxarray)[0]:
# centroids>flood --> error
# reprojection, resampling.average (centroids< flood)
# (transform)
# reprojection change resampling"""
# else:
> if centroids.meta:
E AttributeError: 'Centroids' object has no attribute 'meta'
climada_petals/hazard/river_flood.py:308: AttributeError
Check warning on line 0 in climada_petals.hazard.test.test_flood.TestRiverFlood
github-actions / Petals / Unit Test Results (3.11)
test_flooded_area (climada_petals.hazard.test.test_flood.TestRiverFlood) failed
climada_petals/tests_xml/tests.xml [took 0s]
Raw output
AttributeError: 'Centroids' object has no attribute 'meta'
self = <climada_petals.hazard.test.test_flood.TestRiverFlood testMethod=test_flooded_area>
def test_flooded_area(self):
> testRFset = RiverFlood.from_isimip_nc(countries=['DEU', 'CHE'], dph_path=HAZ_DEMO_FLDDPH,
frc_path=HAZ_DEMO_FLDFRC, ISINatIDGrid=True)
climada_petals/hazard/test/test_flood.py:243:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
cls = <class 'climada_petals.hazard.river_flood.RiverFlood'>
dph_path = PosixPath('/home/runner/climada/demo/data/flddph_2000_DEMO.nc')
frc_path = PosixPath('/home/runner/climada/demo/data/fldfrc_2000_DEMO.nc')
origin = False, centroids = None, countries = ['DEU', 'CHE'], reg = None
shape = None, ISINatIDGrid = True, years = [2000]
@classmethod
def from_isimip_nc(cls, dph_path=None, frc_path=None, origin=False,
centroids=None, countries=None, reg=None, shape=None,
ISINatIDGrid=False, years=None):
"""Wrapper to fill hazard from nc_flood file
Parameters
----------
dph_path : str, optional
Flood file to read (depth)
frc_path : str, optional
Flood file to read (fraction)
origin : bool, optional
Historical or probabilistic event. Default: False
centroids : Centroids, optional
centroids to extract
countries : list of str, optional
If `reg` is None, use this selection of countries (ISO3). Default: None
reg : list of str, optional
Use region code to consider whole areas. If not None, countries and centroids
are ignored. Default: None
shape : str or Path, optional
If `reg` and `countries` are None, use the first geometry in this shape file to cut
out the area of interest. Default: None
ISINatIDGrid : bool, optional
Indicates whether ISIMIP_NatIDGrid is used. Default: False
years : list of int
Years that are considered. Default: None
Returns
-------
haz : RiverFlood instance
Raises
------
NameError
"""
if years is None:
years = [2000]
if dph_path is None:
raise NameError('No flood-depth-path set')
if frc_path is None:
raise NameError('No flood-fraction-path set')
if not Path(dph_path).exists():
raise NameError('Invalid flood-file path %s' % dph_path)
if not Path(frc_path).exists():
raise NameError('Invalid flood-file path %s' % frc_path)
with xr.open_dataset(dph_path) as flood_dph:
time = pd.to_datetime(flood_dph["time"].values)
event_index = np.where(np.isin(time.year, years))[0]
if len(event_index) == 0:
raise AttributeError(f'No events found for selected {years}')
bands = event_index + 1
if countries or reg:
# centroids as points
if ISINatIDGrid:
dest_centroids = RiverFlood._select_exact_area(countries, reg)[0]
meta_centroids = copy.copy(dest_centroids)
meta_centroids.set_lat_lon_to_meta()
haz = cls.from_raster(files_intensity=[dph_path],
files_fraction=[frc_path], band=bands.tolist(),
> transform=meta_centroids.meta['transform'],
width=meta_centroids.meta['width'],
height=meta_centroids.meta['height'],
resampling=Resampling.nearest)
E AttributeError: 'Centroids' object has no attribute 'meta'
climada_petals/hazard/river_flood.py:244: AttributeError
Check warning on line 0 in climada_petals.hazard.test.test_flood.TestRiverFlood
github-actions / Petals / Unit Test Results (3.11)
test_isimip_country_flood (climada_petals.hazard.test.test_flood.TestRiverFlood) failed
climada_petals/tests_xml/tests.xml [took 0s]
Raw output
AttributeError: 'Centroids' object has no attribute 'meta'
self = <climada_petals.hazard.test.test_flood.TestRiverFlood testMethod=test_isimip_country_flood>
def test_isimip_country_flood(self):
> rf = RiverFlood.from_isimip_nc(dph_path=HAZ_DEMO_FLDDPH, frc_path=HAZ_DEMO_FLDFRC,
countries=['DEU'], ISINatIDGrid=True)
climada_petals/hazard/test/test_flood.py:129:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
cls = <class 'climada_petals.hazard.river_flood.RiverFlood'>
dph_path = PosixPath('/home/runner/climada/demo/data/flddph_2000_DEMO.nc')
frc_path = PosixPath('/home/runner/climada/demo/data/fldfrc_2000_DEMO.nc')
origin = False, centroids = None, countries = ['DEU'], reg = None, shape = None
ISINatIDGrid = True, years = [2000]
@classmethod
def from_isimip_nc(cls, dph_path=None, frc_path=None, origin=False,
centroids=None, countries=None, reg=None, shape=None,
ISINatIDGrid=False, years=None):
"""Wrapper to fill hazard from nc_flood file
Parameters
----------
dph_path : str, optional
Flood file to read (depth)
frc_path : str, optional
Flood file to read (fraction)
origin : bool, optional
Historical or probabilistic event. Default: False
centroids : Centroids, optional
centroids to extract
countries : list of str, optional
If `reg` is None, use this selection of countries (ISO3). Default: None
reg : list of str, optional
Use region code to consider whole areas. If not None, countries and centroids
are ignored. Default: None
shape : str or Path, optional
If `reg` and `countries` are None, use the first geometry in this shape file to cut
out the area of interest. Default: None
ISINatIDGrid : bool, optional
Indicates whether ISIMIP_NatIDGrid is used. Default: False
years : list of int
Years that are considered. Default: None
Returns
-------
haz : RiverFlood instance
Raises
------
NameError
"""
if years is None:
years = [2000]
if dph_path is None:
raise NameError('No flood-depth-path set')
if frc_path is None:
raise NameError('No flood-fraction-path set')
if not Path(dph_path).exists():
raise NameError('Invalid flood-file path %s' % dph_path)
if not Path(frc_path).exists():
raise NameError('Invalid flood-file path %s' % frc_path)
with xr.open_dataset(dph_path) as flood_dph:
time = pd.to_datetime(flood_dph["time"].values)
event_index = np.where(np.isin(time.year, years))[0]
if len(event_index) == 0:
raise AttributeError(f'No events found for selected {years}')
bands = event_index + 1
if countries or reg:
# centroids as points
if ISINatIDGrid:
dest_centroids = RiverFlood._select_exact_area(countries, reg)[0]
meta_centroids = copy.copy(dest_centroids)
meta_centroids.set_lat_lon_to_meta()
haz = cls.from_raster(files_intensity=[dph_path],
files_fraction=[frc_path], band=bands.tolist(),
> transform=meta_centroids.meta['transform'],
width=meta_centroids.meta['width'],
height=meta_centroids.meta['height'],
resampling=Resampling.nearest)
E AttributeError: 'Centroids' object has no attribute 'meta'
climada_petals/hazard/river_flood.py:244: AttributeError
Check warning on line 0 in climada_petals.hazard.test.test_flood.TestRiverFlood
github-actions / Petals / Unit Test Results (3.11)
test_meta_centroids_flood (climada_petals.hazard.test.test_flood.TestRiverFlood) failed
climada_petals/tests_xml/tests.xml [took 0s]
Raw output
AttributeError: 'Centroids' object has no attribute 'meta'
self = <climada_petals.hazard.test.test_flood.TestRiverFlood testMethod=test_meta_centroids_flood>
def test_meta_centroids_flood(self):
min_lat, max_lat, min_lon, max_lon = 45.7, 47.8, 7.5, 10.5
cent = Centroids.from_pnt_bounds((min_lon, min_lat, max_lon, max_lat), res=0.05)
> rf_rast = RiverFlood.from_isimip_nc(dph_path=HAZ_DEMO_FLDDPH, frc_path=HAZ_DEMO_FLDFRC,
centroids=cent)
climada_petals/hazard/test/test_flood.py:216:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
cls = <class 'climada_petals.hazard.river_flood.RiverFlood'>
dph_path = PosixPath('/home/runner/climada/demo/data/flddph_2000_DEMO.nc')
frc_path = PosixPath('/home/runner/climada/demo/data/fldfrc_2000_DEMO.nc')
origin = False
centroids = <climada.hazard.centroids.centr.Centroids object at 0x7f458992a190>
countries = None, reg = None, shape = None, ISINatIDGrid = False, years = [2000]
@classmethod
def from_isimip_nc(cls, dph_path=None, frc_path=None, origin=False,
centroids=None, countries=None, reg=None, shape=None,
ISINatIDGrid=False, years=None):
"""Wrapper to fill hazard from nc_flood file
Parameters
----------
dph_path : str, optional
Flood file to read (depth)
frc_path : str, optional
Flood file to read (fraction)
origin : bool, optional
Historical or probabilistic event. Default: False
centroids : Centroids, optional
centroids to extract
countries : list of str, optional
If `reg` is None, use this selection of countries (ISO3). Default: None
reg : list of str, optional
Use region code to consider whole areas. If not None, countries and centroids
are ignored. Default: None
shape : str or Path, optional
If `reg` and `countries` are None, use the first geometry in this shape file to cut
out the area of interest. Default: None
ISINatIDGrid : bool, optional
Indicates whether ISIMIP_NatIDGrid is used. Default: False
years : list of int
Years that are considered. Default: None
Returns
-------
haz : RiverFlood instance
Raises
------
NameError
"""
if years is None:
years = [2000]
if dph_path is None:
raise NameError('No flood-depth-path set')
if frc_path is None:
raise NameError('No flood-fraction-path set')
if not Path(dph_path).exists():
raise NameError('Invalid flood-file path %s' % dph_path)
if not Path(frc_path).exists():
raise NameError('Invalid flood-file path %s' % frc_path)
with xr.open_dataset(dph_path) as flood_dph:
time = pd.to_datetime(flood_dph["time"].values)
event_index = np.where(np.isin(time.year, years))[0]
if len(event_index) == 0:
raise AttributeError(f'No events found for selected {years}')
bands = event_index + 1
if countries or reg:
# centroids as points
if ISINatIDGrid:
dest_centroids = RiverFlood._select_exact_area(countries, reg)[0]
meta_centroids = copy.copy(dest_centroids)
meta_centroids.set_lat_lon_to_meta()
haz = cls.from_raster(files_intensity=[dph_path],
files_fraction=[frc_path], band=bands.tolist(),
transform=meta_centroids.meta['transform'],
width=meta_centroids.meta['width'],
height=meta_centroids.meta['height'],
resampling=Resampling.nearest)
x_i = ((dest_centroids.lon - haz.centroids.meta['transform'][2]) /
haz.centroids.meta['transform'][0]).astype(int)
y_i = ((dest_centroids.lat - haz.centroids.meta['transform'][5]) /
haz.centroids.meta['transform'][4]).astype(int)
fraction = haz.fraction[:, y_i * haz.centroids.meta['width'] + x_i]
intensity = haz.intensity[:, y_i * haz.centroids.meta['width'] + x_i]
haz.centroids = dest_centroids
haz.intensity = sp.sparse.csr_matrix(intensity)
haz.fraction = sp.sparse.csr_matrix(fraction)
else:
if reg:
iso_codes = u_coord.region2isos(reg)
# envelope containing counties
cntry_geom = u_coord.get_land_geometry(iso_codes)
haz = cls.from_raster(files_intensity=[dph_path],
files_fraction=[frc_path],
band=bands.tolist(),
geometry=cntry_geom.geoms)
# self.centroids.set_meta_to_lat_lon()
else:
cntry_geom = u_coord.get_land_geometry(countries)
haz = cls.from_raster(files_intensity=[dph_path],
files_fraction=[frc_path],
band=bands.tolist(),
geometry=cntry_geom.geoms)
# self.centroids.set_meta_to_lat_lon()
elif shape:
shapes = gpd.read_file(shape)
rand_geom = shapes.geometry[0]
if isinstance(rand_geom, MultiPolygon):
rand_geom = rand_geom.geoms
elif isinstance(rand_geom, Polygon) or not isinstance(rand_geom, Iterable):
rand_geom = [rand_geom]
haz = cls.from_raster(files_intensity=[dph_path],
files_fraction=[frc_path],
band=bands.tolist(),
geometry=rand_geom)
elif not centroids:
# centroids as raster
haz = cls.from_raster(files_intensity=[dph_path],
files_fraction=[frc_path],
band=bands.tolist())
# self.centroids.set_meta_to_lat_lon()
else: # use given centroids
# if centroids.meta or grid_is_regular(centroids)[0]:
#TODO: implement case when meta or regulargrid is defined
# centroids.meta or grid_is_regular(centroidsxarray)[0]:
# centroids>flood --> error
# reprojection, resampling.average (centroids< flood)
# (transform)
# reprojection change resampling"""
# else:
> if centroids.meta:
E AttributeError: 'Centroids' object has no attribute 'meta'
climada_petals/hazard/river_flood.py:308: AttributeError