-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_cache.py
More file actions
148 lines (123 loc) · 4.88 KB
/
build_cache.py
File metadata and controls
148 lines (123 loc) · 4.88 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
141
142
143
144
145
146
147
148
#!/usr/bin/env python3
"""
Feature Cache Builder — berechnet alle historischen Region×Jahr-Kombinationen vor.
Der Server nutzt diesen Cache statt Live-APIs für alles außer der aktuellen Saison.
Cache-Struktur: /home/j/crop-mcp/feature_cache/
{region}_{year}.json → {gdd, precip_mm, solar_kwh, soil_moisture}
Nutzt NASA POWER (kein Rate-Limiting). Einmaliger Build, danach nur Updates für neue Jahre.
"""
import json, os, sys, time
from datetime import date
from concurrent.futures import ThreadPoolExecutor, as_completed
sys.path.insert(0, '/home/j/crop-mcp')
from core.regions import REGIONS, get_crop
from sources.power import get_power_data
from sources.power import SOLAR_PARAM, SOIL_M1, T2M_MAX, T2M_MIN, PRECIP
CACHE_DIR = '/home/j/crop-mcp/feature_cache'
DAYS_IN_MONTH = {1:31,2:28,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}
MAX_WORKERS = 4 # NASA POWER toleriert 4 parallele Calls
# Winter wheat parameter
PLANT_M = 10
HARVEST_M = 7
GDD_BASE = 0.0
def get_features_for_region_year(region_code, lat, lon, year):
"""Berechne Features für eine Region×Jahr-Kombination."""
if plant_m > harvest_m:
months = list(range(PLANT_M, 13)) + list(range(1, HARVEST_M + 1))
fetch_start = year - 1
else:
months = list(range(PLANT_M, HARVEST_M + 1))
fetch_start = year
try:
data = get_power_data(lat, lon, fetch_start, year,
params=[T2M_MAX, T2M_MIN, PRECIP, SOLAR_PARAM, SOIL_M1])
except Exception:
return None
total_gdd = total_precip = 0.0
solar_sum = soil_sum = 0.0
data_months = 0
for m in months:
for k in [f"{year - 1}{str(m).zfill(2)}", f"{year}{str(m).zfill(2)}"]:
tmax = data.get(T2M_MAX, {}).get(k)
tmin = data.get(T2M_MIN, {}).get(k)
precip_mm = data.get(PRECIP, {}).get(k)
solar = data.get(SOLAR_PARAM, {}).get(k)
soil = data.get(SOIL_M1, {}).get(k)
if tmax is not None:
break
if tmax is not None and tmin is not None:
days = DAYS_IN_MONTH[m]
daily_gdd = max(0, (tmax + tmin) / 2 - GDD_BASE)
total_gdd += daily_gdd * days
if precip_mm is not None:
total_precip += precip_mm * DAYS_IN_MONTH[m]
if solar is not None:
solar_sum += solar
if soil is not None:
soil_sum += soil
data_months += 1
if data_months == 0 or total_gdd == 0:
return None
return {
"region": region_code,
"year": year,
"gdd": round(total_gdd, 1),
"precip_mm": round(total_precip, 1),
"solar_kwh": round(solar_sum / data_months, 2),
"soil_moisture": round(soil_sum / data_months, 3),
"plant_month": PLANT_M,
"harvest_month": HARVEST_M,
}
# ── MAIN ──
os.makedirs(CACHE_DIR, exist_ok=True)
crop = get_crop("wheat")
plant_m, harvest_m = crop.planting_month, crop.harvest_month
# Bestimme zu berechnende Jahre (2000–2024)
current_year = date.today().year
years = list(range(2000, current_year)) # Alle abgeschlossenen Jahre
# Sammle alle Tasks
tasks = []
for code, r in REGIONS.items():
if r.country not in ['AT', 'BG', 'CZ', 'DE', 'DK', 'ES', 'FR', 'HU', 'IT', 'PL', 'RO']:
continue # Nur Länder mit Eurostat-Daten
for year in years:
cache_path = os.path.join(CACHE_DIR, f"{code}_{year}.json")
if os.path.exists(cache_path):
continue # Bereits gecached
tasks.append((code, r.latitude, r.longitude, year, cache_path))
print(f"🏗️ Feature Cache Builder")
print(f" Regionen: {len([c for c in set(t[0] for t in tasks)])}")
print(f" Fehlende Cache-Einträge: {len(tasks)}")
print(f" Parallele Threads: {MAX_WORKERS}")
if not tasks:
print(" ✅ Bereits vollständig!")
sys.exit(0)
# Baue Cache parallel
done = 0
errors = 0
start = time.time()
with ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor:
futures = {
executor.submit(get_features_for_region_year, code, lat, lon, year): (code, year, path)
for code, lat, lon, year, path in tasks
}
for future in as_completed(futures):
code, year, path = futures[future]
result = future.result()
if result:
with open(path, 'w') as f:
json.dump(result, f)
done += 1
else:
errors += 1
if (done + errors) % 50 == 0:
elapsed = time.time() - start
rate = (done + errors) / elapsed if elapsed > 0 else 0
print(f" [{done+errors}/{len(tasks)}] ✅ {done} ❌ {errors} — {rate:.1f}/s")
elapsed = time.time() - start
print(f"\n{'='*50}")
print(f"✅ CACHE BUILD FINISHED")
print(f" Neu gecached: {done}")
print(f" Fehlgeschlagen: {errors}")
print(f" Dauer: {elapsed:.0f}s ({done/elapsed:.1f}/s)")
print(f" Cache: {CACHE_DIR}/")