Xns11 - basic

Extract cross sections to a pandas DataFrame and plot them.

Overview

from mikeio1d import Xns11
xns = Xns11("../data/mikep_cs_demo.xns11")
xns.xsections
<CrossSectionCollection 103>
xns.xsections.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.xsections['basin_left1','122.042','1'].plot()

Extracting data

xns.xsections['basin_left1','122.042','1'].raw.head()
markers marker_labels x z resistance
0 0.000 59.508 25.0
1 2.062 59.624 25.0
2 1 Left Levee Bank (1) 4.124 59.754 25.0
3 6.186 59.607 25.0
4 14.435 58.882 25.0
xns.xsections['basin_left1','122.042','1'].markers
marker marker_label x z
0 1 Left Levee Bank (1) 4.124 59.754
1 2 Lowest Point (2) 72.914 52.803
2 3 Right Levee Bank (3) 195.897 57.989
xns.xsections['basin_left1','122.042','1'].processed.head()
level flow_area radius storage_width additional_storage_area resistance conveyance_factor
0 52.803000 0.000000 0.000000 0.000000 0.0 25.0 0.000000
1 52.944857 0.160191 0.075849 2.743476 0.0 25.0 0.717592
2 53.086714 0.790062 0.165260 6.136873 0.0 25.0 5.948131
3 53.228571 3.633195 0.171565 29.866016 0.0 25.0 28.044487
4 53.370429 9.264321 0.249458 50.128968 0.0 25.0 91.780854

Selecting cross sections

xns.xsections['basin_left1','122.042','1']
<CrossSection: basin_left1, 122.042, 1>
xns.xsections.sel(location_id='basin_left1', chainage=122.042, topo_id='1')
<CrossSection: basin_left1, 122.042, 1>
# Use ':' or '...' as a wildcard
xns.xsections['basin_left1', :, '1'].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>
# Similar to above, but using the 'sel' method.
xns.xsections.sel(location_id='basin_left1').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>
# Combine multiple cross sections into one plot.
cross_sections = xns.xsections['basin_left1','481.451', ...] | xns.xsections['basin_left1','166.107', ...]
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.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_xsection(xs)
xns_custom.write("my_custom.xns11")
xns_custom.xsections.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.xsections.sel(location_id='basin_left1')
xns_subset = Xns11.from_cross_section_collection(subset)
xns_subset.write("my_subset.xns11")
xns_subset.xsections.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.xsections['basin_left1','122.042','1']
raw_copy = xs.raw.copy()
raw_copy.head()
markers marker_labels x z resistance
0 0.000 59.508 25.0
1 2.062 59.624 25.0
2 1 Left Levee Bank (1) 4.124 59.754 25.0
3 6.186 59.607 25.0
4 14.435 58.882 25.0
raw_copy['z'] = raw_copy['z'] + 1000
raw_copy.head()
markers marker_labels x z resistance
0 0.000 1059.508 25.0
1 2.062 1059.624 25.0
2 1 Left Levee Bank (1) 4.124 1059.754 25.0
3 6.186 1059.607 25.0
4 14.435 1058.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 1059.754
1 2 Lowest Point (2) 72.914 1052.803
2 3 Right Levee Bank (3) 195.897 1057.989
3 4 Left Low Flow Bank (4) 71.119 1053.263
4 5 Right Low Flow Bank (5) 74.003 1053.357
xs.plot(with_marker_labels=False)

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

GeoPandas

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

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

# Plot all of the markers.
xns.xsections.to_geopandas_markers().plot("marker_label", legend=True)

# Plot markers on top of cross sections.

gdf1 = xns.xsections.to_geopandas()
gdf2 = xns.xsections.to_geopandas_markers()

ax = gdf1.plot()

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