-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfabdem_twi_alberta.py
More file actions
156 lines (137 loc) · 5.01 KB
/
Copy pathfabdem_twi_alberta.py
File metadata and controls
156 lines (137 loc) · 5.01 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
# ---
# 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, aligned to the ABMI 1 km
# reference grid (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
from utils.compute_report import ComputeReport
from utils.gee_utils import (
define_study_area,
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
USE_TEST_AOI = True # True: small test AOI; False: Alberta
COMPUTE_REPORT = True # write EECU usage report (txt);
# blocks until the export task finishes
# 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=FOCAL_BASE_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_to_reference_grid aggregates TWI to the 1 km ABMI grid
# by area mean and exports it on the grid's exact CRS and
# transform. Set wait=True to block; otherwise monitor progress
# at https://code.earthengine.google.com/tasks
task = export_to_reference_grid(
image=twi,
aoi=aoi,
description="FABDEM_TWI_Alberta_1km",
folder=DRIVE_FOLDER,
file_name_prefix="fabdem_twi_alberta_1km",
wait=False,
)
# 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.
report.log_task(task)
report.write()
# End of script ----