### Persefone - a socio-economic-ecological model of European agricultural landscapes.
###
### This file contains submodel that calculates insect biomass
###

##TODO write tests for this

"""
    insectbiomass(pixel, model)

Calculate the insect biomass in this location, using the factors configured
in the `nature.insectmodel` settings (any of: "season", "habitat", "weather",
"pesticides"). Returns a float value in mg/m².

**Biological note:** this is a very approximate calculation! Insect biomass
varies wildly in time and space and is hard to measure. This calculation is
based on the idea of a parabolic seasonal development of insect abundance,
modified habitat suitability, weather, and pesticide application. Although it
is based on empirical studies, it can only deliver a rough, order-of-magnitude
estimation of likely insect biomass in a given location.

**Sources:**

- Odderskær et al. (1997). Skylark Reproduction in Pesticide Treated and
Untreated Fields (32; Pesticides Research). Danish Environmental Protection Agency.
- Grüebler et al. (2008). A predictive model of the density of airborne
insects in agricultural environments. Agriculture, Ecosystems & Environment,
123(1), 75–80. https://doi.org/10.1016/j.agee.2007.05.001
- Paquette et al. (2013). Seasonal patterns in Tree Swallow prey (Diptera)
abundance are affected by agricultural intensification. Ecological Applications,
23(1), 122–133. https://doi.org/10.1890/12-0068.1
- Püttmanns et al. (2022). Habitat use and foraging parameters of breeding
Skylarks indicate no seasonal decrease in food availability in heterogeneous
farmland. Ecology and Evolution, 12(1), e8461. https://doi.org/10.1002/ece3.8461
"""
function insectbiomass(pixel::Pixel, model::AgentBasedModel)::Float64

    ## if no factors are configure, insect abundance defaults to 300 mg/m²,
    ## a value in the upper range of insect biomass density in agricultural landscapes
    baseline = 300.0
    seasonfactor = 0.0
    habitatfactor = 1.0
    weatherfactor = 1.0
    pesticidefactor = 1.0

    ## parabolic curve of seasonal development (peaking on the 6th of July),
    ## based on fig. 3a in Paquette et al. (2013)
    if "season" in @param(nature.insectmodel)
        calendarday = dayofyear(model.date)
        seasonfactor = -0.085(calendarday-187)^2
    end

    ## habitat dependence of insect biomass,
    ## based on fig. 1 in Grübler et al. (2008) and fig. 5c in Püttmanns et al. (2022)
    if "habitat" in @param(nature.insectmodel)
        if pixel.landcover == soil || pixel.landcover == forest
            habitatfactor = 2.0
        elseif pixel.landcover == grass
            habitatfactor = 1.5
        elseif pixel.landcover == agriculture
            habitatfactor = 1.0
        else
            habitatfactor = 0.0
        end
    end

    ## temperature dependence of insect biomass,
    ## based on fig. 3b in Paquette et al. (2013)
    ##XXX (and possibly table 3 in Grübler et al. (2008))
    if "weather" in @param(nature.insectmodel)
        #TODO add this once we have a working weather module
    end

    ## effect of pesticides on insect abundance,
    ## based on figs. 3.6 and 3.7 in Odderskær et al. (1997)
    ## Note that this is a simplification: it ignores that insect biomass
    ## tends to rise rapidly a few weeks after pesticide application.
    if "pesticides" in @param(nature.insectmodel) && pesticide in pixel.events
        pesticidefactor = 0.5
    end

    ## calculate biomass using a parabolic equation in the vertex form
    biomass = seasonfactor+baseline*habitatfactor*pesticidefactor*weatherfactor
    biomass > 0 ? biomass : 0.0
end