-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhihydrosoil_v2.py
More file actions
538 lines (482 loc) · 17.9 KB
/
Copy pathhihydrosoil_v2.py
File metadata and controls
538 lines (482 loc) · 17.9 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
# ---
# title: HiHydroSoil v2.0 Layers Export
# author: Brendan Casey
# created: 2026-07-10
# inputs:
# - HiHydroSoil v2.0 ImageCollections
# (FutureWater / sat-io)
# - Hydrologic_Soil_Group_250m Image
# (FutureWater / sat-io)
# - Alberta boundary (FAO GAUL level1)
# - XY points asset (may include locations outside AB)
# outputs:
# - Multiband HiHydroSoil images clipped to Alberta,
# exported at native (~250 m) and 1000 m resolution.
# - Per-batch CSVs of point-level extracted values.
# notes:
# HiHydroSoil v2.0 provides global soil hydraulic
# properties at 250 m, derived from SoilGrids250m v2.0
# by FutureWater. Most continuous layers are stored as
# int16 * 10000 and are rescaled to physical units by
# multiplying by 0.0001. The Soil Texture Class (stc)
# and Hydrologic Soil Group (HSG) layers are
# categorical and are exported without rescaling.
#
# Most assets are ImageCollections representing the six
# standard soil depths. They are collapsed to a
# multiband image using .toBands(), producing band
# names of the form <index>_<asset>. The
# Hydrologic_Soil_Group_250m asset is a single Image.
#
# 1000 m exports use ee.Reducer.mean() for continuous
# layers and ee.Reducer.mode() for categorical layers
# (STC, HSG) to avoid producing meaningless averages of
# class codes.
#
# Citation:
# Simons, G.W.H., R. Koster, P. Droogers. 2020.
# HiHydroSoil v2.0 - A high resolution soil map of
# global hydraulic properties. FutureWater Report 213.
#
# Setup (once):
# pip install earthengine-api
# earthengine authenticate
# Then set EE_PROJECT in _gee_config.py to your
# registered Earth Engine cloud project and run.
# ---
import os
import sys
import ee
# Make utils importable regardless of the working
# directory VS Code runs the script from
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from _gee_config import DRIVE_FOLDER
from utils.compute_report import ComputeReport
from utils.gee_utils import initialize_ee
# 1. Setup ----
# 1.1 User parameters ----
NATIVE_SCALE = 250 # Native resolution (m)
COARSE_SCALE = 1000 # Aggregated resolution (m)
CRS = "EPSG:4326" # Alternative: 'EPSG:3400' (AB 10-TM)
# Base path for HiHydroSoil v2.0 assets.
BASE_PATH = "projects/sat-io/open-datasets/HiHydroSoilv2_0/"
# Optional asset filter. Set to None (or empty list) to
# keep all assets. Otherwise provide a list of asset short
# names from CONTINUOUS_COLLECTIONS and/or
# CATEGORICAL_COLLECTIONS (e.g. ['ksat'], ['stc']). Assets
# not listed here are skipped at load time.
SELECTED_ASSETS = ["ksat"]
# Optional depth filter (per-image, ImageCollection assets
# only). HiHydroSoil collections contain one image per
# soil depth (plus aggregated topsoil/subsoil layers). The
# exact system:index per image is provider-specific. The
# stats section prints available system:index values for
# every selected asset so you can copy the correct strings
# here on a follow-up run. Set to None to keep all images.
DEPTH_FILTER = ["Ksat_0-5cm_M_250m"]
# Continuous (float) ImageCollection assets. Rescaled by
# multiplying with 0.0001.
CONTINUOUS_COLLECTIONS = [
"alpha", # Mualem-van Genuchten alpha (1/cm)
"crit-wilt", # Water content pF3 - pF4.2 (m3/m3)
"field-crit", # Water content pF2 - pF3 (m3/m3)
"ksat", # Saturated hydraulic conductivity (cm/d)
"N", # Mualem-van Genuchten N (-)
"ormc", # Organic matter content (%)
"sat-field", # Water content sat - pF2 (m3/m3)
"wcavail", # Available water content (m3/m3)
"wcpf2", # Water content at pF2 (m3/m3)
"wcpf3", # Water content at pF3 (m3/m3)
"wcpf4-2", # Water content at pF4.2 (m3/m3)
"wcres", # Residual water content (m3/m3)
"wcsat", # Saturated water content (m3/m3)
]
# Categorical ImageCollection assets. NOT rescaled; use
# mode() for aggregation.
CATEGORICAL_COLLECTIONS = [
"stc", # Soil Texture Class (1-6)
]
# XY points asset. Must contain a 'batch' property with
# integer values matching the loop range below (N_BATCHES).
XY_POINTS_ASSET = (
"projects/ee-bgcasey-abmi/assets/non_abmi_sites_xy_batch"
)
# Batched extraction parameters.
EXTRACT_SCALE = NATIVE_SCALE # 250 m (COARSE_SCALE for 1 km)
TILE_SCALE = 16 # higher -> more tiles, lower per-tile mem
N_BATCHES = 50 # Match the number of batches assigned in R
PRINT_STATS = True # min/max check (slow for large AOIs)
USE_TEST_AOI = True # True: small test AOI; False: Alberta
COMPUTE_REPORT = True # write EECU usage report (txt)
# 1.2 Initialize Earth Engine ----
# Project ID is read from _gee_config.py
initialize_ee()
# 1.3 Set up compute usage report ----
# Profiles EECU usage per section. Best used with
# USE_TEST_AOI = True to find choke points cheaply.
report = ComputeReport(
"hihydrosoil_v2",
enabled=COMPUTE_REPORT,
)
# 2. Define study area ----
# Uses a small test polygon when USE_TEST_AOI is True;
# otherwise filters the FAO GAUL provinces for Alberta.
if USE_TEST_AOI:
# Small aoi for testing purposes
aoi = ee.Geometry.Polygon([
[-113.5, 55.5], # Top-left corner
[-113.5, 55.0], # Bottom-left corner
[-112.8, 55.0], # Bottom-right corner
[-112.8, 55.5], # Top-right corner
])
else:
aoi = (
ee.FeatureCollection(
"FAO/GAUL_SIMPLIFIED_500m/2015/level1"
)
.filter(ee.Filter.eq("ADM0_NAME", "Canada"))
.filter(ee.Filter.eq("ADM1_NAME", "Alberta"))
.geometry()
)
# 2.1 Apply the asset filter. Empty/None = no filter. The
# Hydrologic_Soil_Group asset is loaded separately (single
# Image). Use the name 'hydrologic_soil_group' to include
# it in the filter.
use_filter = bool(SELECTED_ASSETS)
include_hsg = True
if use_filter:
continuous_collections = [
name for name in CONTINUOUS_COLLECTIONS
if name in SELECTED_ASSETS
]
categorical_collections = [
name for name in CATEGORICAL_COLLECTIONS
if name in SELECTED_ASSETS
]
include_hsg = "hydrologic_soil_group" in SELECTED_ASSETS
else:
continuous_collections = list(CONTINUOUS_COLLECTIONS)
categorical_collections = list(CATEGORICAL_COLLECTIONS)
# 3. Build HiHydroSoil image ----
# Process each collection into a multiband image, rescale
# continuous layers, then combine all layers into a single
# multiband image.
def collection_to_image(asset_name, index_filter):
"""Load a collection and collapse it to a multiband image.
Optionally filters by system:index, collapses to a
multiband image via toBands(), and assigns clean band
names. The image is NOT clipped to AOI here; clipping
is applied later only for raster exports so XY
extraction still gets values outside Alberta.
Band naming rules:
- With a filter, each band is renamed to the
system:index of its source image.
- Without a filter, bands are renamed to
'<asset_name>_<system:index>' to disambiguate
across multiple assets.
The trailing '_b1' that toBands() inserts for
single-band images is stripped in both cases.
Args:
asset_name (str): Short asset name (e.g. 'ksat').
index_filter (list or None): system:index strings
to keep. If None, all images are kept.
Returns:
ee.Image: Multiband image (global extent).
"""
ic = ee.ImageCollection(BASE_PATH + asset_name)
has_filter = bool(index_filter)
if has_filter:
ic = ic.filter(
ee.Filter.inList("system:index", index_filter)
)
# toBands() creates bands '<system:index>_<origBand>'.
img = ic.toBands()
# Strip trailing '_b1' from single-band source images,
# then optionally prefix with the asset name.
def rename_band(bn):
stripped = ee.String(bn).replace("_b1$", "")
if has_filter:
return stripped # system:index already clean
return ee.String(asset_name).cat("_").cat(stripped)
band_names = img.bandNames().map(rename_band)
return img.rename(band_names)
# 3.1 Continuous collections (rescale by 0.0001).
continuous_images = [
collection_to_image(name, DEPTH_FILTER)
.multiply(0.0001)
.toFloat()
for name in continuous_collections
]
# 3.2 Categorical collections (no rescale, Int16).
categorical_images = [
collection_to_image(name, DEPTH_FILTER).toInt16()
for name in categorical_collections
]
# 3.3 Hydrologic Soil Group (single Image, categorical),
# only if it passed the asset filter. Not clipped here.
hsg = None
if include_hsg:
hsg = (
ee.Image(BASE_PATH + "Hydrologic_Soil_Group_250m")
.rename("hydrologic_soil_group")
.toInt16()
)
# 3.4 Combine into continuous and categorical multiband
# images. Either group may be empty after filtering;
# downstream sections guard with the has_* flags.
has_continuous = len(continuous_images) > 0
has_categorical = (
len(categorical_images) > 0 or hsg is not None
)
hihydro_continuous = None
if has_continuous:
hihydro_continuous = ee.Image(continuous_images[0])
for img in continuous_images[1:]:
hihydro_continuous = hihydro_continuous.addBands(img)
hihydro_categorical = None
if has_categorical:
if len(categorical_images) > 0:
hihydro_categorical = ee.Image(categorical_images[0])
for img in categorical_images[1:]:
hihydro_categorical = (
hihydro_categorical.addBands(img)
)
if hsg is not None:
hihydro_categorical = (
hihydro_categorical.addBands(hsg)
)
elif hsg is not None:
hihydro_categorical = hsg
# 3.5 Combine continuous and categorical stacks into a
# single extraction image. At bufferSize = 0 there is no
# reducer distinction, so sampleRegions just reads the
# pixel value for both. Either group may be empty.
hihydro_combined = None
if has_continuous and has_categorical:
hihydro_combined = hihydro_continuous.addBands(
hihydro_categorical
)
elif has_continuous:
hihydro_combined = hihydro_continuous
elif has_categorical:
hihydro_combined = hihydro_categorical
# 4. Check bands (optional) ----
# Print band names, available system:index values, and
# min/max stats. Earth Engine is lazy, so the profiler
# needs an evaluated computation (getInfo) to measure
# per-algorithm EECU usage.
if PRINT_STATS or COMPUTE_REPORT:
with report.section("HiHydroSoil band names"):
if has_continuous:
print(
"HiHydroSoil Continuous bands:",
hihydro_continuous.bandNames().getInfo(),
)
if has_categorical:
print(
"HiHydroSoil Categorical bands:",
hihydro_categorical.bandNames().getInfo(),
)
# 4.1 Inspect collection contents (system:index).
# Use this output to populate DEPTH_FILTER if you want
# to keep only specific depth(s).
with report.section("Inspect system:index values"):
assets_to_inspect = (
continuous_collections + categorical_collections
)
for name in assets_to_inspect:
ic = ee.ImageCollection(BASE_PATH + name)
ids = ic.aggregate_array("system:index").getInfo()
print(name + " system:index values:", ids)
# 4.2 Print min/max for all continuous bands.
if has_continuous:
with report.section("Continuous band min/max"):
bands = hihydro_continuous.bandNames().getInfo()
for band in bands:
stats = (
hihydro_continuous.select(band)
.reduceRegion(
reducer=ee.Reducer.minMax(),
geometry=aoi,
scale=1000,
maxPixels=1e13,
bestEffort=True,
tileScale=4,
)
.getInfo()
)
print(band + " Min and Max:", stats)
# 4.3 Confirm the native projection of a sample asset.
with report.section("Projection check"):
hh = ee.Image(
"projects/sat-io/open-datasets/HiHydroSoilv2_0/"
"ksat/Ksat_0-5cm_M_250m"
)
print(
"HiHydroSoil projection:",
hh.projection().getInfo(),
)
# 5. Extract HiHydroSoil values to XY points (batched) ----
# Use sampleRegions to extract the pixel value at each XY
# location. With large point sets a single extraction
# exceeds GEE's per-tile memory cap, so the points asset is
# pre-tagged with a 'batch' column (set in R before upload)
# and this loop launches one export task per batch. Each
# batch exports a separate CSV named
# 'hihydrosoil_xy_batchNN'. Merge the CSVs in R afterward.
# 5.1 Load XY points.
xy_points = ee.FeatureCollection(XY_POINTS_ASSET)
# 5.2 Diagnostic: inspect the batch column. If distinct
# batch values print as strings (e.g. '1', '2', ...)
# instead of numbers, the column is stored as character
# and the Filter.eq calls below need to pass strings, e.g.
# ee.Filter.eq('batch', ee.Number(b).format())
if PRINT_STATS or COMPUTE_REPORT:
with report.section("Batch diagnostics"):
print("Total points:", xy_points.size().getInfo())
print(
"First feature properties:",
xy_points.first().getInfo(),
)
print(
"Distinct batch values:",
xy_points.aggregate_array("batch")
.distinct()
.sort()
.getInfo(),
)
# First rows of batch 1 for a sanity check.
if hihydro_combined is not None:
batch1 = xy_points.filter(
ee.Filter.eq("batch", 1)
)
sample = hihydro_combined.sampleRegions(
collection=batch1.limit(5),
scale=EXTRACT_SCALE,
tileScale=TILE_SCALE,
geometries=False,
)
print(
"Batch 1 sample extraction (first 5):",
sample.getInfo(),
)
# 5.3 Launch one export task per batch. Loop runs
# 1..N_BATCHES (inclusive) to match the 1-indexed batch
# values assigned in R.
if hihydro_combined is not None:
for b in range(1, N_BATCHES + 1):
batch_pts = xy_points.filter(
ee.Filter.eq("batch", b)
)
extracted = hihydro_combined.sampleRegions(
collection=batch_pts,
scale=EXTRACT_SCALE,
tileScale=TILE_SCALE,
geometries=False,
)
# Zero-pad batch number to 2 digits for filenames.
batch_str = str(b).zfill(2)
task = ee.batch.Export.table.toDrive(
collection=extracted,
description="hihydrosoil_xy_batch" + batch_str,
folder=DRIVE_FOLDER,
fileNamePrefix="hihydrosoil_xy_batch" + batch_str,
fileFormat="CSV",
)
task.start()
print("Started export task:", task.config[
"description"
])
# 6. Aggregate to 1000 m (Alberta only) ----
# Clip to AOI first so aggregation and export only operate
# on Alberta pixels. setDefaultProjection is required
# before reduceResolution when aggregating by more than a
# factor of 64.
hihydro_continuous_ab = None
hihydro_categorical_ab = None
hihydro_continuous_1km = None
hihydro_categorical_1km = None
# 6.1 Continuous: clip to AB and aggregate to 1 km (mean).
if has_continuous:
hihydro_continuous_ab = hihydro_continuous.clip(aoi)
hihydro_continuous_1km = (
hihydro_continuous_ab.setDefaultProjection(
crs=CRS, scale=NATIVE_SCALE
)
.reduceResolution(
reducer=ee.Reducer.mean(), maxPixels=1024
)
.reproject(crs=CRS, scale=COARSE_SCALE)
.toFloat()
)
# 6.2 Categorical: clip to AB and aggregate to 1 km (mode).
if has_categorical:
hihydro_categorical_ab = hihydro_categorical.clip(aoi)
hihydro_categorical_1km = (
hihydro_categorical_ab.setDefaultProjection(
crs=CRS, scale=NATIVE_SCALE
)
.reduceResolution(
reducer=ee.Reducer.mode(), maxPixels=1024
)
.reproject(crs=CRS, scale=COARSE_SCALE)
.toInt16()
)
# 7. Export raster outputs ----
# Export native-resolution and 1000 m images to Google
# Drive. These are large exports (especially native
# continuous). Monitor the Tasks tab and expect
# substantial processing time. Either group is skipped if
# no assets passed the filter.
if has_continuous:
# 7.1 Continuous layers - native resolution (~250 m).
ee.batch.Export.image.toDrive(
image=hihydro_continuous_ab,
description="HiHydroSoil_Continuous_AB_250m",
folder=DRIVE_FOLDER,
fileNamePrefix="hihydrosoil_continuous_ab_250m",
region=aoi,
scale=NATIVE_SCALE,
crs=CRS,
maxPixels=1e13,
).start()
# 7.2 Continuous layers - 1000 m.
ee.batch.Export.image.toDrive(
image=hihydro_continuous_1km,
description="HiHydroSoil_Continuous_AB_1000m",
folder=DRIVE_FOLDER,
fileNamePrefix="hihydrosoil_continuous_ab_1000m",
region=aoi,
scale=COARSE_SCALE,
crs=CRS,
maxPixels=1e13,
).start()
if has_categorical:
# 7.3 Categorical layers - native resolution (~250 m).
ee.batch.Export.image.toDrive(
image=hihydro_categorical_ab,
description="HiHydroSoil_Categorical_AB_250m",
folder=DRIVE_FOLDER,
fileNamePrefix="hihydrosoil_categorical_ab_250m",
region=aoi,
scale=NATIVE_SCALE,
crs=CRS,
maxPixels=1e13,
).start()
# 7.4 Categorical layers - 1000 m.
ee.batch.Export.image.toDrive(
image=hihydro_categorical_1km,
description="HiHydroSoil_Categorical_AB_1000m",
folder=DRIVE_FOLDER,
fileNamePrefix="hihydrosoil_categorical_ab_1000m",
region=aoi,
scale=COARSE_SCALE,
crs=CRS,
maxPixels=1e13,
).start()
# 8. Compute usage report ----
# Multiple export tasks are launched above, so this does
# not block on any single task; it writes the collected
# section profiles to gee_compute_reports/.
report.write()
# End of script ----