-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtmfpython.sh
More file actions
executable file
·214 lines (187 loc) · 8.15 KB
/
Copy pathtmfpython.sh
File metadata and controls
executable file
·214 lines (187 loc) · 8.15 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
#!/bin/bash
#run with command: scripts/tmfpython.sh -i 'maps/aew85/projects' -o '/maps/aew85/tmf_pipe_out' -p 1113 -t 2010 ...
#i: input dir - directory containing project shapefiles
#o: output dir - directory containing pipeline outputs
#p: project name/ID - must match name of shapefile
#t: year of project start (t0)
#e: evaluation year (default: 2022)
#v: verbose - whether to run an ex-post evaluation and knit the results in an R notebook (true/false, default: false).
#NB running evaluations requires the evaluations code
# Check which branch is currently checked out
#current_branch=$(git rev-parse --abbrev-ref HEAD)
set -e
############ DEFAULTS ###############
input_dir=""
output_dir=""
eval_year=2022
verbose=false
#####################################
function display_help() {
echo "Usage: $0 [options]"
echo
echo "Options:"
echo " -i <input_dir> Input directory"
echo " -o <output_dir> Output directory"
echo " -p <proj> Project name"
echo " -t <t0> Start year"
echo " -e <year> Evaluation year"
echo " -v <verbose> Knit ex post evaluation as .Rmd? (true/false)"
echo " -h Display this help message"
echo "Example:"
echo " $0 -i '/maps/aew85/projects' -o '/maps/aew85/tmf_pipe_out -p 1201 -t 2012"
}
# Parse arguments
while getopts "i:o:p:t:e:v:h" flag
do
case "${flag}" in
i) input_dir=${OPTARG};;
o) output_dir=${OPTARG};;
p) proj=${OPTARG};;
t) t0=${OPTARG};;
e) eval_year=${OPTARG};;
r) verbose=${OPTARG};;
a) ex_ante=${OPTARG};;
h) display_help; exit 0;;
*) echo "Invalid option: -${OPTARG}" >&2; display_help; exit 1;;
esac
done
echo "Input directory: $input_dir"
echo "Output directory: $output_dir"
echo "Project: $proj"
echo "t0: $t0"
echo "Evaluation year: $eval_year"
echo "Ex-post evaluation: $verbose"
if [ $# -eq 0 ]; then
display_help
exit 1
fi
# Make project output folder
mkdir -p "${output_dir}/${proj}"
echo "--Folder created.--"
#Make buffer
tmfpython3 -m methods.inputs.generate_boundary --project "${input_dir}/${proj}.geojson" --output "${output_dir}/${proj}/buffer.geojson"
echo "--Buffer created.--"
#Make leakage area
tmfpython3 -m methods.inputs.generate_leakage --project "${input_dir}/${proj}.geojson" --output "${output_dir}/${proj}/leakage.geojson"
echo "--Leakage created.--"
# Get GEDI data
tmfpython3 -m methods.inputs.locate_gedi_data --granules /maps/4C/gedi/granule/info/ \
--buffer "${output_dir}/${proj}/buffer.geojson" \
--output "${output_dir}/${proj}/gedi_names.csv"
tmfpython3 -m methods.inputs.download_gedi_data /maps/4C/gedi/granule/info/* /maps/4C/gedi/granule/
tmfpython3 -m methods.inputs.filter_gedi_data --granules /maps/4C/gedi/granule/ \
--buffer "${output_dir}/${proj}/buffer.geojson" \
--csv "${output_dir}/${proj}/gedi_names.csv" \
--output "${output_dir}/${proj}/gedi.geojson"
tmfpython3 -m methods.inputs.generate_carbon_density --jrc /maps/forecol/data/JRC/v1_2022/AnnualChange/tifs \
--gedi "${output_dir}/${proj}/gedi.geojson" \
--output "${output_dir}/${proj}/carbon-density.csv"
echo "--GEDI data obtained.--"
#Generate list of overlapping countries
tmfpython3 -m methods.inputs.generate_country_list \
--leakage "${output_dir}/${proj}/leakage.geojson" \
--countries /maps/4C/osm_boundaries.geojson \
--output "${output_dir}/${proj}/country-list.json"
echo "--Country list created.--"
#Generate matching area
tmfpython3 -m methods.inputs.generate_matching_area --project "${input_dir}/${proj}.geojson" \
--countrycodes "${output_dir}/${proj}/country-list.json" \
--countries /maps/4C/osm_boundaries.geojson \
--ecoregions /maps/4C/ecoregions/ecoregions.geojson \
--projects /maps/mwd24/tmf-data/projects \
--output "${output_dir}/${proj}/matching-area.geojson"
echo "--Matching area created.--"
#Download SRTM data
tmfpython3 -m methods.inputs.download_srtm_data --project "${input_dir}/${proj}.geojson" \
--matching "${output_dir}/${proj}/matching-area.geojson" \
--zips "${output_dir}/srtm/zip" \
--tifs "${output_dir}/srtm/tif"
echo "--SRTM downloaded.--"
#Generate slopes
tmfpython3 -m methods.inputs.generate_slope --input "${output_dir}/srtm/tif" --output "${output_dir}/slopes"
echo "--Slope created.--"
#Rescale to JRC tiles
tmfpython3 -m methods.inputs.rescale_tiles_to_jrc --jrc /maps/forecol/data/JRC/v1_2022/AnnualChange/tifs \
--tiles "${output_dir}/srtm/tif" \
--output "${output_dir}/rescaled-elevation"
tmfpython3 -m methods.inputs.rescale_tiles_to_jrc \
--jrc /maps/forecol/data/JRC/v1_2022/AnnualChange/tifs \
--tiles "${output_dir}/slopes" \
--output "${output_dir}/rescaled-slopes"
echo "--JRC rescaled.--"
#Create country raster
tmfpython3 -m methods.inputs.generate_country_raster --jrc /maps/forecol/data/JRC/v1_2022/AnnualChange/tifs \
--matching "${output_dir}/${proj}/matching-area.geojson" \
--countries /maps/4C/osm_boundaries.geojson \
--output "${output_dir}/${proj}/countries.tif"
echo "--Country raster created.--"
#Matching: calculate set K
tmfpython3 -m methods.matching.calculate_k \
--project "${input_dir}/${proj}.geojson" \
--start_year "$t0" \
--evaluation_year "$eval_year" \
--jrc /maps/forecol/data/JRC/v1_2022/AnnualChange/tifs \
--cpc /maps/rhm31/fine_circular_coverage/forecol_complete/ \
--ecoregions /maps/4C/ecoregions/ \
--elevation "${output_dir}/rescaled-elevation" \
--slope "${output_dir}/rescaled-slopes" \
--access /maps/4C/access \
--countries-raster "${output_dir}/${proj}/countries.tif" \
--output "${output_dir}/${proj}/k.parquet"
echo "--Set K created.--"
#Matching: calculate set M
tmfpython3 -m methods.matching.find_potential_matches \
--k "${output_dir}/${proj}/k.parquet" \
--matching "${output_dir}/${proj}/matching-area.geojson" \
--start_year "$t0" \
--evaluation_year "$eval_year" \
--jrc /maps/forecol/data/JRC/v1_2022/AnnualChange/tifs \
--cpc /maps/rhm31/fine_circular_coverage/forecol_complete/ \
--ecoregions /maps/4C/ecoregions/ \
--elevation "${output_dir}/rescaled-elevation" \
--slope "${output_dir}/rescaled-slopes" \
--access /maps/4C/access \
--countries-raster "${output_dir}/${proj}/countries.tif" \
--output "${output_dir}/${proj}/matches"
tmfpython3 -m methods.matching.build_m_raster \
--rasters_directory "${output_dir}/${proj}/matches" \
--output "${output_dir}/${proj}/matches.tif" \
-j 20
tmfpython3 -m methods.matching.build_m_table \
--raster "${output_dir}/${proj}/matches.tif" \
--matching "${output_dir}/${proj}/matching-area.geojson" \
--start_year "$t0" \
--evaluation_year "$eval_year" \
--jrc /maps/forecol/data/JRC/v1_2022/AnnualChange/tifs \
--cpc /maps/rhm31/fine_circular_coverage/forecol_complete/ \
--ecoregions /maps/4C/ecoregions/ \
--elevation "${output_dir}/rescaled-elevation" \
--slope "${output_dir}/rescaled-slopes" \
--access /maps/4C/access \
--countries-raster "${output_dir}/${proj}/countries.tif" \
--output "${output_dir}/${proj}/matches.parquet"
echo "--Set M created.--"
#Matching: find pairs
tmfpython3 -m methods.matching.find_pairs \
--k "${output_dir}/${proj}/k.parquet" \
--m "${output_dir}/${proj}/matches.parquet" \
--start_year "$t0" \
--output "${output_dir}/${proj}/pairs" \
--seed 42 \
-j 1
echo "--Pairs matched.--"
#Calculate additionality
tmfpython3 -m methods.outputs.calculate_additionality \
--project "${input_dir}/${proj}.geojson" \
--project_start "$t0" \
--evaluation_year "$eval_year" \
--density "${output_dir}/${proj}/carbon-density.csv" \
--matches "${output_dir}/${proj}/pairs" \
--output "${output_dir}/${proj}/additionality.csv"
echo "--Additionality calculated.--"
# Run ex post evaluation
if [ "$verbose" == "true" ]; then
evaluations_dir="~/evaluations"
ep_output_file="${evaluations_dir}/${proj}_ex_post_evaluation.html"
Rscript -e "rmarkdown::render(input='~/evaluations/R/ex_post_evaluation_template.Rmd',output_file='${ep_output_file}',params=list(proj='${proj}',t0='${t0}',eval_year='${eval_year}',input_dir='${input_dir}',output_dir='${output_dir}'))"
fi