Carbon notebook
The SWAT+ source tree ships one analysis notebook: jupyter_notebooks/carbon.ipynb. It reads the carbon-output files for a single HRU and draws time-series plots of every carbon pool the C-FARM / Century model tracks. Use it after running a project with the carbon model enabled.
What it reads¶
The notebook reads the following files from a SWAT+ output folder:
hru-data.hruandlanduse.lum. Used to look up the soil name, thelu_mgtname, and the management identifier for a chosen HRU.mgt_out.txt. Operation log: planting, harvest, tillage, fertilization. The notebook overlays these as event markers on every plot.hru_cbn_lyr.txt. Total soil carbon per soil layer.hru_seq_lyr.txt. Sequestered carbon per soil layer.hru_rsdc_stat.txt. Surface residue and soil carbon pools.hru_plc_stat.txt. Plant carbon pools.hru_cpool_stat.txt. Residue and soil carbon pools by layer (metabolic, structural, lignin, microbial, slow humus, passive humus).hru_org_trans_vars.txt. Organic-matter transformation variables.hru_cflux_stat.txt. Carbon flux statistics including CO2.
The notebook expects these to be produced by a run where print.prt lists each of those output rows with at least one frequency turned on. Daily output is what the notebook is designed for.
What it draws¶
The notebook is organised by output file. Each section opens with an "Initialize" cell that loads the file into a DataFrame, then one or more graph cells produce a Plotly figure. Sections, in order:
- Total carbon by layer depth (
hru_cbn_lyr). - Total sequestered carbon and 300 mm sequestered (
hru_seq_lyr). - Surface residue and soil carbon pools at 10 mm depth (
hru_rsdc_stat). - Soil carbon pools at a user-specified depth.
- Live root mass by soil layer.
- Plant stats from
hru_plc_stat. - Residue and soil carbon pools (
hru_cpool_stat) at 10 mm. - Same pools at a user-specified depth.
- Soil residue, metabolic, structural, lignin, microbial, slow humus, and passive humus pools by soil layer.
- Soil water content by soil layer.
- Organic-matter transformation variables (
hru_org_trans_vars). - Carbon fluxes (
hru_cflux_stat), non-CO2 and CO2.
Every plot shows the chosen variable as a time series. PLANT, HARVEST, and TILLAGE events from mgt_out.txt are overlaid as vertical markers so you can read the management context off the figure.
How to run it¶
- Run SWAT+ on a project with the carbon model on. In
codes.bsn, setcarbon = 1(C-FARM) orcarbon = 2(Century). TheAmes_sub1reference dataset already hascarbon = 1. - Turn on the relevant outputs in
print.prt. At minimum,hru_cband the carbon-pool outputs needdailyset toy. - Open
carbon.ipynbin JupyterLab or VS Code. - Edit the first code cell. Set:
hru_idto the HRU you want to plot.wdirandsdirso thatf"{wdir}/{sdir}"is the folder that holds the SWAT+ outputs.- Run all cells (Run All).
The notebook uses pandas, plotly, and the standard library. Install with:
What you should see¶
Each graph cell renders a Plotly figure inline. Event markers from mgt_out.txt line up the plots with the management schedule. The "Initialize" cells print "File not found" and stop rendering that section if the corresponding output file is missing; this is expected when an output is not enabled in print.prt.
Related¶
- Theory: Carbon covers what the pools and fluxes represent.
- print.prt for the output switches.
- Cookbook: enabling the carbon model for the input edits.