Spaces:
Runtime error
Runtime error
Hugo Massonnat
commited on
Commit
·
196cbb2
1
Parent(s):
a07ee0c
Yield computation
Browse files- compute_yield.py +60 -7
- forecast.py +1 -0
- utils/soil_utils.py +6 -2
compute_yield.py
CHANGED
@@ -1,4 +1,7 @@
|
|
|
|
|
|
1 |
from forecast import get_forecast_data
|
|
|
2 |
from utils.soil_utils import get_soil_properties
|
3 |
|
4 |
|
@@ -52,20 +55,42 @@ def calculate_yield_projection(Yx, ETx, ETa, Ky):
|
|
52 |
Returns:
|
53 |
float: Projected yield (quintal/ha)
|
54 |
"""
|
|
|
55 |
Ya = Yx * (1 - Ky * (1 - ETa / ETx))
|
|
|
|
|
56 |
return round(Ya, 2)
|
57 |
|
58 |
|
59 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
monthly_forecast = get_forecast_data(latitude, longitude, scenario=scenario, shading_coef=shading_coef)
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
65 |
|
66 |
soil_properties = get_soil_properties(latitude, longitude)
|
67 |
|
68 |
-
ETo = monthly_forecast["
|
69 |
|
70 |
ETx = calculate_ETx(Kc, ETo)
|
71 |
|
@@ -76,6 +101,34 @@ def get_yield_forecast(latitude: float, longitude: float, scenario: str = "pessi
|
|
76 |
soil_properties["wilting_point"],
|
77 |
)
|
78 |
|
79 |
-
projected_yield = calculate_yield_projection(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
-
|
|
|
|
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
|
5 |
from utils.soil_utils import get_soil_properties
|
6 |
|
7 |
|
|
|
55 |
Returns:
|
56 |
float: Projected yield (quintal/ha)
|
57 |
"""
|
58 |
+
|
59 |
Ya = Yx * (1 - Ky * (1 - ETa / ETx))
|
60 |
+
Ya.loc[ETx == 0] = 0
|
61 |
+
|
62 |
return round(Ya, 2)
|
63 |
|
64 |
|
65 |
+
def add_cultural_coefs(monthly_forecast: pd.DataFrame, cultural_coefs: pd.DataFrame) -> pd.DataFrame:
|
66 |
+
monthly_forecast["Kc"] = 0
|
67 |
+
monthly_forecast["Ky"] = 0
|
68 |
+
for month in range(1, 13):
|
69 |
+
Kc = cultural_coefs["Kc"][cultural_coefs.Mois == month].iloc[0]
|
70 |
+
Ky = cultural_coefs["Ky"][cultural_coefs.Mois == month].iloc[0]
|
71 |
+
monthly_forecast.loc[(monthly_forecast.month==month).to_numpy(), "Kc"] = Kc
|
72 |
+
monthly_forecast.loc[(monthly_forecast.month==month).to_numpy(), "Ky"] = Ky
|
73 |
+
return monthly_forecast
|
74 |
+
|
75 |
+
|
76 |
+
def compute_yield_forecast(
|
77 |
+
latitude: float,
|
78 |
+
longitude: float,
|
79 |
+
culture: str = "Colza d'hiver",
|
80 |
+
region: str = "Bourgogne-Franche-Comté",
|
81 |
+
scenario: str = "pessimist",
|
82 |
+
shading_coef: float = 0.,
|
83 |
+
):
|
84 |
monthly_forecast = get_forecast_data(latitude, longitude, scenario=scenario, shading_coef=shading_coef)
|
85 |
|
86 |
+
cultural_coefs, max_yield = get_coefs_Kc_Ky_and_max_yield(culture, region)
|
87 |
+
monthly_forecast = add_cultural_coefs(monthly_forecast, cultural_coefs)
|
88 |
+
Kc = monthly_forecast["Kc"]
|
89 |
+
Ky = monthly_forecast["Ky"]
|
90 |
|
91 |
soil_properties = get_soil_properties(latitude, longitude)
|
92 |
|
93 |
+
ETo = monthly_forecast["Evaporation (including sublimation and transpiration) (kg m-2 s-1)"]
|
94 |
|
95 |
ETx = calculate_ETx(Kc, ETo)
|
96 |
|
|
|
101 |
soil_properties["wilting_point"],
|
102 |
)
|
103 |
|
104 |
+
projected_yield = calculate_yield_projection(
|
105 |
+
Yx=max_yield,
|
106 |
+
ETx=ETx,
|
107 |
+
ETa=ETa,
|
108 |
+
Ky=Ky)
|
109 |
+
monthly_forecast["Estimated yield (quintal/ha)"] = projected_yield
|
110 |
+
|
111 |
+
return monthly_forecast
|
112 |
+
|
113 |
+
|
114 |
+
def get_annual_yield(monthly_forecast: pd.DataFrame) -> pd.Series:
|
115 |
+
yield_forecast = pd.Series(
|
116 |
+
index=monthly_forecast["time"],
|
117 |
+
data=monthly_forecast["Estimated yield (quintal/ha)"].to_numpy(),
|
118 |
+
)
|
119 |
+
yield_forecast = yield_forecast.resample("1YE").mean()
|
120 |
+
return yield_forecast
|
121 |
+
|
122 |
+
|
123 |
+
if __name__ == '__main__':
|
124 |
+
monthly_forecast = compute_yield_forecast(
|
125 |
+
latitude=47,
|
126 |
+
longitude=5,
|
127 |
+
culture="Colza d'hiver",
|
128 |
+
scenario="pessimist",
|
129 |
+
shading_coef=0.,
|
130 |
+
)
|
131 |
+
print(monthly_forecast.head())
|
132 |
|
133 |
+
yield_forecast = get_annual_yield(monthly_forecast)
|
134 |
+
print(yield_forecast)
|
forecast.py
CHANGED
@@ -128,6 +128,7 @@ def preprocess_forectast_data(df: pd.DataFrame, latitude, longitude, shading_coe
|
|
128 |
|
129 |
# Convert 'time' to datetime and calculate Julian day
|
130 |
preprocessed_data['time'] = pd.to_datetime(preprocessed_data['time'], errors='coerce')
|
|
|
131 |
preprocessed_data['day_of_year'] = preprocessed_data['time'].dt.dayofyear
|
132 |
|
133 |
# Compute ET0
|
|
|
128 |
|
129 |
# Convert 'time' to datetime and calculate Julian day
|
130 |
preprocessed_data['time'] = pd.to_datetime(preprocessed_data['time'], errors='coerce')
|
131 |
+
preprocessed_data['month'] = preprocessed_data['time'].dt.month
|
132 |
preprocessed_data['day_of_year'] = preprocessed_data['time'].dt.dayofyear
|
133 |
|
134 |
# Compute ET0
|
utils/soil_utils.py
CHANGED
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
1 |
import geopandas as gpd
|
2 |
from dotenv import load_dotenv
|
3 |
from geopy.geocoders import Nominatim
|
@@ -5,7 +8,8 @@ from shapely.geometry import Point
|
|
5 |
|
6 |
load_dotenv()
|
7 |
|
8 |
-
|
|
|
9 |
df = gpd.read_file(file_rmqs)
|
10 |
|
11 |
def get_city_coordinates(city_name):
|
@@ -53,7 +57,7 @@ def get_soil_properties(latitude, longitude):
|
|
53 |
"silt": silt,
|
54 |
"sand": sand,
|
55 |
"soc": soc,
|
56 |
-
"
|
57 |
"field_capacity": field_capacity,
|
58 |
"wilting_point": wilting_point,
|
59 |
}
|
|
|
1 |
+
import os
|
2 |
+
from pathlib import Path
|
3 |
+
|
4 |
import geopandas as gpd
|
5 |
from dotenv import load_dotenv
|
6 |
from geopy.geocoders import Nominatim
|
|
|
8 |
|
9 |
load_dotenv()
|
10 |
|
11 |
+
project_root = Path(__file__).parent.parent
|
12 |
+
file_rmqs = os.path.join(project_root, 'data/soil_data/raw_data/rmqs.geojson')
|
13 |
df = gpd.read_file(file_rmqs)
|
14 |
|
15 |
def get_city_coordinates(city_name):
|
|
|
57 |
"silt": silt,
|
58 |
"sand": sand,
|
59 |
"soc": soc,
|
60 |
+
"soil_moisture": soil_mosture,
|
61 |
"field_capacity": field_capacity,
|
62 |
"wilting_point": wilting_point,
|
63 |
}
|