{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Understanding and working with Landlab data fields" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "For more Landlab tutorials, click here: https://landlab.readthedocs.io/en/latest/user_guide/tutorials.html\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Data fields, or just fields for short, are the primary way that components share model data amongst themselves. This tutorial gives a short introduction to what fields are, what they do, and how to work with them.\n", "\n", "Let's start by importing the modules we'll need for this tutorial, and instantiating a simple grid to work with for the first part of the tutorial:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from landlab import RasterModelGrid, FieldError\n", "from landlab.components import LinearDiffuser\n", "\n", "mg = RasterModelGrid((3, 4))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A discussed in the grid tutorial, all data stored on the grid exists as \"flat\" one-dimensional arrays. This means that information can be retrieved from these grids using the ID of an grid element as the index:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# demonstrate that arrays of properties are n-elements long\n", "(mg.x_of_node.shape == (mg.number_of_nodes, )\n", " and mg.length_of_link.shape == (mg.number_of_links, ))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# what's the length of the link with ID 6 (the 7th link)?\n", "mg.length_of_link[6]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Any values we defined across the grid are indexed in the same way, e.g., an array of elevations would be of shape (n-nodes, ).\n", "\n", "A Landlab field, then, is simply an array like this explicitly linked to an element type, and stored within the grid object itself. Doing this serves four main purposes:\n", "\n", "1. It means that if a component has access to the grid, it also has access to all the data defined on the grid.\n", "2. It allows us to enforce the idea that an array of values of nodes is always n-nodes-long, an array on links is always n-links-long, etc.\n", "3. It provides a standardized interface where the nomenclature used by a given component for input-output is both unambiguous and clear, in the spirit of the [CSDMS standard names](https://csdms.colorado.edu/wiki/CSDMS_Standard_Names).\n", "4. The field structure also allows us to bind the measurement unit to the field, if we so wish.\n", "\n", "Note that Landlab components generally follow a \"CSDMS-like\" naming conventiomn, where the name looks like `thing_that_is_described__quantity_described`, with a double underscore in the middle. In cases where the equivalent Standard Name would be excessively long, a shorter alternatively is usually used.\n", "\n", "\n", "## Making fields on the grid\n", "\n", "There are several ways to create a field within the grid. These include functions to create fields filled just with ones or zeros, similar to the numpy functions `np.ones` and `np.zeros`, and functions to create fields from existing value arrays that you want to join to the grid.\n", "\n", "The first term supplied is always the element on which the field is defined, i.e., 'node', 'link', 'cell', etc. The second is the name to give the field.\n", "\n", "All these creation routines also return a reference to the field. This can be a useful shorthand to get at the grid without having to write out the full field name every time:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "no_1a = mg.add_zeros('field__number_one', at='node')\n", "no_1b = mg.add_ones('field__number_two', at='link',\n", " dtype=int) # fns can also take dtype\n", "no_1b[mg.active_links] = 0\n", "print(no_1b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "All the field creation routines share two optional keywords: `units` and `clobber`. `units` (default: '-') allows a unit to be associated with a field if desired. `clobber` (default: `False`) prevents accidental overwriting of an existing field. If you want to overwrite, set it to `False`.\n", "\n", "Let's try creating a field from an existing array here (`grid.add_field()`). In this case, there's an additional keyword `copy` (default = `False`) that controls whether the field refers to the actual first array, or whether a copy of the data is made:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "input_array = np.arange(mg.number_of_nodes, dtype=float)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "try:\n", " no_1c = mg.add_field('field__number_one',\n", " input_array,\n", " at='node',\n", " copy=False,\n", " units='m')\n", "except FieldError:\n", " print('ERROR: This field name already exists!')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# ...let's try that again:\n", "no_1c = mg.add_field('field__number_one',\n", " input_array,\n", " at='node',\n", " copy=False,\n", " units='m',\n", " clobber=True)\n", "print(no_1c)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# note that the keyword `copy=False` means that the field array *is* the input_array...\n", "input_array[:] = -1.\n", "print(no_1c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Accessing a data field, deleting a data field\n", "\n", "We've already seen that the array creation routines return a reference to the field data. But sometimes, you want to access the field directly.\n", "\n", "In practical terms, think of the names themselves as nested inside the grid as if the grid itself were a Python dictionary. The element type is the first key, and the field name is the second key.\n", "\n", "(In detail, the type is actually a Landlab-specific object called a ScalarDataField, but it behaves essentially as an enhanced Python dictionary)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mg['node']['field__number_one']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You'll also very commonly see some common \"syntactic sugar\" for this, where the element key is replaced by a grid property called `grid.at_[element]`. i.e.," ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mg.at_node['field__number_one'] is mg['node']['field__number_one']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Because these structures are dictionary-like, we can use the usual set of Python dictionary methods to interact with them too:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mg.at_node.keys() # see the existing fields at nodes" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mg.at_node.clear() # delete all fields at nodes" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mg.at_node.keys()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mg.at_link.keys()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mg.at_link.pop(\n", " 'field__number_two') # return the field, and remove it from the array" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mg.at_link.keys()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The units are recorded in a further dict-like structure attached to `at_[element]`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "z = mg.add_ones('field__number_3', at='node', units='km', clobber=True)\n", "mg.at_node.units['field__number_3']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Click here for more Landlab tutorials" ] } ], "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.7.3" } }, "nbformat": 4, "nbformat_minor": 1 }