Running Wflow using the ewatercycle system
This notebooks shows how to run Wflow model using an example use-case. More information about data, configuration and installation instructions can be found in the System setup chapter in the eWaterCycle documentation.
1 In:
import logging
import warnings
warnings.filterwarnings("ignore", category=UserWarning)
logging.basicConfig(level=logging.WARN)
2 In:
import ewatercycle.forcing
import ewatercycle.models
import ewatercycle.parameter_sets
Setting up the model
The model needs a parameter set and forcing. The parameter set can be gotten from the available parameters sets on the system and the forcing can derived from the parameter set.
3 In:
parameter_set = ewatercycle.parameter_sets.get_parameter_set("wflow_rhine_sbm_nc")
print(parameter_set)
Parameter set
-------------
name=wflow_rhine_sbm_nc
directory=/home/verhoes/git/eWaterCycle/ewatercycle/docs/examples/parameter-sets/wflow_rhine_sbm_nc
config=/home/verhoes/git/eWaterCycle/ewatercycle/docs/examples/parameter-sets/wflow_rhine_sbm_nc/wflow_sbm_NC.ini
doi=N/A
target_model=wflow
supported_model_versions={'2020.1.2', '2020.1.1'}
4 In:
forcing = ewatercycle.forcing.load_foreign(
directory=str(parameter_set.directory),
target_model=parameter_set.target_model,
start_time="1991-01-01T00:00:00Z",
end_time="1991-12-31T00:00:00Z",
forcing_info=dict(
# Additional information about the external forcing data needed for the model configuration
netcdfinput="inmaps.nc",
Precipitation="/P",
EvapoTranspiration="/PET",
Temperature="/TEMP",
),
)
print(forcing)
Forcing data for Wflow
----------------------
Directory: /home/verhoes/git/eWaterCycle/ewatercycle/docs/examples/parameter-sets/wflow_rhine_sbm_nc
Start time: 1991-01-01T00:00:00Z
End time: 1991-12-31T00:00:00Z
Shapefile: None
Additional information for model config:
- netcdfinput: inmaps.nc
- Precipitation: /P
- Temperature: /TEMP
- EvapoTranspiration: /PET
- Inflow: None
Pick a version of Wflow model, so the right model code can be executed which understands the parameter set and forcing.
5 In:
ewatercycle.models.Wflow.available_versions
5 Out:
('2020.1.1', '2020.1.2')
6 In:
model = ewatercycle.models.Wflow(
version="2020.1.2", parameter_set=parameter_set, forcing=forcing
)
WARNING:ewatercycle.models.wflow:Config file from parameter set is missing API section, adding section
WARNING:ewatercycle.models.wflow:Config file from parameter set is missing RiverRunoff option in API section, added it with value '2, m/s option'
7 In:
print(model)
eWaterCycle Wflow
-------------------
Version = 2020.1.2
Parameter set =
Parameter set
-------------
name=wflow_rhine_sbm_nc
directory=/home/verhoes/git/eWaterCycle/ewatercycle/docs/examples/parameter-sets/wflow_rhine_sbm_nc
config=/home/verhoes/git/eWaterCycle/ewatercycle/docs/examples/parameter-sets/wflow_rhine_sbm_nc/wflow_sbm_NC.ini
doi=N/A
target_model=wflow
supported_model_versions={'2020.1.2', '2020.1.1'}
Forcing =
Forcing data for Wflow
----------------------
Directory: /home/verhoes/git/eWaterCycle/ewatercycle/docs/examples/parameter-sets/wflow_rhine_sbm_nc
Start time: 1991-01-01T00:00:00Z
End time: 1991-12-31T00:00:00Z
Shapefile: None
Additional information for model config:
- netcdfinput: inmaps.nc
- Precipitation: /P
- Temperature: /TEMP
- EvapoTranspiration: /PET
- Inflow: None
The pre-configured parameters are shown below and can be overwritten with setup()
8 In:
model.parameters
8 Out:
[('start_time', '1991-01-01T00:00:00Z'), ('end_time', '1991-12-31T00:00:00Z')]
9 In:
cfg_file, cfg_dir = model.setup(end_time="1991-02-28T00:00:00Z")
10 In:
print(cfg_file)
print(cfg_dir)
/home/verhoes/git/eWaterCycle/ewatercycle/docs/examples/output/wflow_20211008_084304/wflow_ewatercycle.ini
/home/verhoes/git/eWaterCycle/ewatercycle/docs/examples/output/wflow_20211008_084304
The config file can be edited, but for now we will initialize the model with the config file as is
11 In:
model.initialize(cfg_file)
Running the model
12 In:
while model.time < model.end_time:
model.update()
print(model.time_as_isostr)
1991-01-01T00:00:00Z
1991-01-02T00:00:00Z
1991-01-03T00:00:00Z
1991-01-04T00:00:00Z
1991-01-05T00:00:00Z
1991-01-06T00:00:00Z
1991-01-07T00:00:00Z
1991-01-08T00:00:00Z
1991-01-09T00:00:00Z
1991-01-10T00:00:00Z
1991-01-11T00:00:00Z
1991-01-12T00:00:00Z
1991-01-13T00:00:00Z
1991-01-14T00:00:00Z
1991-01-15T00:00:00Z
1991-01-16T00:00:00Z
1991-01-17T00:00:00Z
1991-01-18T00:00:00Z
1991-01-19T00:00:00Z
1991-01-20T00:00:00Z
1991-01-21T00:00:00Z
1991-01-22T00:00:00Z
1991-01-23T00:00:00Z
1991-01-24T00:00:00Z
1991-01-25T00:00:00Z
1991-01-26T00:00:00Z
1991-01-27T00:00:00Z
1991-01-28T00:00:00Z
1991-01-29T00:00:00Z
1991-01-30T00:00:00Z
1991-01-31T00:00:00Z
1991-02-01T00:00:00Z
1991-02-02T00:00:00Z
1991-02-03T00:00:00Z
1991-02-04T00:00:00Z
1991-02-05T00:00:00Z
1991-02-06T00:00:00Z
1991-02-07T00:00:00Z
1991-02-08T00:00:00Z
1991-02-09T00:00:00Z
1991-02-10T00:00:00Z
1991-02-11T00:00:00Z
1991-02-12T00:00:00Z
1991-02-13T00:00:00Z
1991-02-14T00:00:00Z
1991-02-15T00:00:00Z
1991-02-16T00:00:00Z
1991-02-17T00:00:00Z
1991-02-18T00:00:00Z
1991-02-19T00:00:00Z
1991-02-20T00:00:00Z
1991-02-21T00:00:00Z
1991-02-22T00:00:00Z
1991-02-23T00:00:00Z
1991-02-24T00:00:00Z
1991-02-25T00:00:00Z
1991-02-26T00:00:00Z
1991-02-27T00:00:00Z
1991-02-28T00:00:00Z
Inspect the results
The RiverRunnoff values of the current model state can be fetched as a xarray dataset.
13 In:
da = model.get_value_as_xarray("RiverRunoff")
da
13 Out:
<xarray.DataArray 'RiverRunoff' (latitude: 169, longitude: 187)> array([[0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], ..., [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.]]) Coordinates: * longitude (longitude) float64 5.227 5.264 5.3 5.337 ... 11.97 12.01 12.05 * latitude (latitude) float64 45.89 45.93 45.97 46.0 ... 51.98 52.02 52.05 time object 1991-02-28 00:00:00 Attributes: units: m/s
- latitude: 169
- longitude: 187
- 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
array([[0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], ..., [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.]])
- longitude(longitude)float645.227 5.264 5.3 ... 12.01 12.05
array([ 5.227163, 5.26383 , 5.300497, 5.337163, 5.37383 , 5.410497, 5.447163, 5.48383 , 5.520497, 5.557163, 5.59383 , 5.630497, 5.667163, 5.70383 , 5.740497, 5.777164, 5.81383 , 5.850497, 5.887163, 5.92383 , 5.960497, 5.997163, 6.03383 , 6.070497, 6.107163, 6.14383 , 6.180497, 6.217164, 6.25383 , 6.290497, 6.327163, 6.36383 , 6.400496, 6.437163, 6.47383 , 6.510497, 6.547163, 6.58383 , 6.620497, 6.657163, 6.69383 , 6.730497, 6.767163, 6.80383 , 6.840497, 6.877163, 6.91383 , 6.950497, 6.987164, 7.02383 , 7.060497, 7.097163, 7.13383 , 7.170496, 7.207163, 7.24383 , 7.280497, 7.317163, 7.35383 , 7.390497, 7.427163, 7.46383 , 7.500497, 7.537163, 7.57383 , 7.610497, 7.647163, 7.68383 , 7.720497, 7.757164, 7.79383 , 7.830497, 7.867163, 7.90383 , 7.940496, 7.977163, 8.01383 , 8.050497, 8.087163, 8.12383 , 8.160497, 8.197164, 8.23383 , 8.270496, 8.307163, 8.34383 , 8.380497, 8.417163, 8.45383 , 8.490497, 8.527164, 8.56383 , 8.600496, 8.637163, 8.67383 , 8.710497, 8.747164, 8.78383 , 8.820497, 8.857163, 8.89383 , 8.930496, 8.967163, 9.00383 , 9.040497, 9.077164, 9.11383 , 9.150496, 9.187163, 9.22383 , 9.260497, 9.297163, 9.33383 , 9.370497, 9.407164, 9.44383 , 9.480496, 9.517163, 9.55383 , 9.590497, 9.627163, 9.66383 , 9.700497, 9.737164, 9.77383 , 9.810496, 9.847163, 9.88383 , 9.920497, 9.957163, 9.99383 , 10.030497, 10.067163, 10.10383 , 10.140496, 10.177163, 10.21383 , 10.250497, 10.287164, 10.32383 , 10.360497, 10.397163, 10.43383 , 10.470497, 10.507163, 10.54383 , 10.580497, 10.617164, 10.65383 , 10.690496, 10.727163, 10.76383 , 10.800497, 10.837163, 10.87383 , 10.910497, 10.947164, 10.98383 , 11.020496, 11.057163, 11.09383 , 11.130497, 11.167163, 11.20383 , 11.240497, 11.277164, 11.31383 , 11.350496, 11.387163, 11.42383 , 11.460497, 11.497164, 11.53383 , 11.570497, 11.607163, 11.64383 , 11.680496, 11.717163, 11.75383 , 11.790497, 11.827164, 11.86383 , 11.900496, 11.937163, 11.97383 , 12.010497, 12.047163])
- latitude(latitude)float6445.89 45.93 45.97 ... 52.02 52.05
array([45.894268, 45.930935, 45.967602, 46.004265, 46.040932, 46.077599, 46.114265, 46.150932, 46.187599, 46.224266, 46.260933, 46.2976 , 46.334267, 46.370934, 46.4076 , 46.444267, 46.480934, 46.517601, 46.554268, 46.590935, 46.627602, 46.664268, 46.700932, 46.737598, 46.774265, 46.810932, 46.847599, 46.884266, 46.920933, 46.9576 , 46.994267, 47.030933, 47.0676 , 47.104267, 47.140934, 47.177601, 47.214268, 47.250935, 47.287601, 47.324268, 47.360935, 47.397598, 47.434265, 47.470932, 47.507599, 47.544266, 47.580933, 47.617599, 47.654266, 47.690933, 47.7276 , 47.764267, 47.800934, 47.837601, 47.874268, 47.910934, 47.947601, 47.984268, 48.020935, 48.057598, 48.094265, 48.130932, 48.167599, 48.204266, 48.240932, 48.277599, 48.314266, 48.350933, 48.3876 , 48.424267, 48.460934, 48.497601, 48.534267, 48.570934, 48.607601, 48.644268, 48.680935, 48.717602, 48.754265, 48.790932, 48.827599, 48.864265, 48.900932, 48.937599, 48.974266, 49.010933, 49.0476 , 49.084267, 49.120934, 49.1576 , 49.194267, 49.230934, 49.267601, 49.304268, 49.340935, 49.377602, 49.414268, 49.450932, 49.487598, 49.524265, 49.560932, 49.597599, 49.634266, 49.670933, 49.7076 , 49.744267, 49.780933, 49.8176 , 49.854267, 49.890934, 49.927601, 49.964268, 50.000935, 50.037601, 50.074268, 50.110935, 50.147598, 50.184265, 50.220932, 50.257599, 50.294266, 50.330933, 50.367599, 50.404266, 50.440933, 50.4776 , 50.514267, 50.550934, 50.587601, 50.624268, 50.660934, 50.697601, 50.734268, 50.770935, 50.807598, 50.844265, 50.880932, 50.917599, 50.954266, 50.990932, 51.027599, 51.064266, 51.100933, 51.1376 , 51.174267, 51.210934, 51.247601, 51.284267, 51.320934, 51.357601, 51.394268, 51.430935, 51.467602, 51.504265, 51.540932, 51.577599, 51.614265, 51.650932, 51.687599, 51.724266, 51.760933, 51.7976 , 51.834267, 51.870934, 51.9076 , 51.944267, 51.980934, 52.017601, 52.054268])
- time()object1991-02-28 00:00:00
array(cftime.DatetimeGregorian(1991, 2, 28, 0, 0, 0, 0), dtype=object)
- units :
- m/s
14 In:
print(da)
<xarray.DataArray 'RiverRunoff' (latitude: 169, longitude: 187)>
array([[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.]])
Coordinates:
* longitude (longitude) float64 5.227 5.264 5.3 5.337 ... 11.97 12.01 12.05
* latitude (latitude) float64 45.89 45.93 45.97 46.0 ... 51.98 52.02 52.05
time object 1991-02-28 00:00:00
Attributes:
units: m/s
15 In:
qm = da.plot(robust=True, cmap="GnBu", figsize=(10, 8))
# Add some verification points
target_longitudes = [8.4, 10, 11]
target_latitudes = [50, 50.15, 49]
# Add some crosses to check that 'get_value_at_coords' works correctly below
qm.axes.scatter(target_longitudes, target_latitudes, s=250, c="r", marker="x", lw=2)
15 Out:
<matplotlib.collections.PathCollection at 0x7fc7fd4816d0>

Instead of getting the whole spatial grid, you can also get RiverRunoff values at some coordinates (red crosses in above plot).
16 In:
model.get_value_at_coords("RiverRunoff", lon=target_longitudes, lat=target_latitudes)
16 Out:
array([200.49531555, 44.60787582, 0. ])
We are done with the model so let’s clean it up.
17 In:
model.finalize()
In: