-
Notifications
You must be signed in to change notification settings - Fork 6
/
Makefile
356 lines (308 loc) · 12.2 KB
/
Makefile
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
.PHONY: biophysical_table_shade station_measurements landsat_features \
regression_df regressor swiss_dem tair_regr_maps ref_et calibrate_ucm \
tair_ucm_maps download_zenodo_data
#################################################################################
# GLOBALS #
#################################################################################
## variables
DATA_DIR = data
DATA_RAW_DIR := $(DATA_DIR)/raw
DATA_INTERIM_DIR := $(DATA_DIR)/interim
DATA_PROCESSED_DIR := $(DATA_DIR)/processed
MODELS_DIR = models
CODE_DIR = lausanne_heat_islands
NOTEBOOKS_DIR = notebooks
## rules
define MAKE_DATA_SUB_DIR
$(DATA_SUB_DIR): | $(DATA_DIR)
mkdir $$@
endef
$(DATA_DIR):
mkdir $@
$(foreach DATA_SUB_DIR, \
$(DATA_RAW_DIR) $(DATA_INTERIM_DIR) $(DATA_PROCESSED_DIR), \
$(eval $(MAKE_DATA_SUB_DIR)))
$(MODELS_DIR):
mkdir $@
#################################################################################
# COMMANDS #
#################################################################################
#################################################################################
# Utilities to be used in several tasks
## variables
CRS = EPSG:2056
### code
DOWNLOAD_S3_PY := $(CODE_DIR)/download_s3.py
UTILS_PY := $(CODE_DIR)/utils.py
#################################################################################
# LULC
## Download the agglom. extent, LULC, tree canopy and compute the shade column of
## the biophysical table
### variables
AGGLOM_EXTENT_DIR := $(DATA_RAW_DIR)/agglom-extent
AGGLOM_EXTENT_ZENODO_URI = \
https://zenodo.org/record/4311544/files/agglom-extent.zip?download=1
AGGLOM_EXTENT_SHP := $(AGGLOM_EXTENT_DIR)/agglom-extent.shp
AGGLOM_LULC_ZENODO_URI = \
https://zenodo.org/record/4311544/files/agglom-lulc.tif?download=1
AGGLOM_LULC_TIF := $(DATA_RAW_DIR)/agglom-lulc.tif
TREE_CANOPY_ZENODO_URI = \
https://zenodo.org/record/4310112/files/tree-canopy.tif?download=1
TREE_CANOPY_TIF := $(DATA_RAW_DIR)/tree-canopy.tif
BIOPHYSICAL_TABLE_ZENODO_URI = \
https://zenodo.org/record/4384675/files/biophysical-table.csv?download=1
BIOPHYSICAL_TABLE_CSV := $(DATA_RAW_DIR)/biophysical-table.csv
BIOPHYSICAL_TABLE_SHADE_CSV := $(DATA_INTERIM_DIR)/biophysical-table-shade.csv
#### code
MAKE_BIOPHYSICAL_TABLE_SHADE_PY := $(CODE_DIR)/make_biophysical_table_shade.py
### rules
$(AGGLOM_EXTENT_DIR): | $(DATA_RAW_DIR)
mkdir $@
$(AGGLOM_EXTENT_DIR)/%.zip: | $(AGGLOM_EXTENT_DIR)
wget $(AGGLOM_EXTENT_ZENODO_URI) -O $@
$(AGGLOM_EXTENT_DIR)/%.shp: $(AGGLOM_EXTENT_DIR)/%.zip
unzip $< -d $(AGGLOM_EXTENT_DIR)
touch $@
$(AGGLOM_LULC_TIF): | $(DATA_RAW_DIR)
wget $(AGGLOM_LULC_ZENODO_URI) -O $@
$(TREE_CANOPY_TIF): | $(DATA_RAW_DIR)
wget $(TREE_CANOPY_ZENODO_URI) -O $@
$(BIOPHYSICAL_TABLE_CSV): | $(DATA_RAW_DIR)
wget $(BIOPHYSICAL_TABLE_ZENODO_URI) -O $@
$(BIOPHYSICAL_TABLE_SHADE_CSV): $(AGGLOM_LULC_TIF) $(TREE_CANOPY_TIF) \
$(BIOPHYSICAL_TABLE_CSV) $(MAKE_BIOPHYSICAL_TABLE_SHADE_PY)
python $(MAKE_BIOPHYSICAL_TABLE_SHADE_PY) $(AGGLOM_LULC_TIF) \
$(TREE_CANOPY_TIF) $(BIOPHYSICAL_TABLE_CSV) $@
biophysical_table_shade: $(BIOPHYSICAL_TABLE_SHADE_CSV)
#################################################################################
# STATIONS
### variables
STATION_RAW_DIR := $(DATA_RAW_DIR)/stations
LANDSAT_TILES_ZENODO_URI = \
https://zenodo.org/record/4384675/files/landsat-tiles.csv?download=1
LANDSAT_TILES_CSV := $(DATA_RAW_DIR)/landsat-tiles.csv
STATION_RAW_FILENAMES = agrometeo-tre200s0.csv meteoswiss-lausanne-tre000s0.zip \
meteoswiss-lausanne-tre200s0.zip WSLLAF.txt \
VaudAir_EnvoiTemp20180101-20200128_EPFL_20200129.xlsx
STATION_RAW_FILEPATHS := $(addprefix $(STATION_RAW_DIR)/, \
$(STATION_RAW_FILENAMES))
STATION_LOCATIONS_ZENODO_URI = \
https://zenodo.org/record/4384675/files/station-locations.csv?download=1
STATION_LOCATIONS_CSV := $(STATION_RAW_DIR)/station-locations.csv
STATION_TAIR_CSV := $(DATA_INTERIM_DIR)/station-tair.csv
#### code
MAKE_STATION_TAIR_DF_PY := $(CODE_DIR)/make_station_tair_df.py
### rules
$(LANDSAT_TILES_CSV): | $(DATA_RAW_DIR)
wget $(LANDSAT_TILES_ZENODO_URI) -O $@
$(STATION_RAW_DIR): | $(DATA_RAW_DIR)
mkdir $@
$(STATION_LOCATIONS_CSV): | $(STATION_RAW_DIR)
wget $(STATION_LOCATIONS_ZENODO_URI) -O $@
define DOWNLOAD_STATION_DATA
$(STATION_RAW_DIR)/$(STATION_RAW_FILENAME): | $(STATION_RAW_DIR)
python $(DOWNLOAD_S3_PY) \
cantons/vaud/air-temperature/$(STATION_RAW_FILENAME) $$@
endef
$(foreach STATION_RAW_FILENAME, $(STATION_RAW_FILENAMES), \
$(eval $(DOWNLOAD_STATION_DATA)))
$(STATION_TAIR_CSV): $(LANDSAT_TILES_CSV) $(STATION_RAW_FILEPATHS) \
$(MAKE_STATION_TAIR_DF_PY) | $(DATA_INTERIM_DIR)
python $(MAKE_STATION_TAIR_DF_PY) $(LANDSAT_TILES_CSV) \
$(STATION_RAW_DIR) $@
station_measurements: $(STATION_TAIR_CSV)
#################################################################################
# REGRESSION
CODE_REGRESSION_DIR := $(CODE_DIR)/regression
## 1. Landsat features
### variables
REGRESSION_DIR := $(DATA_INTERIM_DIR)/regression
LANDSAT_FEATURES_NC := $(REGRESSION_DIR)/landsat-features.nc
# Notes:
# LC08_L1TP_195028_20190809_20190820_01_T1 has a "negligible" cloud stripe
# LC08_L1TP_195028_20180518_20180604_01_T1 excluded due to clouds affecting NDWI
#### code
MAKE_LANDSAT_FEATURES_PY := $(CODE_REGRESSION_DIR)/make_landsat_features.py
### rules
$(REGRESSION_DIR): | $(DATA_INTERIM_DIR)
mkdir $@
$(LANDSAT_FEATURES_NC): $(LANDSAT_TILES_CSV) $(AGGLOM_EXTENT_SHP) \
$(MAKE_LANDSAT_FEATURES_PY) | $(REGRESSION_DIR)
python $(MAKE_LANDSAT_FEATURES_PY) $(LANDSAT_TILES_CSV) \
$(AGGLOM_EXTENT_SHP) $@
landsat_features: $(LANDSAT_FEATURES_NC)
## 2. Download the data and assemble a data frame of the air temperature stations
### variables
REGRESSION_DF_CSV := $(REGRESSION_DIR)/regression-df.csv
#### code
MAKE_REGRESSION_DF_PY := $(CODE_REGRESSION_DIR)/make_regression_df.py
### rules
$(REGRESSION_DF_CSV): $(STATION_LOCATIONS_CSV) $(STATION_TAIR_CSV) \
$(LANDSAT_FEATURES_NC) $(MAKE_REGRESSION_DF_PY)
python $(MAKE_REGRESSION_DF_PY) $(STATION_LOCATIONS_CSV) \
$(STATION_TAIR_CSV) $(LANDSAT_FEATURES_NC) $@
regression_df: $(REGRESSION_DF_CSV)
## 3. Train a regressor
### variables
REGRESSOR_JOBLIB := $(MODELS_DIR)/regressor.joblib
#### code
MAKE_REGRESSOR_PY := $(CODE_REGRESSION_DIR)/make_regressor.py
### rules
$(REGRESSOR_JOBLIB): $(REGRESSION_DF_CSV) $(MAKE_REGRESSOR_PY) | $(MODELS_DIR)
python $(MAKE_REGRESSOR_PY) $< $@
regressor: $(REGRESSOR_JOBLIB)
## 4. Predict
### variables
#### first, download and reproject the dem
DHM200_DIR := $(DATA_RAW_DIR)/dhm200
DHM200_URI = \
https://data.geo.admin.ch/ch.swisstopo.digitales-hoehenmodell_25/data.zip
DHM200_ASC := $(DHM200_DIR)/DHM200.asc
SWISS_DEM_TIF := $(DATA_INTERIM_DIR)/swiss-dem.tif
#### predicted air temperature data array
TAIR_REGR_MAPS_NC := $(DATA_PROCESSED_DIR)/tair-regr-maps.nc
#### code
MAKE_TAIR_REGR_MAPS_PY := $(CODE_REGRESSION_DIR)/make_tair_regr_maps.py
### rules
$(DHM200_DIR): | $(DATA_RAW_DIR)
mkdir $@
$(DHM200_DIR)/%.zip: | $(DHM200_DIR)
wget $(DHM200_URI) -O $@
$(DHM200_DIR)/%.asc: $(DHM200_DIR)/%.zip
unzip -j $< 'data/DHM200*' -d $(DHM200_DIR)
touch $@
#### reproject ASCII grid. See https://bit.ly/2WEBxoL
TEMP_VRT := $(DATA_INTERIM_DIR)/temp.vrt
$(SWISS_DEM_TIF): $(DHM200_ASC)
gdalwarp -s_srs EPSG:21781 -t_srs $(CRS) -of vrt $< $(TEMP_VRT)
gdal_translate -of GTiff $(TEMP_VRT) $@
rm $(TEMP_VRT)
swiss_dem: $(SWISS_DEM_TIF)
$(TAIR_REGR_MAPS_NC): $(AGGLOM_EXTENT_SHP) $(STATION_TAIR_CSV) \
$(LANDSAT_FEATURES_NC) $(SWISS_DEM_TIF) $(REGRESSOR_JOBLIB) \
$(MAKE_TAIR_REGR_MAPS_PY) | $(DATA_PROCESSED_DIR)
python $(MAKE_TAIR_REGR_MAPS_PY) $(AGGLOM_EXTENT_SHP) \
$(STATION_TAIR_CSV) $(LANDSAT_FEATURES_NC) $(SWISS_DEM_TIF) \
$(REGRESSOR_JOBLIB) $@
tair_regr_maps: $(TAIR_REGR_MAPS_NC)
#################################################################################
# InVEST urban cooling model
CODE_INVEST_DIR := $(CODE_DIR)/invest
## 0. Some code that we need for all the experiments
### variables
DATA_INVEST_DIR := $(DATA_INTERIM_DIR)/invest
REF_ET_NC := $(DATA_INVEST_DIR)/ref-et.nc
#### code
MAKE_REF_ET_PY := $(CODE_INVEST_DIR)/make_ref_et.py
### rules
$(DATA_INVEST_DIR): | $(DATA_INTERIM_DIR)
mkdir $@
$(REF_ET_NC): $(AGGLOM_LULC_TIF) $(AGGLOM_EXTENT_SHP) $(STATION_TAIR_CSV) \
$(MAKE_REF_ET_PY) | $(DATA_INVEST_DIR)
python $(MAKE_REF_ET_PY) $(AGGLOM_LULC_TIF) $(AGGLOM_EXTENT_SHP) \
$(STATION_TAIR_CSV) $@
ref_et: $(REF_ET_NC)
## 1. Calibrate the model
### variables
CALIBRATED_PARAMS_JSON := $(DATA_INVEST_DIR)/calibrated-params.json
#### code
MAKE_CALIBRATE_UCM_PY := $(CODE_INVEST_DIR)/make_calibrate_ucm.py
### rules
$(CALIBRATED_PARAMS_JSON): $(AGGLOM_LULC) $(BIOPHYSICAL_TABLE_SHADE_CSV) \
$(REF_ET_NC) $(STATION_LOCATIONS_CSV) $(STATION_TAIR_CSV) \
$(MAKE_CALIBRATE_UCM_PY)
python $(MAKE_CALIBRATE_UCM_PY) $(AGGLOM_LULC_TIF) \
$(BIOPHYSICAL_TABLE_SHADE_CSV) $(REF_ET_NC) \
$(STATION_LOCATIONS_CSV) $(STATION_TAIR_CSV) $@
calibrate_ucm: $(CALIBRATED_PARAMS_JSON)
## 2. Simulate the air temperature maps
### variables
TAIR_UCM_MAPS_NC := $(DATA_PROCESSED_DIR)/tair-ucm-maps.nc
#### code
MAKE_TAIR_UCM_MAPS_PY := $(CODE_INVEST_DIR)/make_tair_ucm_maps.py
### rules
$(TAIR_UCM_MAPS_NC): $(CALIBRATED_PARAMS_JSON) $(AGGLOM_EXTENT_SHP) \
$(AGGLOM_LULC_TIF) $(BIOPHYSICAL_TABLE_SHADE_CSV) $(REF_ET_NC) \
$(STATION_TAIR_CSV) $(STATION_LOCATIONS_CSV) $(MAKE_TAIR_UCM_MAPS_PY) \
| $(DATA_PROCESSED_DIR)
python $(MAKE_TAIR_UCM_MAPS_PY) $(CALIBRATED_PARAMS_JSON) \
$(AGGLOM_EXTENT_SHP) $(AGGLOM_LULC_TIF) \
$(BIOPHYSICAL_TABLE_SHADE_CSV) $(REF_ET_NC) $(STATION_TAIR_CSV) \
$(STATION_LOCATIONS_CSV) $@
tair_ucm_maps: $(TAIR_UCM_MAPS_NC)
#################################################################################
# DEDICATED ZENODO DATA https://zenodo.org/record/4384675
## variables
STATION_TAIR_ZENODO_URI = \
https://zenodo.org/record/4384675/files/station-tair.csv?download=1
REF_ET_ZENODO_URI = https://zenodo.org/record/4384675/files/ref-et.nc?download=1
## rules
### This target serves to bypass some of the steps of the computational workflow
### which require access to proprietary data that we cannot share. This target
### will download some intermediate targets and tweak some prerequisites so that
### the computational workflow can be executed until the end so that all the
### results can be reproduced
download_zenodo_data: $(AGGLOM_LULC_TIF) $(AGGLOM_EXTENT_SHP) | \
$(STATION_RAW_DIR) $(DATA_INTERIM_DIR) $(DATA_INVEST_DIR)
touch $(STATION_RAW_FILEPATHS)
wget --no-use-server-timestamps $(STATION_TAIR_ZENODO_URI) -O \
$(STATION_TAIR_CSV)
wget --no-use-server-timestamps $(REF_ET_ZENODO_URI) -O $(REF_ET_NC)
#################################################################################
# Self Documenting Commands #
#################################################################################
.DEFAULT_GOAL := help
# Inspired by <http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html>
# sed script explained:
# /^##/:
# * save line in hold space
# * purge line
# * Loop:
# * append newline + line to hold space
# * go to next line
# * if line starts with doc comment, strip comment character off and loop
# * remove target prerequisites
# * append hold space (+ newline) to line
# * replace newline plus comments by `---`
# * print line
# Separate expressions are necessary because labels cannot be delimited by
# semicolon; see <http://stackoverflow.com/a/11799865/1968>
.PHONY: help
help:
@echo "$$(tput bold)Available rules:$$(tput sgr0)"
@echo
@sed -n -e "/^## / { \
h; \
s/.*//; \
:doc" \
-e "H; \
n; \
s/^## //; \
t doc" \
-e "s/:.*//; \
G; \
s/\\n## /---/; \
s/\\n/ /g; \
p; \
}" ${MAKEFILE_LIST} \
| LC_ALL='C' sort --ignore-case \
| awk -F '---' \
-v ncol=$$(tput cols) \
-v indent=19 \
-v col_on="$$(tput setaf 6)" \
-v col_off="$$(tput sgr0)" \
'{ \
printf "%s%*s%s ", col_on, -indent, $$1, col_off; \
n = split($$2, words, " "); \
line_length = ncol - indent; \
for (i = 1; i <= n; i++) { \
line_length -= length(words[i]) + 1; \
if (line_length <= 0) { \
line_length = ncol - indent - length(words[i]) - 1; \
printf "\n%*s ", -indent, " "; \
} \
printf "%s ", words[i]; \
} \
printf "\n"; \
}' \
| more $(shell test $(shell uname) = Darwin && echo '--no-init --raw-control-chars')