-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfabdem_dev_alberta.py
More file actions
238 lines (218 loc) · 8.38 KB
/
Copy pathfabdem_dev_alberta.py
File metadata and controls
238 lines (218 loc) · 8.38 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
# ---
# title: FABDEM DEV (Deviation from Mean Elevation) for Alberta
# author: Brendan Casey
# created: 2026-07-11
# inputs:
# - FABDEM ImageCollection
# (projects/sat-io/open-datasets/FABDEM)
# - AB2020 provincial boundary (Earth Engine asset;
# _gee_config.PROVINCIAL_BOUNDARY_ASSET) for the crop
# outputs:
# - One DEV GeoTIFF per focal radius 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 DEV (deviation from mean
# elevation) from the FABDEM bare-earth DEM (30 m, forests
# and buildings removed), following De Reu et al. (2013):
#
# DEV = (z - mean_z) / SD_z
#
# where mean_z and SD_z are the mean and standard deviation
# of elevation within a focal window. The numerator is the
# Topographic Position Index (TPI); dividing by
# SD_z standardizes it by local relief, so DEV is expressed
# in standard-deviation units rather than metres. A 5 m rise
# on flat muskeg and a 300 m ridge in the Rockies can then
# score alike, and values are comparable across radii by
# construction. DEV is computed at each focal radius in
# DEV_RADII, aggregated to the 1 km reference grid, and
# exported to Google Drive per radius.
#
# DEV 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
#
# De Reu, J., et al. (2013). Application of the topographic
# position index to heterogeneous landscapes. Geomorphology,
# 186, 39-49. doi:10.1016/j.geomorph.2012.12.015
#
# 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 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 DEV is computed at before
# aggregating to 1 km (>= ~50 m for a full-province run; keep
# it <= ~min(DEV_RADII) / 10 so the focal window is well
# resolved).
FOCAL_BASE_M = 50
DEV_RADII = [250, 1000, 2000] # one export per radius
# Raster export target, applied to every radius. "reference_grid"
# aggregates DEV (area mean) onto the ABMI 1 km grid so it stacks
# with the other 1 km covariates. "native" skips the aggregation
# and exports DEV at FOCAL_BASE_M in the grid CRS, ungridded.
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
if EXPORT_TARGET not in ("native", "reference_grid"):
raise ValueError(
"Unknown EXPORT_TARGET: "
f"{EXPORT_TARGET!r} (use 'native' or 'reference_grid')"
)
DEV_WINDOW_SHAPE = "circle" # "circle" or "square"
DEV_UNITS = "meters" # "meters" or "pixels"
SD_EPSILON = 0.001 # floor for SD_z to avoid divide-by-zero
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_dev_alberta",
enabled=COMPUTE_REPORT,
)
# 2. Define study area ----
# aoi is the export / crop boundary; aoi_compute adds a ring
# (the largest focal radius) so the focal mean and SD are
# unbiased at the true AOI edge.
aoi, aoi_compute = define_study_area(
use_test_aoi=USE_TEST_AOI,
buffer_m=max(max(DEV_RADII), AGG_BUFFER_M),
)
# 3. Prepare the DEM ----
# FABDEM at the FOCAL_BASE_M base projection so the focal
# radius maps to real ground distance. The same elevation
# image feeds every focal radius below.
elevation = fabdem_elevation(aoi_compute, base_m=FOCAL_BASE_M)
# 4. Compute, aggregate, and export DEV per focal radius ----
# For each radius in DEV_RADII, elevation mean and SD are
# reduced over one shared kernel, DEV = (z - mean) / SD is
# formed, aggregated to the 1 km grid, and exported to Google
# Drive as its own GeoTIFF. Larger radii use bigger kernels
# and cost proportionally more compute; the per-task batch
# EECU-seconds in the report show where. Set wait=True on the
# export to block; otherwise monitor progress at
# https://code.earthengine.google.com/tasks
tasks = []
for radius in DEV_RADII:
# One kernel drives both the mean and SD reductions, so
# numerator and denominator share the exact same window.
if DEV_WINDOW_SHAPE == "circle":
kernel = ee.Kernel.circle(radius=radius, units=DEV_UNITS)
elif DEV_WINDOW_SHAPE == "square":
kernel = ee.Kernel.square(radius=radius, units=DEV_UNITS)
else:
raise ValueError(
f"Unsupported window shape: {DEV_WINDOW_SHAPE}"
)
# Mean and standard deviation of elevation in the window
mean_z = elevation.reduceNeighborhood(
reducer=ee.Reducer.mean(),
kernel=kernel,
)
sd_z = elevation.reduceNeighborhood(
reducer=ee.Reducer.stdDev(),
kernel=kernel,
)
# DEV: TPI (z - mean) standardized by local relief (SD).
# Floor SD at SD_EPSILON so near-flat windows (SD ~ 0) do
# not blow up the ratio. DEV is unitless (SD units) and
# continuous, so round_values stays False on export.
dev = (
elevation
.subtract(mean_z)
.divide(sd_z.max(SD_EPSILON))
.rename(f"dev_{radius}")
)
# 4.1 Export this radius at the chosen EXPORT_TARGET ----
# "reference_grid" aggregates DEV to the 1 km grid; "native"
# writes it at FOCAL_BASE_M in the grid CRS instead.
# EXPORT_TARGET is validated once at the top of the script.
if EXPORT_TARGET == "reference_grid":
task = export_to_reference_grid(
image=dev,
aoi=aoi,
description=f"FABDEM_DEV_Alberta_r{radius}_abmi1km",
folder=DRIVE_FOLDER,
file_name_prefix=f"fabdem_dev_alberta_r{radius}_abmi1km",
wait=False,
)
else:
task = export_image_to_drive(
image=dev.clip(aoi),
description=(
f"FABDEM_DEV_Alberta_r{radius}_native"
),
region=aoi,
folder=DRIVE_FOLDER,
file_name_prefix=(
f"fabdem_dev_alberta_r{radius}_native"
),
scale=FOCAL_BASE_M,
crs=GRID_CRS,
max_pixels=1e13,
wait=False,
)
tasks.append(task)
# 5. Compute usage report ----
# This section waits for each 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:
for task in tasks:
report.log_task(task)
report.write()
# End of script ----