Dfsu - Distance to land

Calculate the distance to land for each element in mesh and save to dfsu file

import numpy as np
import matplotlib.pyplot as plt
import mikeio
msh = mikeio.Mesh("../tests/testdata/odense_rough.mesh")
msh
Flexible Mesh
number of elements: 654
number of nodes: 399
projection: UTM-33
msh.plot();

msh.geometry.plot.outline();

Get a list of land nodes

ncland = msh.node_coordinates[msh.codes==1]

plt.scatter(ncland[:,0], ncland[:,1]);

Get element coordinates

ec = msh.element_coordinates

Calculate distance to nearest land node

i = 0
ne = ec.shape[0]
d = np.zeros(ne)

for i in range(ne):
    d[i] = np.min(np.sqrt((ec[i,0] - ncland[:,0])**2 + (ec[i,1] -ncland[:,1])**2))
da = mikeio.DataArray(data=d,
                      geometry=msh.geometry,
                      item=mikeio.ItemInfo("Distance to land", mikeio.EUMType.Distance, mikeio.EUMUnit.meter))
da
<mikeio.DataArray>
name: Distance to land
dims: (element:654)
time: 2018-01-01 00:00:00 (time-invariant)
geometry: Dfsu2D (654 elements, 399 nodes)
values: [1697, 435, ..., 1194]
da.plot();

Store result in a new Dfsu file

da.to_dfs("distance.dfsu")

Clean up

import os
os.remove("distance.dfsu")