From 0332362ad4734b4c81d665de893d54fe1df281c1 Mon Sep 17 00:00:00 2001
From: Marco Matthies <71844+marcom@users.noreply.github.com>
Date: Mon, 22 Jul 2024 15:32:56 +0200
Subject: [PATCH] Add cropcover, cropyield functions and add some tests

---
 src/Persefone.jl       |  6 ++++--
 src/crop/almass.jl     |  6 +++++-
 src/crop/farmplot.jl   |  5 ++---
 src/crop/simplecrop.jl |  9 +++++++--
 test/crop_tests.jl     | 26 +++++++++++++++++++++++++-
 5 files changed, 43 insertions(+), 9 deletions(-)

diff --git a/src/Persefone.jl b/src/Persefone.jl
index f7a5c30..eeb8ec0 100644
--- a/src/Persefone.jl
+++ b/src/Persefone.jl
@@ -46,7 +46,7 @@ export
     Weather,
     FarmEvent,
     ModelAgent,
-    # FarmPlot,
+    FarmPlot,
     Animal,
     Farmer,
     DataOutput,
@@ -103,7 +103,9 @@ export
     loadmodelobject,
     croptype,
     cropname,
-    cropheight
+    cropheight,
+    cropcover,
+    cropyield
 
 ## Import and define units and dimensions
 import Unitful: cm, m, km, ha, mg, g, kg, Length, Area, Mass
diff --git a/src/crop/almass.jl b/src/crop/almass.jl
index 116cd50..68d60eb 100644
--- a/src/crop/almass.jl
+++ b/src/crop/almass.jl
@@ -21,7 +21,9 @@ import Persefone:
     stepagent!,
     croptype,
     cropname,
-    cropheight
+    cropheight,
+    cropcover,
+    cropyield
 using Dates: Date, month, monthday
 using CSV: CSV
 
@@ -89,6 +91,8 @@ end
 croptype(cs::CropState) = cs.croptype
 cropname(cs::CropState) = cropname(croptype(cs))
 cropheight(cs::CropState) = cs.height
+cropcover(cs::CropState) = 0.0
+cropyield(cs::CropState) = 0.0
 
 """
     Base.tryparse(type, str)
diff --git a/src/crop/farmplot.jl b/src/crop/farmplot.jl
index a3c283e..0758219 100644
--- a/src/crop/farmplot.jl
+++ b/src/crop/farmplot.jl
@@ -3,9 +3,6 @@
 ### This file contains code for the fields that farmers manage.
 ###
 
-# abstract type AbstractFarmPlot <: ModelAgent end
-
-# mutable struct FarmPlot{T <: AbstractCropState} <: AbstractFarmPlot
 mutable struct FarmPlot{T} <: ModelAgent
     const id::Int64
     pixels::Vector{Tuple{Int64, Int64}}
@@ -15,6 +12,8 @@ end
 croptype(f::FarmPlot{T}) where {T} = croptype(f.crop_state)
 cropname(f::FarmPlot{T}) where {T} = cropname(croptype(f))
 cropheight(f::FarmPlot{T}) where {T} = cropheight(f.crop_state)
+cropcover(f::FarmPlot{T}) where {T} = cropcover(f.crop_state)
+cropyield(f::FarmPlot{T}) where {T} = cropyield(f.crop_state)
 
 """
     initfields_fill_with!(make_farmplot_fn, model)
diff --git a/src/crop/simplecrop.jl b/src/crop/simplecrop.jl
index 7c622e7..d7b61b2 100644
--- a/src/crop/simplecrop.jl
+++ b/src/crop/simplecrop.jl
@@ -10,7 +10,9 @@ import Persefone:
     stepagent!,
     croptype,
     cropname,
-    cropheight
+    cropheight,
+    cropcover,
+    cropyield
 
 struct CropType
     name::String
@@ -20,12 +22,15 @@ cropname(ct::CropType) = ct.name
 
 mutable struct CropState
     croptype::CropType
-    cropheight::Length{Float64}
+    height::Length{Float64}
 end
 
 croptype(cs::CropState) = cs.croptype
 cropname(cs::CropState) = cropname(croptype(cs))
 cropheight(cs::CropState) = cs.height
+cropcover(cs::CropState) = 0.0
+cropyield(cs::CropState) = 0.0
+
 
 """
     stepagent!(farmplot, model)
diff --git a/test/crop_tests.jl b/test/crop_tests.jl
index b2d12fb..7a097ce 100644
--- a/test/crop_tests.jl
+++ b/test/crop_tests.jl
@@ -6,7 +6,6 @@
 const TESTPARAM_ALMASS = joinpath(pkgdir(Persefone), "test", "test_parameters_almass.toml")
 const TESTPARAM_SIMPLECROP = joinpath(pkgdir(Persefone), "test", "test_parameters_simplecrop.toml")
 
-#"Model initialisation" begin
 @testset for paramfile in (TESTPARAM_ALMASS, TESTPARAM_SIMPLECROP)
     @testset "Model initialisation" begin
         model = initialise(paramfile)
@@ -19,3 +18,28 @@ const TESTPARAM_SIMPLECROP = joinpath(pkgdir(Persefone), "test", "test_parameter
     end
 end
 
+@testset "Submodule ALMaSS" begin
+    ct = Ps.ALMaSS.CropType("olive tree", missing, missing, missing, missing, missing,
+                            missing, missing)
+    fp = FarmPlot(0, [(0,0)],
+                  Ps.ALMaSS.CropState(ct, Ps.ALMaSS.janfirst, 0.0, 0.0m, 0.0, 0.0, Ps.EventType[]))
+    @test fp isa FarmPlot
+    @test fp isa FarmPlot{Ps.ALMaSS.CropState}
+    @test croptype(fp) isa Ps.ALMaSS.CropType
+    @test cropname(fp) isa String
+    @test cropheight(fp) isa Length{Float64}
+    @test cropcover(fp) isa Float64
+    @test cropyield(fp) isa Float64
+end
+
+@testset "Submodule SimpleCrop" begin
+    ct = Ps.SimpleCrop.CropType("olive tree")
+    fp = FarmPlot(0, [(0,0)], Ps.SimpleCrop.CropState(ct, 0.0m))
+    @test fp isa FarmPlot
+    @test fp isa FarmPlot{Ps.SimpleCrop.CropState}
+    @test croptype(fp) isa Ps.SimpleCrop.CropType
+    @test cropname(fp) isa String
+    @test cropheight(fp) isa Length{Float64}
+    @test cropcover(fp) isa Float64
+    @test cropyield(fp) isa Float64
+end
-- 
GitLab