import mikeio
Dfsu - Read
= "../tests/testdata/HD2D.dfsu"
filename = mikeio.read(filename)
ds ds
<mikeio.Dataset>
dims: (time:9, element:884)
time: 1985-08-06 07:00:00 - 1985-08-07 03:00:00 (9 records)
geometry: Dfsu2D (884 elements, 529 nodes)
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)
# to read specific variables
= mikeio.read(filename, items=["Surface elevation","Current speed"])
ds ds
<mikeio.Dataset>
dims: (time:9, element:884)
time: 1985-08-06 07:00:00 - 1985-08-07 03:00:00 (9 records)
geometry: Dfsu2D (884 elements, 529 nodes)
items:
0: Surface elevation <Surface Elevation> (meter)
1: Current speed <Current Speed> (meter per sec)
ds.describe()
Surface elevation | Current speed | |
---|---|---|
count | 7956.000000 | 7956.000000 |
mean | 0.034152 | 0.073658 |
std | 0.534890 | 0.119606 |
min | -0.758378 | 0.000082 |
25% | -0.579662 | 0.029917 |
50% | 0.101636 | 0.046848 |
75% | 0.462013 | 0.070259 |
max | 1.172707 | 1.714988 |
Filter in space to the element at our POI, (discrete values, no interpolation)
=606200, y=6905480) ds.sel(x
<mikeio.Dataset>
dims: (time:9)
time: 1985-08-06 07:00:00 - 1985-08-07 03:00:00 (9 records)
geometry: GeometryPoint2D(x=606202.7806372638, y=6905474.639383219)
items:
0: Surface elevation <Surface Elevation> (meter)
1: Current speed <Current Speed> (meter per sec)
Interpolate in space to the location of our POI
=606200, y=6905480) ds.interp(x
<mikeio.Dataset>
dims: (time:9)
time: 1985-08-06 07:00:00 - 1985-08-07 03:00:00 (9 records)
geometry: GeometryPoint2D(x=606200, y=6905480)
items:
0: Surface elevation <Surface Elevation> (meter)
1: Current speed <Current Speed> (meter per sec)
=606200, y=6905480).Surface_elevation.plot(); ds.interp(x
Convert to a dataframe.
= ds.sel(x=606200, y=6905480).to_dataframe()
df df.head()
Surface elevation | Current speed | |
---|---|---|
1985-08-06 07:00:00 | 0.459460 | 0.009572 |
1985-08-06 09:30:00 | 0.806965 | 0.011064 |
1985-08-06 12:00:00 | 0.100285 | 0.017169 |
1985-08-06 14:30:00 | -0.727009 | 0.014452 |
1985-08-06 17:00:00 | -0.579541 | 0.007349 |
; df.plot()
Other ways to subset data
Assume that we interested in these 3 points only
= (606200, 6905480)
pt1 = (606300, 6905410)
pt2 = (606400, 6905520)
pt3 = [pt1[0], pt2[0], pt3[0]]
pts_x = [pt1[1], pt2[1], pt3[1]]
pts_y = ds.geometry.find_index(pts_x, pts_y) elem_ids
We can use these element ids either when we select the data from the complete dataset using the method isel() as shown above or already when we read the data from file (particular useful for files larger than memory)
= mikeio.read(filename, elements=elem_ids)
ds_pts ds_pts
<mikeio.Dataset>
dims: (time:9, element:3)
time: 1985-08-06 07:00:00 - 1985-08-07 03:00:00 (9 records)
geometry: Dfsu2D (3 elements, 7 nodes)
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)
Create a new dfsu file
- Subset of items
- Renamed variables
First inspect the source file:
= mikeio.read("../tests/testdata/HD2D.dfsu")
ds ds
<mikeio.Dataset>
dims: (time:9, element:884)
time: 1985-08-06 07:00:00 - 1985-08-07 03:00:00 (9 records)
geometry: Dfsu2D (884 elements, 529 nodes)
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)
= "HD2D_selected.dfsu"
outfilename2
= ds[["U velocity", "V velocity"]].rename({'U velocity': 'eastward_sea_water_velocity',
newds 'V velocity': 'northward_sea_water_velocity'})
newds
<mikeio.Dataset>
dims: (time:9, element:884)
time: 1985-08-06 07:00:00 - 1985-08-07 03:00:00 (9 records)
geometry: Dfsu2D (884 elements, 529 nodes)
items:
0: eastward_sea_water_velocity <u velocity component> (meter per sec)
1: northward_sea_water_velocity <v velocity component> (meter per sec)
newds.to_dfs(outfilename2)
Read the newly created file to verify the contents.
= mikeio.read(outfilename2)
newds2 newds2
<mikeio.Dataset>
dims: (time:9, element:884)
time: 1985-08-06 07:00:00 - 1985-08-07 03:00:00 (9 records)
geometry: Dfsu2D (884 elements, 529 nodes)
items:
0: eastward_sea_water_velocity <u velocity component> (meter per sec)
1: northward_sea_water_velocity <v velocity component> (meter per sec)
Write mesh from dfsu file
Don’t you have the original mesh? No problem - you can re-create it from the dfsu file…
= 'mesh_from_HD2D.mesh'
outmesh ds.geometry.to_mesh(outmesh)
Clean up
import os
os.remove(outfilename2) os.remove(outmesh)