import numpy as np
import matplotlib.pyplot as plt
import mikeio
DataArray - Masking
Similar to numpy arrays, DataArrays can be filtered based on values (e.g. all values above a threshold), which will return a DataArray with boolean values.
Surface elevation in Oresund
Read 3 timesteps of Surface elevation from dfsu2d file.
= "../tests/testdata/oresundHD_run1.dfsu"
fn = mikeio.read(fn, items="Surface elevation", time=[0,2,4])[0]
da da
<mikeio.DataArray>
name: Surface elevation
dims: (time:3, element:3612)
time: 2018-03-07 00:00:00 - 2018-03-11 00:00:00 (3 records)
geometry: Dfsu2D (3612 elements, 2046 nodes)
= plt.subplots(1, da.n_timesteps, figsize=(11,5), sharey=True)
_, ax for step in range(da.n_timesteps):
=ax[step], vmin=0.08, vmax=0.35) da[step].plot(ax
Mask values below 0.2m
Assume that we are not interested in values below 0.2m. Let us find those and call the DataArray mask.
= 0.2
threshold = da<threshold
mask mask
<mikeio.DataArray>
name: Boolean
dims: (time:3, element:3612)
time: 2018-03-07 00:00:00 - 2018-03-11 00:00:00 (3 records)
geometry: Dfsu2D (3612 elements, 2046 nodes)
Now let’s set define a new DataArray wl_capped for which we set all values below the threshold to NaN.
= da.copy()
wl_capped = np.nan
wl_capped[mask] wl_capped
<mikeio.DataArray>
name: Surface elevation
dims: (time:3, element:3612)
time: 2018-03-07 00:00:00 - 2018-03-11 00:00:00 (3 records)
geometry: Dfsu2D (3612 elements, 2046 nodes)
Now let’s plot both the boolean mask and the new capped DataArray for each of the three timesteps
= plt.subplots(2, da.n_timesteps, figsize=(11,10), sharey=True)
_, ax for step in range(da.n_timesteps):
=ax[0,step], cmap="Reds", add_colorbar=False)
mask[step].plot(ax=ax[1,step], vmin=0.08, vmax=0.35) wl_capped[step].plot(ax
Boolean indexing with numpy array
The boolean indexing can also be done with a plain numpy array (it does not need to be a mikeio.DataArray).
In this example, we set all elements with depth lower than -10m to NaN.
= da.geometry.element_coordinates[:,2]
ze ze.shape
(3612,)
= da.copy()
wl_shallow <-10] = np.nan # select all elements with depth lower than -10m
wl_shallow[ze wl_shallow.shape
(3, 3612)
; wl_shallow.plot()