Xns11 - basic

Extract cross sections to a pandas DataFrame and plot them.

Overview

import mikeio1d
xns = mikeio1d.open("../data/mikep_cs_demo.xns11")
xns
<mikeio1d.Xns11 (103)>
xns.to_dataframe().head()
cross_section
location_id chainage topo_id
basin_left1 2.004 1 <CrossSection: basin_left1, 2.004, 1>
33.774 1 <CrossSection: basin_left1, 33.774, 1>
80.945 1 <CrossSection: basin_left1, 80.945, 1>
122.042 1 <CrossSection: basin_left1, 122.042, 1>
166.107 1 <CrossSection: basin_left1, 166.107, 1>

Plotting

xns['basin_left1','122.042','1'].plot()

Extracting data

xns['basin_left1','122.042','1'].raw.head()
markers marker_labels x z resistance
0 0.000 4059.508 25.0
1 2.062 4059.624 25.0
2 1 Left Levee Bank (1) 4.124 4059.754 25.0
3 6.186 4059.607 25.0
4 14.435 4058.882 25.0
xns['basin_left1','122.042','1'].markers
marker marker_label x z
0 1 Left Levee Bank (1) 4.124 4059.754
1 2 Lowest Point (2) 72.914 4052.803
2 3 Right Levee Bank (3) 195.897 4057.989
3 4 Left Low Flow Bank (4) 71.119 4053.263
4 5 Right Low Flow Bank (5) 74.003 4053.357
xns['basin_left1','122.042','1'].processed.head()
level flow_area radius storage_width additional_storage_area resistance conveyance_factor
0 4052.803000 0.000000 0.000000 0.000000 0.0 25.0 0.000000
1 4052.944857 0.160191 0.075978 2.743476 0.0 25.0 0.718409
2 4053.086714 0.790062 0.165315 6.136873 0.0 25.0 5.949433
3 4053.228571 3.633195 0.172199 29.866016 0.0 25.0 28.113531
4 4053.370429 9.264321 0.251109 50.128968 0.0 25.0 92.185348

Selecting cross sections

xns['basin_left1','122.042','1']
<CrossSection: basin_left1, 122.042, 1>
xns.sel(location_id='basin_left1', chainage=122.042, topo_id='1')
<CrossSection: basin_left1, 122.042, 1>
# Use ':' or '...' as a wildcard to return a list of sections
xns['basin_left1', :, '1']
[<CrossSection: basin_left1, 2.004, 1>,
 <CrossSection: basin_left1, 33.774, 1>,
 <CrossSection: basin_left1, 80.945, 1>,
 <CrossSection: basin_left1, 122.042, 1>,
 <CrossSection: basin_left1, 166.107, 1>,
 <CrossSection: basin_left1, 184.886, 1>,
 <CrossSection: basin_left1, 210.212, 1>,
 <CrossSection: basin_left1, 264.614, 1>,
 <CrossSection: basin_left1, 284.638, 1>,
 <CrossSection: basin_left1, 341.152, 1>,
 <CrossSection: basin_left1, 413.617, 1>,
 <CrossSection: basin_left1, 481.451, 1>]
# Similar to above, but using the 'sel' method.
xns.sel(location_id='basin_left1')
[<CrossSection: basin_left1, 2.004, 1>,
 <CrossSection: basin_left1, 33.774, 1>,
 <CrossSection: basin_left1, 80.945, 1>,
 <CrossSection: basin_left1, 122.042, 1>,
 <CrossSection: basin_left1, 166.107, 1>,
 <CrossSection: basin_left1, 184.886, 1>,
 <CrossSection: basin_left1, 210.212, 1>,
 <CrossSection: basin_left1, 264.614, 1>,
 <CrossSection: basin_left1, 284.638, 1>,
 <CrossSection: basin_left1, 341.152, 1>,
 <CrossSection: basin_left1, 413.617, 1>,
 <CrossSection: basin_left1, 481.451, 1>]
# Combine multiple cross sections into one plot.
from mikeio1d import Xns11

cross_sections = [*xns['basin_left1','481.451', ...], *xns['basin_left1','166.107', ...]]
cross_sections = Xns11(cross_sections)
cross_sections.plot()

Creating cross sections

import numpy as np
import matplotlib.pyplot as plt

from mikeio1d.xns11 import CrossSection
n_points = 11
x = np.linspace(0, 100, n_points)
z = 10**-3 * (x-50)**2
plt.grid(True)
plt.plot(x, z)

location_id = "my_reach"
chainage = 100.0
topo_id = "my_topo"

xs = CrossSection.from_xz(x, z, location_id, chainage, topo_id)
xs.plot()

Writing Xns11 files

xns_custom = Xns11()
xns_custom.add(xs)
xns_custom.write("my_custom.xns11")
xns_custom.to_dataframe()
cross_section
location_id chainage topo_id
my_reach 100.000 my_topo <CrossSection: my_reach, 100.000, my_topo>
# Subset an existing Xns11 file
subset = xns.sel(location_id='basin_left1')
xns_subset = Xns11(subset)
xns_subset.write("my_subset.xns11")
xns_subset.to_dataframe()
cross_section
location_id chainage topo_id
basin_left1 2.004 1 <CrossSection: basin_left1, 2.004, 1>
33.774 1 <CrossSection: basin_left1, 33.774, 1>
80.945 1 <CrossSection: basin_left1, 80.945, 1>
122.042 1 <CrossSection: basin_left1, 122.042, 1>
166.107 1 <CrossSection: basin_left1, 166.107, 1>
184.886 1 <CrossSection: basin_left1, 184.886, 1>
210.212 1 <CrossSection: basin_left1, 210.212, 1>
264.614 1 <CrossSection: basin_left1, 264.614, 1>
284.638 1 <CrossSection: basin_left1, 284.638, 1>
341.152 1 <CrossSection: basin_left1, 341.152, 1>
413.617 1 <CrossSection: basin_left1, 413.617, 1>
481.451 1 <CrossSection: basin_left1, 481.451, 1>

Modifying cross sections

xs = xns['basin_left1','122.042','1']
raw_copy = xs.raw.copy()
raw_copy.head()
markers marker_labels x z resistance
0 0.000 4059.508 25.0
1 2.062 4059.624 25.0
2 1 Left Levee Bank (1) 4.124 4059.754 25.0
3 6.186 4059.607 25.0
4 14.435 4058.882 25.0
raw_copy['z'] = raw_copy['z'] + 1000
raw_copy.head()
markers marker_labels x z resistance
0 0.000 5059.508 25.0
1 2.062 5059.624 25.0
2 1 Left Levee Bank (1) 4.124 5059.754 25.0
3 6.186 5059.607 25.0
4 14.435 5058.882 25.0
xs.raw = raw_copy
xs.plot()

from mikeio1d.cross_sections import Marker

xs.set_marker(Marker.LEFT_LOW_FLOW_BANK, x=70)
xs.set_marker(Marker.RIGHT_LOW_FLOW_BANK, x=74)
xs.markers
marker marker_label x z
0 1 Left Levee Bank (1) 4.124 5059.754
1 2 Lowest Point (2) 72.914 5052.803
2 3 Right Levee Bank (3) 195.897 5057.989
3 4 Left Low Flow Bank (4) 71.119 5053.263
4 5 Right Low Flow Bank (5) 74.003 5053.357
xs.plot(with_marker_labels=False)

# Write the modified cross section back to the Xns11 file.
xns.write()

GeoPandas

gdf = xns.to_geopandas()
gdf.plot()

gdf = xns.to_geopandas()
gdf.plot(column='topo_id', legend=True, legend_kwds={'title': 'Topo'})

# Plot all of the markers.
xns.to_geopandas(mode='markers').plot("marker_label", legend=True)

# Plot markers on top of cross sections.

gdf1 = xns.to_geopandas(mode='sections')
gdf2 = xns.to_geopandas(mode='markers')

ax = gdf1.plot()

gdf2.plot(ax=ax, column="marker_label", markersize=10, zorder=10, legend=True)