Spaces:
Sleeping
Sleeping
Upload realtime_pipeline.py
Browse files- realtime_pipeline.py +45 -34
realtime_pipeline.py
CHANGED
|
@@ -136,34 +136,48 @@ class RealTimeFeatureExtractor:
|
|
| 136 |
|
| 137 |
def _estimate_npk_nutrients(self, soil_props: Dict) -> Dict:
|
| 138 |
try:
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
# ==================== NITROGEN ====================
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
nitrogen_percent = soc_percent / 11.5 # Brady & Weil (2008)
|
| 156 |
nitrogen_percent = max(0.05, min(0.50, nitrogen_percent))
|
| 157 |
else:
|
| 158 |
nitrogen_percent = 0.15 # متوسط التربة المصرية
|
| 159 |
|
| 160 |
# ==================== PHOSPHORUS ====================
|
| 161 |
-
if
|
| 162 |
-
|
| 163 |
-
base_p = soc_percent * 8.5
|
| 164 |
|
| 165 |
# CEC Factor
|
| 166 |
-
cec_cmol =
|
| 167 |
if cec_cmol > 20:
|
| 168 |
cec_factor = 1.0 - ((cec_cmol - 20) / 200)
|
| 169 |
else:
|
|
@@ -171,7 +185,6 @@ class RealTimeFeatureExtractor:
|
|
| 171 |
cec_factor = max(0.7, min(1.3, cec_factor))
|
| 172 |
|
| 173 |
# Clay Factor
|
| 174 |
-
clay_percent = clay / 10.0 # g/kg×10 → %
|
| 175 |
if clay_percent > 30:
|
| 176 |
clay_factor = 1.0 - ((clay_percent - 30) / 150)
|
| 177 |
else:
|
|
@@ -180,7 +193,7 @@ class RealTimeFeatureExtractor:
|
|
| 180 |
|
| 181 |
# pH Factor
|
| 182 |
if 6.0 <= ph_actual <= 7.5:
|
| 183 |
-
ph_factor = 1.25
|
| 184 |
elif ph_actual < 6.0:
|
| 185 |
ph_factor = 0.85 - ((6.0 - ph_actual) * 0.05)
|
| 186 |
else:
|
|
@@ -194,13 +207,14 @@ class RealTimeFeatureExtractor:
|
|
| 194 |
phosphorus_mg_kg = max(5.0, min(35.0, phosphorus_mg_kg))
|
| 195 |
|
| 196 |
# ==================== POTASSIUM ====================
|
| 197 |
-
if
|
| 198 |
-
|
| 199 |
-
#
|
| 200 |
-
base_k = (cec_cmol * 40) + 50
|
| 201 |
|
| 202 |
-
#
|
| 203 |
-
|
|
|
|
|
|
|
| 204 |
if clay_percent < 20:
|
| 205 |
clay_factor = 0.8
|
| 206 |
elif 20 <= clay_percent <= 40:
|
|
@@ -210,7 +224,6 @@ class RealTimeFeatureExtractor:
|
|
| 210 |
clay_factor = max(0.7, min(1.2, clay_factor))
|
| 211 |
|
| 212 |
# Silt Factor
|
| 213 |
-
silt_percent = silt / 10.0 # g/kg×10 → %
|
| 214 |
if 30 <= silt_percent <= 50:
|
| 215 |
silt_factor = 1.05
|
| 216 |
elif silt_percent > 50:
|
|
@@ -220,8 +233,7 @@ class RealTimeFeatureExtractor:
|
|
| 220 |
silt_factor = max(0.9, min(1.1, silt_factor))
|
| 221 |
|
| 222 |
# SOC Factor
|
| 223 |
-
if
|
| 224 |
-
soc_percent = soc / 100.0
|
| 225 |
if soc_percent > 2.0:
|
| 226 |
soc_factor = 1.05
|
| 227 |
elif soc_percent > 1.0:
|
|
@@ -238,9 +250,9 @@ class RealTimeFeatureExtractor:
|
|
| 238 |
potassium_mg_kg = max(120.0, min(350.0, potassium_mg_kg))
|
| 239 |
|
| 240 |
# ==================== CONFIDENCE ====================
|
| 241 |
-
if
|
| 242 |
confidence = 'High'
|
| 243 |
-
elif
|
| 244 |
confidence = 'Medium'
|
| 245 |
else:
|
| 246 |
confidence = 'Low'
|
|
@@ -259,8 +271,7 @@ class RealTimeFeatureExtractor:
|
|
| 259 |
'phosphorus_mg_kg': 12.0,
|
| 260 |
'potassium_mg_kg': 150.0,
|
| 261 |
'npk_confidence': 'Low - Error'
|
| 262 |
-
}
|
| 263 |
-
|
| 264 |
def get_realtime_weather(self, latitude, longitude):
|
| 265 |
if not Config.OPENWEATHER_API_KEY:
|
| 266 |
self.logger.warning("⚠️ OpenWeather API key not set")
|
|
|
|
| 136 |
|
| 137 |
def _estimate_npk_nutrients(self, soil_props: Dict) -> Dict:
|
| 138 |
try:
|
| 139 |
+
# Get original values from SoilGrids
|
| 140 |
+
soc_cg_kg = soil_props.get('soc', 0) or 0 # cg/kg
|
| 141 |
+
cec_mmol_kg_x10 = soil_props.get('cec', 0) or 0 # mmol/kg × 10
|
| 142 |
+
clay_g_kg_x10 = soil_props.get('clay', 0) or 0 # g/kg × 10
|
| 143 |
+
silt_g_kg_x10 = soil_props.get('silt', 0) or 0 # g/kg × 10
|
| 144 |
+
ph_x10 = soil_props.get('ph', 70) or 70 # pH × 10
|
| 145 |
+
|
| 146 |
+
# ==================== CONVERT TO HISTORICAL FORMAT ====================
|
| 147 |
+
# Convert to same units as historical data
|
| 148 |
+
|
| 149 |
+
# SOC: cg/kg → %
|
| 150 |
+
# 1% = 1 g/100g = 10 g/kg = 1000 cg/kg
|
| 151 |
+
soc_percent = soc_cg_kg / 1000.0 if soc_cg_kg else 0
|
| 152 |
+
|
| 153 |
+
# CEC: mmol/kg × 10 → mmol/kg (already in historical format)
|
| 154 |
+
cec_mmol_kg = cec_mmol_kg_x10 / 10.0 if cec_mmol_kg_x10 else 0
|
| 155 |
+
|
| 156 |
+
# Clay: g/kg × 10 → %
|
| 157 |
+
# 1% = 10 g/kg
|
| 158 |
+
clay_percent = clay_g_kg_x10 / 10.0 if clay_g_kg_x10 else 0
|
| 159 |
+
|
| 160 |
+
# Silt: g/kg × 10 → %
|
| 161 |
+
silt_percent = silt_g_kg_x10 / 10.0 if silt_g_kg_x10 else 0
|
| 162 |
+
|
| 163 |
+
# pH: pH × 10 → actual pH
|
| 164 |
+
ph_actual = ph_x10 / 10.0 if ph_x10 else 7.0
|
| 165 |
|
| 166 |
# ==================== NITROGEN ====================
|
| 167 |
+
if soc_percent > 0:
|
| 168 |
+
# Brady & Weil (2008) - N = SOC / C:N ratio
|
| 169 |
+
nitrogen_percent = soc_percent / 11.5 # C:N ratio = 11.5
|
|
|
|
| 170 |
nitrogen_percent = max(0.05, min(0.50, nitrogen_percent))
|
| 171 |
else:
|
| 172 |
nitrogen_percent = 0.15 # متوسط التربة المصرية
|
| 173 |
|
| 174 |
# ==================== PHOSPHORUS ====================
|
| 175 |
+
if soc_percent > 0:
|
| 176 |
+
# Abdel-Fattah (2012) - Base P = SOC × 8.5
|
| 177 |
+
base_p = soc_percent * 8.5
|
| 178 |
|
| 179 |
# CEC Factor
|
| 180 |
+
cec_cmol = cec_mmol_kg / 10.0 # mmol/kg → cmol/kg
|
| 181 |
if cec_cmol > 20:
|
| 182 |
cec_factor = 1.0 - ((cec_cmol - 20) / 200)
|
| 183 |
else:
|
|
|
|
| 185 |
cec_factor = max(0.7, min(1.3, cec_factor))
|
| 186 |
|
| 187 |
# Clay Factor
|
|
|
|
| 188 |
if clay_percent > 30:
|
| 189 |
clay_factor = 1.0 - ((clay_percent - 30) / 150)
|
| 190 |
else:
|
|
|
|
| 193 |
|
| 194 |
# pH Factor
|
| 195 |
if 6.0 <= ph_actual <= 7.5:
|
| 196 |
+
ph_factor = 1.25 # أقصى توفر
|
| 197 |
elif ph_actual < 6.0:
|
| 198 |
ph_factor = 0.85 - ((6.0 - ph_actual) * 0.05)
|
| 199 |
else:
|
|
|
|
| 207 |
phosphorus_mg_kg = max(5.0, min(35.0, phosphorus_mg_kg))
|
| 208 |
|
| 209 |
# ==================== POTASSIUM ====================
|
| 210 |
+
if cec_mmol_kg > 0:
|
| 211 |
+
# El-Baroudy (2016) - K = (CEC × 7.2) + adjustments
|
| 212 |
+
cec_cmol = cec_mmol_kg / 10.0 # mmol/kg → cmol/kg
|
|
|
|
| 213 |
|
| 214 |
+
# Base K for Egypt
|
| 215 |
+
base_k = cec_cmol * 7.2
|
| 216 |
+
|
| 217 |
+
# Clay Factor (معدل للتربة المصرية)
|
| 218 |
if clay_percent < 20:
|
| 219 |
clay_factor = 0.8
|
| 220 |
elif 20 <= clay_percent <= 40:
|
|
|
|
| 224 |
clay_factor = max(0.7, min(1.2, clay_factor))
|
| 225 |
|
| 226 |
# Silt Factor
|
|
|
|
| 227 |
if 30 <= silt_percent <= 50:
|
| 228 |
silt_factor = 1.05
|
| 229 |
elif silt_percent > 50:
|
|
|
|
| 233 |
silt_factor = max(0.9, min(1.1, silt_factor))
|
| 234 |
|
| 235 |
# SOC Factor
|
| 236 |
+
if soc_percent > 0:
|
|
|
|
| 237 |
if soc_percent > 2.0:
|
| 238 |
soc_factor = 1.05
|
| 239 |
elif soc_percent > 1.0:
|
|
|
|
| 250 |
potassium_mg_kg = max(120.0, min(350.0, potassium_mg_kg))
|
| 251 |
|
| 252 |
# ==================== CONFIDENCE ====================
|
| 253 |
+
if soc_cg_kg > 0 and cec_mmol_kg_x10 > 0 and clay_g_kg_x10 > 0:
|
| 254 |
confidence = 'High'
|
| 255 |
+
elif soc_cg_kg > 0 or cec_mmol_kg_x10 > 0:
|
| 256 |
confidence = 'Medium'
|
| 257 |
else:
|
| 258 |
confidence = 'Low'
|
|
|
|
| 271 |
'phosphorus_mg_kg': 12.0,
|
| 272 |
'potassium_mg_kg': 150.0,
|
| 273 |
'npk_confidence': 'Low - Error'
|
| 274 |
+
}
|
|
|
|
| 275 |
def get_realtime_weather(self, latitude, longitude):
|
| 276 |
if not Config.OPENWEATHER_API_KEY:
|
| 277 |
self.logger.warning("⚠️ OpenWeather API key not set")
|