Skip to content
Snippets Groups Projects
Commit 9bd0f267 authored by xo30xoqa's avatar xo30xoqa
Browse files

Export outputdata() and other small changes

parent cdc20ca1
No related branches found
No related tags found
No related merge requests found
name = "Persefone"
uuid = "039acd1d-2a07-4b33-b082-83a1ff0fd136"
authors = ["Daniel Vedder <daniel.vedder@idiv.de>"]
version = "0.3.3"
version = "0.3.4"
[deps]
Agents = "46ada45e-f475-11e8-01d0-f70cc89e6671"
......
docs/persefonejl_logo.png

101 KiB

......@@ -79,6 +79,7 @@ export
stepsimulation!,
createevent!,
finalise!,
outputdata,
visualisemap,
populationtrends,
visualiseoutput,
......
......@@ -14,11 +14,12 @@ Returns a Makie figure object.
"""
function visualisemap(model::AgentBasedModel,date=nothing,landcover=nothing)
# load and plot the map
isnothing(landcover) && (landcover = load(@param(world.landcovermap)))
# Note: if the landcover map is supplied, it needs to be rotr90'ed
isnothing(landcover) && (landcover = rotr90(load(@param(world.landcovermap))))
f = Figure()
ax = Axis(f[1,1])
hidedecorations!(ax)
image!(f[1,1], rotr90(landcover))
image!(f[1,1], landcover)
ax.aspect = DataAspect()
# check if there are individuals and plot them
inds = model.datatables["individuals"]
......
......@@ -180,6 +180,10 @@ end
Deserialise a model object that was previously saved with `[savemodelobject](@ref)`.
"""
function loadmodelobject(fullfilename::String)
if !isfile(fullfilename)
@warn "File $(fullfilename) does not exist. Loading failed."
return
end
object = deserialize(fullfilename)
# Do basic integrity checks
if !(typeof(object) <: Dict && typeof(object["model"]) <: AgentBasedModel)
......
......@@ -172,12 +172,13 @@ function newdataoutput!(model::AgentBasedModel, name::String, header::Vector{Str
end
"""
outputdata(model)
outputdata(model, force=false)
Cycle through all registered data outputs and activate them according to their
configured frequency.
configured frequency. If `force` is `true`, activate all outputs regardless
of their configuration.
"""
function outputdata(model::AgentBasedModel)
function outputdata(model::AgentBasedModel, force=false)
#XXX enable output every X days, or weekly?
#XXX all output functions except for "end" are run on the first update
# -> should they all be run on the last update, too?
......@@ -185,9 +186,9 @@ function outputdata(model::AgentBasedModel)
isnextmonth = d -> (day(d) == day(startdate))
isnextyear = d -> (month(d) == month(startdate) && day(d) == day(startdate))
for output in model.dataoutputs
(output.frequency == "never") && continue
(!force && output.frequency == "never") && continue
# check if this output should be activated today
if (output.frequency == "daily") ||
if force || (output.frequency == "daily") ||
(output.frequency == "monthly" && isnextmonth(model.date)) ||
(output.frequency == "yearly" && isnextyear(model.date)) ||
(output.frequency == "end" && model.date == @param(core.enddate))
......@@ -236,7 +237,8 @@ function savemodelobject(model::AgentBasedModel, filename::String)
object = Dict("model"=>model,
"modelversion"=>pkgversion(Persefone),
"juliaversion"=>VERSION)
filename = joinpath(@param(core.outdir), filename*".dat")
!endswith(filename, ".dat") && (filename *= ".dat")
filename = joinpath(@param(core.outdir), filename)
serialize(filename, object)
@debug "Saved model object to $(filename)."
end
......@@ -62,9 +62,6 @@ function initmodel(settings::Dict{String, Any})
createdatadir(settings["core.outdir"], settings["core.overwrite"])
logger = modellogger(settings["core.loglevel"], settings["core.outdir"])
with_logger(logger) do
events = Vector{FarmEvent}()
dataoutputs = Vector{DataOutput}()
datatables = Dict{String, DataFrame}()
landscape = initlandscape(settings["world.landcovermap"],
settings["world.farmfieldsmap"])
weather = initweather(settings["world.weatherfile"],
......@@ -79,16 +76,16 @@ function initmodel(settings::Dict{String, Any})
:landscape=>landscape,
:weather=>weather,
:crops=>crops,
:dataoutputs=>dataoutputs,
:datatables=>datatables,
:events=>events)
model = AgentBasedModel(Union{Farmer,Animal,FarmPlot}, space, properties=properties,
:dataoutputs=>Vector{DataOutput}(),
:datatables=>Dict{String, DataFrame}(),
:events=>Vector{FarmEvent}())
model = AgentBasedModel(Union{Farmer,Animal,FarmPlot},
space, properties=properties,
rng=StableRNG(settings["core.seed"]), warn=false)
saveinputfiles(model)
initfields!(model)
initfarms!(model)
initnature!(model)
#outputdata(model) #XXX record data before run starts?
model
end
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment