-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal_compute.py
More file actions
286 lines (217 loc) · 9.21 KB
/
Copy pathlocal_compute.py
File metadata and controls
286 lines (217 loc) · 9.21 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
"""Local satellite data computation -- replaces GEE for downloaded products."""
import math
import time
from pathlib import Path
import numpy as np
import rasterio
from rasterio.windows import from_bounds
LOCAL_DATA = Path(__file__).parent / "local_data"
# Bangladesh bounds
BD_BOUNDS = {"west": 88.0, "south": 20.5, "east": 92.7, "north": 26.7}
# Mean latitude of Bangladesh (~23.7N) for degree-to-km conversion
_BD_MEAN_LAT = 23.7
_DEG_LAT_KM = 111.32
_DEG_LON_KM = 111.32 * math.cos(math.radians(_BD_MEAN_LAT))
# =============================================================================
# Helpers
# =============================================================================
def _read_bd(path):
"""Read a GeoTIFF clipped to Bangladesh bounds.
Returns (array, transform, pixel_area_km2).
"""
with rasterio.open(path) as src:
window = from_bounds(
BD_BOUNDS["west"], BD_BOUNDS["south"],
BD_BOUNDS["east"], BD_BOUNDS["north"],
transform=src.transform,
)
data = src.read(1, window=window)
transform = src.window_transform(window)
res = src.res # (y_deg, x_deg)
pixel_area_km2 = (res[0] * _DEG_LAT_KM) * (res[1] * _DEG_LON_KM)
return data, transform, pixel_area_km2
def _find_file(pattern, subdir=None):
"""Find a single file matching a glob pattern under LOCAL_DATA.
Prefers *_bd.tif (clipped to Bangladesh) over raw global tiles.
"""
base = LOCAL_DATA / subdir if subdir else LOCAL_DATA
matches = sorted(base.glob(pattern))
if not matches:
raise FileNotFoundError(f"No file matching '{pattern}' in {base}")
# Prefer BD-clipped files
bd_matches = [m for m in matches if "_bd" in m.stem]
return bd_matches[0] if bd_matches else matches[0]
# =============================================================================
# Forest (Hansen Global Forest Change)
# =============================================================================
def compute_forest_stats_local(tree_threshold=30):
"""Compute forest cover stats from Hansen treecover2000, loss, gain.
Returns dict with forest_2000_km2, forest_loss_km2, forest_gain_km2.
"""
t0 = time.perf_counter()
tc_path = _find_file("*treecover2000*")
loss_path = _find_file("*loss*")
gain_path = _find_file("*gain*")
tc, _, px_area = _read_bd(tc_path)
loss, _, _ = _read_bd(loss_path)
gain, _, _ = _read_bd(gain_path)
forest_mask = tc >= tree_threshold
forest_2000_km2 = float(np.count_nonzero(forest_mask) * px_area)
forest_loss_km2 = float(np.count_nonzero(loss > 0) * px_area)
forest_gain_km2 = float(np.count_nonzero(gain > 0) * px_area)
elapsed = time.perf_counter() - t0
print(f" Local forest stats: {elapsed:.2f}s (vs ~120s on GEE)")
return {
"forest_2000_km2": round(forest_2000_km2, 2),
"forest_loss_km2": round(forest_loss_km2, 2),
"forest_gain_km2": round(forest_gain_km2, 2),
}
def compute_forest_loss_by_year_local(tree_threshold=30):
"""Compute annual forest loss area from Hansen lossyear band.
lossyear values: 0 = no loss, 1-23 = year since 2000 (2001-2023).
Returns list of {year, loss_km2}.
"""
t0 = time.perf_counter()
ly_path = _find_file("*lossyear*")
tc_path = _find_file("*treecover2000*")
lossyear, _, px_area = _read_bd(ly_path)
tc, _, _ = _read_bd(tc_path)
forest_mask = tc >= tree_threshold
results = []
for yr_code in range(1, 24):
yr_loss = (lossyear == yr_code) & forest_mask
loss_km2 = float(np.count_nonzero(yr_loss) * px_area)
results.append({
"year": 2000 + yr_code,
"loss_km2": round(loss_km2, 4),
})
elapsed = time.perf_counter() - t0
print(f" Local forest loss by year: {elapsed:.2f}s (vs ~180s on GEE)")
return results
# =============================================================================
# Water (JRC Global Surface Water)
# =============================================================================
def compute_water_stats_local():
"""Compute water area stats from JRC occurrence layer.
Classifies pixels by occurrence percentage:
permanent: >75%, seasonal: 25-75%, rare: <25% (but >0).
Returns dict with permanent_km2, seasonal_km2, rare_km2.
"""
t0 = time.perf_counter()
occ_path = _find_file("*occurrence*")
occ, _, px_area = _read_bd(occ_path)
permanent = occ > 75
seasonal = (occ >= 25) & (occ <= 75)
rare = (occ > 0) & (occ < 25)
permanent_km2 = float(np.count_nonzero(permanent) * px_area)
seasonal_km2 = float(np.count_nonzero(seasonal) * px_area)
rare_km2 = float(np.count_nonzero(rare) * px_area)
elapsed = time.perf_counter() - t0
print(f" Local water stats: {elapsed:.2f}s (vs ~90s on GEE)")
return {
"permanent_km2": round(permanent_km2, 2),
"seasonal_km2": round(seasonal_km2, 2),
"rare_km2": round(rare_km2, 2),
}
# =============================================================================
# Rainfall (CHIRPS)
# =============================================================================
def compute_rainfall_stats_local(year):
"""Compute rainfall stats from CHIRPS monthly GeoTIFFs.
Expects files in local_data/chirps/ named like chirps_<year>_<MM>.tif.
Returns dict with annual_mean_mm, annual_max_mm, monsoon_total_mm.
"""
t0 = time.perf_counter()
chirps_dir = LOCAL_DATA / "chirps"
monthly_means = []
monthly_maxes = []
monsoon_totals = [] # Jun(6) - Sep(9)
for month in range(1, 13):
try:
path = _find_file(f"*{year}*{month:02d}*", subdir="chirps")
except FileNotFoundError:
continue
data, _, _ = _read_bd(path)
valid = data[data > 0]
if valid.size == 0:
continue
month_mean = float(np.mean(valid))
month_max = float(np.max(valid))
monthly_means.append(month_mean)
monthly_maxes.append(month_max)
if 6 <= month <= 9:
monsoon_totals.append(month_mean)
annual_mean_mm = sum(monthly_means) if monthly_means else 0.0
annual_max_mm = max(monthly_maxes) if monthly_maxes else 0.0
monsoon_total_mm = sum(monsoon_totals) if monsoon_totals else 0.0
elapsed = time.perf_counter() - t0
print(f" Local rainfall stats ({year}): {elapsed:.2f}s (vs ~60s on GEE)")
return {
"year": year,
"annual_mean_mm": round(annual_mean_mm, 2),
"annual_max_mm": round(annual_max_mm, 2),
"monsoon_total_mm": round(monsoon_total_mm, 2),
}
# =============================================================================
# Population (WorldPop)
# =============================================================================
def compute_population_stats_local():
"""Compute population stats from WorldPop GeoTIFF.
Returns dict with total_population and mean_density_per_km2.
"""
t0 = time.perf_counter()
pop_path = _find_file("*bgd_ppp*") or _find_file("*worldpop*")
pop, _, px_area = _read_bd(pop_path)
# WorldPop uses nodata values (often -99999 or very negative)
valid = pop[pop > 0]
total_pop = float(np.sum(valid))
n_valid = valid.size
total_area_km2 = n_valid * px_area
mean_density = total_pop / total_area_km2 if total_area_km2 > 0 else 0.0
elapsed = time.perf_counter() - t0
print(f" Local population stats: {elapsed:.2f}s (vs ~45s on GEE)")
return {
"total_population": round(total_pop),
"mean_density_per_km2": round(mean_density, 2),
}
# =============================================================================
# Orchestrator
# =============================================================================
def run_local_analysis(year=2023):
"""Run all local computations on downloaded satellite data.
Returns results dict compatible with the existing pipeline output format.
"""
print("\n" + "=" * 60)
print("LOCAL SATELLITE DATA ANALYSIS (no GEE)")
print("=" * 60)
t_total = time.perf_counter()
results = {}
print("\n[1/5] Forest cover statistics (Hansen)...")
try:
results["forest_stats"] = compute_forest_stats_local()
except FileNotFoundError as e:
print(f" Skipped: {e}")
print("[2/5] Annual forest loss (Hansen)...")
try:
results["forest_loss_annual"] = compute_forest_loss_by_year_local()
except FileNotFoundError as e:
print(f" Skipped: {e}")
print("[3/5] Surface water statistics (JRC)...")
try:
results["water_stats"] = compute_water_stats_local()
except FileNotFoundError as e:
print(f" Skipped: {e}")
print(f"[4/5] Rainfall statistics (CHIRPS {year})...")
try:
results["rainfall_stats"] = compute_rainfall_stats_local(year)
except FileNotFoundError as e:
print(f" Skipped: {e}")
print("[5/5] Population statistics (WorldPop)...")
try:
results["population_stats"] = compute_population_stats_local()
except FileNotFoundError as e:
print(f" Skipped: {e}")
elapsed_total = time.perf_counter() - t_total
n_completed = sum(1 for v in results.values() if v is not None)
print(f"\nLocal analysis complete: {n_completed}/5 modules in {elapsed_total:.2f}s")
return results