diff --git a/CHANGELOG.md b/CHANGELOG.md index 02f4db6a62c29aed709a41a5f1fb5ae4922486db..a9d18b0df29d45fa82b13bec6b9c2fff70e7037c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,22 +10,28 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 *Aim: 3 species, 2 crop growth models, farm model, GAEC scenarios, experimental analysis* -## [0.6.0] - in planning +## [0.6.0] - unreleased *Plan: decouple CairoMakie (#81), fix & test ALMaSS, set up first experiments* ### Added +- `crop.cropdirectory` parameter specifies folder in which all crop data files for the + selected crop model can be found. + ### Changed -- `preprocessparameters()` checks whether the map directory is reachable from the current working - directory. If not, it checks whether it can be reached from the package directory. This makes - running simulations easier when Persefone has been installed as a package. +- `preprocessparameters()` checks whether the map and crop directories are reachable from the + current working directory. If not, it checks whether it can be reached from the package directory. + This makes running simulations easier when Persefone has been installed as a package. ### Deprecated ### Removed +- `crop.cropfile` and `crop.growthfile` parameters -> user configuration is now done via + `crop.cropdirectory`, names of ALMaSS input files are specified as constants in `almass.jl` + ### Fixed --- diff --git a/data/crops/almass/almass_crop_growth_curves.csv b/data/crops/almass/crop_growth_curves.csv similarity index 100% rename from data/crops/almass/almass_crop_growth_curves.csv rename to data/crops/almass/crop_growth_curves.csv diff --git a/src/core/input.jl b/src/core/input.jl index 26478351958dfaa3729f337ea9bc008a64034762..803c9ab8121aaec2abcbd9de2a6c6f0e620c5424 100644 --- a/src/core/input.jl +++ b/src/core/input.jl @@ -85,24 +85,35 @@ Take the raw input parameters and process them where necessary (e.g. convert typ perform checks). This is a helper function for [`getsettings`](@ref). """ function preprocessparameters(settings::Dict{String,Any}, defaultoutdir::String) + # miscellaneous processing (settings["core.seed"] == 0) && (settings["core.seed"] = abs(rand(RandomDevice(), Int32))) + settings["world.mapresolution"] = settings["world.mapresolution"] * 1m + # create a standardised name for the output directory if settings["core.outdir"] == defaultoutdir outdir = defaultoutdir*"_"*string(Dates.today())*"_s"*string(settings["core.seed"]) settings["core.outdir"] = outdir end - if settings["core.startdate"] > settings["core.enddate"] - Base.error("Enddate is earlier than startdate.") #TODO replace with exception - end + # check if input data are present in the working directory or the package directory if !isdir(settings["world.mapdirectory"]) - #TODO do equivalent check for crop directory, once this is implemented - # check if it's relative to the package directory if isdir(abspath(pkgdir(@__MODULE__), settings["world.mapdirectory"])) settings["world.mapdirectory"] = abspath(pkgdir(@__MODULE__), settings["world.mapdirectory"]) + @debug "Using package directory to load map data: $(settings["world.mapdirectory"])". else Base.error("Couldn't find map directory $(settings["world.mapdirectory"]).") end end - settings["world.mapresolution"] = settings["world.mapresolution"] * 1m + if !isdir(settings["crop.cropdirectory"]) + if isdir(abspath(pkgdir(@__MODULE__), settings["crop.cropdirectory"])) + settings["crop.cropdirectory"] = abspath(pkgdir(@__MODULE__), settings["crop.cropdirectory"]) + @debug "Using package directory to load crop data: $(settings["crop.cropdirectory"])". + else + Base.error("Couldn't find map directory $(settings["crop.cropdirectory"]).") + end + end + # sanity checks + if settings["core.startdate"] > settings["core.enddate"] + Base.error("Enddate is earlier than startdate.") #TODO replace with exception + end if !(settings["crop.cropmodel"] in AVAILABLE_CROPMODELS) error("crop.cropmodel = \"$(settings["crop.cropmodel"])\", but has to be one of: $AVAILABLE_CROPMODELS") end diff --git a/src/core/simulation.jl b/src/core/simulation.jl index 5d150d541db8b52cf919c9e8c7bd0f2afebc8d82..4d227a07d636019a1a3aacfcf75a50d7c2d7ce8a 100644 --- a/src/core/simulation.jl +++ b/src/core/simulation.jl @@ -117,8 +117,7 @@ function initmodel(settings::Dict{String, Any}) settings["core.startdate"], settings["core.enddate"]) crops, Tcroptype, Tcropstate = initcropmodel(settings["crop.cropmodel"], - settings["crop.cropfile"], - settings["crop.growthfile"]) + settings["crop.cropdirectory"]) farmers = Vector{Farmer}() farmplots = Vector{FarmPlot{Tcropstate}}() model = AgricultureModel{Tcroptype,Tcropstate}( diff --git a/src/crop/almass.jl b/src/crop/almass.jl index eb7db59037c188e707de12c30e65eb43c0aacf49..a3d47c238c953b703ad3dccd72b6eac9d0efec7d 100644 --- a/src/crop/almass.jl +++ b/src/crop/almass.jl @@ -11,6 +11,10 @@ module ALMaSS +## The two input files required by ALMaSS. Must be present in the crop directory. +const CROPFILE = "crop_data_general.csv" +const GROWTHFILE = "crop_growth_curves.csv" + using Persefone: AnnualDate, Management, @@ -152,16 +156,16 @@ function buildgrowthcurve(data::Vector{CSV.Row}) end """ - readcropparameters(generalcropfile, cropgrowthfile) + readcropparameters(cropdirectory) Parse a CSV file containing the required parameter values for each crop (as produced from the original ALMaSS file by `convert_almass_data.py`). """ -function readcropparameters(generalcropfile::String, growthfile::String) +function readcropparameters(cropdirectory::String) @debug "Reading crop parameters" - cropdata = CSV.File(generalcropfile, missingstring="NA", + cropdata = CSV.File(joinpath(cropdirectory, CROPFILE), missingstring="NA", types=[String,AnnualDate,AnnualDate,AnnualDate,AnnualDate,Float64,String]) - growthdata = CSV.File(growthfile, missingstring="NA", + growthdata = CSV.File(joinpath(cropdirectory, GROWTHFILE), missingstring="NA", types=[Int,String,String,GrowthPhase,String, Float64,Float64,Float64,Float64]) croptypes = Dict{String,CropType}() diff --git a/src/crop/cropmodels.jl b/src/crop/cropmodels.jl index 8653a1439e8d0a5dcfe14b09c326ef9a4a0a3bad..83497fd176c08ea39f33e4c618fc37d624a57bcc 100644 --- a/src/crop/cropmodels.jl +++ b/src/crop/cropmodels.jl @@ -9,11 +9,11 @@ Initialise the crop model. Returns the crop types available in the simulation, as well as the types `Tcroptype` and `Tcropstate`. """ -function initcropmodel(cropmodel::AbstractString, cropfile::AbstractString, growthfile::AbstractString) +function initcropmodel(cropmodel::AbstractString, cropdirectory::AbstractString) if cropmodel == "almass" Tcroptype = ALMaSS.CropType Tcropstate = ALMaSS.CropState - crops = ALMaSS.readcropparameters(cropfile, growthfile) + crops = ALMaSS.readcropparameters(cropdirectory) elseif cropmodel == "simple" Tcroptype = SimpleCrop.CropType Tcropstate = SimpleCrop.CropState diff --git a/src/nature/species/skylark.jl b/src/nature/species/skylark.jl index feaa063a7be5e538c88895f2bef913bc8d06e965..811301065aa4a51a0982c3da05003642d98ce2fc 100644 --- a/src/nature/species/skylark.jl +++ b/src/nature/species/skylark.jl @@ -163,7 +163,7 @@ Females returning from migration move around to look for a suitable partner with # ...check if he is still alive and the female wants to stay with him #XXX is mate-faithfulness decided by the female when she returns, # or by the male taking whichever female comes to him first? - @debug "$(animalid(self)) and $(animalid(n)) have mated again." + @debug "$(animalid(self)) and $(self.mate) have mated again." @setphase(nesting) return else diff --git a/src/parameters.toml b/src/parameters.toml index 5b742db393bc02a17f13f33895adf18443b11cc0..0cb6ea75c6cce17d5fe3cf81f286801ca19f5732 100644 --- a/src/parameters.toml +++ b/src/parameters.toml @@ -46,6 +46,5 @@ insectmodel = ["season", "habitat", "pesticides", "weather"] # factors affecting [crop] cropmodel = "almass" # crop growth model to use, "almass", "aquacrop", or "simple" -cropfile = "data/crops/almass/crop_data_general.csv" # file with general crop parameters -growthfile = "data/crops/almass/almass_crop_growth_curves.csv" # file with crop growth parameters +cropdirectory = "data/crops/almass" # the directory storing all data files for the selected crop model