From c438f0ce84afa147aac7d8bb84c9032b916b92f9 Mon Sep 17 00:00:00 2001 From: Daniel Vedder <daniel.vedder@idiv.de> Date: Thu, 27 Jul 2023 11:20:20 +0200 Subject: [PATCH] Wrote weather.jl --- CONTRIBUTORS.md | 2 +- Manifest.toml | 28 ++++++----- Project.toml | 3 +- src/Persefone.jl | 5 +- src/core/input.jl | 2 +- src/core/simulation.jl | 7 ++- src/parameters.toml | 1 + src/world/weather.jl | 105 ++++++++++++++++++++++++++++++++++++++++- 8 files changed, 135 insertions(+), 18 deletions(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index fc099a9..66c7ebd 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -4,7 +4,7 @@ - Daniel Vedder -- Lea Kolb +- (Lea Kolb) ### Code reviewers diff --git a/Manifest.toml b/Manifest.toml index b0f2721..b441b3b 100644 --- a/Manifest.toml +++ b/Manifest.toml @@ -1,8 +1,8 @@ # This file is machine-generated - editing it directly is not advised -julia_version = "1.9.0-alpha1" +julia_version = "1.9.1" manifest_format = "2.0" -project_hash = "7ba06fde8b5defbbec66accfbf1488a9d06b6954" +project_hash = "95079802d452de8f9a12096a3facc5e629c3d6d3" [[deps.AbstractFFTs]] deps = ["ChainRulesCore", "LinearAlgebra"] @@ -105,10 +105,10 @@ uuid = "fa961155-64e5-5f13-b03f-caf6b980ea82" version = "0.4.2" [[deps.CSV]] -deps = ["CodecZlib", "Dates", "FilePathsBase", "InlineStrings", "Mmap", "Parsers", "PooledArrays", "SentinelArrays", "SnoopPrecompile", "Tables", "Unicode", "WeakRefStrings", "WorkerUtilities"] -git-tree-sha1 = "8c73e96bd6817c2597cfd5615b91fca5deccf1af" +deps = ["CodecZlib", "Dates", "FilePathsBase", "InlineStrings", "Mmap", "Parsers", "PooledArrays", "PrecompileTools", "SentinelArrays", "Tables", "Unicode", "WeakRefStrings", "WorkerUtilities"] +git-tree-sha1 = "44dbf560808d49041989b8a96cae4cffbeb7966a" uuid = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b" -version = "0.10.8" +version = "0.10.11" [[deps.Calculus]] deps = ["LinearAlgebra"] @@ -184,7 +184,7 @@ version = "4.5.0" [[deps.CompilerSupportLibraries_jll]] deps = ["Artifacts", "Libdl"] uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae" -version = "1.0.1+0" +version = "1.0.2+0" [[deps.CompositionsBase]] git-tree-sha1 = "455419f7e328a1a2493cabc6428d79e951349769" @@ -683,7 +683,7 @@ version = "1.1.7" [[deps.MbedTLS_jll]] deps = ["Artifacts", "Libdl"] uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1" -version = "2.28.0+0" +version = "2.28.2+0" [[deps.Meshes]] deps = ["CategoricalArrays", "CircularArrays", "Distances", "IterTools", "IteratorInterfaceExtensions", "LinearAlgebra", "NearestNeighbors", "Random", "RecipesBase", "ReferenceFrameRotations", "SimpleTraits", "SparseArrays", "SpecialFunctions", "StaticArrays", "StatsBase", "TableTraits", "Tables"] @@ -764,7 +764,7 @@ version = "1.12.8" [[deps.OpenBLAS_jll]] deps = ["Artifacts", "CompilerSupportLibraries_jll", "Libdl"] uuid = "4536629a-c528-5b80-bd46-f80d51c5b363" -version = "0.3.21+0" +version = "0.3.21+4" [[deps.OpenJpeg_jll]] deps = ["Artifacts", "JLLWrappers", "Libdl", "Libtiff_jll", "LittleCMS_jll", "Pkg", "libpng_jll"] @@ -839,7 +839,7 @@ version = "2.5.2" [[deps.Pkg]] deps = ["Artifacts", "Dates", "Downloads", "FileWatching", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"] uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" -version = "1.8.0" +version = "1.9.0" [[deps.PooledArrays]] deps = ["DataAPI", "Future"] @@ -853,6 +853,12 @@ git-tree-sha1 = "17275485f373e6673f7e7f97051f703ed5b15b20" uuid = "85a6dd25-e78a-55b7-8502-1745935b8125" version = "0.2.4" +[[deps.PrecompileTools]] +deps = ["Preferences"] +git-tree-sha1 = "9673d39decc5feece56ef3940e5dafba15ba0f81" +uuid = "aea7be01-6a6a-4083-8856-8a6e6704d82a" +version = "1.1.2" + [[deps.Preferences]] deps = ["TOML"] git-tree-sha1 = "47e5f437cc0e7ef2ce8406ce1e7e24d44915f88d" @@ -1104,7 +1110,7 @@ uuid = "4607b0f0-06f3-5cda-b6b1-a6196a1729e9" [[deps.SuiteSparse_jll]] deps = ["Artifacts", "Libdl", "Pkg", "libblastrampoline_jll"] uuid = "bea87d4a-7f5b-5778-9afe-8cc45184846c" -version = "5.10.1+0" +version = "5.10.1+6" [[deps.TOML]] deps = ["Dates"] @@ -1239,7 +1245,7 @@ version = "1.76.0+1" [[deps.libblastrampoline_jll]] deps = ["Artifacts", "Libdl"] uuid = "8e850b90-86db-534c-a0d3-1478176c7d93" -version = "5.2.0+0" +version = "5.8.0+0" [[deps.libgeotiff_jll]] deps = ["Artifacts", "JLLWrappers", "LibCURL_jll", "Libdl", "Libtiff_jll", "PROJ_jll", "Pkg"] diff --git a/Project.toml b/Project.toml index d02153b..8c1f7e3 100644 --- a/Project.toml +++ b/Project.toml @@ -1,11 +1,12 @@ name = "Persefone" uuid = "039acd1d-2a07-4b33-b082-83a1ff0fd136" authors = ["Daniel Vedder <daniel.vedder@idiv.de>"] -version = "0.2" +version = "0.2.0" [deps] Agents = "46ada45e-f475-11e8-01d0-f70cc89e6671" ArgParse = "c7e460c6-2fb9-53a9-8c5b-16f535851c63" +CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b" Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b" GeoArrays = "2fb1d81b-e6a0-5fc5-82e6-8e06903437ab" diff --git a/src/Persefone.jl b/src/Persefone.jl index 0c76d61..1164f18 100644 --- a/src/Persefone.jl +++ b/src/Persefone.jl @@ -1,7 +1,6 @@ ### Persefone - a socio-economic-ecological model of European agricultural landscapes. ### ### Daniel Vedder <daniel.vedder@idiv.de> -### Lea Kolb <lea-deborah.kolb@idiv.de> ### (c) 2022-2023, licensed under the terms of the MIT license ### ### This file defines the module/package for the Persefone model. @@ -15,6 +14,7 @@ module Persefone using Agents, ArgParse, + CSV, Dates, Distributed, GeoArrays, #XXX this is a big dependency - can we get rid of it? @@ -24,7 +24,7 @@ using StableRNGs, TOML -## Packages to check out: +## Packages that may be useful later on: # MacroTools, http://fluxml.ai/MacroTools.jl/stable/utilities/ # Debugger, https://github.com/JuliaDebug/Debugger.jl # Makie, https://docs.makie.org/stable/ @@ -37,6 +37,7 @@ using export #types Pixel, + Weather, FarmEvent, FarmPlot, Animal, diff --git a/src/core/input.jl b/src/core/input.jl index b08dc61..f09117c 100644 --- a/src/core/input.jl +++ b/src/core/input.jl @@ -142,7 +142,7 @@ Certain software parameters can be set via the commandline. function parsecommandline() versionstring = """ Persefone $(pkgversion(Persefone)) - © 2022-2023 Daniel Vedder, Lea Kolb (MIT license) + © 2022-2023 Daniel Vedder (MIT license) https://git.idiv.de/xo30xoqa/persefone """ s = ArgParseSettings(add_version=true, version=versionstring) diff --git a/src/core/simulation.jl b/src/core/simulation.jl index 18c0dd0..cff0692 100644 --- a/src/core/simulation.jl +++ b/src/core/simulation.jl @@ -64,12 +64,17 @@ function initmodel(settings::Dict{String, Any}) with_logger(logger) do events = Vector{FarmEvent}() dataoutputs = Vector{DataOutput}() - landscape = initlandscape(settings["world.landcovermap"], settings["world.farmfieldsmap"]) + landscape = initlandscape(settings["world.landcovermap"], + settings["world.farmfieldsmap"]) + weather = initweather(settings["world.weatherfile"], + settings["core.startdate"], + settings["core.enddate"]) space = GridSpace(size(landscape), periodic=false) properties = Dict{Symbol,Any}(:settings=>settings, :logger=>logger, :date=>settings["core.startdate"], :landscape=>landscape, + :weather=>weather, :dataoutputs=>dataoutputs, :events=>events) model = AgentBasedModel(Union{Farmer,Animal,FarmPlot}, space, properties=properties, diff --git a/src/parameters.toml b/src/parameters.toml index abd543d..172575d 100644 --- a/src/parameters.toml +++ b/src/parameters.toml @@ -21,6 +21,7 @@ enddate = 2022-12-31 [world] landcovermap = "data/landcover_jena.tif" # location of the landcover map farmfieldsmap = "data/fields_jena.tif" # location of the field geometry map +weatherfile = "data/weather_jena.csv" # location of the weather data file [farm] farmmodel = "FieldManager" # which version of the farm model to use (not yet implemented) diff --git a/src/world/weather.jl b/src/world/weather.jl index 2a66875..9855d13 100644 --- a/src/world/weather.jl +++ b/src/world/weather.jl @@ -4,4 +4,107 @@ ### to the rest of the model. ### -##TODO (issue #8) +##TODO write tests + +""" + Weather + +A single weather datum, combining the observations from one day. +""" +struct Weather + windspeed::Float64 + precipitation::Float64 + sunshine::Float64 + vapourpressure::Float64 + meantemp::Float64 + maxtemp::Float64 + mintemp::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) + 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 + 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 -- GitLab