diff --git a/src/crop/farmplot.jl b/src/crop/farmplot.jl index c10030631ca6323d34e8f4a9130fb39568772a51..ecd4baf5a26aa927bc836dedffdf27f09af0b152 100644 --- a/src/crop/farmplot.jl +++ b/src/crop/farmplot.jl @@ -96,11 +96,9 @@ end Calculate the average field size in hectares for the model landscape. """ function averagefieldsize(model::SimulationModel) - sizes::Vector{Float64} = [] - for fp in model.farmplots - push!(sizes, @areaof(length(fp.pixels))) - end - return sum(sizes)/length(sizes) |> ha + area_sum = sum(@areaof(length(fp.pixels)) for fp in model.farmplots; init=0.0u"m^2") + area_avg = area_sum / length(model.farmplots) + return area_avg |> ha end """ diff --git a/test/landscape_tests.jl b/test/landscape_tests.jl index d80afb2ab09ac9a712dae6697d78b2955f170d27..9aa3cb3b96c7a60ea039c767096511070611f8a2 100644 --- a/test/landscape_tests.jl +++ b/test/landscape_tests.jl @@ -14,7 +14,7 @@ @test Ps.landcover((400,400), model) == Ps.grass @test Ps.landcover((800,800), model) == Ps.agriculture @test Ps.landcover((1100,1100), model) == Ps.builtup - @test Ps.averagefieldsize(model) == 5.37 + @test isapprox(Ps.averagefieldsize(model), 5.374u"ha"; atol=1e-3u"ha") @test count(f -> ismissing(f.fieldid), model.landscape) == 1685573 @test length(Ps.farmplot((800,800), model).pixels) == 4049 end diff --git a/test/runtests.jl b/test/runtests.jl index 4ee512ea3a25baea82eb68774c3267bf3befac3f..7fb635a5c663fc831d963786a07cf353b8961e15 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -20,7 +20,7 @@ const Ps = Persefone const TESTPARAMETERS = joinpath(pkgdir(Persefone), "test/test_parameters.toml") const TESTSETTINGS = Ps.getsettings(TESTPARAMETERS) -import Unitful: cm, m, km, ha, mg, g, kg, Length, Area, Mass +import Unitful: @u_str, cm, m, km, ha, mg, g, kg, Length, Area, Mass const m² = m^2 const km² = km^2