Deltares / ddlpy

API to Dutch Rijkswaterstaat archive (DDL, waterinfo.rws.nl) of monitoring water data
https://deltares.github.io/ddlpy/
GNU General Public License v3.0
20 stars 6 forks source link

add check for nodata for entire period instead of retrieving all periods #26

Closed veenstrajelmer closed 8 months ago

veenstrajelmer commented 8 months ago

Description

When retrieving data for a station/period where no data is present, ddlpy loops over all months which can be avoided by using the CheckWaarnemingenAanwezig waterwebservice.

What I Did

import datetime as dt
import ddlpy

locations = ddlpy.locations()

bool_stations = locations.index.isin(['OTDM'])
bool_grootheid = locations['Grootheid.Code'].isin(['WATHTE'])
selected = locations.loc[bool_stations & bool_grootheid]

# numtiple parameters avaialble per location
records = selected.iloc[0]

# if we pass one row to the measurements function you can get all the measurements
measurements = ddlpy.measurements(records, dt.datetime(2019,1,1), dt.datetime(2020,1,1))
print(len(measurements)==0)

The above code returns an empty list since there was no data. However, all 12 months were checked first. Make this more efficient by adding a check for data availability like so:

import requests

# check of er waarnemingen zijn, snelle manier voor gehele periode
url_ddl = 'https://waterwebservices.rijkswaterstaat.nl/ONLINEWAARNEMINGENSERVICES_DBO/CheckWaarnemingenAanwezig'
request_ddl = {"AquoMetadataLijst" :
               [{"Compartiment":{"Code":"OW"},"Eenheid":{"Code":"cm"}}],
               # "Groeperingsperiode" : "Week",
               "LocatieLijst" : [{"X" :518882.333320247,"Y" :5760829.11729589,"Code":"EURPFM"}],
               "Periode" : {"Begindatumtijd" : "2012-01-16T14:00:00.000+01:00","Einddatumtijd": "2012-01-16T16:00:00.000+01:00"
                            }
               }
resp = requests.post(url_ddl, json=request_ddl)
if not resp.ok:
    raise Exception('%s for %s: %s'%(resp.reason, resp.url, str(resp.text)))
result = resp.json()
if not result['Succesvol']:
    raise Exception('query not succesful, Foutmelding: %s from %s'%(result['Foutmelding'],url_ddl))
print(result['WaarnemingenAanwezig'])

Additional info

Also consider preventing empty list as returned object, instead return empty dataframe or maybe None?