From b077630dcd8687430acf11b6659bb3a6830967fd Mon Sep 17 00:00:00 2001
From: Marco Matthies <71844+marcom@users.noreply.github.com>
Date: Mon, 28 Oct 2024 12:01:50 +0100
Subject: [PATCH] ALMaSS: allow choosing highnutrientcurve when calling sow!

`sow!(::ALMaSS.CropState, ...)` calls `setphase!`, which needs to know
which growth curve to use.  Some crop types (e.g. "permanent
set-aside") do not have a lownutrientgrowthcurve, which then causes an
error when `setphase!` tries to access the `missing` growth curve.

The highnutrientgrowthcurve would only be chosen when the cropstate
had `fertilising` in its events.  As the events for a cropstate are
now `empty!`ied in `sow!`, it's not possible to set the `fertilising`
event before calling `sow!`.

This previously didn't cause a bug, as the logic for which growth
curve to choose was the wrong way around, choosing the
highnutrientgrowth curve when no `fertilising` event was set for the
cropstate.
---
 src/crop/almass.jl   | 8 ++++++--
 src/crop/farmplot.jl | 4 ++--
 2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/src/crop/almass.jl b/src/crop/almass.jl
index 25d6c4d..eff3ad0 100644
--- a/src/crop/almass.jl
+++ b/src/crop/almass.jl
@@ -796,13 +796,17 @@ supply_global_radiation(model::SimulationModel) =
 
 Change the cropstate to sow the specified crop.
 """
-function sow!(cs::CropState, model::SimulationModel, cropname::String)
+function sow!(cs::CropState, model::SimulationModel, cropname::String;
+              highnutrients::Bool=false)
     !ismissing(cs.croptype.minsowdate) && model.date < cs.croptype.minsowdate &&
         @warn "$(model.date) is earlier than the minimum sowing date for $(cropname)."
+    empty!(cs.events)
+    if highnutrients
+        push!(cs.events, fertiliser)
+    end
     cs.croptype = model.crops[cropname]
     setphase!(cs, sow, model)
     cs.mature = false
-    empty!(cs.events)
 
     # cs.vegddegs = 0.0
     # cs.ddegs = 0.0
diff --git a/src/crop/farmplot.jl b/src/crop/farmplot.jl
index c44d6ad..d11b2ff 100644
--- a/src/crop/farmplot.jl
+++ b/src/crop/farmplot.jl
@@ -36,9 +36,9 @@ end
 
 Sow the specified crop on the farmplot.
 """
-function sow!(farmplot::FarmPlot, model::SimulationModel, cropname::String)
+function sow!(farmplot::FarmPlot, model::SimulationModel, cropname::String; kwargs...)
     createevent!(model, farmplot.pixels, sowing)
-    sow!(farmplot.cropstate, model, cropname)
+    sow!(farmplot.cropstate, model, cropname; kwargs...)
     @debug "Farmer $(farmplot.farmer) sowed $(cropname) on farmplot $(farmplot.id)."
 end
 
-- 
GitLab