-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
326 lines (285 loc) · 11.4 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
.PHONY: reclassify station_measurements ref_et tair_ucm scenarios \
scenario_metrics statpop 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_greening_scenarios
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
### code
DOWNLOAD_S3_PY := $(CODE_DIR)/download_s3.py
#################################################################################
# LULC
## 1. Download the data
### 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
CADASTRE_DIR := $(DATA_RAW_DIR)/cadastre
CADASTRE_FILE_KEY = cantons/vaud/cadastre/Cadastre_agglomeration.zip
CADASTRE_UNZIP_FILEPATTERN := \
Cadastre/(NPCS|MOVD)_CAD_TPR_(BATHS|CSBOIS|CSDIV|CSDUR|CSEAU|CSVERT)_S.*
CADASTRE_SHP := $(CADASTRE_DIR)/cadastre.shp
#### code
MAKE_CADASTRE_SHP_FROM_ZIP_PY := $(CODE_DIR)/make_cadastre_shp_from_zip.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 $@
$(CADASTRE_DIR): | $(DATA_RAW_DIR)
mkdir $@
$(CADASTRE_DIR)/%.zip: | $(CADASTRE_DIR)
python $(DOWNLOAD_S3_PY) $(CADASTRE_FILE_KEY) $@
$(CADASTRE_DIR)/%.shp: $(CADASTRE_DIR)/%.zip $(MAKE_CADASTRE_SHP_FROM_ZIP_PY)
python $(MAKE_CADASTRE_SHP_FROM_ZIP_PY) $< $@ \
"$(CADASTRE_UNZIP_FILEPATTERN)"
touch $@
## 2. Reclassify according to tree cover
### variables
# BIOPHYSICAL_TABLE_FILE_KEY := other/biophysical-table.csv
BIOPHYSICAL_TABLE_ZENODO_URI = \
https://zenodo.org/record/4316572/files/biophysical-table.csv?download=1
BIOPHYSICAL_TABLE_CSV := $(DATA_RAW_DIR)/biophysical-table.csv
DATA_RECLASSIF_DIR := $(DATA_INTERIM_DIR)/reclassif
TREE_COVER_TIF := $(DATA_RECLASSIF_DIR)/tree-cover.tif
BLDG_COVER_TIF := $(DATA_RECLASSIF_DIR)/bldg-cover.tif
RECLASSIF_TABLE_CSV := $(DATA_PROCESSED_DIR)/biophysical-table.csv
RECLASSIF_LULC_TIF := $(DATA_PROCESSED_DIR)/agglom-lulc.tif
#### code
CODE_RECLASSIFY_DIR := $(CODE_DIR)/reclassify
MAKE_PIXEL_TREE_COVER_PY := $(CODE_RECLASSIFY_DIR)/make_pixel_tree_cover.py
MAKE_PIXEL_BLDG_COVER_PY := $(CODE_RECLASSIFY_DIR)/make_pixel_bldg_cover.py
MAKE_RECLASSIFY_PY := $(CODE_RECLASSIFY_DIR)/make_reclassify.py
### rules
$(DATA_RECLASSIF_DIR): | $(DATA_INTERIM_DIR)
mkdir $@
$(TREE_COVER_TIF): $(AGGLOM_LULC_TIF) $(TREE_CANOPY_TIF) \
$(MAKE_PIXEL_TREE_COVER_PY) | $(DATA_RECLASSIF_DIR)
python $(MAKE_PIXEL_TREE_COVER_PY) $(AGGLOM_LULC_TIF) \
$(TREE_CANOPY_TIF) $@
$(BLDG_COVER_TIF): $(AGGLOM_LULC_TIF) $(CADASTRE_SHP) \
$(MAKE_PIXEL_BLDG_COVER_PY) | $(DATA_RECLASSIF_DIR)
python $(MAKE_PIXEL_BLDG_COVER_PY) $(AGGLOM_LULC_TIF) \
$(CADASTRE_SHP) $@
$(BIOPHYSICAL_TABLE_CSV): | $(DATA_RAW_DIR)
wget $(BIOPHYSICAL_TABLE_ZENODO_URI) -O $@
$(RECLASSIF_LULC_TIF) $(RECLASSIF_TABLE_CSV): $(TREE_COVER_TIF) \
$(BLDG_COVER_TIF) $(BIOPHYSICAL_TABLE_CSV) $(MAKE_RECLASSIFY_PY) \
| $(DATA_PROCESSED_DIR)
python $(MAKE_RECLASSIFY_PY) $(AGGLOM_LULC_TIF) $(TREE_COVER_TIF) \
$(BLDG_COVER_TIF) $(BIOPHYSICAL_TABLE_CSV) \
$(RECLASSIF_LULC_TIF) $(RECLASSIF_TABLE_CSV)
#### Rule with multiple targets https://bit.ly/35B8YdU
$(RECLASSIF_TABLE_CSV): $(RECLASSIF_LULC_TIF)
reclassify: $(RECLASSIF_LULC_TIF) $(RECLASSIF_TABLE_CSV)
#################################################################################
# STATIONS
### variables
STATION_RAW_DIR := $(DATA_RAW_DIR)/stations
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/4316572/files/station-locations.csv?download=1
STATION_LOCATIONS_CSV := $(STATION_RAW_DIR)/station-locations.csv
STATION_T_CSV := $(DATA_INTERIM_DIR)/station-t.csv
#### code
MAKE_STATION_T_DF_PY := $(CODE_DIR)/make_station_tair_df.py
### rules
$(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_T_CSV): $(STATION_RAW_FILEPATHS) \
$(MAKE_STATION_T_DF_PY) | $(DATA_INTERIM_DIR)
python $(MAKE_STATION_T_DF_PY) $(STATION_RAW_DIR) $@
station_measurements: $(STATION_T_CSV)
#################################################################################
# Scenarios
## 0. Preprocess the inputs required to simulate scenario temperatures with the
## InVEST urban cooling model
### variables
REF_ET_TIF := $(DATA_PROCESSED_DIR)/ref-et.tif
#### code
CODE_SCENARIOS_DIR := $(CODE_DIR)/scenarios
MAKE_REF_ET_PY := $(CODE_SCENARIOS_DIR)/make_ref_et.py
### rules
$(REF_ET_TIF): $(AGGLOM_LULC_TIF) $(AGGLOM_EXTENT_SHP) $(STATION_T_CSV) \
$(MAKE_REF_ET_PY) | $(DATA_PROCESSED_DIR)
python $(MAKE_REF_ET_PY) $(AGGLOM_LULC_TIF) $(AGGLOM_EXTENT_SHP) \
$(STATION_T_CSV) $@
ref_et: $(REF_ET_TIF)
## 1. Generate scenario datasets
### variables
CALIBRATED_PARAMS_ZENODO_URI = \
https://zenodo.org/record/4316572/files/invest-calibrated-params.json?download=1
CALIBRATED_PARAMS_JSON := $(DATA_RAW_DIR)/invest-calibrated-params.json
SCENARIO_DS_NC := $(DATA_PROCESSED_DIR)/scenarios.nc
#### code
MAKE_SCENARIO_DS_PY := $(CODE_SCENARIOS_DIR)/make_scenario_ds.py
### rules
$(CALIBRATED_PARAMS_JSON): | $(DATA_RAW_DIR)
wget $(CALIBRATED_PARAMS_ZENODO_URI) -O $@
$(SCENARIO_DS_NC): $(RECLASSIF_LULC_TIF) $(RECLASSIF_TABLE_CSV) \
$(STATION_T_CSV) $(REF_ET_TIF) $(CALIBRATED_PARAMS_JSON) \
$(MAKE_SCENARIO_DS_PY)
python $(MAKE_SCENARIO_DS_PY) $(RECLASSIF_LULC_TIF) \
$(RECLASSIF_TABLE_CSV) $(STATION_T_CSV) $(REF_ET_TIF) \
$(CALIBRATED_PARAMS_JSON) $@
scenarios: $(SCENARIO_DS_NC)
## 2. Compute landscape metrics of each scenario
### variables
SCENARIO_METRICS_CSV := $(DATA_PROCESSED_DIR)/scenario-metrics.csv
#### code
MAKE_SCENARIO_METRICS_PY := $(CODE_SCENARIOS_DIR)/make_scenario_metrics.py
### rules
$(SCENARIO_METRICS_CSV): $(SCENARIO_DS_NC) $(RECLASSIF_TABLE_CSV) \
$(MAKE_SCENARIO_METRICS_PY)
python $(MAKE_SCENARIO_METRICS_PY) $(SCENARIO_DS_NC) \
$(RECLASSIF_TABLE_CSV) $@
scenario_metrics: $(SCENARIO_METRICS_CSV)
#################################################################################
# STATPOP
### variables
#### Statpop 2018: https://www.bfs.admin.ch/bfsstatic/dam/assets/9947069/master
STATPOP_URI = https://www.bfs.admin.ch/bfsstatic/dam/assets/14027479/master
STATPOP_DIR := $(DATA_RAW_DIR)/statpop
STATPOP_CSV := $(STATPOP_DIR)/statpop-2019.csv
### rules
$(STATPOP_DIR): | $(DATA_RAW_DIR)
mkdir $@
$(STATPOP_DIR)/%.zip: | $(STATPOP_DIR)
wget $(STATPOP_URI) -O $@
$(STATPOP_DIR)/%.csv: $(STATPOP_DIR)/%.zip
unzip -j $< 'STATPOP2019.csv' -d $(STATPOP_DIR)
mv $(STATPOP_DIR)/STATPOP2019.csv $(STATPOP_CSV)
touch $@
statpop: $(STATPOP_CSV)
#################################################################################
# DEDICATED ZENODO DATA https://zenodo.org/record/4316572
## variables
BLDG_COVER_ZENODO_URI = \
https://zenodo.org/record/4314832/files/bldg-cover.tif?download=1
STATION_T_ZENODO_URI = \
https://zenodo.org/record/4316572/files/station-t.csv?download=1
REF_ET_ZENODO_URI = https://zenodo.org/record/4316572/files/ref-et.tif?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) | \
$(CADASTRE_DIR) $(DATA_RECLASSIF_DIR) $(DATA_INTERIM_DIR) \
$(STATION_RAW_DIR) $(DATA_PROCESSED_DIR)
touch $(CADASTRE_SHP)
wget --no-use-server-timestamps $(BLDG_COVER_ZENODO_URI) -O \
$(BLDG_COVER_TIF)
touch $(STATION_RAW_FILEPATHS)
wget --no-use-server-timestamps $(STATION_T_ZENODO_URI) -O \
$(STATION_T_CSV)
wget --no-use-server-timestamps $(REF_ET_ZENODO_URI) -O $(REF_ET_TIF)
#################################################################################
# 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')