Spaces:
Sleeping
Sleeping
Upload realtime_pipeline.py
Browse files- realtime_pipeline.py +42 -2
realtime_pipeline.py
CHANGED
|
@@ -277,7 +277,42 @@ class TemporalDataManager:
|
|
| 277 |
except Exception as e:
|
| 278 |
self.logger.error(f"❌ ERA5 climate failed: {e}")
|
| 279 |
return self._get_climate_fallback(lat, date.month)
|
| 280 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
@staticmethod
|
| 282 |
def _compute_rh(t_celsius: float, td_celsius: float) -> float:
|
| 283 |
try:
|
|
@@ -913,6 +948,8 @@ class RealTimeFeatureExtractor:
|
|
| 913 |
soil_year = current_date.year
|
| 914 |
soil_data = self.temporal_manager.get_historical_soil_data(latitude, longitude, soil_year)
|
| 915 |
|
|
|
|
|
|
|
| 916 |
# جيب NPK من التربة
|
| 917 |
npk_data = self._estimate_npk_nutrients(soil_data)
|
| 918 |
|
|
@@ -950,7 +987,8 @@ class RealTimeFeatureExtractor:
|
|
| 950 |
'ph': round(soil_data.get('ph'), 1) if soil_data.get('ph') else None,
|
| 951 |
'cec': round(soil_data.get('cec'), 1) if soil_data.get('cec') else None,
|
| 952 |
'soil_version': soil_data.get('_version_year'),
|
| 953 |
-
|
|
|
|
| 954 |
# NPK (نفسها لكل الشهور)
|
| 955 |
'nitrogen_percent': npk_data['nitrogen_percent'],
|
| 956 |
'phosphorus_mg_kg': npk_data['phosphorus_mg_kg'],
|
|
@@ -1156,6 +1194,7 @@ async def get_12_months_history_ml(request: HistoricalFeaturesRequest):
|
|
| 1156 |
'soc': month_data['soc'],
|
| 1157 |
'ph': month_data['ph'],
|
| 1158 |
'cec': month_data['cec'],
|
|
|
|
| 1159 |
'ndvi': month_data['ndvi'],
|
| 1160 |
't2m_c': month_data['temperature_c'],
|
| 1161 |
'td2m_c': month_data['td2m_c'],
|
|
@@ -1185,6 +1224,7 @@ async def get_12_months_history_ml(request: HistoricalFeaturesRequest):
|
|
| 1185 |
'soil_method': 'Temporal_SoilGrids',
|
| 1186 |
'weather_method': 'ERA5_LAND',
|
| 1187 |
'npk_method': 'Estimated_from_Soil',
|
|
|
|
| 1188 |
'note': 'Fixed 12 months dataset for forecasting models'
|
| 1189 |
}
|
| 1190 |
}
|
|
|
|
| 277 |
except Exception as e:
|
| 278 |
self.logger.error(f"❌ ERA5 climate failed: {e}")
|
| 279 |
return self._get_climate_fallback(lat, date.month)
|
| 280 |
+
|
| 281 |
+
def get_historical_bdod_data(self, lat: float, lon: float, year: int) -> Optional[float]:
|
| 282 |
+
"""جلب بيانات BDOD التاريخية"""
|
| 283 |
+
try:
|
| 284 |
+
point = ee.Geometry.Point([lon, lat])
|
| 285 |
+
version = self.get_soil_version(year)
|
| 286 |
+
|
| 287 |
+
# BDOD موجود في نفس المجموعة
|
| 288 |
+
bdod = ee.Image(f"{version['collection']}/bdod_mean").select("bdod_0-5cm_mean")
|
| 289 |
+
|
| 290 |
+
for scale in [250, 500, 1000]:
|
| 291 |
+
try:
|
| 292 |
+
bdod_data = bdod.reduceRegion(
|
| 293 |
+
reducer=ee.Reducer.mean(),
|
| 294 |
+
geometry=point,
|
| 295 |
+
scale=scale,
|
| 296 |
+
bestEffort=True,
|
| 297 |
+
maxPixels=1e9
|
| 298 |
+
).getInfo()
|
| 299 |
+
|
| 300 |
+
bdod_value = bdod_data.get('bdod_0-5cm_mean')
|
| 301 |
+
if bdod_value is not None:
|
| 302 |
+
bdod_kg_m3 = bdod_value * 10 # التحويل من cg/cm³ إلى kg/m³
|
| 303 |
+
bdod_g_cm3 = bdod_kg_m3 / 1000.0 # التحويل إلى g/cm³
|
| 304 |
+
self.logger.info(f"✅ BDOD for {year}: {bdod_g_cm3:.2f} g/cm³ (scale: {scale}m)")
|
| 305 |
+
return bdod_g_cm3
|
| 306 |
+
|
| 307 |
+
except Exception as e:
|
| 308 |
+
self.logger.warning(f"⚠️ BDOD scale {scale}m failed: {str(e)[:50]}")
|
| 309 |
+
continue
|
| 310 |
+
|
| 311 |
+
return None
|
| 312 |
+
|
| 313 |
+
except Exception as e:
|
| 314 |
+
self.logger.error(f"❌ Historical BDOD fetch failed: {e}")
|
| 315 |
+
return None
|
| 316 |
@staticmethod
|
| 317 |
def _compute_rh(t_celsius: float, td_celsius: float) -> float:
|
| 318 |
try:
|
|
|
|
| 948 |
soil_year = current_date.year
|
| 949 |
soil_data = self.temporal_manager.get_historical_soil_data(latitude, longitude, soil_year)
|
| 950 |
|
| 951 |
+
bdod_data = self.temporal_manager.get_historical_bdod_data(latitude, longitude, soil_year)
|
| 952 |
+
|
| 953 |
# جيب NPK من التربة
|
| 954 |
npk_data = self._estimate_npk_nutrients(soil_data)
|
| 955 |
|
|
|
|
| 987 |
'ph': round(soil_data.get('ph'), 1) if soil_data.get('ph') else None,
|
| 988 |
'cec': round(soil_data.get('cec'), 1) if soil_data.get('cec') else None,
|
| 989 |
'soil_version': soil_data.get('_version_year'),
|
| 990 |
+
'bdod': round(bdod_data, 2) if bdod_data else None,
|
| 991 |
+
|
| 992 |
# NPK (نفسها لكل الشهور)
|
| 993 |
'nitrogen_percent': npk_data['nitrogen_percent'],
|
| 994 |
'phosphorus_mg_kg': npk_data['phosphorus_mg_kg'],
|
|
|
|
| 1194 |
'soc': month_data['soc'],
|
| 1195 |
'ph': month_data['ph'],
|
| 1196 |
'cec': month_data['cec'],
|
| 1197 |
+
'bdod': month_data['bdod'],
|
| 1198 |
'ndvi': month_data['ndvi'],
|
| 1199 |
't2m_c': month_data['temperature_c'],
|
| 1200 |
'td2m_c': month_data['td2m_c'],
|
|
|
|
| 1224 |
'soil_method': 'Temporal_SoilGrids',
|
| 1225 |
'weather_method': 'ERA5_LAND',
|
| 1226 |
'npk_method': 'Estimated_from_Soil',
|
| 1227 |
+
'bdod_included': True,
|
| 1228 |
'note': 'Fixed 12 months dataset for forecasting models'
|
| 1229 |
}
|
| 1230 |
}
|