forked from calliope-project/euro-calliope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakefile
365 lines (315 loc) · 14 KB
/
Snakefile
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
357
358
359
360
361
362
363
364
365
import glob
from snakemake.utils import validate
ALL_WIND_AND_SOLAR_TECHNOLOGIES = [
"wind-onshore", "wind-offshore", "open-field-pv",
"rooftop-pv", "rooftop-pv-n", "rooftop-pv-e-w", "rooftop-pv-s-flat"
]
BIOFUEL_FEEDSTOCKS = [
"forestry-energy-residues",
"landscape-care-residues",
"manure",
"municipal-waste",
"primary-agricultural-residues",
"roundwood-chips",
"roundwood-fuelwood",
"secondary-forestry-residues-sawdust",
"secondary-forestry-residues-woodchips",
"sludge"
]
include: "./rules/shapes.smk"
include: "./rules/hydro.smk"
include: "./rules/sync.smk"
localrules: all, download_raw_load, model, clean, parameterise_template, download_potentials
localrules: download_capacity_factors_wind_and_solar
configfile: "config/default.yaml"
validate(config, "config/schema.yaml")
wildcard_constraints:
resolution = "((continental)|(national)|(regional))"
root_dir = config["root-directory"] + "/" if config["root-directory"] not in ["", "."] else ""
__version__ = open(f"{root_dir}VERSION").readlines()[0].strip()
script_dir = f"{root_dir}scripts/"
template_dir = f"{root_dir}templates/"
test_dir = f"{root_dir}tests/"
onstart:
shell("mkdir -p build/logs")
onsuccess:
if "email" in config.keys():
shell("echo "" | mail -s 'euro-calliope succeeded' {config[email]}")
onerror:
if "email" in config.keys():
shell("echo "" | mail -s 'euro-calliope failed' {config[email]}")
rule all:
message: "Generate euro-calliope pre-built models and run tests."
input:
"build/logs/continental/model.done",
"build/logs/national/model.done",
"build/logs/regional/model.done",
"build/logs/continental/test-report.html",
"build/logs/national/test-report.html",
rule download_potentials:
message: "Download potential data."
params: url = config["data-sources"]["potentials"]
output: protected("data/automatic/raw-potentials.zip")
conda: "envs/shell.yaml"
shell: "curl -sLo {output} '{params.url}'"
rule potentials:
message: "Unzip potentials."
input: rules.download_potentials.output[0]
shadow: "minimal"
output:
land_eligibility_km2 = "build/data/{resolution}/technical-potential/areas.csv",
shared_coast = "build/data/{resolution}/shared-coast.csv",
demand = "build/data/{resolution}/demand.csv",
population = "build/data/{resolution}/population.csv",
land_cover = "build/data/{resolution}/land-cover.csv"
conda: "envs/shell.yaml"
shell: "unzip -o {input} -d build/data"
rule parameterise_template:
message: "Apply config parameters to file {wildcards.template} from templates."
input:
script = script_dir + "parameterise_templates.py",
template = template_dir + "{template}",
biofuel_cost = "build/data/regional/biofuel/{scenario}/costs-eur-per-mwh.csv".format(
scenario=config["parameters"]["jrc-biofuel"]["scenario"]
)
params:
scaling_factors = config["scaling-factors"],
capacity_factors = config["capacity-factors"]["average"],
max_power_density = config["parameters"]["maximum-installable-power-density"],
biofuel_efficiency = config["parameters"]["biofuel-efficiency"]
output: "build/model/{template}"
wildcard_constraints:
template = "((link-techs.yaml)|(storage-techs.yaml)|(demand-techs.yaml)|(renewable-techs.yaml)|(README.md)|(environment.yaml)|(interest-rate.yaml))"
conda: "envs/default.yaml"
script: "scripts/parameterise_templates.py"
rule hydro_capacities:
message: "Determine hydro capacities on {wildcards.resolution} resolution."
input:
script = script_dir + "hydro_capacities.py",
locations = rules.units.output[0],
plants = rules.preprocess_hydro_stations.output[0]
output: "build/data/{resolution}/hydro-capacities-mw.csv"
conda: "envs/geo.yaml"
script: "scripts/hydro_capacities.py"
rule biofuels:
message: "Determine biofuels potential on {wildcards.resolution} resolution for scenario {wildcards.scenario}."
input:
script = script_dir + "biofuels.py",
units = rules.units.output[0],
land_cover = rules.potentials.output.land_cover,
population = rules.potentials.output.population,
national_potentials = expand(config["data-sources"]["biofuel-potentials"], feedstock=BIOFUEL_FEEDSTOCKS),
costs = expand(config["data-sources"]["biofuel-costs"], feedstock=BIOFUEL_FEEDSTOCKS)
params:
potential_year = config["parameters"]["jrc-biofuel"]["potential-year"],
cost_year = config["parameters"]["jrc-biofuel"]["cost-year"]
output:
potentials = "build/data/{resolution}/biofuel/{scenario}/potential-mwh-per-year.csv",
costs = "build/data/{resolution}/biofuel/{scenario}/costs-eur-per-mwh.csv" # not actually resolution dependent
conda: "envs/geo.yaml"
wildcard_constraints:
scenario = "((low)|(medium)|(high))"
script: "scripts/biofuels.py"
rule locations:
message: "Generate locations for {wildcards.resolution} resolution."
input:
script = script_dir + "locations.py",
shapes = rules.units.output[0],
land_eligibility_km2 = rules.potentials.output.land_eligibility_km2,
hydro_capacities = rules.hydro_capacities.output[0],
biofuel = "build/data/{{resolution}}/biofuel/{scenario}/potential-mwh-per-year.csv".format(scenario=config["parameters"]["jrc-biofuel"]["scenario"])
params:
flat_roof_share = config["parameters"]["roof-share"]["flat"],
maximum_installable_power_density = config["parameters"]["maximum-installable-power-density"],
scaling_factors = config["scaling-factors"],
biofuel_efficiency = config["parameters"]["biofuel-efficiency"]
output:
yaml = "build/model/{resolution}/locations.yaml",
csv = "build/model/{resolution}/locations.csv"
conda: "envs/geo.yaml"
script: "scripts/locations.py"
rule directional_rooftop_pv:
message: "Generate override for directional rooftop PV in {wildcards.resolution} resolution."
input:
script = script_dir + "directional_rooftop.py",
shapes = rules.units.output[0],
land_eligibility_km2 = rules.potentials.output.land_eligibility_km2,
params:
roof_shares = config["parameters"]["roof-share"],
maximum_installable_power_density = config["parameters"]["maximum-installable-power-density"],
scaling_factors = config["scaling-factors"],
output: "build/model/{resolution}/directional-rooftop.yaml"
conda: "envs/geo.yaml"
script: "scripts/directional_rooftop.py"
rule load_shedding:
message: "Generate override allowing load shedding."
input:
script = script_dir + "load_shedding.py",
shapes = rules.units.output[0]
output: "build/model/{resolution}/load-shedding.yaml"
conda: "envs/geo.yaml"
script: "scripts/load_shedding.py"
rule download_capacity_factors_wind_and_solar:
message: "Download data/automatic/capacityfactors/{wildcards.filename}."
params: url = lambda wildcards: config["data-sources"]["capacity-factors"].format(filename=wildcards.filename)
output: protected("data/automatic/capacityfactors/{filename}")
conda: "envs/shell.yaml"
shell: "curl -sLo {output} '{params.url}'"
rule capacity_factors_onshore_wind_and_solar:
message: "Generate capacityfactor time series disaggregated by location on "
"{wildcards.resolution} resolution for {wildcards.technology}."
input:
script = script_dir + "capacityfactors.py",
locations = rules.units.output[0],
timeseries = ancient("data/automatic/capacityfactors/{technology}-timeseries.nc"),
coordinates = ancient("data/automatic/capacityfactors/wind-onshore-timeseries.nc")
params:
threshold = config["capacity-factors"]["min"],
year = config["year"],
trim_ts = config["capacity-factors"]["trim-ninja-timeseries"]
wildcard_constraints:
technology = "((wind-onshore)|(rooftop-pv)|(open-field-pv)|(rooftop-pv-n)|(rooftop-pv-e-w)|(rooftop-pv-s-flat))"
output: "build/model/{resolution}/capacityfactors-{technology}.csv"
conda: "envs/geo.yaml"
script: "scripts/capacityfactors.py"
rule capacity_factors_offshore:
message: "Generate capacityfactor time series disaggregated by location on "
"{wildcards.resolution} resolution for wind-offshore."
input:
script = script_dir + "capacityfactors_offshore.py",
eez = rules.eez.output[0],
shared_coast = rules.potentials.output.shared_coast,
timeseries = ancient("data/automatic/capacityfactors/wind-offshore-timeseries.nc")
params:
threshold = config["capacity-factors"]["min"],
year = config["year"],
trim_ts = config["capacity-factors"]["trim-ninja-timeseries"]
output: "build/model/{resolution}/capacityfactors-wind-offshore.csv"
conda: "envs/geo.yaml"
script: "scripts/capacityfactors_offshore.py"
rule capacity_factors_hydro:
message: "Generate capacityfactor time series for hydro electricity on {wildcards.resolution} resolution."
input:
script = script_dir + "capacityfactors_hydro.py",
capacities = rules.hydro_capacities.output[0],
stations = rules.inflow_mwh.output[0],
locations = rules.units.output[0]
params:
threshold = config["capacity-factors"]["min"]
output:
ror = "build/model/{resolution}/capacityfactors-hydro-ror.csv",
reservoir = "build/model/{resolution}/capacityfactors-hydro-reservoir-inflow.csv"
conda: "envs/geo.yaml"
script: "scripts/capacityfactors_hydro.py"
rule download_raw_load:
message: "Download raw load."
params: url = config["data-sources"]["load"]
output: protected("data/automatic/raw-load-data.csv")
conda: "envs/shell.yaml"
shell: "curl -sLo {output} '{params.url}'"
rule electricity_load_national:
message: "Preprocess raw electricity load data and retrieve load time series per country."
input:
script = script_dir + "national_load.py",
load = rules.download_raw_load.output[0]
output: "build/data/electricity-demand-national.csv"
params:
year = config["year"],
acceptable_gap_hours = config["quality-control"]["load"]["acceptable-load-data-gap-hours"],
outlier_thresholds = config["quality-control"]["load"]["outlier-data-thresholds"],
entsoe_priority = config["quality-control"]["load"]["entsoe-data-priority"],
countries = config["scope"]["countries"]
conda: "envs/default.yaml"
script: "scripts/national_load.py"
rule electricity_load:
message: "Generate electricity load time series for every location on {wildcards.resolution} resolution."
input:
script = script_dir + "load.py",
units = rules.units.output[0],
demand_per_unit = rules.potentials.output.demand,
national_load = rules.electricity_load_national.output[0]
params:
scaling_factor = config["scaling-factors"]["power"]
output: "build/model/{resolution}/electricity-demand.csv"
conda: "envs/geo.yaml"
script: "scripts/load.py"
rule link_neighbours:
message: "Create links between all direct neighbours on {wildcards.resolution} resolution."
input:
script = script_dir + "link_neighbours.py",
units = rules.units.output[0]
params:
sea_connections = lambda wildcards: config["sea-connections"][wildcards.resolution]
output: "build/model/{resolution}/link-all-neighbours.yaml"
conda: "envs/geo.yaml"
script: "scripts/link_neighbours.py"
rule build_metadata:
message: "Generate build metadata."
input:
script = script_dir + "metadata.py"
params:
config = config,
version = __version__
output: "build/model/build-metadata.yaml"
conda: "envs/default.yaml"
script: "scripts/metadata.py"
rule model:
message: "Generate euro-calliope with {wildcards.resolution} resolution."
input:
"build/model/interest-rate.yaml",
"build/model/link-techs.yaml",
"build/model/renewable-techs.yaml",
"build/model/storage-techs.yaml",
"build/model/demand-techs.yaml",
"build/model/environment.yaml",
"build/model/README.md",
rules.locations.output,
rules.electricity_load.output,
rules.link_neighbours.output,
rules.capacity_factors_hydro.output,
rules.hydro_capacities.output,
rules.directional_rooftop_pv.output,
expand(
"build/model/{{resolution}}/capacityfactors-{technology}.csv",
technology=ALL_WIND_AND_SOLAR_TECHNOLOGIES
),
rules.build_metadata.output,
example_model = template_dir + "example-model.yaml"
output:
log = "build/logs/{resolution}/model.done",
example_model = "build/model/{resolution}/example-model.yaml"
shell:
"""
cp {input.example_model} {output.example_model}
touch {output.log}
"""
rule clean: # removes all generated results
shell:
"""
rm -r build/
echo "Data downloaded to data/automatic/ has not been cleaned."
"""
rule docs:
message: "Build workflow documentation"
input: *glob.glob("docs/source/*")
conda: "envs/docs.yaml"
output: directory("docs/build/html")
shell: "sphinx-build -b html docs/source {output}"
rule test:
message: "Run tests"
input:
test_dir + "test_runner.py",
test_dir + "test_model.py",
test_dir + "test_capacityfactors.py",
"build/logs/{resolution}/model.done",
model = test_dir + "resources/{resolution}/model.yaml",
example_model = "build/model/{resolution}/example-model.yaml",
capacity_factor_timeseries = expand(
"build/model/{{resolution}}/capacityfactors-{technology}.csv",
technology=ALL_WIND_AND_SOLAR_TECHNOLOGIES + ["hydro-ror", "hydro-reservoir-inflow"]
)
params:
config = config
output: "build/logs/{resolution}/test-report.html"
conda: "./envs/test.yaml"
script: "./tests/test_runner.py"