-
Notifications
You must be signed in to change notification settings - Fork 124
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/wrapper_functions_api #500
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0f5ad82
Function to get base centroids API
zeliest 01d953d
Merge branch 'develop' of https://github.com/CLIMADA-project/climada_…
zeliest 941e524
Merge branch 'develop' of https://github.com/CLIMADA-project/climada_…
zeliest 6241869
Add wraper function to get centroids from the API
zeliest d293925
Update get_centroids method
zeliest 184acfd
Update get_centroids method
zeliest 7d4c3cc
change get_litpop_default to get_litpop() and update no result error …
zeliest 8258dec
Update api tutorial
zeliest 3ef9f6c
Change default extent of centroids to remove poles
zeliest 2e4a2ea
Merge branch 'develop' into feature/get_centroids_api
emanuel-schmid a603bbd
Merge branch 'develop' into feature/get_centroids_api
emanuel-schmid 4d41e0a
api_client: improve error handling
emanuel-schmid 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,7 @@ | |
|
||
from climada import CONFIG | ||
from climada.entity import Exposures | ||
from climada.hazard import Hazard | ||
from climada.hazard import Hazard, Centroids | ||
from climada.util.constants import SYSTEM_DIR | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
@@ -430,7 +430,10 @@ def get_dataset_info(self, data_type=None, name=None, version=None, properties=N | |
raise Client.AmbiguousResult("there are several datasets meeting the requirements:" | ||
f" {jarr}") | ||
if len(jarr) < 1: | ||
raise Client.NoResult("there is no dataset meeting the requirements") | ||
data_info = self.list_dataset_infos(data_type) | ||
properties = self.get_property_values(data_info) | ||
raise Client.NoResult("there is no dataset meeting the requirements, the following" | ||
f" property values are available for {data_type}: {properties}") | ||
return jarr[0] | ||
|
||
def get_dataset_info_by_uuid(self, uuid): | ||
|
@@ -532,12 +535,19 @@ def _tracked_download(self, remote_url, local_path): | |
raise Exception("tracked download requires a path to a file not a directory") | ||
path_as_str = str(local_path.absolute()) | ||
try: | ||
dlf = Download.create(url=remote_url, path=path_as_str, startdownload=datetime.utcnow()) | ||
dlf = Download.create(url=remote_url, | ||
path=path_as_str, | ||
startdownload=datetime.utcnow()) | ||
except IntegrityError as ierr: | ||
dlf = Download.get(Download.path==path_as_str) | ||
dlf = Download.get(Download.path==path_as_str) # path is the table's one unique column | ||
if not Path(path_as_str).is_file(): # in case the file has been removed | ||
dlf.delete_instance() # delete entry from database | ||
return self._tracked_download(remote_url, local_path) # and try again | ||
if dlf.url != remote_url: | ||
raise Exception("this file has been downloaded from another url, " | ||
"please purge the entry from data base before trying again") from ierr | ||
raise Exception(f"this file ({path_as_str}) has been downloaded from another url" | ||
f" ({dlf.url}), possibly because it belongs to a dataset with a" | ||
" recent version update. Please remove the file or purge the entry" | ||
" from data base before trying again") from ierr | ||
return dlf | ||
try: | ||
self._download(url=remote_url, path=local_path, replace=True) | ||
|
@@ -726,7 +736,6 @@ def to_hazard(self, dataset, dump_dir=SYSTEM_DIR): | |
""" | ||
target_dir = self._organize_path(dataset, dump_dir) \ | ||
if dump_dir == SYSTEM_DIR else dump_dir | ||
|
||
hazard_list = [ | ||
Hazard.from_hdf5(self._download_file(target_dir, dsf)) | ||
for dsf in dataset.files | ||
|
@@ -813,7 +822,7 @@ def to_exposures(self, dataset, dump_dir=SYSTEM_DIR): | |
exposures_concat.check() | ||
return exposures_concat | ||
|
||
def get_litpop_default(self, country=None, dump_dir=SYSTEM_DIR): | ||
def get_litpop(self, country=None, exponents=(1,1), dump_dir=SYSTEM_DIR): | ||
"""Get a LitPop instance on a 150arcsec grid with the default parameters: | ||
exponents = (1,1) and fin_mode = 'pc'. | ||
|
||
|
@@ -822,6 +831,11 @@ def get_litpop_default(self, country=None, dump_dir=SYSTEM_DIR): | |
country : str or list, optional | ||
List of country name or iso3 codes for which to create the LitPop object. | ||
If None is given, a global LitPop instance is created. Defaut is None | ||
exponents : tuple of two integers, optional | ||
Defining power with which lit (nightlights) and pop (gpw) go into LitPop. To get | ||
nightlights^3 without population count: (3, 0). | ||
To use population count alone: (0, 1). | ||
Default: (1, 1) | ||
dump_dir : str | ||
directory where the files should be downoladed. Default: SYSTEM_DIR | ||
|
||
|
@@ -831,9 +845,7 @@ def get_litpop_default(self, country=None, dump_dir=SYSTEM_DIR): | |
default litpop Exposures object | ||
""" | ||
properties = { | ||
'exponents': '(1,1)', | ||
'fin_mode': 'pc' | ||
} | ||
'exponents': "".join(['(',str(exponents[0]),',',str(exponents[1]),')'])} | ||
if country is None: | ||
properties['spatial_coverage'] = 'global' | ||
elif isinstance(country, str): | ||
|
@@ -844,6 +856,49 @@ def get_litpop_default(self, country=None, dump_dir=SYSTEM_DIR): | |
raise ValueError("country must be string or list of strings") | ||
return self.get_exposures(exposures_type='litpop', dump_dir=dump_dir, properties=properties) | ||
|
||
def get_centroids(self, res_arcsec_land=150, res_arcsec_ocean=1800, | ||
extent=(-180, 180, -60, 60), country=None, | ||
dump_dir=SYSTEM_DIR): | ||
"""Get centroids from teh API | ||
|
||
Parameters | ||
---------- | ||
res_land_arcsec : int | ||
resolution for land centroids in arcsec. Default is 150 | ||
res_ocean_arcsec : int | ||
resolution for ocean centroids in arcsec. Default is 1800 | ||
country : str | ||
country name, numeric code or iso code based on pycountry. Default is None (global). | ||
extent : tuple | ||
Format (min_lon, max_lon, min_lat, max_lat) tuple. | ||
If min_lon > lon_max, the extend crosses the antimeridian and is | ||
[lon_max, 180] + [-180, lon_min] | ||
Borders are inclusive. Default is (-180, 180, -60, 60). | ||
dump_dir : str | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
directory where the files should be downoladed. Default: SYSTEM_DIR | ||
Returns | ||
------- | ||
climada.hazard.centroids.Centroids | ||
Centroids from the api | ||
""" | ||
|
||
properties = { | ||
'res_arcsec_land': str(res_arcsec_land), | ||
'res_arcsec_ocean': str(res_arcsec_ocean), | ||
'extent': '(-180, 180, -90, 90)' | ||
} | ||
dataset = self.get_dataset_info('centroids', properties=properties) | ||
target_dir = self._organize_path(dataset, dump_dir) \ | ||
if dump_dir == SYSTEM_DIR else dump_dir | ||
centroids = Centroids.from_hdf5(self._download_file(target_dir, dataset.files[0])) | ||
if country: | ||
reg_id = pycountry.countries.lookup(country).numeric | ||
centroids = centroids.select(reg_id=int(reg_id), extent=extent) | ||
if extent: | ||
centroids = centroids.select(extent=extent) | ||
|
||
return centroids | ||
|
||
@staticmethod | ||
def get_property_values(dataset_infos, known_property_values=None, | ||
exclude_properties=None): | ||
|
@@ -874,8 +929,6 @@ def get_property_values(dataset_infos, known_property_values=None, | |
if known_property_values: | ||
for key, val in known_property_values.items(): | ||
ppdf = ppdf[ppdf[key] == val] | ||
if len(ppdf) == 0: | ||
raise Client.NoResult("there is no dataset meeting the requirements") | ||
|
||
property_values = dict() | ||
for col in ppdf.columns: | ||
|
Large diffs are not rendered by default.
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.
Should we put the default to have
min_lat = -60
andmax_lat = 60
? Also, the default is not explained. Is it the whole world?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.
yes for now I put the whole world, I would keep it like that. Because for most hazards it makes sense to have the entire globe
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.
But is it cut at high/low latitudes or not? This would be important to know. The default extent could then be with +-60degrees?