Skip to content
Snippets Groups Projects
Select Git revision
  • 3b06f1bd576ab7a6d77b304593de389651654b34
  • main default protected
2 results

.gitattributes

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    farmdata.jl 1.47 KiB
    ### Persefone.jl - a model of agricultural landscapes and ecosystems in Europe.
    ###
    ### This file handles the data output of the farm submodel.
    ###    
    
    """
        initfarmdata()
    
    Create output files for each data group collected by the farm model.
    """
    function initfarmdata(model::SimulationModel)
        newdataoutput!(model, "fields", ["Date", "Crop", "Area", "Height"],
                       @param(farm.fieldoutfreq), savefielddata, croptrends)
        #XXX add later: income per year
    end
    
    """
        savefielddata(model)
    
    Return a data table (to be printed to `fields.csv`), giving the current
    date, and the area and average of each crop in the landscape. May be called
    never, daily, monthly, yearly, or at the end of a simulation, depending on
    the parameter `farm.fieldoutfreq`.
    """
    function savefielddata(model::SimulationModel)
        croparea = Dict(c => 0.0 for c in keys(model.crops))
        cropheights = Dict(c => [0.0, 0.0] for c in keys(model.crops))
        for f in model.farmplots
            c = cropname(f)
            croparea[c] += ustrip(ha, @areaof(length(f.pixels))) # total area with this crop
            cropheights[c][1] += 1 # number of fields with this crop
            cropheights[c][2] += ustrip(cm, cropheight(f)) # summed height of the plants of this crop
        end
        data = []
        for p in keys(croparea)
            averageheight = cropheights[p][2] == 0 ? 0 :
                round(cropheights[p][2]/cropheights[p][1], digits=2)
            push!(data, [model.date, p, croparea[p], averageheight])
        end
        data
    end