<jupyter_start><jupyter_text>Surface water flow and erosion simulationHere we compute surface runoff, and sediment transport using existingpath sampling model.Resources:    * [GRASS GIS overview and manual](https://grass.osgeo.org/grass76/manuals/index.html)    * [GRASS book](http://www.grassbook.org/)For animating in GRASS GIS 7, see the[video instructions](https://www.youtube.com/watch?v=Pi1ESk9iPwk) orinstructions for the[  Spatio-temporal data handling and visualization in GRASS GIS workshop](http://ncsu-geoforall-lab.github.io/grass-temporal-workshop/) Start GRASS GISStart GRASS - click on GRASS icon or type<jupyter_code># using Python to initialize GRASS GIS
# This is a quick introduction into Jupyter Notebook.
# Python code can be executed like this:
a = 6
b = 7
c = a * b
print("Answer is", c)
# Python code can be mixed with command line code (Bash).
# It is enough just to prefix the command line with an exclamation mark:
!echo "Answer is $c"
# Use Shift+Enter to execute this cell. The result is below.
# using Python to initialize GRASS GIS
import os
import sys
import subprocess
from IPython.display import Image

# create GRASS GIS runtime environment
gisbase = subprocess.check_output(["grass", "--config", "path"], text=True).strip()
os.environ['GISBASE'] = gisbase
sys.path.append(os.path.join(gisbase, "etc", "python"))

# do GRASS GIS imports
import grass.script as gs
import grass.script.setup as gsetup

# set GRASS GIS session data
rcfile = gsetup.init(gisbase, "/home/jovyan/grassdata", "nc_spm_08_grass7", "user1")
# using Python to initialize GRASS GIS
# default font displays
os.environ['GRASS_FONT'] = 'sans'
# overwrite existing maps
os.environ['GRASS_OVERWRITE'] = '1'
gs.set_raise_on_error(True)
gs.set_capture_stderr(True)
# using Python to initialize GRASS GIS
# set display modules to render into a file (named map.png by default)
os.environ['GRASS_RENDER_IMMEDIATE'] = 'cairo'
os.environ['GRASS_RENDER_FILE_READ'] = 'TRUE'
os.environ['GRASS_LEGEND_FILE'] = 'legend.txt'<jupyter_output><empty_output><jupyter_text>In startup pannel set GRASS GIS Database Directory to path to datasets,for example on MS Windows, `C:\Users\myname\grassdata`.For GRASS Location select nc_spm_08_grass7 (North Carolina, State Plane, meters) andfor GRASS Mapset create a new mapset (called e.g. HW_interpolation_2).Click Start GRASS session.Change working directory:_Settings_ > _GRASS working environment_ > _Change working directory_ > select/create any directoryor type `cd` (stands for change directory) into the GUI_Console_ and hit Enter: Download all text files with color rules (see above)to the selected directory. Now you can use the commands from the assignment requiring the text filewithout the need to specify the full path to the file. Overland flow depth and discharge<jupyter_code>!g.region rural_1m res=2 -p<jupyter_output><empty_output><jupyter_text>Calculate partial derivatives to define the gradient of elevation surface:<jupyter_code>!v.surf.rst -d input=elev_lid792_bepts elevation=elev_lid792_2m slope=dx_2m aspect=dy_2m tension=15 smooth=1.5 npmin=150<jupyter_output><empty_output><jupyter_text>Note that partial derivatives can be also computed using _r.slope.aspect_.Compute spatial pattern of overland flow depth and dischargeby running the SIMWE model implemented inthe _r.sim.water_ module.We will use uniform rainfall excess, infiltration and land cover.<jupyter_code>!r.sim.water -t elevation=elev_lid792_2m dx=dx_2m dy=dy_2m rain_value=50 infil_value=0 man_value=0.05 depth=wdp_2m discharge=disch_2m nwalkers=100000 niterations=30 output_step=2<jupyter_output><empty_output><jupyter_text>Display the results, add legend for individual raster and whatever you think is appropriate.<jupyter_code>!d.rast wdp_2m.30
!d.rast disch_2m.30
Image(filename="map.png")<jupyter_output><empty_output><jupyter_text>Optionally, animate the time series:_File > Animation tool_ Peak runoff with predefined flow directionCompute peak runoff with predefined flow directionalong the stream inluding culvert under the road.First, compute direction (aspect) of the given streams:<jupyter_code>!v.to.rast streams output=streams_dir_2m use=dir<jupyter_output><empty_output><jupyter_text>Compute stream dx and dy using direction and slope equal to 2 degrees:<jupyter_code>!r.mapcalc "dxstr_2m = tan(2.)*cos(streams_dir_2m)"
!r.mapcalc "dystr_2m = tan(2.)*sin(streams_dir_2m)"<jupyter_output><empty_output><jupyter_text>Compute combined DEM and stream dx and dy:<jupyter_code>!r.mapcalc "dxdemstr_2m = if(isnull(dxstr_2m), dx_2m, dxstr_2m)"
!r.mapcalc "dydemstr_2m = if(isnull(dystr_2m), dy_2m, dystr_2m)"<jupyter_output><empty_output><jupyter_text>Run the model:<jupyter_code>!r.sim.water -t elevation=elev_lid792_2m dx=dxdemstr_2m dy=dydemstr_2m rain_value=50 infil_value=0 man_value=0.05 depth=wdpstr_2m discharge=dischstr_2m nwalkers=100000 niterations=30 output_step=2
!d.rast dischstr_2m.30
Image(filename="map.png")<jupyter_output><empty_output><jupyter_text>Use Animation tool to animate the discharge. Runoff for spatially variable landcover and rainfall excessCompute runoff for spatially variable landcoverrepresented by spatially variable Mannings and rainfall excess.We will use variable Mannings coefficient defined by reclassifyingland cover class. Here is the content of a file called`land_to_mannings.txt` with reclassification rules forthe _r.recode_ module.<jupyter_code>%%file land_to_mannings.txt
1:1:0.9:0.9
2:2:0.5:0.5
3:3:0.01:0.01
4:4:0.03:0.03
5:5:0.01:0.01
6:6:0.05:0.05
7:7:0.1:0.1
8:8:0.1:0.1
9:9:0.9:0.9
10:10:0.03:0.03
11:11:0.5:0.5
!r.recode input=landcover_1m output=mancover_2m rules=land_to_mannings.txt<jupyter_output><empty_output><jupyter_text>Similarly, we will create raster with spatially variable rainfall excessrates based on the land cover classes. We use file named`land_to_rain.txt` to specify the rates for individual classes.<jupyter_code>%%file land_to_rain.txt
1:1:50:50
2:2:5:5
3:3:40:40
4:4:35:35
5:5:50:50
6:6:40:40
7:7:25:25
8:8:15:15
9:9:50.:50.
10:10:40:40
11:11:10:10<jupyter_output><empty_output><jupyter_text>Again, we use the file as rules for the _r.recode_ module.<jupyter_code>!r.recode input=landcover_1m output=raincover_2m rules=land_to_rain.txt<jupyter_output><empty_output><jupyter_text>Run the model:<jupyter_code>!r.sim.water -t elevation=elev_lid792_2m dx=dxdemstr_2m dy=dydemstr_2m rain=raincover_2m infil_value=0 man=mancover_2m depth=wdpstrcov_2m discharge=distrcov_2m nwalkers=100000 niterations=30 output_step=2<jupyter_output><empty_output><jupyter_text>Display the static results.<jupyter_code>!d.rast wdpstrcov_2m.30
!d.legend wdpstrcov_2m.30
!d.rast distrcov_2m.30
!d.legend distrcov_2m.30
Image(filename="map.png")<jupyter_output><empty_output><jupyter_text>Animate the time series using the Animation tool. Sediment flow rate, erosion and depositionCompute sediment flow rate and net erosion/deposition using sedimenttransport part of the SIMWE model implemented in thethe _r.sim.sediment_ module.To make the computations faster, set region just to the upper part ofthe watershed:<jupyter_code>!g.region s=s+290<jupyter_output><empty_output><jupyter_text>Compute input transport capacity and detachment coefficient maps:<jupyter_code>!r.mapcalc "tranin = 0.001"
!r.mapcalc "detin = 0.001"<jupyter_output><empty_output><jupyter_text>Compute input critical shear stress:<jupyter_code>!r.mapcalc "tauin = 0.01"<jupyter_output><empty_output><jupyter_text>Run the model using the last depth from previous run:<jupyter_code>!g.copy rast=wdp_2m.30,wdp_2m
!r.sim.sediment elevation=elev_lid792_2m dx=dx_2m dy=dy_2m water_depth=wdp_2m detachment_coeff=detin transport_coeff=tranin shear_stress=tauin man_value=0.05 nwalkers=1000000 niterations=30 transport_capacity=tcapacity tlimit_erosion_deposition=erdepmax sediment_flux=sedflow erosion_deposition=erdepsimwe<jupyter_output><empty_output><jupyter_text>Display these results after few seconds:<jupyter_code>!d.rast tcapacity
!d.rast erdepmax
Image(filename="map.png")<jupyter_output><empty_output><jupyter_text>Display the final results:<jupyter_code>!d.rast sedflow
!d.legend sedflow
!d.rast erdepsimwe
!d.legend erdepsimwe
Image(filename="map.png")
# end the GRASS session
os.remove(rcfile)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09505978982968957, 'fraction_numerical': 0.03937673632081169, 'mean_word_length': 7.423194303153611, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 85, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '20', 'dir': 'jupyter-structured-clean-dedup', 'id': '373884', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 3134, 'n_tokens_neox': 2922, 'n_words': 741, 'path': 'notebooks/simwe.ipynb', 'repo_name': 'wenzeslaus/geospatial-modeling-course-jupyter', 'size': '8279'}