Spaces:
Runtime error
Runtime error
Hugo Massonnat
commited on
Commit
·
8ff070c
1
Parent(s):
a8dd096
fix yield computation
Browse files- compute_yield.py +39 -18
- forecast.py +3 -3
compute_yield.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import pandas as pd
|
|
|
|
| 2 |
|
| 3 |
from forecast import get_forecast_data
|
| 4 |
from retrieve_coefs_max_yield import get_coefs_Kc_Ky_and_max_yield
|
|
@@ -19,29 +20,27 @@ def calculate_ETx(Kc, ETo):
|
|
| 19 |
ETx = Kc * ETo
|
| 20 |
return ETx
|
| 21 |
|
| 22 |
-
|
|
|
|
| 23 |
"""
|
| 24 |
Calculate the actual evapotranspiration (ETa) using the maximum evapotranspiration (ETx), soil moisture, field capacity, and wilting point.
|
| 25 |
|
| 26 |
Parameters:
|
| 27 |
ETx (float): Maximum evapotranspiration (mm)
|
| 28 |
-
soil_moisture (
|
| 29 |
field_capacity (float): Field capacity of the soil (%)
|
| 30 |
wilting_point (float): Wilting point of the soil (%)
|
| 31 |
|
| 32 |
Returns:
|
| 33 |
float: Actual evapotranspiration (ETa) in mm
|
| 34 |
"""
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
Ks = ( WA - water_deficit)/ WA # coef de stress hydrique
|
| 43 |
-
ETa = ETx * Ks
|
| 44 |
-
|
| 45 |
return ETa
|
| 46 |
|
| 47 |
|
|
@@ -93,15 +92,17 @@ def compute_yield_forecast(
|
|
| 93 |
|
| 94 |
soil_properties = get_soil_properties(latitude, longitude)
|
| 95 |
|
| 96 |
-
ETo = monthly_forecast["Evaporation (
|
| 97 |
|
| 98 |
ETx = calculate_ETx(Kc, ETo)
|
| 99 |
|
| 100 |
ETa = calculate_ETa(
|
| 101 |
ETx,
|
| 102 |
-
|
| 103 |
soil_properties["field_capacity"],
|
| 104 |
soil_properties["wilting_point"],
|
|
|
|
|
|
|
| 105 |
)
|
| 106 |
|
| 107 |
projected_yield = calculate_yield_projection(
|
|
@@ -124,14 +125,34 @@ def get_annual_yield(monthly_forecast: pd.DataFrame) -> pd.Series:
|
|
| 124 |
|
| 125 |
|
| 126 |
if __name__ == '__main__':
|
|
|
|
|
|
|
|
|
|
| 127 |
monthly_forecast = compute_yield_forecast(
|
| 128 |
latitude=47,
|
| 129 |
longitude=5,
|
| 130 |
-
culture=
|
| 131 |
-
scenario=
|
| 132 |
shading_coef=0.,
|
| 133 |
)
|
| 134 |
-
print(monthly_forecast.head())
|
| 135 |
|
| 136 |
yield_forecast = get_annual_yield(monthly_forecast)
|
| 137 |
-
print(yield_forecast)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
+
from matplotlib import pyplot as plt
|
| 3 |
|
| 4 |
from forecast import get_forecast_data
|
| 5 |
from retrieve_coefs_max_yield import get_coefs_Kc_Ky_and_max_yield
|
|
|
|
| 20 |
ETx = Kc * ETo
|
| 21 |
return ETx
|
| 22 |
|
| 23 |
+
|
| 24 |
+
def calculate_ETa(ETx, soil_moisture, field_capacity, wilting_point, water_deficit, ETo):
|
| 25 |
"""
|
| 26 |
Calculate the actual evapotranspiration (ETa) using the maximum evapotranspiration (ETx), soil moisture, field capacity, and wilting point.
|
| 27 |
|
| 28 |
Parameters:
|
| 29 |
ETx (float): Maximum evapotranspiration (mm)
|
| 30 |
+
soil_moisture (Series): Current soil moisture content (%)
|
| 31 |
field_capacity (float): Field capacity of the soil (%)
|
| 32 |
wilting_point (float): Wilting point of the soil (%)
|
| 33 |
|
| 34 |
Returns:
|
| 35 |
float: Actual evapotranspiration (ETa) in mm
|
| 36 |
"""
|
| 37 |
+
Ks = 1 - (water_deficit / (2 * ETo)) # coef de stress hydrique = precipitation / et0
|
| 38 |
+
Ks = Ks.clip(lower=0, upper=1)
|
| 39 |
+
ETa = ETx * Ks
|
| 40 |
+
|
| 41 |
+
ETa.loc[soil_moisture > field_capacity] = ETx.loc[soil_moisture > field_capacity]
|
| 42 |
+
ETa.loc[soil_moisture < wilting_point] = 0
|
| 43 |
+
|
|
|
|
|
|
|
|
|
|
| 44 |
return ETa
|
| 45 |
|
| 46 |
|
|
|
|
| 92 |
|
| 93 |
soil_properties = get_soil_properties(latitude, longitude)
|
| 94 |
|
| 95 |
+
ETo = monthly_forecast["Evaporation (mm/day)"]
|
| 96 |
|
| 97 |
ETx = calculate_ETx(Kc, ETo)
|
| 98 |
|
| 99 |
ETa = calculate_ETa(
|
| 100 |
ETx,
|
| 101 |
+
monthly_forecast["Moisture in Upper Portion of Soil Column (kg m-2)"],
|
| 102 |
soil_properties["field_capacity"],
|
| 103 |
soil_properties["wilting_point"],
|
| 104 |
+
water_deficit=monthly_forecast["Water Deficit (mm/day)"],
|
| 105 |
+
ETo=ETo,
|
| 106 |
)
|
| 107 |
|
| 108 |
projected_yield = calculate_yield_projection(
|
|
|
|
| 125 |
|
| 126 |
|
| 127 |
if __name__ == '__main__':
|
| 128 |
+
culture = "Colza d'hiver"
|
| 129 |
+
scenario = "pessimist"
|
| 130 |
+
shading_coef = 0.2
|
| 131 |
monthly_forecast = compute_yield_forecast(
|
| 132 |
latitude=47,
|
| 133 |
longitude=5,
|
| 134 |
+
culture=culture,
|
| 135 |
+
scenario=scenario,
|
| 136 |
shading_coef=0.,
|
| 137 |
)
|
| 138 |
+
# print(monthly_forecast.head())
|
| 139 |
|
| 140 |
yield_forecast = get_annual_yield(monthly_forecast)
|
| 141 |
+
# print(yield_forecast)
|
| 142 |
+
|
| 143 |
+
monthly_forecast_with_shading = compute_yield_forecast(
|
| 144 |
+
latitude=47,
|
| 145 |
+
longitude=5,
|
| 146 |
+
culture=culture,
|
| 147 |
+
scenario=scenario,
|
| 148 |
+
shading_coef=shading_coef,
|
| 149 |
+
)
|
| 150 |
+
# print(monthly_forecast_with_shading.head())
|
| 151 |
+
|
| 152 |
+
yield_forecast_with_shading = get_annual_yield(monthly_forecast_with_shading)
|
| 153 |
+
# print(yield_forecast)
|
| 154 |
+
|
| 155 |
+
plt.plot(yield_forecast.rolling(5).mean(), label="No shading")
|
| 156 |
+
plt.plot(yield_forecast_with_shading.rolling(5).mean(), label="20% Shading")
|
| 157 |
+
plt.legend()
|
| 158 |
+
plt.show()
|
forecast.py
CHANGED
|
@@ -133,15 +133,15 @@ def preprocess_forectast_data(df: pd.DataFrame, latitude, longitude, shading_coe
|
|
| 133 |
|
| 134 |
# Compute ET0
|
| 135 |
et0 = compute_et0(preprocessed_data, latitude, longitude)
|
| 136 |
-
preprocessed_data['Evaporation (mm/day)'] = et0
|
| 137 |
|
| 138 |
# Convert Precipitation from kg/m²/s to mm/day
|
| 139 |
preprocessed_data['Precipitation (mm/day)'] = 86400 * preprocessed_data['Precipitation (kg m-2 s-1)']
|
| 140 |
|
| 141 |
# Calculate Water Deficit: Water Deficit = ET0 - P + M
|
| 142 |
preprocessed_data['Water Deficit (mm/day)'] = (
|
| 143 |
-
|
| 144 |
-
|
| 145 |
)
|
| 146 |
|
| 147 |
return preprocessed_data
|
|
|
|
| 133 |
|
| 134 |
# Compute ET0
|
| 135 |
et0 = compute_et0(preprocessed_data, latitude, longitude)
|
| 136 |
+
preprocessed_data['Evaporation (mm/day)'] = et0.clip(lower=0)
|
| 137 |
|
| 138 |
# Convert Precipitation from kg/m²/s to mm/day
|
| 139 |
preprocessed_data['Precipitation (mm/day)'] = 86400 * preprocessed_data['Precipitation (kg m-2 s-1)']
|
| 140 |
|
| 141 |
# Calculate Water Deficit: Water Deficit = ET0 - P + M
|
| 142 |
preprocessed_data['Water Deficit (mm/day)'] = (
|
| 143 |
+
preprocessed_data['Evaporation (mm/day)'] - preprocessed_data['Precipitation (mm/day)']
|
| 144 |
+
# + preprocessed_data['Moisture in Upper Portion of Soil Column (kg m-2)'])
|
| 145 |
)
|
| 146 |
|
| 147 |
return preprocessed_data
|