gaia / retrieve_coefs_max_yield.py
mohamed.tsouli
fix lower case culture
6394e9c
import pandas as pd
def get_coefs_Kc_Ky_and_max_yield(culture: str, region: str = "Bourgogne-Franche-Comté"):
"""
Get the crop coefficient (Kc) per month, yield response factor (Ky) per month, and maximum yield (Yx) for a specific crop.
Args:
region (str): Region of the crop (default: "Bourgogne-Franche-Comté")
culture (str): Crop name ["Colza d'hiver", "Orge d'hiver", "Blé tendre d'hiver"]
Returns:
DataFrame: Crop coefficients (Kc) per month, yield response factors (Ky) per month
Yx : Maximum yield (quintal/ha)
"""
#CHECK THE REGION AND CROP
if region not in ["Bourgogne-Franche-Comté"]:
raise ValueError("The data region is not available.")
if culture.lower() not in ["colza d'hiver", "orge d'hiver", "blé tendre d'hiver"]:
raise ValueError("The crop is not available.")
if culture.lower()=="colza d'hiver":
culture_yield = "Colza grain d'hiver"
else:
culture_yield = culture
# read the csv file
df_coef = pd.read_csv("data/data_yield/Coefs_yield_franche_comte.csv", sep =";")
# filter the region
df_coef = df_coef[df_coef["Région"]==region]
df_coef["Culture"] = df_coef["Culture"].str.lower()
# filter the crop
df_coef = df_coef[df_coef["Culture"]==culture.lower()]
# drop the columns "Stade de développement"
df_coef = df_coef.drop(columns=["Stade de développement","Région","Culture"])
# retrieve the maximum yield
# read the csv file
df_yield = pd.read_csv("data/data_yield/data_rendement.csv")
# filter the region
df_yield = df_yield[df_yield["LIB_REG2"]==region]
# filter the crop
# check if the culture name is part of the string "LIB_SAA"
df_yield["LIB_SAA"] = df_yield["LIB_SAA"].str.lower()
isdf_yield = df_yield["LIB_SAA"].str.contains(culture_yield.lower())
# filter the crop
df_yield = df_yield[isdf_yield]
# calculate the max yield from columns REND_2010 to REND_2023
columns_rend= [f"REND_{i}" for i in range(2010,2024)]
yield_max = df_yield[columns_rend].max(axis=1) *1.2
return df_coef, yield_max.iloc[0]
if __name__ == "__main__":
# Exemple d'utilisation
region = "Bourgogne-Franche-Comté"
culture = "blé tendre d'hiver"
df_coef, yield_max = get_coefs_Kc_Ky_and_max_yield(culture, region)
print(df_coef)
print(yield_max)