import mikeio
from matplotlib_inline.backend_inline import set_matplotlib_formats
'png') set_matplotlib_formats(
Dfsu - 3D sigma-z
This notebook demonstrates, reading from a sigma-z dfsu file, top- and bottom layers, extracting a profile, save selected layers to new dfsu file and save to mesh.
It also shows how to read a sigma-z vertical slice file.
= "../tests/testdata/oresund_sigma_z.dfsu"
filename = mikeio.open(filename)
dfs dfs
Dfsu3DSigmaZ
number of elements: 17118
number of nodes: 12042
projection: UTM-33
number of sigma layers: 4
max number of z layers: 5
items:
0: Temperature <Temperature> (degree Celsius)
1: Salinity <Salinity> (PSU)
time: 3 steps with dt=10800.0s
1997-09-15 21:00:00 -- 1997-09-16 03:00:00
; dfs.geometry.plot.mesh()
Save geometry to new mesh file
Note that for sigma-z files, the mesh will be not match the original mesh in areas where z-layers are present!
= "mesh_oresund_extracted.mesh"
outmesh dfs.to_mesh(outmesh)
Evaluate top layer
= dfs.read(layers="top")
ds print(ds)
= ds['Temperature'].to_numpy().max()
max_t print(f'Maximum temperature in top layer: {max_t:.1f}')
<mikeio.Dataset>
dims: (time:3, element:3700)
time: 1997-09-15 21:00:00 - 1997-09-16 03:00:00 (3 records)
geometry: Dfsu2D (3700 elements, 2090 nodes)
items:
0: Temperature <Temperature> (degree Celsius)
1: Salinity <Salinity> (PSU)
Maximum temperature in top layer: 17.5
Find position of max temperature and plot
Find position of max temperature in first time step
= 0
timestep = ds['Temperature'][timestep].to_numpy().argmax()
elem = ds.geometry.element_coordinates[elem,:2]
max_x, max_y print(f'Position of maximum temperature: (x,y) = ({max_x:.1f}, {max_y:.1f})')
Position of maximum temperature: (x,y) = (333934.1, 6158101.5)
= dfs.plot(z=ds['Temperature'][timestep], figsize=(6,7))
ax ='*', markersize=20); ax.plot(max_x, max_y, marker
Read 1D profile from 3D file
Find water column which has highest temperature and plot profile for all 3 time steps.
= dfs.read(x=max_x, y=max_y) # select vertical column from dfsu-3d
dsp "Temperature"].plot(); dsp[
Note that the vertical column data is extrapolated to the bottom and surface!
The extrapolation can avoided using “extrapolate=False”:
"Temperature"].plot(extrapolate=False, marker='o'); dsp[
If the data has more than a few timesteps, it can be more convenient to plot as 2d pcolormesh. We will simulate this by interpolating to 30min data.
Note that pcolormesh will plot using the static z information!
= dsp.Salinity.interp_time(dt=1800)
dspi ; dspi.plot.pcolormesh()
Read top layer of a smaller area
= [310000, 6192000, 380000, 6198000]
bbox = dfs.read(area=bbox, layers="top")
ds_sub ds_sub
<mikeio.Dataset>
dims: (time:3, element:118)
time: 1997-09-15 21:00:00 - 1997-09-16 03:00:00 (3 records)
geometry: Dfsu2D (118 elements, 87 nodes)
items:
0: Temperature <Temperature> (degree Celsius)
1: Salinity <Salinity> (PSU)
; ds_sub.Temperature.plot()
Plot subset inside original model domain
= ds_sub.Temperature.plot(figsize=(6,7))
ax =ax, title=None); dfs.geometry.plot.outline(ax
"oresund_data_extracted.dfsu") ds_sub.to_dfs(
Select top 2 layers and write to new file
get_layer_elements() can take a list of layers. Layers are counted positive from the bottom starting at 0 or alternatively counted negative from the top starting at -1. Here we take layers -1 and -2, i.e., the two top layers.
Next data is read from source file and finally written to a new dfsu file (which is now sigma-only dfsu file).
= dfs.read(layers=[-2, -1])
ds_top2 ds_top2
<mikeio.Dataset>
dims: (time:3, element:7400)
time: 1997-09-15 21:00:00 - 1997-09-16 03:00:00 (3 records)
geometry: Dfsu3DSigma (7400 elements, 2 sigma-layers, 0 z-layers)
items:
0: Temperature <Temperature> (degree Celsius)
1: Salinity <Salinity> (PSU)
= "oresund_top2_layers.dfsu"
outfile ds_top2.to_dfs(outfile)
Read vertical slice (transect)
= "../tests/testdata/oresund_vertical_slice.dfsu"
filename = mikeio.read(filename)
ds ds
<mikeio.Dataset>
dims: (time:3, element:441)
time: 1997-09-15 21:00:00 - 1997-09-16 03:00:00 (3 records)
geometry: DfsuVerticalProfileSigmaZ (441 elements, 4 sigma-layers, 5 z-layers)
items:
0: Temperature <Temperature> (degree Celsius)
1: Salinity <Salinity> (PSU)
print(ds.geometry.bottom_elements[:9])
print(ds.geometry.n_layers_per_column[:9])
print(ds.geometry.top_elements[:9])
[ 0 5 10 15 20 24 28 32 36]
[5 5 5 5 4 4 4 4 4]
[ 4 9 14 19 23 27 31 35 39]
; ds.Temperature.plot()
Clean up
import os
"mesh_oresund_extracted.mesh")
os.remove("oresund_data_extracted.dfsu")
os.remove("oresund_top2_layers.dfsu") os.remove(