Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
weather.jl 2.87 KiB
### Persefone.jl - a model of agricultural landscapes and ecosystems in Europe.
###
### This file reads in weather data for the selected location and makes it available
### to the rest of the model.
###

"""
    Weather

A single weather datum, combining the observations from one day.
"""
struct Weather
    windspeed::Union{Missing,Float64}
    precipitation::Union{Missing,Float64}
    sunshine::Union{Missing,Float64}
    vapourpressure::Union{Missing,Float64}
    meantemp::Union{Missing,Float64}
    maxtemp::Union{Missing,Float64}
    mintemp::Union{Missing,Float64}
end

"""
    initweather(weatherfile, startdate, enddate)

Load a weather file, extract the values that are relevant to this model run
(specified by start and end dates), and return a dictionary of Weather objects
mapped to dates.

**Note:** This requires a weather file in the format produced by `data/extract_weather_data.R`.
"""
function initweather(weatherfile::String, startdate::Date, enddate::Date)
    @debug "Initialising weather"
    data = CSV.File(weatherfile, missingstring="NA", dateformat="yyyymmdd",
                    types=[Date, Float64, Float64, Float64, Float64,
                           Float64, Float64, Float64])
    weather = Dict{Date,Weather}()
    for row in data
        if row.date >= startdate && row.date <= enddate
            weather[row.date] = Weather(row.mean_windspeed, row.precipitation,
                                        row.sunshine_hours, row.mean_vapour_pressure,
                                        row.mean_temperature, row.max_temperature,
                                        row.min_temperature)
        end
    end
    if length(weather) <= Dates.value(enddate-startdate)
        @warn "There are missing days in the weather input file."
    end
    weather
end

"""
    windspeed(model)

Return today's average windspeed in m/s.
"""
function windspeed(model::AgentBasedModel)
    model.weather[model.date].windspeed
end

"""
    precipitation(model)

Return today's total precipitation in mm.
"""
function precipitation(model::AgentBasedModel)
    model.weather[model.date].precipitation
end

"""
    sunshine(model)

Return today's sunshine duration in hours.
"""
function sunshine(model::AgentBasedModel)
    model.weather[model.date].sunshine
end

"""
    vapourpressure(model)

Return today's average vapour pressure in hPa.
"""
function vapourpressure(model::AgentBasedModel)
    model.weather[model.date].vapourpressure
end

"""
    meantemp(model)

Return today's mean temperature in °C.
"""
function meantemp(model::AgentBasedModel)
    model.weather[model.date].meantemp
end

"""
    maxtemp(model)

Return today's maximum temperature in °C.
"""
function maxtemp(model::AgentBasedModel)
    model.weather[model.date].maxtemp
end

"""
    mintemp(model)

Return today's minimum temperature in °C.
"""
function mintemp(model::AgentBasedModel)
    model.weather[model.date].mintemp
end