3_CropYield / softcal workflow¶
Purpose¶
The 3_CropYield/softcal folder prepares a SWAT+ setup for later hard calibration by checking and adjusting process-relevant parameters before formal calibration.
The scripts focus on three tasks:
- adjusting crop days to maturity (
days_mat) and PHU behavior, - adjusting crop-growth parameters to improve simulated yields,
- adjusting water-yield-related parameters, especially
escoand optionallyepco.
The workflow is deliberately semi-automatic. It runs parameter samples, produces diagnostic plots, and then requires manual selection of parameter values based on plausibility and comparison with observed crop-yield ranges.
Key files and folders¶
| Path | Role |
|---|---|
softcal.Rproj |
RStudio project file. |
workflow/01_crop_phu.R |
Samples days_mat, runs SWAT+, and supports manual selection of days-to-maturity values. |
workflow/02_crop_yield.R |
Samples crop-growth parameters and writes accepted plants.plt. |
workflow/02_wateryield.R |
Samples esco or esco + epco and optionally writes hydrology.hyd. |
observation/crop_yields.csv |
Main observed-yield table used by the scripts. |
observation/crop_yield_all.csv |
Extended observed-yield table; not directly used by the current scripts. |
./clean_setup |
Expected SWAT+ model folder. |
./simulation |
Created/used by SWATrunR for simulation outputs. |
./backup |
Expected location for original plants.plt and hydrology.hyd. |
Observation table¶
The main observation file is observation/crop_yields.csv.
| Crop | Yield minimum | Yield maximum | Yield mean |
|---|---|---|---|
fesc |
4 | 6 | 5.5 |
lupn |
1.72 | 2.15 | 1.935 |
oats |
2.58 | 3.612 | 2.99925 |
pota |
4.4 | 8.8 | 6.215 |
canp |
1.82 | 3.64 | 2.764125 |
wwht |
3.01 | 4.73 | 3.88075 |
Requirements¶
| Requirement | Details |
|---|---|
| R packages | SWATtunR, SWATrunR, dplyr, tibble; 02_wateryield.R also requires SWATreadR. |
| SWAT+ model folder | ./clean_setup must contain a runnable SWAT+ setup. |
| SWAT+ outputs | mgtout, basin_wb_aa, and basin_aqu_aa outputs must be available or requested correctly. |
| Observed crop-yield statistics | ./observation/crop_yields.csv must contain crop names matching SWAT+ plant names. |
| Backup files | ./backup/plants.plt and ./backup/hydrology.hyd are used to restore original parameters. |
| Compute resources | Scripts use n_cores = 5, 10, and 25; adapt to the machine. |
| Execution mode | Interactive RStudio execution is preferred. |
Recommended execution order¶
- Confirm that
./clean_setupis runnable. - Confirm that
./observation/crop_yields.csvis correct. - Run
workflow/01_crop_phu.R. - Inspect PHU, yield, and biomass plots.
- Manually select
dmat_sel. - Keep
dmat_selin memory or save it before restarting R. - Run
workflow/02_crop_yield.R. - Inspect dotty yield plots.
- Manually select
crop_par_sel. - Run the final crop-parameter check.
- Overwrite
plants.pltonly when results are acceptable. - Run
workflow/02_wateryield.R. - Select alternative A or B.
- Check water-yield ratio and crop-yield behavior.
- Write
hydrology.hydonly after both water-yield and crop-yield checks are acceptable.
workflow/01_crop_phu.R¶
This script adjusts days_mat values so that simulated crop development is compatible with crop characteristics and management schedules.
| Part | What the script does |
|---|---|
| Load packages | Loads SWATtunR, SWATrunR, dplyr, and tibble. |
| Define paths | Sets model_path and yield_obs_path. |
| Select crops | Uses all plant_name entries in crop_yields.csv. |
Reset plants.plt |
Copies the backup file when reset = TRUE. |
Sample days_mat |
Generates parameter sets using sample_days_mat(). |
| Run simulations | Runs SWAT+ and extracts yield, biomass, and PHU from mgtout. |
| Load latest run | Loads the latest timestamped sim_dmat run. |
| Plot diagnostics | Plots PHU, yield, and biomass against adjusted days_mat. |
| Manual selection | Defines dmat_sel manually and prepares plant parameters. |
Note
The target is not simply to maximize yield. The first crop-calibration step should make crop maturity plausible.
workflow/02_crop_yield.R¶
This script assumes that dmat_sel already exists from 01_crop_phu.R.
| Part | What the script does |
|---|---|
| Dependency | Requires dmat_sel in the active R environment. |
| Parameter bounds | Defines changes for lai_pot, harv_idx, tmp_base, and bm_e. |
| Sampling | Generates 40 Latin Hypercube samples. |
| Add PHU settings | Combines crop-growth samples with selected days_mat values. |
| Run simulations | Runs SWAT+ and extracts yield from mgtout. |
| Plot dotty figures | Plots yield responses against sampled parameter changes. |
| Manual selection | Defines crop_par_sel. |
| Final check | Runs final simulation and plots PHU, yield, and biomass. |
Write plants.plt |
Copies accepted plants.plt into clean_setup. |
workflow/02_wateryield.R¶
This script calibrates the water-yield ratio using either:
- Alternative A:
escoonly, - Alternative B:
escoandepcotogether.
| Part | What the script does |
|---|---|
| Choose alternative | Default is alternative B. |
| Define target | Sets target water-yield ratio to 0.133. |
| Load observations | Loads crop-yield observations for yield checks. |
| Optional reset | Restores hydrology.hyd from backup. |
| Generate parameter set | Samples esco or full esco x epco grid. |
| Run simulations | Extracts basin precipitation, runoff, lateral flow, tile flow, and aquifer flow. |
| Plot WYR response | Compares simulated water-yield ratio to target. |
| Check selected set | Runs follow-up simulation with crop outputs. |
Write hydrology.hyd |
Uses SWATreadR to write fixed esco and epco. |
| Optional calibration ranges | Creates parameter boundaries for later calibration. |
Outputs¶
| Output | Produced by | Meaning |
|---|---|---|
./simulation/<timestamp>_sim_dmat |
01_crop_phu.R |
Batch for sampled days_mat values. |
./simulation/<timestamp>_sim_yld |
02_crop_yield.R |
Batch for sampled crop-growth parameters. |
./simulation/<timestamp>_sim_check01 |
02_crop_yield.R |
Final crop-parameter check run. |
./simulation/<timestamp>_sim_wbal |
02_wateryield.R |
Batch for esco/epco combinations. |
./simulation/<timestamp>_sim_check02 |
02_wateryield.R |
Final water-yield parameter check run. |
./clean_setup/plants.plt |
02_crop_yield.R |
Final crop-parameter table. |
./clean_setup/hydrology.hyd |
02_wateryield.R |
Final hydrology parameter table, if written. |
Interpretation rules¶
- PHU fractions should be plausible for the crop and harvest schedule.
- Simulated crop yields should fall within or near the observed minimum–maximum range unless deviation is justified.
- Biomass should be agronomically plausible and consistent with yield response.
- Water-yield ratio should approach the target value, but selected
esco/epcovalues must also preserve crop-yield plausibility. - Parameter changes should remain biologically and hydrologically defensible.