Skip to content
Snippets Groups Projects
Commit df23cc87 authored by Marco Matthies's avatar Marco Matthies
Browse files

Fix AquaCrop cropfield setup when there are not enough data points

parent c912735d
No related branches found
No related tags found
No related merge requests found
...@@ -108,6 +108,8 @@ end ...@@ -108,6 +108,8 @@ end
function make_aquacrop_cropfield(croptype::CropType, function make_aquacrop_cropfield(croptype::CropType,
model::SimulationModel, model::SimulationModel,
soiltype::SoilType) soiltype::SoilType)
# TODO: put this constant somewhere else?
MIN_DATASET_LEN = 366
# TODO: get this mapping for soil types from a CSV file? # TODO: get this mapping for soil types from a CSV file?
soiltype_to_aquacrop = Dict(soiltype => replace(string(soiltype), "_" => " ") soiltype_to_aquacrop = Dict(soiltype => replace(string(soiltype), "_" => " ")
for soiltype in instances(SoilType)) for soiltype in instances(SoilType))
...@@ -118,10 +120,19 @@ function make_aquacrop_cropfield(croptype::CropType, ...@@ -118,10 +120,19 @@ function make_aquacrop_cropfield(croptype::CropType,
start_daynum = daynumber(model.weather, start_date) start_daynum = daynumber(model.weather, start_date)
end_daynum = daynumber(model.weather, end_date) end_daynum = daynumber(model.weather, end_date)
dayrange = start_daynum:end_daynum dayrange = start_daynum:end_daynum
input_Tmin = @view model.weather.mintemp[dayrange] numdays = length(dayrange)
input_Tmax = @view model.weather.maxtemp[dayrange] if numdays >= MIN_DATASET_LEN
input_ETo = @view model.weather.evapotranspiration[dayrange] input_Tmin = @view model.weather.mintemp[dayrange]
input_Rain = @view model.weather.precipitation[dayrange] input_Tmax = @view model.weather.maxtemp[dayrange]
input_ETo = @view model.weather.evapotranspiration[dayrange]
input_Rain = @view model.weather.precipitation[dayrange]
else
# Append fake data so that there are always more than MIN_DATASET_LEN datapoints
input_Tmin = append!(model.weather.mintemp[dayrange], fill(10.0, MIN_DATASET_LEN - numdays))
input_Tmax = append!(model.weather.maxtemp[dayrange], fill(16.0, MIN_DATASET_LEN - numdays))
input_ETo = append!(model.weather.evapotranspiration[dayrange], fill(1.0, MIN_DATASET_LEN - numdays))
input_Rain = append!(model.weather.precipitation[dayrange], fill(1.0, MIN_DATASET_LEN - numdays))
end
# Generate the keyword object for the AquaCrop simulation # Generate the keyword object for the AquaCrop simulation
kwargs = ( kwargs = (
...@@ -163,8 +174,8 @@ mutable struct CropState <: AbstractCropState ...@@ -163,8 +174,8 @@ mutable struct CropState <: AbstractCropState
soiltype::SoilType soiltype::SoilType
cropstate::AquaCrop.AquaCropField cropstate::AquaCrop.AquaCropField
function CropState(croptype::CropType, soiltype::SoilType, function CropState(croptype::CropType, model::SimulationModel,
model::SimulationModel, height::Tlength=0.0cm) soiltype::SoilType, height::Tlength=0.0cm)
aquacrop_cropfield = make_aquacrop_cropfield(croptype, model, soiltype) aquacrop_cropfield = make_aquacrop_cropfield(croptype, model, soiltype)
return new(croptype, height, soiltype, aquacrop_cropfield) return new(croptype, height, soiltype, aquacrop_cropfield)
end end
...@@ -180,7 +191,7 @@ function isharvestable(cs::CropState) ...@@ -180,7 +191,7 @@ function isharvestable(cs::CropState)
return !ismissing(stage) && stage == 0 return !ismissing(stage) && stage == 0
end end
makecropstate(croptype::CropType, model::SimulationModel, soiltype::SoilType) = makecropstate(croptype::CropType, model::SimulationModel, soiltype::SoilType) =
CropState(croptype, soiltype, model) CropState(croptype, model, soiltype)
function setsoiltype!(cs::CropState, soiltype::SoilType) function setsoiltype!(cs::CropState, soiltype::SoilType)
# TODO: this does not affect the actual soil type of the state of # TODO: this does not affect the actual soil type of the state of
# the aquacrop simulation # the aquacrop simulation
......
...@@ -65,7 +65,7 @@ end ...@@ -65,7 +65,7 @@ end
pixels = [(0, 0)] pixels = [(0, 0)]
farmer = 0 farmer = 0
soiltype = Ps.sand soiltype = Ps.sand
fp = FarmPlot(id, pixels, farmer, soiltype, PsAC.CropState(ct, soiltype, model)) fp = FarmPlot(id, pixels, farmer, soiltype, PsAC.CropState(ct, model, soiltype))
@test fp isa FarmPlot @test fp isa FarmPlot
@test fp.cropstate isa PsAC.CropState @test fp.cropstate isa PsAC.CropState
@test croptype(fp) isa PsAC.CropType @test croptype(fp) isa PsAC.CropType
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment