-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/coastal flooding #100
Open
aleeciu
wants to merge
21
commits into
develop
Choose a base branch
from
feature/coastal_flooding
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
cb6019d
coastal flood class
aleeciu 01a9deb
draft tutorial
aleeciu 7ecce6b
add wdir
aleeciu d1b8ba7
Merge branch 'develop' into feature/coastal_flooding
aleeciu faa2b1e
fix file reading
aleeciu ed890b1
update config file
aleeciu 94a2004
complete docstring; use config; clean function
aleeciu e43831d
fix attrs types
aleeciu d5b39d0
add tests
aleeciu f112cb8
remove function to assign names, correct path handling
aleeciu f370506
update tutorial
aleeciu 5d62b5d
fix typo
aleeciu aaa4e36
add fix request
aleeciu 8d0e038
add todo
aleeciu df70290
Merge branch 'develop' into feature/coastal_flooding
aleeciu 8b6ce4b
Merge branch 'develop' into feature/coastal_flooding
aleeciu 50e9e16
Merge branch 'develop' into feature/coastal_flooding
emanuel-schmid 00b77f5
Merge branch 'develop' into feature/coastal_flooding
aleeciu 72d444a
add bracket to config file
aleeciu 01b4588
remove comment to bring on PR
aleeciu 5ef5a88
remove deprecated function
aleeciu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
from typing import Iterable, Union, Optional | ||
import numpy as np | ||
from shapely.geometry import Polygon | ||
|
||
from climada.hazard import Hazard | ||
from climada.util import files_handler as u_fh | ||
import climada.util.coordinates as u_coord | ||
from climada import CONFIG | ||
|
||
AQUEDUCT_SOURCE_LINK = CONFIG.hazard.coastal_flood.resources.aqueduct.str() | ||
DOWNLOAD_DIRECTORY = CONFIG.hazard.coastal_flood.local_data.aqueduct.dir() | ||
|
||
HAZ_TYPE = 'CF' | ||
"""Hazard type acronym CoastalFlood""" | ||
|
||
__all__ = ['CoastalFlood'] | ||
|
||
class CoastalFlood(Hazard): | ||
""" | ||
Contains coastal flood events pulled by the Acqueduct project : | ||
https://www.wri.org/data/aqueduct-floods-hazard-maps | ||
""" | ||
|
||
def __init__(self, **kwargs): | ||
"""Empty constructor""" | ||
|
||
Hazard.__init__(self, HAZ_TYPE, **kwargs) | ||
|
||
@classmethod | ||
def from_aqueduct_tif(cls, | ||
Check warning on line 30 in climada_petals/hazard/coastal_flood.py Jenkins - WCR / Pylinttoo-many-arguments
Raw output
Check warning on line 30 in climada_petals/hazard/coastal_flood.py Jenkins - WCR / Pylinttoo-many-positional-arguments
Raw output
|
||
rcp: str, | ||
target_year : str, | ||
return_periods: Union[int, Iterable[int]], | ||
subsidence: Optional[str] = 'wtsub', | ||
percentile: str = '95', | ||
countries: Optional[Union[str, Iterable[str]]] = None, | ||
boundaries: Iterable[float] = None, | ||
dwd_dir=DOWNLOAD_DIRECTORY): | ||
""" | ||
Download and load coastal flooding events from the Aqueduct dataset. | ||
For more info on the data see: | ||
https://files.wri.org/d8/s3fs-public/aqueduct-floods-methodology.pdf | ||
|
||
Parameters | ||
---------- | ||
rcp : str | ||
RCPs scenario. Possible values are historical, 45 and 85. | ||
target_year : str | ||
future target year. Possible values are hist, 2030, 2050 and 2080. | ||
return_periods : int or list of int | ||
events' return periods. | ||
Possible values are 2, 5, 10, 25, 50, 100, 250, 500, 1000. | ||
subsidence : str | ||
If land subsidence is simulated or not. | ||
Possible values are nosub and wtsub. Default wtsub. | ||
percentile : str | ||
Sea level rise scenario (in percentile). | ||
Possible values are 05, 50 and 95. | ||
countries : str or list of str | ||
countries ISO3 codes | ||
boundaries : tuple of floats | ||
geographical boundaries in the order: | ||
minimum longitude, minimum latitude, | ||
maximum longitude, maximum latitude | ||
Returns | ||
------- | ||
haz : CoastalFlood instance | ||
""" | ||
|
||
if (target_year == 'hist') & (rcp != 'historical'): | ||
raise ValueError(f"RCP{rcp} cannot have hist as target year") | ||
|
||
if (rcp == 'historical') & (subsidence == 'nosub') & (target_year != 'hist'): | ||
raise ValueError("Historical without subsidence can only have hist as target year") | ||
|
||
if (rcp == 'historical') & (percentile != '0'): | ||
raise ValueError("Historical shall not have a assigned percentiles of sea level rise. Leave default values.") | ||
|
||
if isinstance(return_periods, int): | ||
return_periods = [return_periods] | ||
|
||
return_periods.sort(reverse=True) | ||
|
||
if isinstance(countries, str): | ||
countries = [countries] | ||
|
||
rcp_name = f"rcp{rcp[0]}p{rcp[1]}" if rcp in ['45', '85'] else rcp | ||
ret_per_names = [f"{str(rp).zfill(4)}" for rp in return_periods] | ||
perc_name = f"0_perc_{percentile.zfill(2)}" if percentile in ['5', '50'] else '0' | ||
|
||
file_names = [f"inuncoast_{rcp_name}_{subsidence}_{target_year}_" | ||
f"rp{rp.zfill(4)}_{perc_name}.tif" | ||
for rp in ret_per_names] | ||
|
||
file_paths = [] | ||
for file_name in file_names: | ||
link_to_file = "".join([AQUEDUCT_SOURCE_LINK, file_name]) | ||
file_paths.append(dwd_dir / file_name) | ||
|
||
if not file_paths[-1].exists(): | ||
u_fh.download_file(link_to_file, download_dir=dwd_dir) | ||
|
||
if countries: | ||
# TODO: this actually does not work with multiple countries | ||
geom = u_coord.get_land_geometry(countries).geoms | ||
|
||
elif boundaries: | ||
min_lon, min_lat, max_lon, max_lat = boundaries | ||
geom = [Polygon([(min_lon, min_lat), | ||
(max_lon, min_lat), | ||
(max_lon, max_lat), | ||
(min_lon, max_lat)])] | ||
else: | ||
geom = None | ||
|
||
event_id = np.arange(len(file_names)) | ||
frequencies = np.diff(1 / np.array(return_periods), prepend=0) | ||
event_names = [f"1-in-{return_periods[i]}y_{percentile}pct_" | ||
f"{rcp_name}_{target_year}_{subsidence}" | ||
for i in range(len(file_names))] | ||
|
||
haz = cls().from_raster(files_intensity=file_paths, | ||
geometry=geom, | ||
attrs={'event_id': event_id, | ||
'event_name': event_names, | ||
'frequency': frequencies}) | ||
|
||
haz.units = 'm' | ||
|
||
return haz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
|
||
""" | ||
This file is part of CLIMADA. | ||
|
||
Copyright (C) 2017 ETH Zurich, CLIMADA contributors listed in AUTHORS. | ||
|
||
CLIMADA is free software: you can redistribute it and/or modify it under the | ||
terms of the GNU General Public License as published by the Free | ||
Software Foundation, version 3. | ||
|
||
CLIMADA is distributed in the hope that it will be useful, but WITHOUT ANY | ||
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | ||
PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
|
||
You should have received a copy of the GNU General Public License along | ||
with CLIMADA. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
--- | ||
|
||
Tests on Coastal Flood Hazard""" | ||
|
||
import unittest | ||
import requests | ||
import numpy as np | ||
from climada_petals.hazard.coastal_flood import AQUEDUCT_SOURCE_LINK | ||
|
||
RCPS = ['historical', 'rcp4p5', 'rcp8p5'] | ||
TARGET_YEARS = ['hist', '2030', '2050', '2080'] | ||
RETURN_PERIODS = ['0002', '0005', '0010', '0025', | ||
'0050', '0100', '0250', '0500', '1000'] | ||
SUBSIDENCE = ['nosub', 'wtsub'] | ||
PERCENTILES = ['0_perc_05', '0_perc_50', '0'] | ||
|
||
class TestReader(unittest.TestCase): | ||
"""Test that Coastal flood data exist""" | ||
|
||
def test_files_exist(self): | ||
|
||
file_names = [ | ||
f'inuncoast_{rcp}_{sub}_{year}_rp{rp}_{perc}.tif' | ||
for rcp in RCPS | ||
for sub in SUBSIDENCE | ||
for year in TARGET_YEARS | ||
for rp in RETURN_PERIODS | ||
for perc in PERCENTILES | ||
# You can't have: | ||
# - year historic with rcp different than historical | ||
# - rcp historical, no subsidence and year different than historic | ||
# - rcp historical and SLR scenarios' percentiles | ||
if not (((year == 'hist') & (rcp != 'historical')) | | ||
((rcp == 'historical') & (sub == 'nosub') & (year != 'hist')) | | ||
((rcp == 'historical') & (perc != '0'))) | ||
] | ||
|
||
test_files_pos = np.random.choice(range(len(file_names)), | ||
size=10, | ||
replace=False) | ||
for i in test_files_pos: | ||
file_path = "".join([AQUEDUCT_SOURCE_LINK, file_names[i]]) | ||
request_code = requests.get(file_path).status_code | ||
self.assertTrue(request_code == 200) | ||
|
||
# Execute Tests | ||
if __name__ == "__main__": | ||
TESTS = unittest.TestLoader().loadTestsFromTestCase(TestReader) | ||
unittest.TextTestRunner(verbosity=2).run(TESTS) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case I think it makes much more sense to remove the
from
method and put all the functionality in the init. There seems to be no other purpose than to load data (initialize the object).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mhh, as of now maybe yes, but it's likely that the CoastalFlood class will be populated with more data in the future; same as it's happening with the RiverFlood class where we now have ISIMIP and Aqueduct (https://github.com/CLIMADA-project/climada_petals/blob/feature/river_flooding_aqueduct/climada_petals/hazard/river_flood.py) so I would tend to keep it like this.