-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfabdem_twi_alberta.py
More file actions
207 lines (187 loc) · 7.03 KB
/
Copy pathfabdem_twi_alberta.py
File metadata and controls
207 lines (187 loc) · 7.03 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
# ---
# title: FABDEM Topographic Wetness Index for Alberta
# author: Brendan Casey
# created: 2026-07-10
# inputs:
# - FABDEM ImageCollection
# (projects/sat-io/open-datasets/FABDEM)
# - MERIT Hydro upslope area (MERIT/Hydro/v1_0_1)
# - AB2020 provincial boundary (Earth Engine asset;
# _gee_config.PROVINCIAL_BOUNDARY_ASSET) for the crop
# outputs:
# - TWI GeoTIFF for Alberta, either aligned to the ABMI 1 km
# reference grid or at FOCAL_BASE_M resolution,
# selected by EXPORT_TARGET (exported to Google Drive)
# notes:
# This script calculates the Topographic Wetness Index
# (TWI) as ln(a / tan(b)), where a is upslope drainage
# area (m^2) and b is slope (radians).
#
# FABDEM is a bare-earth DEM with no flow-accumulation
# band, and Earth Engine has no native flow-accumulation
# algorithm. This script therefore uses a hybrid: slope
# from FABDEM (computed at FOCAL_BASE_M) and upslope area
# from MERIT Hydro 'upa' (~90 m). The result is aggregated
# to the ABMI 1 km reference grid and exported so it stacks
# with the other grid layers.
#
# Slope is computed at FOCAL_BASE_M (50 m) rather than the
# native 30 m so the 1 km aggregation stays under Earth
# Engine's per-tile reprojection limit (see
# utils.gee_utils.to_reference_grid). The grid / boundary /
# aggregation / export plumbing lives in utils/gee_utils.py.
#
# Data citations:
# Hawker, L., et al. (2022). A 30 m global map of
# elevation with forests and buildings removed.
# Environmental Research Letters, 17(2), 024016.
# doi:10.1088/1748-9326/ac4d4f
#
# Yamazaki, D., et al. (2019). MERIT Hydro: A
# high-resolution global hydrography map based on latest
# topography datasets. Water Resources Research, 55,
# 5053-5073. doi:10.1029/2019WR024873
#
# 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 the
# script.
# ---
import math
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, GRID_CRS
from utils.compute_report import ComputeReport
from utils.gee_utils import (
define_study_area,
export_image_to_drive,
export_to_reference_grid,
fabdem_elevation,
initialize_ee,
)
# 1. Setup ----
# 1.1 User parameters ----
# FOCAL_BASE_M is the resolution slope is computed at before
# aggregating to 1 km (>= ~50 m for a full-province run). Slope
# uses a 3x3 neighborhood, so the compute buffer only needs
# about one base pixel.
FOCAL_BASE_M = 50
# Raster export target. "reference_grid" aggregates TWI (area
# mean) onto the ABMI 1 km grid so it stacks with the other 1 km
# covariates. "native" skips the aggregation and exports the
# image at FOCAL_BASE_M in the grid CRS, ungridded - useful for
# inspecting the input to the aggregation.
EXPORT_TARGET = "reference_grid" # "native" or "reference_grid"
# Compute ring grown around the aoi before the source is
# clipped, sized at 2x the output scale. Every output pixel -
# a 1 km grid cell or a native pixel - is then built from a
# full neighbourhood rather than one truncated at the aoi
# edge; a 1 km cell can touch the aoi at a corner and still
# reach a full diagonal (1414 m) beyond it. The exported
# image is clipped back to the plain aoi, so the ring never
# widens the output.
COARSE_SCALE = 1000 # ABMI reference grid cell (m)
AGG_BUFFER_M = 2 * (
COARSE_SCALE
if EXPORT_TARGET == "reference_grid"
else FOCAL_BASE_M
)
BUFFER_MAX_ERROR_M = 100
USE_TEST_AOI = True # True: small test AOI; False: Alberta
COMPUTE_REPORT = True # write EECU usage report (txt)
# Block until every export task finishes so its batch
# EECU-seconds land in the compute report. Costs the full
# export runtime (hours for a province-wide run), so keep it
# False for production runs and turn it on when profiling a
# test AOI.
WAIT_FOR_EXPORTS = False
# 1.2 Initialize Earth Engine ----
# Project ID is read from _gee_config.py
initialize_ee()
# 1.3 Set up compute usage report ----
# Records total EECU-seconds for each export task.
# Best used with USE_TEST_AOI = True to gauge compute
# cost cheaply before a full-province run.
report = ComputeReport(
"fabdem_twi_alberta",
enabled=COMPUTE_REPORT,
)
# 2. Define study area ----
# aoi is the export / crop boundary; aoi_compute adds a one-
# pixel ring so the 3x3 slope kernel is unbiased at the edge.
aoi, aoi_compute = define_study_area(
use_test_aoi=USE_TEST_AOI,
buffer_m=max(FOCAL_BASE_M, AGG_BUFFER_M),
)
# 3. TWI calculation ----
# TWI from FABDEM slope and MERIT Hydro upslope area. It
# produces a single-band TWI image.
# FABDEM at the FOCAL_BASE_M base projection, then slope.
elevation = fabdem_elevation(aoi_compute, base_m=FOCAL_BASE_M)
slope = ee.Terrain.slope(elevation)
# Upslope area from MERIT Hydro, converted km^2 -> m^2. MERIT
# 'upa' is a stored (pyramided) dataset, so it needs no coarse
# base; clip to the buffered AOI to match the elevation extent.
upslope_area = (
ee.Image("MERIT/Hydro/v1_0_1")
.select("upa")
.clip(aoi_compute)
.multiply(1e6)
.rename("upslope_area")
)
# Convert slope from degrees to radians
slope_rad = slope.multiply(math.pi / 180).rename("slope_rad")
# Floor tan(b) at a small value so flat areas (slope 0) are
# not masked by division by zero
tan_b = slope_rad.tan().max(0.001)
# Calculate TWI: ln(a / tan(b)). Continuous, so no rounding.
twi = upslope_area.divide(tan_b).log().rename("twi")
# 4. Aggregate to the grid and export ----
# EXPORT_TARGET picks the output. "reference_grid" hands TWI to
# export_to_reference_grid, which aggregates it to the 1 km ABMI
# grid by area mean and exports it on the grid's exact CRS and
# transform. "native" skips the aggregation and writes TWI at
# FOCAL_BASE_M in the grid CRS instead. Set wait=True to block;
# otherwise monitor progress at
# https://code.earthengine.google.com/tasks
if EXPORT_TARGET == "reference_grid":
task = export_to_reference_grid(
image=twi,
aoi=aoi,
description="FABDEM_TWI_Alberta_abmi1km",
folder=DRIVE_FOLDER,
file_name_prefix="fabdem_twi_alberta_abmi1km",
wait=False,
)
elif EXPORT_TARGET == "native":
task = export_image_to_drive(
image=twi.clip(aoi),
description="FABDEM_TWI_Alberta_native",
region=aoi,
folder=DRIVE_FOLDER,
file_name_prefix="fabdem_twi_alberta_native",
scale=FOCAL_BASE_M,
crs=GRID_CRS,
max_pixels=1e13,
wait=False,
)
else:
raise ValueError(
"Unknown EXPORT_TARGET: "
f"{EXPORT_TARGET!r} (use 'native' or 'reference_grid')"
)
# 5. Compute usage report ----
# This section waits for the export to finish, records
# its total EECU-seconds, and writes the txt report to
# gee_compute_reports/. Note: a full-province export can
# take hours; for a quick profile use the test AOI.
if WAIT_FOR_EXPORTS:
report.log_task(task)
report.write()
# End of script ----