import numpy as np
import mikeio
Dfsu - Speed and direction
= mikeio.read("../tests/testdata/HD2D.dfsu")
ds ds
<mikeio.Dataset>
Geometry: Dfsu2D
Dimensions: (time:9, element:884)
Time: 1985-08-06 07:00:00 - 1985-08-07 03:00:00 (9 records)
Items:
0: Surface elevation <Surface Elevation> (meter)
1: U velocity <u velocity component> (meter per sec)
2: V velocity <v velocity component> (meter per sec)
3: Current speed <Current Speed> (meter per sec)
This file is missing current direction :-(
Lets’fix that!
Calculate speed & direction
ds.U_velocity
<mikeio.DataArray>
Name: U velocity
Geometry: Dfsu2D
Dimensions: (time:9, element:884)
Time: 1985-08-06 07:00:00 - 1985-08-07 03:00:00 (9 records)
In order to use Numpy functions on a DataArray, we first convert the DataArrays (U, V) to standard NumPy ndarrays.
= ds.U_velocity.to_numpy()
u = ds.V_velocity.to_numpy() v
= np.mod(90 -np.rad2deg(np.arctan2(v,u)),360) direction
Write new file
from mikeio.eum import ItemInfo, EUMUnit, EUMType
"Current direction"] = mikeio.DataArray(direction, time= ds.time, item = ItemInfo("Current direction", EUMType.Current_Direction, EUMUnit.degree), geometry=ds.geometry)
ds[ ds
<mikeio.Dataset>
Geometry: Dfsu2D
Dimensions: (time:9, element:884)
Time: 1985-08-06 07:00:00 - 1985-08-07 03:00:00 (9 records)
Items:
0: Surface elevation <Surface Elevation> (meter)
1: U velocity <u velocity component> (meter per sec)
2: V velocity <v velocity component> (meter per sec)
3: Current speed <Current Speed> (meter per sec)
4: Current direction <Current Direction> (degree)
"speed_direction.dfsu") ds.to_dfs(
= mikeio.read("speed_direction.dfsu")
nds nds
<mikeio.Dataset>
Geometry: Dfsu2D
Dimensions: (time:9, element:884)
Time: 1985-08-06 07:00:00 - 1985-08-07 03:00:00 (9 records)
Items:
0: Surface elevation <Surface Elevation> (meter)
1: U velocity <u velocity component> (meter per sec)
2: V velocity <v velocity component> (meter per sec)
3: Current speed <Current Speed> (meter per sec)
4: Current direction <Current Direction> (degree)
Plot
= 1
step
= ds.Current_speed[step].plot(figsize=(10,10))
ax None, 6903000])
ax.set_ylim([607000, None])
ax.set_xlim([= ds.geometry.element_coordinates
ec = ec[:,0]
x = ec[:,1]
y = ds.U_velocity.to_numpy()
u = ds.V_velocity.to_numpy()
v =6, minshaft=3); ax.quiver(x, y, u[step], v[step], scale
Plot quiver on Cartesian overlay instead
Create overset grid and interpolate data on to this
= ds.geometry.get_overset_grid(dx=50)
g g
<mikeio.Grid2D>
x-axis: nx=33 points from x0=605902 to x1=607502 with dx=50
y-axis: ny=93 points from y0=6.90242e+06 to y1=6.90702e+06 with dy=50
Number of grid points: 3069
g.projection
'UTM-29'
= ds.U_velocity.interp_like(g)
ui = ds.V_velocity.interp_like(g) vi
; ui.plot()
= ds.Current_speed.plot(figsize=(10,10))
ax
= ui.to_numpy()
u = vi.to_numpy()
v =8, minshaft=5)
ax.quiver(g.x, g.y, u[step], v[step], scale
None, 6903000])
ax.set_ylim([607000, None])
ax.set_xlim([f'Current speed with overset grid; {ds.time[step]}')
ax.set_title("Easting (m)")
ax.set_xlabel("Northing (m)"); ax.set_ylabel(
Clean up
import os
"speed_direction.dfsu") os.remove(