diff --git a/src/Persefone.jl b/src/Persefone.jl index 7b666ad3a14cea2c26396bcdd72531cdcfbe0c24..a2c0a7159622c5543be57772e738bbf2506f1ed5 100644 --- a/src/Persefone.jl +++ b/src/Persefone.jl @@ -60,7 +60,7 @@ export @habitat, @landcover, @cropheight, - @croptype, + @cropname, @distanceto, @distancetoedge, @countanimals, diff --git a/src/crop/farmplot.jl b/src/crop/farmplot.jl index 017850651cfd07c363fecfe1e86b6fcc11244d2c..af15dc55a1e0b7be963fb5aa8c5c188e5b3c56ab 100644 --- a/src/crop/farmplot.jl +++ b/src/crop/farmplot.jl @@ -176,6 +176,17 @@ function croptype(pos::Tuple{Int64,Int64}, model::AgentBasedModel) model[model.landscape[pos...].fieldid].croptype end +""" + cropname(model, position) + +Return the name of the crop at this position, or nothing if there is no crop here +(utility wrapper). +""" +function cropname(pos::Tuple{Int64,Int64}, model::AgentBasedModel) + ismissing(model.landscape[pos...].fieldid) ? nothing : + model[model.landscape[pos...].fieldid].croptype.name +end + """ cropheight(model, position) diff --git a/src/nature/nature.jl b/src/nature/nature.jl index e63c9931eb2080ae88c2792f5b5b6baf51dd8329..105644fc24538b327e47d386c5d15fd05fa0a806 100644 --- a/src/nature/nature.jl +++ b/src/nature/nature.jl @@ -287,7 +287,7 @@ and a position, and returns `true` or `false` depending on the conditions specified in the macro body. Several utility macros can be used within the body of `@habitat` as a short-hand for -common expressions: [`@landcover`](@ref), [`@croptype`](@ref), [`@cropheight`](@ref), +common expressions: [`@landcover`](@ref), [`@cropname`](@ref), [`@cropheight`](@ref), [`@distanceto`](@ref), [`@distancetoedge`](@ref), [`@countanimals`](@ref). The variables `model` and `pos` can be used for checks that don't have a macro available. @@ -297,7 +297,7 @@ Two example uses of `@habitat` might look like this: movementhabitat = @habitat(@landcover() in (grass agriculture soil)) nestinghabitat = @habitat((@landcover() == grass || - (@landcover() == agriculture && @croptype() != maize && + (@landcover() == agriculture && @cropname() != "maize" && @cropheight() < 10)) && @distanceto(forest) > 20) ``` @@ -333,14 +333,14 @@ macro landcover() end """ - @croptype + @cropname -Return the local croptype, or nothing if there is no crop here. +Return the name of the local croptype, or nothing if there is no crop here. This is a utility wrapper that can only be used nested within [`@phase`](@ref) or [`@habitat`](@ref). """ -macro croptype() - :(croptype($(esc(:pos)), $(esc(:model)))) +macro cropname() + :(cropname($(esc(:pos)), $(esc(:model)))) end """ diff --git a/test/landscape_tests.jl b/test/landscape_tests.jl index 32f9f65e1ae0f09fd962fbb55d8876fbd10986cc..1fa2ac5a0754fb3ce2952748133b5673e94f149a 100644 --- a/test/landscape_tests.jl +++ b/test/landscape_tests.jl @@ -8,7 +8,8 @@ landscape = Ps.initlandscape(TESTSETTINGS["world.landcovermap"], TESTSETTINGS["world.farmfieldsmap"]) space = GridSpace(size(landscape), periodic=false) - properties = Dict{Symbol,Any}(:landscape=>landscape, :settings=>TESTSETTINGS) + properties = Dict{Symbol,Any}(:landscape=>landscape, :settings=>TESTSETTINGS, + :date=>TESTSETTINGS["core.startdate"]) model = AgentBasedModel(FarmPlot, space, properties=properties, warn=false) # these tests are specific to the Jena maps @test_logs (:info, "Initialised 2092 farm plots.") match_mode=:any Ps.initfields!(model) diff --git a/test/nature_tests.jl b/test/nature_tests.jl index d862f58b12975af4face0b8df3f21d32f213ec0b..614780daa2fc31df723b0d482982354bbdeb4136 100644 --- a/test/nature_tests.jl +++ b/test/nature_tests.jl @@ -8,12 +8,12 @@ model = smalltestlandscape() model.landscape[6,6] = Pixel(Ps.agriculture, 1, []) species::Dict{String,Any} = Dict("name"=>"test_animal") - add_agent!((6,6), FarmPlot, model, [(6,6)], Ps.wheat, 1.2, 3.4) + add_agent!((6,6), FarmPlot, model, [(6,6)], Ps.wheat, 1.2, 3.4) #FIXME add_agent!((3,3), Animal, model, species, Ps.male, 1) add_agent!((4,4), Animal, model, species, Ps.female, 1) # create a set of habitat descriptors h1 = @habitat(@landcover() == Ps.water) - h2 = @habitat(@croptype() == Ps.wheat && + h2 = @habitat(@cropname() == "wheat" && @cropheight() < 2) h3 = @habitat(@distanceto(Ps.water) > 2 && @distancetoedge() <= 2) @@ -77,7 +77,7 @@ end @phase life begin @debug "$(Persefone.animalid(animal)) is swimming happily in its pond." @respond Persefone.pesticide @kill(@trait(pesticidemortality), "poisoning") - @respond Persefone.harvest @setphase(drought) + @respond Persefone.harvesting @setphase(drought) @debug "Animal: $animal" if @trait(sex) == Persefone.female && @countanimals() < 3 && @trait(age) >= @trait(ageofmaturity) && @landcover() == Persefone.water @@ -103,7 +103,7 @@ end @test Ps.countanimals((1,1), model, radius=4) == 0 @test Ps.countanimals(pond, model) == 2 @test model[1].age == 0 - createevent!(model, [pond], Ps.harvest) + createevent!(model, [pond], Ps.harvesting) @test_logs((:debug, "Mermaid 1 is swimming happily in its pond."), (:debug, "Mermaid 2 is swimming happily in its pond."), min_level=Logging.Debug, match_mode=:any, diff --git a/test/runtests.jl b/test/runtests.jl index 9023b6b8df21457e03af13fe946f641b750897fd..cfa6d69c2a1ccfd1528b2f66e872dc16b602328d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -45,9 +45,12 @@ function smalltestlandscape() weather = Ps.initweather(TESTSETTINGS["world.weatherfile"], TESTSETTINGS["core.startdate"], TESTSETTINGS["core.enddate"]) + crops = Ps.readcropparameters(TESTSETTINGS["crop.cropfile"], + TESTSETTINGS["crop.growthfile"]) properties = Dict{Symbol,Any}(:date=>TESTSETTINGS["core.startdate"], :landscape=>landscape, :weather=>weather, + :crops=>crops, :events=>Vector{FarmEvent}(), :logger=>global_logger(), :dataoutputs=>Vector{DataOutput}(), diff --git a/test/test_parameters.toml b/test/test_parameters.toml index ba7d5d000bdaaf4f1a908845af940427cbb730a0..ac63e7c5cae100bd89e8279ae7f9390d41b09a7e 100644 --- a/test/test_parameters.toml +++ b/test/test_parameters.toml @@ -32,6 +32,6 @@ insectmodel = ["season", "habitat", "pesticides"] # which factors affect insect [crop] cropmodel = "almass" # crop growth model to use, "almass" or "aquacrop" -cropfile = "data/crop_data_general.csv" # file with general crop parameters -growthfile = "data/almass_crop_growth_curves.csv" # file with crop growth parameters +cropfile = "crop_data_general.csv" # file with general crop parameters +growthfile = "almass_crop_growth_curves.csv" # file with crop growth parameters