| import numpy as np
|
| import pandas as pd
|
| from ..entities.paramStruct import ParamStruct
|
| from .compute_crop_calendar import compute_crop_calendar
|
| from typing import TYPE_CHECKING
|
|
|
| if TYPE_CHECKING:
|
|
|
| from pandas import DataFrame
|
| from aquacrop.entities.clockStruct import ClockStruct
|
| from aquacrop.entities.crop import Crop
|
| from aquacrop.entities.inititalWaterContent import InitialWaterContent
|
| from aquacrop.entities.soil import Soil
|
|
|
| def read_model_parameters(
|
| clock_struct: "ClockStruct",
|
| soil: "Soil",
|
| crop: "Crop",
|
| weather_df: "DataFrame"):
|
|
|
| """
|
| Finalise soil and crop paramaters including planting and harvest dates
|
| save to new object param_struct
|
|
|
|
|
| Arguments:
|
|
|
| clock_struct (ClockStruct): time params
|
|
|
| soil (Soil): soil object
|
|
|
| crop (Crop): crop object
|
|
|
| weather_df (DataFrame): list of datetimes
|
|
|
| Returns:
|
|
|
| clock_struct (ClockStruct): updated time paramaters
|
|
|
| param_struct (ParamStruct): Contains model crop and soil paramaters
|
|
|
| """
|
|
|
| param_struct = ParamStruct()
|
|
|
| soil.fill_nan()
|
|
|
|
|
| param_struct.Soil = soil
|
|
|
| while soil.zSoil < crop.Zmax + 0.1:
|
| for i in soil.profile.index[::-1]:
|
| if soil.profile.loc[i, "dz"] < 0.25:
|
| soil.profile.loc[i, "dz"] += 0.1
|
| soil.fill_nan()
|
| break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| crop_list = [crop]
|
| param_struct.CropList = crop_list
|
| param_struct.NCrops = 1
|
|
|
|
|
| sim_start_date = clock_struct.simulation_start_date
|
| sim_end_date = clock_struct.simulation_end_date
|
|
|
| if crop.harvest_date is None:
|
| crop = compute_crop_calendar(
|
| crop,
|
| clock_struct.planting_dates,
|
| clock_struct.simulation_start_date,
|
| clock_struct.simulation_end_date,
|
| clock_struct.time_span,
|
| weather_df,
|
| )
|
| mature = int(crop.MaturityCD + 30)
|
| plant = pd.to_datetime("1990/" + crop.planting_date)
|
| harv = plant + np.timedelta64(mature, "D")
|
| new_harvest_date = str(harv.month) + "/" + str(harv.day)
|
| crop.harvest_date = new_harvest_date
|
|
|
|
|
| start_end_years = [sim_start_date.year, sim_end_date.year]
|
|
|
|
|
|
|
| single_year = pd.to_datetime("1990/" + crop.planting_date) < pd.to_datetime(
|
| "1990/" + crop.harvest_date
|
| )
|
|
|
| if single_year:
|
|
|
|
|
|
|
| mock_simulation_end_date = pd.to_datetime("1990/" + f'{sim_end_date.month}' + "/" + f'{sim_end_date.day}')
|
| mock_simulation_start_date = pd.to_datetime("1990/" + crop.planting_date)
|
| last_simulation_year_does_not_start = mock_simulation_end_date <= mock_simulation_start_date
|
|
|
| if last_simulation_year_does_not_start:
|
| start_end_years[1] = start_end_years[1] - 1
|
|
|
|
|
| plant_years = list(range(start_end_years[0], start_end_years[1] + 1))
|
| harvest_years = plant_years
|
| else:
|
|
|
|
|
|
|
| if (
|
| pd.to_datetime(str(start_end_years[1] + 2) + "/" + crop.harvest_date)
|
| < sim_end_date
|
| ):
|
|
|
|
|
| plant_years = list(range(start_end_years[0], start_end_years[1] + 1))
|
| harvest_years = list(range(start_end_years[0] + 1, start_end_years[1] + 2))
|
| else:
|
|
|
| plant_years = list(range(start_end_years[0], start_end_years[1]))
|
| harvest_years = list(range(start_end_years[0] + 1, start_end_years[1] + 1))
|
|
|
|
|
|
|
| if (
|
| pd.to_datetime(str(plant_years[0]) + "/" + crop.planting_date)
|
| < clock_struct.simulation_start_date
|
| ):
|
|
|
| plant_years = plant_years[1:]
|
| harvest_years = harvest_years[1:]
|
|
|
|
|
| assert len(plant_years) == len(harvest_years)
|
|
|
|
|
| planting_dates = []
|
| harvest_dates = []
|
| crop_choices = []
|
|
|
|
|
| for i, _ in enumerate(plant_years):
|
| planting_dates.append(
|
| str(plant_years[i]) + "/" + param_struct.CropList[0].planting_date
|
| )
|
| harvest_dates.append(
|
| str(harvest_years[i]) + "/" + param_struct.CropList[0].harvest_date
|
| )
|
| crop_choices.append(param_struct.CropList[0].Name)
|
|
|
|
|
| param_struct.CropChoices = list(crop_choices)
|
|
|
|
|
| clock_struct.planting_dates = pd.to_datetime(planting_dates)
|
| clock_struct.harvest_dates = pd.to_datetime(harvest_dates)
|
| clock_struct.n_seasons = len(planting_dates)
|
|
|
|
|
| if pd.to_datetime(clock_struct.step_start_time) == clock_struct.planting_dates[0]:
|
| clock_struct.season_counter = 0
|
| else:
|
| clock_struct.season_counter = -1
|
|
|
|
|
| return clock_struct, param_struct
|
|
|