From 7ab4db8b7552294e17dda793fc293aa49a1ca78a Mon Sep 17 00:00:00 2001
From: Daniel Vedder <daniel.vedder@idiv.de>
Date: Thu, 16 May 2024 12:43:41 +0200
Subject: [PATCH] Fixed output functions

---
 src/core/simulation.jl             |  2 +-
 src/nature/ecologicaldata.jl       |  7 +++----
 src/nature/macros.jl               | 14 +-------------
 src/nature/nature.jl               | 13 ++++++++-----
 src/nature/populations.jl          | 15 ++-------------
 src/nature/species/wolpertinger.jl |  3 +--
 6 files changed, 16 insertions(+), 38 deletions(-)

diff --git a/src/core/simulation.jl b/src/core/simulation.jl
index d6552a6..7ec91bc 100644
--- a/src/core/simulation.jl
+++ b/src/core/simulation.jl
@@ -173,7 +173,7 @@ function stepsimulation!(model::SimulationModel)
         end
         updatenature!(model)
         updateevents!(model)
-        #outputdata(model) #FIXME
+        outputdata(model)
         model.date += Day(1)
         model
     end
diff --git a/src/nature/ecologicaldata.jl b/src/nature/ecologicaldata.jl
index 449895f..78e85b7 100644
--- a/src/nature/ecologicaldata.jl
+++ b/src/nature/ecologicaldata.jl
@@ -24,14 +24,13 @@ daily, monthly, yearly, or at the end of a simulation, depending on the paramete
 `nature.popoutfreq`.
 """
 function savepopulationdata(model::SimulationModel)
-    #FIXME
     pops = Dict{String,Int}(s=>0 for s = @param(nature.targetspecies))
     for a in model.animals
         isnothing(a) && continue
-        pops[string(typeof(a))] += 1
+        pops[speciesof(a)] += 1
     end
     for m in model.migrants
-        pops[string(typeof(m.first))] += 1
+        pops[speciesof(m.first)] += 1
     end
     data = []
     for p in keys(pops)
@@ -53,7 +52,7 @@ function saveindividualdata(model::SimulationModel)
     data = []
     for a in model.animals
         isnothing(a) && continue
-        push!(data, [model.date,a.id,a.pos[1],a.pos[2],a.traits["name"],a.sex,a.age])
+        push!(data, [model.date,a.id,a.pos[1],a.pos[2],speciesof(a),a.sex,a.age])
     end
     data
 end
diff --git a/src/nature/macros.jl b/src/nature/macros.jl
index 5d6dcd6..fd8242f 100644
--- a/src/nature/macros.jl
+++ b/src/nature/macros.jl
@@ -350,19 +350,7 @@ Return an iterator over all conspecific animals in the given radius around this
 excluding itself. This can only be used nested within [`@phase`](@ref).
 """
 macro neighbours(args...)
-    :(neighbours($(esc(:self)), $(esc(:model)); $(map(esc, args)...)))
-end
-
-"""
-    @countanimals(radius=0, species="")
-
-Count the number of animals of the given species in this location.
-This is a utility wrapper that can only be used nested within
-[`@phase`](@ref) or [`@habitat`](@ref).
-"""
-macro countanimals(args...)
-    #XXX do I want/need this?
-    :(countanimals($(esc(:pos)), $(esc(:model)); $(map(esc, args)...)))
+    :(neighbours($(esc(:self)), $(esc(:model)), $(map(esc, args)...)))
 end
 
 ##TODO test movement macros
diff --git a/src/nature/nature.jl b/src/nature/nature.jl
index 11975b6..2742a8d 100644
--- a/src/nature/nature.jl
+++ b/src/nature/nature.jl
@@ -39,17 +39,20 @@ function speciesof(a::Animal)
 end
 
 """
-    speciestype(animal)
+    speciestype(name)
 
 Return the Type of this species.
 """
-function speciestype(species::String)
+function speciestype(name::String)
     # get the species Type from its namestring by looking in the module namespace
     speciestype = nothing
-    for name in names(Persefone, all=true)
-        string(name) == species && (speciestype = getfield(Persefone, name))
+    for var in names(Persefone, all=true)
+        if string(var) == name && getfield(Persefone, var) isa Type
+            speciestype = getfield(Persefone, var)
+            break
+        end
     end
-    isnothing(speciestype) && @error("Species $species is not defined.")
+    isnothing(speciestype) && @error("Species $name is not defined.")
     speciestype
 end
     
diff --git a/src/nature/populations.jl b/src/nature/populations.jl
index e7a8818..98cece6 100644
--- a/src/nature/populations.jl
+++ b/src/nature/populations.jl
@@ -214,26 +214,15 @@ function nearby_animals(pos::Tuple{Int64,Int64}, model::SimulationModel;
 end
 
 """
-    neighbours(animal, model; radius=0)
+    neighbours(animal, model, radius=0)
 
 Return a list of conspecific animals in the given radius around this animal, excluding itself.
 """
-function neighbours(animal::Animal, model::SimulationModel; radius::Int64=0)
+function neighbours(animal::Animal, model::SimulationModel, radius::Int64=0)
     filter(a -> a.id != animal.id,
            nearby_animals(animal.pos, model, radius=radius, species=speciesof(animal)))
 end
 
-"""
-    countanimals(pos, model; radius= 0, species="")
-
-Count the number of animals in this location (optionally supplying a species name and radius).
-"""
-function countanimals(pos::Tuple{Int64,Int64}, model::SimulationModel;
-                      radius::Int64=0, species="")
-    #XXX do I want/need this?
-    length(nearby_animals(pos, model, radius=radius, species=species))
-end
-
 """
     followanimal!(follower, leader, model, distance=0)
 
diff --git a/src/nature/species/wolpertinger.jl b/src/nature/species/wolpertinger.jl
index 02ea9c4..cee2c3b 100644
--- a/src/nature/species/wolpertinger.jl
+++ b/src/nature/species/wolpertinger.jl
@@ -27,8 +27,7 @@ and occasionally reproduce by spontaneous parthenogenesis...
         #walk!(animal, direction, model; ifempty=false)
     end
     
-    if @rand() < self.fecundity &&
-        @countanimals(species="Wolpertinger") < self.crowding
+    if @rand() < self.fecundity && length(@neighbours(10)) < self.crowding
         @reproduce()
     end
 
-- 
GitLab