Skip to content

Commit

Permalink
Update documentation with latest Xns11 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-kipawa committed Dec 13, 2024
1 parent e0a87d7 commit 64d9556
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 39 deletions.
48 changes: 25 additions & 23 deletions docs/examples/xns11_basic.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -8,56 +8,57 @@ jupyter: python3
```{python}
from mikeio1d import Xns11
xns = Xns11("../data/mikep_cs_demo.xns11")
xns.xsections
xns
```

```{python}
xns.xsections.to_dataframe().head()
xns.to_dataframe().head()
```

## Plotting

```{python}
xns.xsections['basin_left1','122.042','1'].plot()
xns['basin_left1','122.042','1'].plot()
```

## Extracting data

```{python}
xns.xsections['basin_left1','122.042','1'].raw.head()
xns['basin_left1','122.042','1'].raw.head()
```
```{python}
xns.xsections['basin_left1','122.042','1'].markers
xns['basin_left1','122.042','1'].markers
```

```{python}
xns.xsections['basin_left1','122.042','1'].processed.head()
xns['basin_left1','122.042','1'].processed.head()
```

## Selecting cross sections

```{python}
xns.xsections['basin_left1','122.042','1']
xns['basin_left1','122.042','1']
```

```{python}
xns.xsections.sel(location_id='basin_left1', chainage=122.042, topo_id='1')
xns.sel(location_id='basin_left1', chainage=122.042, topo_id='1')
```

```{python}
# Use ':' or '...' as a wildcard
xns.xsections['basin_left1', :, '1'].to_dataframe()
# Use ':' or '...' as a wildcard to return a list of sections
xns['basin_left1', :, '1']
```

```{python}
# Similar to above, but using the 'sel' method.
xns.xsections.sel(location_id='basin_left1').to_dataframe()
xns.sel(location_id='basin_left1')
```


```{python}
# Combine multiple cross sections into one plot.
cross_sections = xns.xsections['basin_left1','481.451', ...] | xns.xsections['basin_left1','166.107', ...]
cross_sections = [*xns['basin_left1','481.451', ...], *xns['basin_left1','166.107', ...]]
cross_sections = Xns11(cross_sections)
cross_sections.plot()
```

Expand All @@ -75,6 +76,7 @@ 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)
```

Expand All @@ -90,16 +92,16 @@ xs.plot()
## Writing Xns11 files
```{python}
xns_custom = Xns11()
xns_custom.add_xsection(xs)
xns_custom.add(xs)
xns_custom.write("my_custom.xns11")
xns_custom.xsections.to_dataframe()
xns_custom.to_dataframe()
```
```{python}
# Subset an existing Xns11 file
subset = xns.xsections.sel(location_id='basin_left1')
xns_subset = Xns11.from_cross_section_collection(subset)
subset = xns.sel(location_id='basin_left1')
xns_subset = Xns11(subset)
xns_subset.write("my_subset.xns11")
xns_subset.xsections.to_dataframe()
xns_subset.to_dataframe()
```

::: {.hidden}
Expand All @@ -114,7 +116,7 @@ os.remove("my_subset.xns11")
## Modifying cross sections

```{python}
xs = xns.xsections['basin_left1','122.042','1']
xs = xns['basin_left1','122.042','1']
raw_copy = xs.raw.copy()
raw_copy.head()
```
Expand Down Expand Up @@ -148,25 +150,25 @@ xns.write()
## GeoPandas

```{python}
gdf = xns.xsections.to_geopandas()
gdf = xns.to_geopandas()
gdf.plot()
```

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

```{python}
# Plot all of the markers.
xns.xsections.to_geopandas_markers().plot("marker_label", legend=True)
xns.to_geopandas(mode='markers').plot("marker_label", legend=True)
```

```{python}
# Plot markers on top of cross sections.
gdf1 = xns.xsections.to_geopandas()
gdf2 = xns.xsections.to_geopandas_markers()
gdf1 = xns.xsections.to_geopandas(mode='sections')
gdf2 = xns.xsections.to_geopandas(mode='markers')
ax = gdf1.plot()
Expand Down
37 changes: 21 additions & 16 deletions docs/user-guide/xns11.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -12,56 +12,61 @@ xns
```

## Cross section collections
Each Xns11 object contains a collection of cross sections via the `xsections` attribute.
Each Xns11 object is a collection of cross sections (CrossSectionCollection) with a file path.
```{python}
xns.xsections
from mikeio1d.cross_sections import CrossSectionCollection
print(xns.file_path)
isinstance(xns, CrossSectionCollection)
```

### Overview
An overview of a cross section collection can be obtained by calling the 'to_dataframe' method.
```{python}
xns.xsections.to_dataframe()
xns.to_dataframe()
```

### Indexing
Cross section collections are dict-like and can be indexed by a tuple of location ID, chainage, and topo ID.

```{python}
xns.xsections['basin_left1', '122.042', '1']
xns['basin_left1', '122.042', '1']
```
Alternatively, the location ID, chainage, and topo ID can be explicitly expressed with the `sel` method.
```{python}
xns.xsections.sel(location_id='basin_left1', chainage='122.042', topo_id='1')
xns.sel(location_id='basin_left1', chainage='122.042', topo_id='1')
```

### Slicing

Cross section collections can be sliced by location ID, chainage, or topo ID.

```{python}
xns.xsections['basin_left1'] # all cross sections at location 'basin_left1'
xns['basin_left1'] # all cross sections at location 'basin_left1'
```

```{python}
xns.xsections[:, '122.042'] # all cross sections at chainage '122.042'
xns[:, '122.042'] # all cross sections at chainage '122.042'
```

```{python}
xns.xsections[:,:,'1'] # all cross sections with topo ID '1'
xns[:,:,'1'] # all cross sections with topo ID '1'
```

### Combining
Cross section collections can be combined into a new collection.
```{python}
new_collection = xns.xsections['basin_left1', '2.004'] | xns.xsections['basin_left1', '210.212']
sections = [*xns['basin_left1', '2.004'], *xns['basin_left1', '210.212']]
new_collection = Xns11(sections)
new_collection.to_dataframe()
```

### Adding a cross section
A new cross section can be added to a collection with the `add_xsection` method.
A new cross section can be added to a collection with the `add` method.
```{python}
xs_to_add = xns.xsections.sel(location_id='basin_left1', chainage='33.774', topo_id='1')
new_collection.add_xsection(xs_to_add)
xs_to_add = xns.sel(location_id='basin_left1', chainage='33.774', topo_id='1')
new_collection.add(xs_to_add)
new_collection.to_dataframe()
```

Expand Down Expand Up @@ -135,7 +140,7 @@ xs.processed.head()

Cross section collections can be extracted into a GeoDataFrame.
```{python}
gdf = xns.xsections.to_geopandas()
gdf = xns.to_geopandas()
gdf.head()
```

Expand All @@ -145,13 +150,13 @@ gdf.plot(column='location_id', cmap='tab20', legend=True)
It is also possible to extract cross section markers as GeoDataFrames.

```{python}
gdf = xns.xsections.to_geopandas_markers()
gdf = xns.to_geopandas(mode='markers')
gdf.head()
```

```{python}
ax = xns.xsections.to_geopandas().plot()
xns.xsections.to_geopandas_markers().plot(ax=ax, column='marker', markersize=9, legend=True)
ax = xns.to_geopandas().plot()
xns.to_geopandas(mode='markers').plot(ax=ax, column='marker', markersize=9, legend=True)
```

## Examples
Expand Down
1 change: 1 addition & 0 deletions mikeio1d/xns11.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pandas as pd
from DHI.Mike1D.Generic import Location

from .cross_sections import CrossSection
from .cross_sections import CrossSectionCollection


Expand Down
Binary file modified tests/testdata/mikep_cs_demo.xns11
Binary file not shown.

0 comments on commit 64d9556

Please sign in to comment.