### Persefone.jl - a model of agricultural landscapes and ecosystems in Europe. ### ### Functions for switchable crop models. ### """ initfields!(cropmodel, cropfile, growthfile) Initialise the crop model. Returns the crop types available in the simulation, as well as the types `Tcroptype` and `Tcropstate`. """ function initcropmodel(cropmodel::AbstractString, cropfile::AbstractString, growthfile::AbstractString) if cropmodel == "almass" Tcroptype = ALMaSS.CropType Tcropstate = ALMaSS.CropState crops = ALMaSS.readcropparameters(cropfile, growthfile) elseif cropmodel == "simple" Tcroptype = SimpleCrop.CropType Tcropstate = SimpleCrop.CropState crops_almass = ALMaSS.readcropparameters(cropfile, growthfile) crops = Dict(name => SimpleCrop.CropType(ct.name) for (name, ct) in crops_almass) else error("initcropmodel: no implementation for crop model '$cropmodel'") end return crops, Tcroptype, Tcropstate end """ initfields!(model) Initialise the farm plots in the simulation model. """ function initfields!(model::SimulationModel) n = 0 convertid = Dict{Int64,Int64}() width, height = size(model.landscape) for x in 1:width for y in 1:height # for each pixel, we need to extract the field ID given by the map input # file, and convert it into the internal object ID used by Agents.jl, # creating a new agent object if necessary rawid = model.landscape[x,y].fieldid (ismissing(rawid)) && continue if rawid in keys(convertid) objectid = convertid[rawid] model.landscape[x,y].fieldid = objectid push!(model.farmplots[objectid].pixels, (x,y)) else cropstate = make_cropstate(model, @param(crop.cropmodel)) fp = FarmPlot( length(model.farmplots) + 1, [(x, y)], cropstate ) push!(model.farmplots, fp) model.landscape[x,y].fieldid = fp.id convertid[rawid] = fp.id n += 1 end end end @info "Initialised $n farm plots." end # internal utility function function make_cropstate(model::SimulationModel, cropmodel::AbstractString) if cropmodel == "almass" phase = (month(model.date) < 3 ? ALMaSS.janfirst : ALMaSS.marchfirst) cs = ALMaSS.CropState( model.crops["natural grass"], phase, 0.0, 0.0m, 0.0, 0.0, Vector{Management}() ) elseif cropmodel == "simple" cs = SimpleCrop.CropState( model.crops["natural grass"], 0.0m ) else error("Unhandled crop model '$cropmodel' in make_cropstate") end return cs end