-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathsugar_collector.py
More file actions
140 lines (129 loc) · 6.22 KB
/
Copy pathsugar_collector.py
File metadata and controls
140 lines (129 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
"""
AgriQuant AI - Sugar #11 Weather & Data Collector
Regions: Brazil (Center-South), India (Maharashtra/UP), Thailand
Ticker: SB (ICE)
Primary risks: Brazil drought, India monsoon failure, Thailand floods
"""
import requests, logging
from datetime import datetime
from typing import Dict, List
logger = logging.getLogger(__name__)
SUGAR_REGIONS = {
'Brazil_CenterSouth': {
'lat': -22.0, 'lon': -47.9, 'country': 'Brazil',
'production_share': 0.38, 'harvest_months': [4,5,6,7,8,9,10,11],
'drought_threshold_mm': 40, 'priority': 1,
'notes': 'Sao Paulo state - largest sugar producing region globally'
},
'India_Maharashtra': {
'lat': 17.5, 'lon': 75.3, 'country': 'India',
'production_share': 0.18, 'harvest_months': [10,11,12,1,2,3],
'monsoon_months': [6,7,8,9], 'priority': 2,
'normal_monsoon_mm': 650,
'notes': 'Western Maharashtra - Pune/Kolhapur belt'
},
'India_UttarPradesh': {
'lat': 26.5, 'lon': 80.9, 'country': 'India',
'production_share': 0.15, 'harvest_months': [10,11,12,1,2,3],
'monsoon_months': [6,7,8,9], 'priority': 3,
'normal_monsoon_mm': 880,
'notes': 'UP belt - second largest India state producer'
},
'Thailand_Central': {
'lat': 14.5, 'lon': 100.5, 'country': 'Thailand',
'production_share': 0.12, 'harvest_months': [12,1,2,3],
'priority': 4,
'notes': 'Central plains - flood risk Oct-Nov'
},
}
HISTORICAL_SUPPLY_EVENTS = [
{'year': 2022, 'cause': 'India export ban', 'sb_impact_pct': 25.0, 'type': 'policy'},
{'year': 2021, 'cause': 'Brazil drought + frost', 'sb_impact_pct': 18.5, 'type': 'weather'},
{'year': 2020, 'cause': 'Thailand drought', 'sb_impact_pct': 20.3, 'type': 'weather'},
{'year': 2015, 'cause': 'El Nino India drought', 'sb_impact_pct': 12.1, 'type': 'weather'},
{'year': 2010, 'cause': 'India monsoon failure', 'sb_impact_pct': 32.4, 'type': 'weather'},
]
class SugarCollector:
"""
Sugar #11 weather and supply data collection.
Monitors Brazil drought, India monsoon, Thailand floods, and policy signals.
"""
def __init__(self):
self.noaa_base = 'https://api.weather.gov'
self.imd_base = 'https://imdpune.gov.in' # India Meteorological Dept
def get_brazil_sugarcane_forecast(self, lat: float, lon: float) -> Dict:
"""GFS forecast for Sao Paulo sugarcane belt"""
try:
resp = requests.get(f"{self.noaa_base}/points/{lat},{lon}", timeout=10)
if resp.status_code == 200:
grid = resp.json()['properties']
f = requests.get(grid['forecast'], timeout=10)
if f.status_code == 200:
return {'source': 'NOAA_GFS', 'region': 'Brazil_CenterSouth',
'periods': f.json()['properties']['periods'][:8],
'timestamp': datetime.utcnow().isoformat()}
except Exception as e:
logger.error(f"Brazil sugar forecast error: {e}")
return {'source': 'unavailable', 'timestamp': datetime.utcnow().isoformat()}
def assess_india_monsoon(self, region: str, rainfall_data: Dict) -> Dict:
"""
India monsoon assessment. June-September rainfall vs normal.
IMD defines: >110% = excess, 90-110% = normal, 75-90% = deficient,
<75% = large deficient. Large deficient = sugar supply shock.
"""
region_data = SUGAR_REGIONS[region]
normal_mm = region_data.get('normal_monsoon_mm', 700)
actual_mm = rainfall_data.get('cumulative_monsoon_mm', normal_mm)
pct_of_normal = (actual_mm / normal_mm) * 100
if pct_of_normal < 75:
category, price_impact = 'large_deficient', (18, 35)
elif pct_of_normal < 90:
category, price_impact = 'deficient', (8, 18)
elif pct_of_normal <= 110:
category, price_impact = 'normal', (0, 3)
else:
category, price_impact = 'excess', (-5, 0) # bearish signal
return {
'region': region, 'actual_monsoon_mm': actual_mm,
'normal_monsoon_mm': normal_mm, 'pct_of_normal': round(pct_of_normal, 1),
'imd_category': category, 'price_impact_range_pct': price_impact,
'signal': 'LONG' if pct_of_normal < 90 else ('SHORT' if pct_of_normal > 115 else 'NEUTRAL'),
'production_share': region_data['production_share'],
}
def assess_brazil_drought(self, rainfall_data: Dict) -> Dict:
"""Drought assessment for Brazil Center-South sugarcane"""
monthly_mm = rainfall_data.get('monthly_mm', 60)
region = SUGAR_REGIONS['Brazil_CenterSouth']
threshold = region['drought_threshold_mm']
current_month = datetime.utcnow().month
is_harvest = current_month in region['harvest_months']
deficit_pct = max(0, (threshold - monthly_mm) / threshold)
if deficit_pct > 0.6 and is_harvest:
level, impact = 'critical', (12, 22)
elif deficit_pct > 0.4:
level, impact = 'high', (6, 14)
elif deficit_pct > 0.2:
level, impact = 'moderate', (2, 8)
else:
level, impact = 'low', (0, 2)
return {
'monthly_rainfall_mm': monthly_mm, 'threshold_mm': threshold,
'drought_level': level, 'is_harvest_season': is_harvest,
'price_impact_range_pct': impact,
'production_share': region['production_share'],
}
def collect_all_regions(self) -> Dict:
"""Collect all sugar production region data"""
results = {}
logger.info("Collecting sugar production region data...")
for region_name, data in SUGAR_REGIONS.items():
forecast = self.get_brazil_sugarcane_forecast(data['lat'], data['lon'])
if 'India' in region_name:
risk = self.assess_india_monsoon(region_name,
{'cumulative_monsoon_mm': 600})
else:
risk = self.assess_brazil_drought({'monthly_mm': 55})
results[region_name] = {'forecast': forecast, 'risk': risk,
'priority': data['priority']}
return {'commodity': 'SB', 'timestamp': datetime.utcnow().isoformat(),
'regions': results}