Skip to content

Commit

Permalink
made locations more modular with separate catalog function (#87)
Browse files Browse the repository at this point in the history
* made locations more modular with separate catalog function

* added catalog_filter argument to locations and catalog functions

* added testcase for extended locations

* updated whatsnew
  • Loading branch information
veenstrajelmer authored Mar 27, 2024
1 parent 7a248f6 commit b910da6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
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

0 comments on commit b910da6

Please sign in to comment.