From 84b933f87612dcc9168cc44a4e41763fa93076a6 Mon Sep 17 00:00:00 2001
From: Marco Matthies <71844+marcom@users.noreply.github.com>
Date: Thu, 7 Nov 2024 00:14:46 +0100
Subject: [PATCH] AquaCrop: wrap interface code in WrapAquaCrop submodule

---
 src/crop/aquacrop.jl   | 25 +++++++++++++++++++++++++
 src/crop/cropmodels.jl |  8 ++++----
 test/crop_tests.jl     |  9 +++++----
 3 files changed, 34 insertions(+), 8 deletions(-)

diff --git a/src/crop/aquacrop.jl b/src/crop/aquacrop.jl
index 0e17655..7e09b2c 100644
--- a/src/crop/aquacrop.jl
+++ b/src/crop/aquacrop.jl
@@ -1,5 +1,28 @@
+module WrapAquaCrop
+
+# Persefone.jl wrapper for AquaCrop.jl
+# Use different name from `AquaCrop` to avoid name clash
+
 import AquaCrop
 
+using Persefone:
+    AnnualDate,
+    cm,
+    SimulationModel
+
+import Persefone:
+    stepagent!,
+    croptype,
+    cropname,
+    cropheight,
+    cropcover,
+    cropyield,
+    sow!,
+    harvest!,
+    isharvestable
+
+using Unitful: @u_str
+
 # We can't use Length{Float64} as that is a Union type
 const Tlength = typeof(1.0cm)
 
@@ -68,3 +91,5 @@ function harvest!(cs::AquaCropState, model::SimulationModel)
     # TODO: implement this
     return 0.0u"g/m^2"
 end
+
+end # module WrapAquaCrop
diff --git a/src/crop/cropmodels.jl b/src/crop/cropmodels.jl
index dac5c8b..0d2a68b 100644
--- a/src/crop/cropmodels.jl
+++ b/src/crop/cropmodels.jl
@@ -21,11 +21,11 @@ function initcropmodel(cropmodel::AbstractString, cropdirectory::AbstractString)
         crops = Dict(name => SimpleCrop.CropType(ct.name, ct.group, ct.minsowdate, ct.maxsowdate)
                      for (name, ct) in crops_almass)
     elseif cropmodel == "aquacrop"
-        Tcroptype = AquaCropType
-        Tcropstate = AquaCropState
+        Tcroptype = WrapAquaCrop.AquaCropType
+        Tcropstate = WrapAquaCrop.AquaCropState
         # TODO: temporarily using ALMaSS crop types
         crops_almass = ALMaSS.readcropparameters(cropdirectory)
-        crops = Dict(name => AquaCropType(ct.name, ct.group, ct.minsowdate, ct.maxsowdate)
+        crops = Dict(name => WrapAquaCrop.AquaCropType(ct.name, ct.group, ct.minsowdate, ct.maxsowdate)
                      for (name, ct) in crops_almass)
     else
         error("initcropmodel: no implementation for crop model '$cropmodel'")
@@ -81,7 +81,7 @@ function makecropstate(model::SimulationModel)
             0.0m
         )
     elseif @param(crop.cropmodel) == "aquacrop"
-        cs = AquaCropState(model.crops["natural grass"], 0.0cm)
+        cs = WrapAquaCrop.AquaCropState(model.crops["natural grass"], 0.0cm)
     else
         Base.error("Unhandled crop model '$(@param(crop.cropmodel))' in makecropstate().")
     end
diff --git a/test/crop_tests.jl b/test/crop_tests.jl
index e97ea84..23200a7 100644
--- a/test/crop_tests.jl
+++ b/test/crop_tests.jl
@@ -57,14 +57,15 @@ end
 end
 
 @testset "Submodule AquaCrop" begin
-    ct = Ps.AquaCropType("olive tree", "no_crop_group", AnnualDate(4, 1), AnnualDate(5, 1))
+    PsAC = Ps.WrapAquaCrop
+    ct = PsAC.AquaCropType("olive tree", "no_crop_group", AnnualDate(4, 1), AnnualDate(5, 1))
     id = 0
     pixels = [(0, 0)]
     farmer = 0
-    fp = FarmPlot(id, pixels, farmer, Ps.AquaCropState(ct))
+    fp = FarmPlot(id, pixels, farmer, PsAC.AquaCropState(ct))
     @test fp isa FarmPlot
-    @test fp isa FarmPlot{Ps.AquaCropState}
-    @test croptype(fp) isa Ps.AquaCropType
+    @test fp isa FarmPlot{PsAC.AquaCropState}
+    @test croptype(fp) isa PsAC.AquaCropType
     @test cropname(fp) isa String
     @test cropheight(fp) isa Length{Float64}
     @test cropcover(fp) isa Float64
-- 
GitLab