{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Dfs1\n", "\n", "Dfs1 has in addition to the dfs0 file a single spatial dimension.\n", "\n", "This spatial dimension has information on the grid spacing, but do not contain enough metadata to determine their geographical position, but have a relative distance from the origo.\n", "\n", "See [Dfs1 in MIKE IO Documentation](https://dhi.github.io/mikeio/user-guide/dfs1.html)\n", "\n", "See [DFS - Reference manual](https://docs.mikepoweredbydhi.com/core_libraries/dfs/dfs-file-system/)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import mikeio" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ds = mikeio.read(\"data/waterlevel_north.dfs1\")\n", "ds" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ds.geometry" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This dfs1 file contains only two nodes (`nx=2`) and has a grid spacing of 8800 m." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The dataset has a single item `North WL` which we can access in three different ways.\n", "\n", "1. As an attribute `ds.North_WL`\n", "2. As a key in a dictionary `ds[\"North WL\"]`\n", "2. By position `ds[0]`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "da = ds.North_WL\n", "da" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The dataarray variable `da` contains all the data for the `North WL` item from the dfs1 file." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Visualization\n", "\n", "The data can be visualized with the `.plot()` method.\n", "\n", "The default plot show time on the vertical axis and space on the horizontal axis since dfs1 are often used to represent horizontal variations along an open boundary." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "da.plot();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Fort this example, with only a few nodes on the spatial axis, it makes more sense to visualize the data as individual lines in a timeseries plot." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "da.plot.timeseries();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Inline exercise**\n", "\n", "*Are there other possible visualizations?*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Spatial and temporal selection" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Spatial subsetting can be done in two ways:\n", "1. positional index using `.isel()` x=0..(nx-1) (or with negative indexing from the end)\n", "2. distance along the axis using `sel()` x=0..8800 (pick closest node)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "da.isel(x=0).plot(title=\"First\");" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "da.isel(x=-1).plot(title=\"Last\");" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "da.sel(x=8800).plot(title=\"Last (x=8800)\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Calculated items\n", "\n", "Creating a new dataarray in the dataset based on an existing dataarray is easy." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ds[\"North WL calibrated\"] = da + 0.1 # add 0.1 m to the original data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Export to dfs0\n", "\n", "After subsetting into a single spatial point the result no longer has any spatial dimension and has become a simple timeseries which can be saved as a dfs0 file." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ds.isel(x=0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ds.isel(x=0).geometry" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ds.isel(x=0).to_dfs(\"random_0.dfs0\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mikeio.read(\"random_0.dfs0\").plot();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Inline exercise**\n", "\n", "*Export the timeseries in the last spatial element as a csv file*\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import utils\n", "\n", "utils.sysinfo()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.10.5 ('base')", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.5" }, "vscode": { "interpreter": { "hash": "7aec4f91c09090e98e6ae8203c38529831bb4a3ce54cd1b69639b53cb01a6aa9" } } }, "nbformat": 4, "nbformat_minor": 4 }