Download data as anonymous user

from https://hydrometrie.wallonie.be/ calling Kisters webservices

@author : Pierre Archambeau - 2024

Load modules

from datetime import datetime as dt
import matplotlib.pyplot as plt
import pandas as pd
from wolfhece.hydrometry import kiwis
from wolfhece.hydrometry.kiwis import hydrometry, station_fields as sf, timeseries_fields as ts

Create anonymous access to SPW web services

db = hydrometry()

Print available columns in stations

print(db)
Columns in stations :
site_no
station_no
station_name
station_id
station_local_x
station_local_y
station_latitude
station_longitude
river_name
station_gauge_datum
CATCHMENT_SIZE
BV_DCE

Get all stations with flow rate measurements

“Official” groups

Available fields :

  • station_name == name used on hydrometrie.wallonie.be

  • station_no == code identified on hydrometrie.wallonie.be

  • station_id == internal index

  • ts_id == id of the timeserie

  • ts_name == name of the timeserie

  • parametertype_id

  • parametertype_id

Return :

  • pandas dataframe

stations = db.get_timeseries_group('flowrate','1h')
print(stations.columns)
Index(['station_name', 'station_no', 'station_id', 'ts_id', 'ts_name',
       'parametertype_id', 'parametertype_name'],
      dtype='object')

Get ts_id for a specific station

amay_byname = stations[stations[sf.STATION_NAME.value]=='AMAY']
amay_byid = stations[stations[sf.STATION_ID.value]=='7132']

tsid = int(amay_byname[ts.TS_ID.value][0])

print(tsid)
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File D:\ProgrammationGitLab\python3.10\lib\site-packages\pandas\core\indexes\base.py:3805, in Index.get_loc(self, key)
   3804 try:
-> 3805     return self._engine.get_loc(casted_key)
   3806 except KeyError as err:

File index.pyx:167, in pandas._libs.index.IndexEngine.get_loc()

File index.pyx:196, in pandas._libs.index.IndexEngine.get_loc()

File pandas\\_libs\\hashtable_class_helper.pxi:2606, in pandas._libs.hashtable.Int64HashTable.get_item()

File pandas\\_libs\\hashtable_class_helper.pxi:2630, in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 0

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
Cell In[5], line 4
      1 amay_byname = stations[stations[sf.STATION_NAME.value]=='AMAY']
      2 amay_byid = stations[stations[sf.STATION_ID.value]=='7132']
----> 4 tsid = int(amay_byname[ts.TS_ID.value][0])
      6 print(tsid)

File D:\ProgrammationGitLab\python3.10\lib\site-packages\pandas\core\series.py:1112, in Series.__getitem__(self, key)
   1109     return self._values[key]
   1111 elif key_is_scalar:
-> 1112     return self._get_value(key)
   1114 # Convert generator to list before going through hashable part
   1115 # (We will iterate through the generator there to check for slices)
   1116 if is_iterator(key):

File D:\ProgrammationGitLab\python3.10\lib\site-packages\pandas\core\series.py:1228, in Series._get_value(self, label, takeable)
   1225     return self._values[label]
   1227 # Similar to Index.get_value, but we do not fall back to positional
-> 1228 loc = self.index.get_loc(label)
   1230 if is_integer(loc):
   1231     return self._values[loc]

File D:\ProgrammationGitLab\python3.10\lib\site-packages\pandas\core\indexes\base.py:3812, in Index.get_loc(self, key)
   3807     if isinstance(casted_key, slice) or (
   3808         isinstance(casted_key, abc.Iterable)
   3809         and any(isinstance(x, slice) for x in casted_key)
   3810     ):
   3811         raise InvalidIndexError(key)
-> 3812     raise KeyError(key) from err
   3813 except TypeError:
   3814     # If we have a listlike key, _check_indexing_error will raise
   3815     #  InvalidIndexError. Otherwise we fall through and re-raise
   3816     #  the TypeError.
   3817     self._check_indexing_error(key)

KeyError: 0

Get time serie between 2 dates

Info : maximum download = 250.000 values The routine will split the interval if necessary.

Return :

  • pandas timeserie

ts = db.timeseries(fromdate=dt(1960,1,1), todate=dt(2022,2,1), ts_id = tsid)
1960-01-01 00:00:00 1982-10-25 08:00:00
1982-10-25 08:00:00 2005-08-18 16:00:00
2005-08-18 16:00:00 2022-02-01 00:00:00
ts.plot()
<AxesSubplot: xlabel='Timestamp'>
_images/2b84f27d5ae29b8d95d7fd8338ea187f6584aacac69b446f0391b7017df99753.png

Exploring the database more deeply

#Get all time series available for a specific station
id,df = db.timeseries_list(stationcode='7132')
# id,df = db.timeseries_list(stationname='AMAY')

#Select only flow rate "Q"  (other possibilities : "H" or "v")
flowrate_ts = df[df['parametertype_name']=='Q']['ts_name']
flowrate_ts[flowrate_ts.str.contains('Complet')]
14    05a-Debit ultrason.Complet.Alarmes
17             05-Debit ultrason.Complet
Name: ts_name, dtype: object

Get data

fromdate=dt(2021,1,1)
todate=dt(2022,2,1)
ts = db.timeseries(stationname='AMAY', fromdate=fromdate, todate=todate, ts_name='05-Debit ultrason.Complet')
ts_off = db.timeseries(fromdate=fromdate, todate=todate, ts_id = tsid)
fig, ax = plt.subplots(1,2)
fig.set_size_inches(20,8)
ax[0].plot(ts)
ax[1].plot(ts_off)
[<matplotlib.lines.Line2D at 0x2253606d120>]
_images/a396d676352a524b44a0e4ccb95bee24062c9b54c29ebd1e1f4a71d15ccc4790.png