Skip to content
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

made locations more modular with separate catalog function #87

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ History

UNRELEASED
------------------
* nothing yet
* added `catalog_filter` argument to `ddlpy.locations()` to enabling retrieving the extended catalog in https://github.com/Deltares/ddlpy/pull/87


0.3.0 (2023-03-13)
------------------
Expand Down
30 changes: 22 additions & 8 deletions ddlpy/ddlpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,38 @@ class NoDataException(ValueError):
logger = logging.getLogger(__name__)


def locations():
"""
get station information from DDL (metadata from Catalogue). All metadata regarding stations.
The response (result) retrieves more keys

"""
def catalog(catalog_filter=None):
endpoint = ENDPOINTS["collect_catalogue"]
msg = "{} with {}".format(endpoint["url"], json.dumps(endpoint["request"]))

if catalog_filter is None:
# use the default request from endpoints.json
catalog_request = endpoint["request"]
else:
assert isinstance(catalog_filter, list)
catalog_request = {"CatalogusFilter": {x:True for x in catalog_filter}}

msg = "{} with {}".format(endpoint["url"], json.dumps(catalog_request))
logger.debug("requesting: {}".format(msg))

resp = requests.post(endpoint["url"], json=endpoint["request"])
resp = requests.post(endpoint["url"], json=catalog_request)
if not resp.ok:
raise IOError("Failed to request {}: {}".format(msg, resp.text))
result = resp.json()
if not result["Succesvol"]:
logger.exception(str(result))
raise ValueError(result.get("Foutmelding", "No error returned"))
return result


def locations(catalog_filter=None):
"""
get station information from DDL (metadata from Catalogue). All metadata regarding stations.
The response (result) retrieves more keys

"""

result = catalog(catalog_filter=catalog_filter)

df_locations = pd.DataFrame(result["LocatieLijst"])

df_metadata = pd.json_normalize(result["AquoMetadataLijst"])
Expand Down
14 changes: 14 additions & 0 deletions tests/test_ddlpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,23 @@ def measurements(location):


def test_locations(locations):
# the number of columns depend on the catalog filter in endpoints.json
assert locations.shape[1] == 18
# the number of rows is the number of stations, so will change over time
assert locations.shape[0] > 1


def test_locations_extended():
catalog_filter = ['Compartimenten','Eenheden','Grootheden',
'Hoedanigheden','Groeperingen','MeetApparaten',
'Typeringen','WaardeBepalingsmethoden','Parameters']
locations_extended = ddlpy.locations(catalog_filter=catalog_filter)
# the number of columns depend on the provided catalog_filter
assert locations_extended.shape[1] == 24
# the number of rows is the number of stations, so will change over time
assert locations_extended.shape[0] > 1


def test_measurements(measurements):
assert measurements.shape[0] > 1

Expand Down
Loading