{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Setting watershed boundary conditions on a raster grid\n", "\n", "- This tutorial ilustrates how to set watershed boundary conditions on a raster grid.\n", "- Note that a watershed is assumed to have a ring of nodes around the core nodes that are closed boundaries (i.e. no flux can cross these nodes, or more correctly, no flux can cross the faces around the nodes).\n", "- This means that automatically the nodes on the outer perimeter of the grid will be set to be closed boundary.\n", "- By definitation a watershed also has one outlet through which fluxes can pass. Here the outlet is set as the node that has the lowest value, is not a nodata_value node, and is adjacent to at least one closed boundary node. \n", "- This means that an outlet can be on the outer perimeter of the raster. However, the outlet does not need to be on the outer perimeter of the raster." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The first example uses set_watershed_boundary_condition, which finds the outlet for the user.\n", "- First import what we need." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from landlab import RasterModelGrid\n", "import numpy as np\n", "from landlab.plot.imshow import imshow_grid\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Now we create a 5 by 5 grid with a spacing (dx and dy) of 1.\n", "- We also create an elevation field with value of 1. everywhere, except at the outlet, where the elevation is 0. In this case the outlet is in the middle of the bottom row, at location (0,2), and has a node id of 2." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mg1 = RasterModelGrid((5, 5), 1.)\n", "z1 = mg1.add_ones('topographic__elevation', at='node')\n", "mg1.at_node['topographic__elevation'][2] = 0.\n", "mg1.at_node['topographic__elevation']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- The set_watershed_boundary_condition in RasterModelGrid will find the outlet of the watershed.\n", "- This method takes the node data, in this case z, and, optionally the no_data value.\n", "- This method sets all nodes that have no_data values to closed boundaries.\n", "- This example does not have any no_data values, which is fine. \n", "- In this case, the code will set all of the perimeter nodes as BC_NODE_IS_CLOSED(boundary status 4) in order to create this boundary around the core nodes. \n", "- The exception on the perimeter is node 2 (with elevation of 0). Although it is on the perimeter, it has a value and it has the lowest value. So in this case node 2 will be set as BC_NODE_IS_FIXED_VALUE (boundary status 1).\n", "- The rest of the nodes are set as a CORE_NODE (boundary status 0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mg1.set_watershed_boundary_condition(z1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Check to see that node status were set correctly.\n", "- imshow_grid will default to not plot the value of BC_NODE_IS_CLOSED nodes, which is why we override this below with the option color_for_closed" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "imshow_grid(mg1, mg1.status_at_node, color_for_closed='blue')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The second example uses set_watershed_boundary_condition_outlet_coords \n", "- In this case the user knows the coordinates of the outlet node.\n", "- First instantiate a new grid, with new data values." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mg2 = RasterModelGrid((5, 5), 10.)\n", "z2 = mg2.add_ones('topographic__elevation', at='node')\n", "mg2.at_node['topographic__elevation'][1] = 0.\n", "mg2.at_node['topographic__elevation']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Note that the node with zero elevation, which will be the outlet, is now at location (0,1).\n", "- Note that even though this grid has a dx & dy of 10., the outlet coords are still (0,1).\n", "- Set the boundary conditions." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mg2.set_watershed_boundary_condition_outlet_coords((0, 1), z2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Plot grid of boundary status information" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "imshow_grid(mg2, mg2.status_at_node, color_for_closed='blue')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The third example uses set_watershed_boundary_condition_outlet_id \n", "- In this case the user knows the node id value of the outlet node.\n", "- First instantiate a new grid, with new data values." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mg3 = RasterModelGrid((5, 5), 5.)\n", "z3 = mg3.add_ones('topographic__elevation', at='node')\n", "mg3.at_node['topographic__elevation'][5] = 0.\n", "mg3.at_node['topographic__elevation']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Set boundary conditions with the outlet id.\n", "- Note that here we know the id of the node that has a value of zero and choose this as the outlet. But the code will not complain if you give it an id value of a node that does not have the smallest data value." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mg3.set_watershed_boundary_condition_outlet_id(5, z3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Another plot to illustrate the results." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "imshow_grid(mg3, mg3.status_at_node, color_for_closed='blue')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The final example uses set_watershed_boundary_condition on a watershed that was exported from Arc.\n", "- First import read_esri_ascii and then import the DEM data.\n", "- An optional value of halo=1 is used with read_esri_ascii. This puts a perimeter of nodata values around the DEM.\n", "- This is done just in case there are data values on the edge of the raster. These would have to become closed to set watershed boundary conditions, but in order to avoid that, we add a perimeter to the data." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from landlab.io import read_esri_ascii\n", "(grid_bijou, z_bijou) = read_esri_ascii('west_bijou_gully.asc', halo=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Let's plot the data to see what the topography looks like." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "imshow_grid(grid_bijou, z_bijou)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- In this case the nodata value is zero. This skews the colorbar, but we can at least see the shape of the watershed.\n", "- Let's set the boundary condition. Remember we don't know the outlet id." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "grid_bijou.set_watershed_boundary_condition(z_bijou, 0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Now we can look at the boundary status of the nodes to see where the found outlet was." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "imshow_grid(grid_bijou, grid_bijou.status_at_node, color_for_closed='blue')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- This looks sensible.\n", "- Now that the boundary conditions ae set, we can also look at the topography. \n", "- imshow_grid will default to show boundaries as black, as illustrated below. But that can be overwridden as we have been doing all along." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "imshow_grid(grid_bijou, z_bijou)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.8.0" } }, "nbformat": 4, "nbformat_minor": 1 }