Skip to content
Snippets Groups Projects
Commit 1c0f44e3 authored by xo30xoqa's avatar xo30xoqa
Browse files

Model now saves per-species abundances each time step

parent 6d840396
No related branches found
No related tags found
No related merge requests found
......@@ -3,6 +3,9 @@
### This file includes functions for saving the model output.
###
const LOGFILE = "simulation.log"
const POPFILE = "populations.csv"
## Note: `setupdatadir()` was adapted from the GeMM model by Leidinger et al.
## (https://github.com/CCTB-Ecomods/gemm/blob/master/src/output.jl)
......@@ -29,7 +32,7 @@ function setupdatadir()
elseif param("core.loglevel") == "quiet"
loglevel = Logging.Warn
end
logfile = open(joinpath(param("core.outdir"), "simulation.log"), "w+")
logfile = open(joinpath(param("core.outdir"), LOGFILE), "w+")
simulationlogger = TeeLogger(ConsoleLogger(logfile, loglevel),
ConsoleLogger(stdout, loglevel))
global_logger(simulationlogger)
......@@ -51,5 +54,27 @@ function setupdatadir()
!(isfile(ffmap)) && Base.error("The map file $(ffmap) doesn't exist.")
cp(lcmap, joinpath(param("core.outdir"), basename(lcmap)), force = true)
cp(ffmap, joinpath(param("core.outdir"), basename(ffmap)), force = true)
# Create the data output file(s)
open(joinpath(param("core.outdir"), POPFILE), "w") do f
println(f, "Date;Species;Abundance")
end
end
"""
savepopulationdata(model)
Print a comma-separated set of lines to `populations.csv`, giving the
current date and population size for each animal species.
"""
function savepopulationdata(model::AgentBasedModel)
pops = Dict{String,Int}(s=>0 for s = param("nature.targetspecies"))
for a in allagents(model)
(typeof(a) != Animal) && continue
pops[a.species.name] += 1
end
open(joinpath(param("core.outdir"), POPFILE), "a") do f
for p in keys(pops)
println(f, "$(model.date);$(p);$(pops[p])")
end
end
end
......@@ -44,6 +44,7 @@ function stepsimulation!(model::AgentBasedModel)
for a in Schedulers.ByType((Farmer,Animal,CropPlot), true)(model)
stepagent!(getindex(model, a), model)
end
savepopulationdata(model)
end
"""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment