forked from open-energy-transition/pypsa-eur
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathretrieve.smk
More file actions
executable file
·1809 lines (1533 loc) · 61 KB
/
Copy pathretrieve.smk
File metadata and controls
executable file
·1809 lines (1533 loc) · 61 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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-FileCopyrightText: Contributors to Open-TYNDP <https://github.com/open-energy-transition/open-tyndp>
# SPDX-FileCopyrightText: Contributors to PyPSA-Eur <https://github.com/pypsa/pypsa-eur>
#
# SPDX-License-Identifier: MIT
import os
import requests
from datetime import datetime
from dateutil.relativedelta import relativedelta
from shutil import move, unpack_archive, rmtree, copy2
from zipfile import ZipFile
# Configure the default storage provider for accessing remote files using http
# and the special storage plugin for accessing Zenodo files
storage:
provider="http",
keep_local=True,
retries=3,
storage cached_http:
provider="cached-http",
ARCHIVE_SOURCES = {"archive", "tyndp-archive"}
if (EUROSTAT_BALANCES_DATASET := dataset_version("eurostat_balances"))["source"] in [
"primary",
*ARCHIVE_SOURCES,
]:
rule retrieve_eurostat_balances:
input:
tsv_gz=storage(EUROSTAT_BALANCES_DATASET["url"]),
output:
tsv_gz=f"{EUROSTAT_BALANCES_DATASET['folder']}/estat_nrg_bal_c.tsv.gz",
message:
"Retrieving Eurostat balances data"
run:
copy2(input["tsv_gz"], output["tsv_gz"])
if (
EUROSTAT_HOUSEHOLD_BALANCES_DATASET := dataset_version(
"eurostat_household_balances"
)
)["source"] in [
"primary",
*ARCHIVE_SOURCES,
]:
rule retrieve_eurostat_household_balances:
input:
csv=storage(EUROSTAT_HOUSEHOLD_BALANCES_DATASET["url"]),
output:
csv=f"{EUROSTAT_HOUSEHOLD_BALANCES_DATASET['folder']}/nrg_d_hhq.csv",
message:
"Retrieving Eurostat household balances data"
run:
copy2(input["csv"], output["csv"])
if (SWISS_ENERGY_BALANCES_DATASET := dataset_version("swiss_energy_balances"))[
"source"
] in [
"archive",
"primary",
]:
rule retrieve_swiss_energy_balances:
input:
xlsx=storage(SWISS_ENERGY_BALANCES_DATASET["url"]),
output:
xlsx=f"{SWISS_ENERGY_BALANCES_DATASET['folder']}/12361-VWZ_Webtabellen_2024.xlsx",
message:
"Retrieving Swiss energy balances data"
run:
copy2(input["xlsx"], output["xlsx"])
if (NUTS3_POPULATION_DATASET := dataset_version("nuts3_population"))["source"] in [
"primary",
*ARCHIVE_SOURCES,
]:
rule retrieve_nuts3_population:
input:
gz=storage(NUTS3_POPULATION_DATASET["url"]),
output:
gz=f"{NUTS3_POPULATION_DATASET['folder']}/nama_10r_3popgdp.tsv.gz",
retries: 2
message:
"Retrieving NUTS3 population data"
run:
copy2(input["gz"], output["gz"])
if (CORINE_DATASET := dataset_version("corine"))["source"] in ARCHIVE_SOURCES:
rule retrieve_corine:
input:
zip_file=storage(CORINE_DATASET["url"]),
output:
zip_file=f"{CORINE_DATASET['folder']}/corine.zip",
tif_file=f"{CORINE_DATASET['folder']}/corine.tif",
message:
"Retrieving Corine land cover data"
run:
output_folder = Path(output["zip_file"]).parent
unpack_archive(input["zip_file"], output_folder)
copy2(input["zip_file"], output["zip_file"])
copy2(
f"{output_folder}/corine/g250_clc06_V18_5.tif", output["tif_file"]
)
elif (CORINE_DATASET := dataset_version("corine"))["source"] in ["primary"]:
rule retrieve_corine:
output:
zip=f"{CORINE_DATASET['folder']}/corine.zip",
tif_file=f"{CORINE_DATASET['folder']}/corine.tif",
log:
logs("retrieve_corine_primary.log"),
retries: 2
resources:
mem_mb=1000,
params:
apikey=os.environ.get("CORINE_API_TOKEN", ""),
message:
"Retrieving Corine land cover data"
script:
scripts("retrieve_corine_dataset_primary.py")
if (H2_SALT_CAVERNS_DATASET := dataset_version("h2_salt_caverns"))[
"source"
] in ARCHIVE_SOURCES:
rule retrieve_h2_salt_caverns:
input:
geojson=storage(H2_SALT_CAVERNS_DATASET["url"]),
output:
geojson=f"{H2_SALT_CAVERNS_DATASET['folder']}/h2_salt_caverns_GWh_per_sqkm.geojson",
retries: 2
message:
"Retrieving H2 salt caverns data"
run:
copy2(input["geojson"], output["geojson"])
if (GDP_PER_CAPITA_DATASET := dataset_version("gdp_per_capita"))[
"source"
] in ARCHIVE_SOURCES:
rule retrieve_gdp_per_capita:
input:
gdp=storage(GDP_PER_CAPITA_DATASET["url"]),
output:
gdp=f"{GDP_PER_CAPITA_DATASET['folder']}/GDP_per_capita_PPP_1990_2015_v2.nc",
retries: 2
message:
"Retrieving GDP per capita data"
run:
copy2(input["gdp"], output["gdp"])
if (POPULATION_COUNT_DATASET := dataset_version("population_count"))["source"] in [
"primary",
*ARCHIVE_SOURCES,
]:
rule retrieve_population_count:
input:
tif=storage(POPULATION_COUNT_DATASET["url"]),
output:
tif=f"{POPULATION_COUNT_DATASET['folder']}/ppp_2019_1km_Aggregated.tif",
retries: 2
message:
"Retrieving population count data"
run:
copy2(input["tif"], output["tif"])
if POPULATION_COUNT_DATASET["source"] == "primary":
import xarray as xr
import rioxarray as rio
file_path = output["tif"]
ds = xr.open_dataarray(file_path)
ds_reqd = ds.sel(x=slice(15.55, 40.41), y=slice(52.49, 41.72))
ds_reqd.rio.to_raster(file_path)
if (GHG_EMISSIONS_DATASET := dataset_version("ghg_emissions"))["source"] in [
"primary",
*ARCHIVE_SOURCES,
]:
rule retrieve_ghg_emissions:
input:
ghg=storage(GHG_EMISSIONS_DATASET["url"]),
output:
csv=f"{GHG_EMISSIONS_DATASET['folder']}/UNFCCC_v23.csv",
zip=(
f"{GHG_EMISSIONS_DATASET['folder']}/UNFCCC_v23.csv.zip"
if GHG_EMISSIONS_DATASET["source"] == "primary"
else []
),
directory=(
directory(GHG_EMISSIONS_DATASET["folder"])
if GHG_EMISSIONS_DATASET["source"] == "primary"
else []
),
retries: 2
message:
"Retrieving GHG emissions data"
run:
if GHG_EMISSIONS_DATASET["source"] == "primary":
copy2(input["ghg"], output["zip"])
unpack_archive(output["zip"], GHG_EMISSIONS_DATASET["folder"])
else:
copy2(input["ghg"], output["csv"])
if (GEBCO_DATASET := dataset_version("gebco"))["source"] in [
"primary",
*ARCHIVE_SOURCES,
]:
rule retrieve_gebco:
input:
storage(GEBCO_DATASET["url"]),
output:
gebco=f"{GEBCO_DATASET['folder']}/GEBCO_2014_2D.nc",
zip_file=(
f"{GEBCO_DATASET['folder']}/GEBCO_2014.zip"
if GEBCO_DATASET["source"] == "primary"
else []
),
message:
"Retrieving GEBCO bathymetry data"
run:
if GEBCO_DATASET["source"] == "primary":
import xarray as xr
copy2(input[0], output["zip_file"])
output_folder = Path(output["zip_file"]).parent
unpack_archive(output["zip_file"], output_folder)
# Limit extent to Europe to reduce file size
ds = xr.open_dataset(output["gebco"])
ds = ds.sel(lat=slice(32, 73), lon=slice(-21, 45))
ds.to_netcdf(output["gebco"])
else:
copy2(input[0], output["gebco"])
if (ATTRIBUTED_PORTS_DATASET := dataset_version("attributed_ports"))["source"] in [
"primary",
*ARCHIVE_SOURCES,
]:
rule retrieve_attributed_ports:
input:
json=storage(ATTRIBUTED_PORTS_DATASET["url"]),
output:
json=f"{ATTRIBUTED_PORTS_DATASET['folder']}/attributed_ports.json",
retries: 2
message:
"Retrieving attributed ports data"
run:
copy2(input["json"], output["json"])
if (JRC_IDEES_DATASET := dataset_version("jrc_idees"))["source"] in [
"primary",
*ARCHIVE_SOURCES,
]:
rule retrieve_jrc_idees:
input:
zip_file=storage(JRC_IDEES_DATASET["url"]),
output:
zip_file=f"{JRC_IDEES_DATASET['folder']}/jrc_idees.zip",
directory=directory(JRC_IDEES_DATASET["folder"]),
message:
"Retrieving JRC IDEES data"
run:
copy2(input["zip_file"], output["zip_file"])
output_folder = Path(output["zip_file"]).parent
unpack_archive(output["zip_file"], output_folder)
if (EU_NUTS2013_DATASET := dataset_version("eu_nuts2013"))["source"] in [
"primary",
*ARCHIVE_SOURCES,
]:
rule retrieve_eu_nuts_2013:
input:
shapes=storage(EU_NUTS2013_DATASET["url"]),
output:
zip_file=f"{EU_NUTS2013_DATASET['folder']}/ref-nuts-2013-03m.geojson.zip",
folder=directory(
f"{EU_NUTS2013_DATASET['folder']}/ref-nuts-2013-03m.geojson"
),
shapes_level_3=f"{EU_NUTS2013_DATASET['folder']}/ref-nuts-2013-03m.geojson/NUTS_RG_03M_2013_4326_LEVL_3.geojson",
shapes_level_2=f"{EU_NUTS2013_DATASET['folder']}/ref-nuts-2013-03m.geojson/NUTS_RG_03M_2013_4326_LEVL_2.geojson",
message:
"Retrieving EU NUTS 2013 data"
run:
copy2(input["shapes"], output["zip_file"])
unpack_archive(output["zip_file"], Path(output.shapes_level_3).parent)
if (EU_NUTS2021_DATASET := dataset_version("eu_nuts2021"))["source"] in [
"primary",
*ARCHIVE_SOURCES,
]:
rule retrieve_eu_nuts_2021:
input:
shapes=storage(EU_NUTS2021_DATASET["url"]),
output:
zip_file=f"{EU_NUTS2021_DATASET['folder']}/ref-nuts-2021-01m.geojson.zip",
folder=directory(
f"{EU_NUTS2021_DATASET['folder']}/ref-nuts-2021-01m.geojson"
),
shapes_level_3=f"{EU_NUTS2021_DATASET['folder']}/ref-nuts-2021-01m.geojson/NUTS_RG_01M_2021_4326_LEVL_3.geojson",
shapes_level_2=f"{EU_NUTS2021_DATASET['folder']}/ref-nuts-2021-01m.geojson/NUTS_RG_01M_2021_4326_LEVL_2.geojson",
shapes_level_1=f"{EU_NUTS2021_DATASET['folder']}/ref-nuts-2021-01m.geojson/NUTS_RG_01M_2021_4326_LEVL_1.geojson",
shapes_level_0=f"{EU_NUTS2021_DATASET['folder']}/ref-nuts-2021-01m.geojson/NUTS_RG_01M_2021_4326_LEVL_0.geojson",
message:
"Retrieving EU NUTS 2021 data"
run:
copy2(input["shapes"], output["zip_file"])
unpack_archive(output["zip_file"], Path(output.shapes_level_3).parent)
if (
BIDDING_ZONES_ELECTRICITYMAPS_DATASET := dataset_version(
"bidding_zones_electricitymaps"
)
)["source"] in ["primary", *ARCHIVE_SOURCES]:
rule retrieve_bidding_zones_electricitymaps:
input:
geojson=storage(BIDDING_ZONES_ELECTRICITYMAPS_DATASET["url"]),
output:
geojson=f"{BIDDING_ZONES_ELECTRICITYMAPS_DATASET['folder']}/bidding_zones_electricitymaps.geojson",
log:
"logs/retrieve_bidding_zones_electricitymaps.log",
retries: 2
resources:
mem_mb=1000,
run:
copy2(input["geojson"], output["geojson"])
if (BIDDING_ZONES_ENTSOEPY_DATASET := dataset_version("bidding_zones_entsoepy"))[
"source"
] in ["primary", *ARCHIVE_SOURCES]:
rule retrieve_bidding_zones_entsoepy:
output:
geojson=f"{BIDDING_ZONES_ENTSOEPY_DATASET['folder']}/bidding_zones_entsoepy.geojson",
log:
"logs/retrieve_bidding_zones_entsoepy.log",
retries: 2
resources:
mem_mb=1000,
run:
import entsoe
import geopandas as gpd
import logging
import requests
from requests.exceptions import HTTPError, Timeout, ConnectionError
logger = logging.getLogger(__name__)
logger.setLevel(config["logging"]["level"])
logger.addHandler(logging.FileHandler(log[0]))
logger.info("Downloading entsoe-py zones...")
gdfs: list[gpd.GeoDataFrame] = []
url = f"{BIDDING_ZONES_ENTSOEPY_DATASET['url']}"
for area in entsoe.Area:
name = area.name
logger.debug(f"Retrieving area {name}")
try:
file_url = f"{url}/{name}.geojson"
response = requests.get(file_url, timeout=60)
response.raise_for_status()
features = response.json().get("features", [])
if features:
gdf = gpd.GeoDataFrame.from_features(
features, crs="EPSG:4326"
)
gdfs.append(gdf)
logger.debug(f"Successfully retrieved area {name}")
except HTTPError as e:
logger.debug(f"Area file not available for {name}: {e}")
continue
except (Timeout, ConnectionError, TimeoutError) as e:
raise Exception(f"Network error retrieving {name}: {e}")
shapes = pd.concat(gdfs, ignore_index=True) # type: ignore
logger.info("Downloading entsoe-py zones... Done")
shapes.to_file(output.geojson)
if (CUTOUT_DATASET := dataset_version("cutout"))["source"] in ARCHIVE_SOURCES:
rule retrieve_cutout:
input:
storage(CUTOUT_DATASET["url"] + "/{cutout}.nc"),
output:
CUTOUT_DATASET["folder"] + "/{cutout}.nc",
log:
"logs/retrieve_cutout/{cutout}.log",
retries: 2
resources:
mem_mb=5000,
message:
"Retrieving cutout data for {wildcards.cutout}"
run:
copy2(input[0], output[0])
if (COUNTRY_RUNOFF_DATASET := dataset_version("country_runoff"))[
"source"
] in ARCHIVE_SOURCES:
rule retrieve_country_runoff:
input:
storage(COUNTRY_RUNOFF_DATASET["url"]),
output:
era5_runoff=f"{COUNTRY_RUNOFF_DATASET['folder']}/era5-runoff-per-country.csv",
message:
"Retrieving country runoff data"
run:
copy2(input[0], output[0])
if (COUNTRY_HDD_DATASET := dataset_version("country_hdd"))["source"] in ARCHIVE_SOURCES:
rule retrieve_country_hdd:
input:
storage(COUNTRY_HDD_DATASET["url"]),
output:
era5_runoff=f"{COUNTRY_HDD_DATASET['folder']}/era5-HDD-per-country.csv",
message:
"Retrieving country heating degree days data"
run:
copy2(input[0], output[0])
if (COSTS_DATASET := dataset_version("costs"))["source"] in [
"primary",
*ARCHIVE_SOURCES,
]:
rule retrieve_cost_data:
input:
costs=storage(COSTS_DATASET["url"] + "/costs_{planning_horizons}.csv"),
output:
costs=COSTS_DATASET["folder"] + "/costs_{planning_horizons}.csv",
message:
"Retrieving cost data for {wildcards.planning_horizons}"
run:
copy2(input["costs"], output["costs"])
if (POWERPLANTS_DATASET := dataset_version("powerplants"))["source"] in [
"primary",
*ARCHIVE_SOURCES,
]:
rule retrieve_powerplants:
input:
powerplants=storage(POWERPLANTS_DATASET["url"]),
output:
powerplants=f"{POWERPLANTS_DATASET['folder']}/powerplants.csv",
message:
"Retrieving powerplants data"
run:
copy2(input["powerplants"], output["powerplants"])
if (SCIGRID_GAS_DATASET := dataset_version("scigrid_gas"))["source"] in [
"primary",
*ARCHIVE_SOURCES,
]:
rule retrieve_gas_infrastructure_data:
input:
zip_file=storage(SCIGRID_GAS_DATASET["url"]),
output:
zip_file=f"{SCIGRID_GAS_DATASET['folder']}/IGGIELGN.zip",
entry=f"{SCIGRID_GAS_DATASET['folder']}/data/IGGIELGN_BorderPoints.geojson",
storage=f"{SCIGRID_GAS_DATASET['folder']}/data/IGGIELGN_Storages.geojson",
gas_network=f"{SCIGRID_GAS_DATASET['folder']}/data/IGGIELGN_PipeSegments.geojson",
message:
"Retrieving SciGRID gas infrastructure data"
run:
copy2(input["zip_file"], output["zip_file"])
output_folder = Path(output["zip_file"]).parent
unpack_archive(output["zip_file"], output_folder)
if (OPSD_DEMAND_DATA := dataset_version("opsd_electricity_demand"))["source"] in [
"build"
]:
rule retrieve_electricity_demand_opsd:
output:
csv=f"{OPSD_DEMAND_DATA['folder']}/electricity_demand_opsd_raw.csv",
log:
"logs/retrieve_electricity_demand_opsd.log",
retries: 2
resources:
mem_mb=5000,
params:
versions=["2019-06-05", "2020-10-06"],
message:
"Retrieving electricity demand data from OPSD from build source"
script:
scripts("retrieve_electricity_demand_opsd.py")
if (OPSD_DEMAND_DATA := dataset_version("opsd_electricity_demand"))[
"source"
] in ARCHIVE_SOURCES:
rule retrieve_electricity_demand_opsd:
input:
csv=storage(OPSD_DEMAND_DATA["url"]),
output:
csv=f"{OPSD_DEMAND_DATA['folder']}/electricity_demand_opsd_raw.csv",
retries: 2
message:
"Retrieving electricity demand data from OPSD from archive"
run:
copy2(input["csv"], output["csv"])
if (ENTSOE_DEMAND_DATA := dataset_version("entsoe_electricity_demand"))["source"] in [
"build"
]:
ENTSOE_COUNTRIES = [
"AL",
"AT",
"BE",
"BA",
"BG",
"CH",
"CY",
"CZ",
"DE",
"DK",
"EE",
"ES",
"FI",
"FR",
"GB",
"GR",
"HR",
"HU",
"IE",
"IT",
"LT",
"LU",
"LV",
"MD",
"ME",
"MK",
"NL",
"NO",
"PL",
"PT",
"RO",
"RS",
"SE",
"SI",
"SK",
"UA",
"XK",
]
rule retrieve_electricity_demand_entsoe_country:
output:
csv=f"{ENTSOE_DEMAND_DATA['folder']}"
+ "/electricity_demand_entsoe_raw_{country}.csv",
log:
"logs/retrieve_electricity_demand_entsoe_{country}.log",
retries: 2
resources:
mem_mb=2000,
params:
entsoe_token=os.environ.get("ENTSOE_API_TOKEN", ""),
message:
"Retrieving electricity demand data from ENTSO-E for {wildcards.country}"
script:
scripts("retrieve_electricity_demand_entsoe.py")
rule retrieve_electricity_demand_entsoe:
input:
csvs=expand(
f"{ENTSOE_DEMAND_DATA['folder']}"
+ "/electricity_demand_entsoe_raw_{country}.csv",
country=ENTSOE_COUNTRIES,
),
output:
csv=f"{ENTSOE_DEMAND_DATA['folder']}/electricity_demand_entsoe_raw.csv",
message:
"Retrieving electricity demand data from ENTSO-E from build source"
run:
import pandas as pd
loads = [pd.read_csv(csv, index_col=0) for csv in input.csvs]
df = pd.concat(loads, axis=1, join="outer").sort_index()
df.to_csv(output.csv)
if (ENTSOE_DEMAND_DATA := dataset_version("entsoe_electricity_demand"))[
"source"
] in ARCHIVE_SOURCES:
rule retrieve_electricity_demand_entsoe:
input:
csv=storage(ENTSOE_DEMAND_DATA["url"]),
output:
csv=f"{ENTSOE_DEMAND_DATA['folder']}/electricity_demand_entsoe_raw.csv",
retries: 2
message:
"Retrieving electricity demand data from ENTSO-E from archive"
run:
copy2(input["csv"], output["csv"])
if (NESO_DEMAND_DATA := dataset_version("neso_electricity_demand"))["source"] in [
"build"
]:
rule retrieve_electricity_demand_neso:
output:
csv=f"{NESO_DEMAND_DATA['folder']}/electricity_demand_neso_raw.csv",
log:
"logs/retrieve_electricity_demand_neso.log",
retries: 2
resources:
mem_mb=5000,
message:
"Retrieving electricity demand data from NESO from build source"
script:
scripts("retrieve_electricity_demand_neso.py")
if (NESO_DEMAND_DATA := dataset_version("neso_electricity_demand"))[
"source"
] in ARCHIVE_SOURCES:
rule retrieve_electricity_demand_neso:
input:
csv=storage(NESO_DEMAND_DATA["url"]),
output:
csv=f"{NESO_DEMAND_DATA['folder']}/electricity_demand_neso_raw.csv",
retries: 2
message:
"Retrieving electricity demand data from NESO from archive"
run:
copy2(input["csv"], output["csv"])
if (
SYNTHETIC_ELECTRICITY_DEMAND_DATASET := dataset_version(
"synthetic_electricity_demand"
)
)["source"] in [
"primary",
*ARCHIVE_SOURCES,
]:
rule retrieve_synthetic_electricity_demand:
input:
csv=storage(SYNTHETIC_ELECTRICITY_DEMAND_DATASET["url"]),
output:
csv=f"{SYNTHETIC_ELECTRICITY_DEMAND_DATASET['folder']}/load_synthetic_raw.csv",
retries: 2
message:
"Retrieving synthetic electricity demand data"
run:
copy2(input["csv"], output["csv"])
if (ENERGY_ATLAS_DATASET := dataset_version("jrc_energy_atlas"))["source"] in [
"primary",
*ARCHIVE_SOURCES,
]:
rule retrieve_electricity_demand_energy_atlas:
output:
tif=f"{ENERGY_ATLAS_DATASET['folder']}/electricity_tot_demand_2019.tif",
message:
"Retrieving JRC Energy Atlas electricity demand data raster"
run:
import requests
url = ENERGY_ATLAS_DATASET["url"]
response = requests.get(url)
response.raise_for_status()
with open(output["tif"], "wb") as f:
f.write(response.content)
if (
DESNZ_ELECTRICITY_CONSUMPTION_DATASET := dataset_version(
"desnz_electricity_consumption"
)
)["source"] in ["primary", *ARCHIVE_SOURCES]:
rule retrieve_desnz_electricity_consumption:
output:
xlsx=f"{DESNZ_ELECTRICITY_CONSUMPTION_DATASET['folder']}/Subnational_electricity_consumption_statistics_2005-2024.xlsx",
message:
"Retrieving DESNZ subnational electricity consumption data"
run:
import requests
url = DESNZ_ELECTRICITY_CONSUMPTION_DATASET["url"]
response = requests.get(url)
response.raise_for_status()
with open(output["xlsx"], "wb") as f:
f.write(response.content)
if (ONS_LAD_DATASET := dataset_version("ons_lad"))["source"] in [*ARCHIVE_SOURCES]:
rule retrieve_ons_lad:
input:
geojson=storage(ONS_LAD_DATASET["url"]),
output:
geojson=f"{ONS_LAD_DATASET['folder']}/Local_Authority_Districts_May_2024_Boundaries__UK_BSC.geojson",
message:
"Retrieving UK ONS Local Authority Districts (LAD) Boundaries data"
run:
copy2(input["geojson"], output["geojson"])
elif ONS_LAD_DATASET["source"] in ["primary"]:
rule retrieve_ons_lad:
output:
geojson=f"{ONS_LAD_DATASET['folder']}/Local_Authority_Districts_May_2024_Boundaries__UK_BSC.geojson",
message:
"Retrieving UK ONS Local Authority Districts (LAD) Boundaries data"
run:
import requests
url = ONS_LAD_DATASET["url"]
params = {
"outFields": "*",
"where": "1=1",
"f": "geojson",
}
response = requests.get(url, params=params)
with open(output["geojson"], "wb") as f:
f.write(response.content)
if (SHIP_RASTER_DATASET := dataset_version("ship_raster"))["source"] in [
"primary",
*ARCHIVE_SOURCES,
]:
rule retrieve_ship_raster:
input:
zip_file=storage(SHIP_RASTER_DATASET["url"]),
output:
zip_file=f"{SHIP_RASTER_DATASET['folder']}/shipdensity_global.zip",
log:
"logs/retrieve_ship_raster.log",
retries: 2
resources:
mem_mb=5000,
message:
"Retrieving shipping raster data"
run:
copy2(input["zip_file"], output["zip_file"])
if (ENSPRESO_BIOMASS_DATASET := dataset_version("enspreso_biomass"))["source"] in [
"primary",
*ARCHIVE_SOURCES,
]:
rule retrieve_enspreso_biomass:
input:
xlsx=storage(ENSPRESO_BIOMASS_DATASET["url"]),
output:
xlsx=f"{ENSPRESO_BIOMASS_DATASET['folder']}/ENSPRESO_BIOMASS.xlsx",
retries: 1
message:
"Retrieving ENSPRESO biomass data"
run:
copy2(input["xlsx"], output["xlsx"])
if (HOTMAPS_INDUSTRIAL_SITES := dataset_version("hotmaps_industrial_sites"))[
"source"
] in [
"primary",
*ARCHIVE_SOURCES,
]:
rule retrieve_hotmaps_industrial_sites:
input:
csv=storage(HOTMAPS_INDUSTRIAL_SITES["url"]),
output:
csv=f"{HOTMAPS_INDUSTRIAL_SITES['folder']}/Industrial_Database.csv",
retries: 1
message:
"Retrieving Hotmaps industrial sites"
run:
copy2(input["csv"], output["csv"])
if (NITROGEN_STATISTICS_DATASET := dataset_version("nitrogen_statistics"))[
"source"
] in [
"primary",
*ARCHIVE_SOURCES,
]:
rule retrieve_nitrogen_statistics:
input:
xlsx=storage(NITROGEN_STATISTICS_DATASET["url"]),
output:
xlsx=f"{NITROGEN_STATISTICS_DATASET['folder']}/nitro-ert.xlsx",
retries: 1
message:
"Retrieving nitrogen statistics data"
run:
copy2(input["xlsx"], output["xlsx"])
if (COPERNICUS_LAND_COVER_DATASET := dataset_version("copernicus_land_cover"))[
"source"
] in ["primary", *ARCHIVE_SOURCES]:
# Downloading Copernicus Global Land Cover for land cover and land use:
# Website: https://land.copernicus.eu/global/products/lc
rule download_copernicus_land_cover:
input:
tif=storage(COPERNICUS_LAND_COVER_DATASET["url"]),
output:
tif=f"{COPERNICUS_LAND_COVER_DATASET['folder']}/Copernicus_LC100_global_v3.0.1_2019-nrt_Discrete-Classification-map_EPSG-4326.tif",
message:
"Retrieving Copernicus land cover data"
run:
copy2(input["tif"], output["tif"])
if (LUISA_LAND_COVER_DATASET := dataset_version("luisa_land_cover"))["source"] in [
"primary",
*ARCHIVE_SOURCES,
]:
# Downloading LUISA Base Map for land cover and land use:
# Website: https://ec.europa.eu/jrc/en/luisa
rule retrieve_luisa_land_cover:
input:
tif=storage(LUISA_LAND_COVER_DATASET["url"]),
output:
tif=f"{LUISA_LAND_COVER_DATASET['folder']}/LUISA_basemap_020321_50m.tif",
message:
"Retrieving LUISA land cover data"
run:
copy2(input["tif"], output["tif"])
if (EEZ_DATASET := dataset_version("eez"))["source"] in ["primary"]:
rule retrieve_eez:
output:
zip_file=f"{EEZ_DATASET['folder']}/World_EEZ_{EEZ_DATASET['version']}_LR.zip",
gpkg=f"{EEZ_DATASET['folder']}/World_EEZ_{EEZ_DATASET['version']}_LR/eez_{EEZ_DATASET['version'].split('_')[0]}_lowres.gpkg",
message:
"Retrieving EEZ data"
run:
from uuid import uuid4
name = str(uuid4())[:8]
org = str(uuid4())[:8]
response = requests.post(
f"{EEZ_DATASET['url']}",
params={"name": f"World_EEZ_{EEZ_DATASET['version']}_LR.zip"},
data={
"name": name,
"organisation": org,
"email": f"{name}@{org}.org",
"country": "Germany",
"user_category": "academia",
"purpose_category": "Research",
"agree": "1",
},
)
with open(output["zip_file"], "wb") as f:
f.write(response.content)
output_folder = Path(output["zip_file"]).parent
unpack_archive(output["zip_file"], output_folder)
elif (EEZ_DATASET := dataset_version("eez"))["source"] in ARCHIVE_SOURCES:
rule retrieve_eez:
input:
zip_file=storage(
EEZ_DATASET["url"],
),
output:
zip_file=f"{EEZ_DATASET['folder']}/World_EEZ_{EEZ_DATASET['version']}_LR.zip",
gpkg=f"{EEZ_DATASET['folder']}/World_EEZ_{EEZ_DATASET['version']}_LR/eez_{EEZ_DATASET['version'].split('_')[0]}_lowres.gpkg",
message:
"Retrieving EEZ data"
run:
output_folder = Path(output["zip_file"]).parent
copy2(input["zip_file"], output["zip_file"])
unpack_archive(output["zip_file"], output_folder)
if (WB_URB_POP_DATASET := dataset_version("worldbank_urban_population"))["source"] in [
"primary",
*ARCHIVE_SOURCES,
]:
rule retrieve_worldbank_urban_population:
input:
zip=storage(WB_URB_POP_DATASET["url"]),
output:
zip=f"{WB_URB_POP_DATASET['folder']}/API_SP.URB.TOTL.IN.ZS_DS2_en_csv_v2.zip",
csv=f"{WB_URB_POP_DATASET['folder']}/API_SP.URB.TOTL.IN.ZS_DS2_en_csv_v2.csv",
message:
"Retrieving World Bank urban population data"
run:
copy2(input["zip"], output["zip"])
unpack_archive(output["zip"], WB_URB_POP_DATASET["folder"])
# Filename contains some added numbers when downloaded,
# remove them to have a consistent filename across versions
target_filename = Path(output["csv"])
origin_filename = next(
Path(WB_URB_POP_DATASET["folder"]).rglob(
target_filename.stem + "*" + target_filename.suffix
)
)
origin_filename.rename(output.csv)
if (CO2STOP_DATASET := dataset_version("co2stop"))["source"] in [
"primary",
*ARCHIVE_SOURCES,
]:
rule retrieve_co2stop:
input:
zip_file=storage(CO2STOP_DATASET["url"]),
output:
zip_file=f"{CO2STOP_DATASET['folder']}/co2jrc_openformats.zip",
storage_table=f"{CO2STOP_DATASET['folder']}/CO2JRC_OpenFormats/CO2Stop_DataInterrogationSystem/Hydrocarbon_Storage_Units.csv",
storage_map=f"{CO2STOP_DATASET['folder']}/CO2JRC_OpenFormats/CO2Stop_Polygons Data/StorageUnits_March13.kml",
traps_table1=f"{CO2STOP_DATASET['folder']}/CO2JRC_OpenFormats/CO2Stop_DataInterrogationSystem/Hydrocarbon_Traps.csv",
traps_table2=f"{CO2STOP_DATASET['folder']}/CO2JRC_OpenFormats/CO2Stop_DataInterrogationSystem/Hydrocarbon_Traps_Temp.csv",
traps_table3=f"{CO2STOP_DATASET['folder']}/CO2JRC_OpenFormats/CO2Stop_DataInterrogationSystem/Hydrocarbon_Traps1.csv",
traps_map=f"{CO2STOP_DATASET['folder']}/CO2JRC_OpenFormats/CO2Stop_Polygons Data/DaughterUnits_March13.kml",
message:
"Retrieving CO2STOP data"
run:
output_folder = Path(output["zip_file"]).parent
output_folder.mkdir(parents=True, exist_ok=True)
copy2(input["zip_file"], output["zip_file"])
unpack_archive(output["zip_file"], output_folder)
if (GEM_EUROPE_GAS_TRACKER_DATASET := dataset_version("gem_europe_gas_tracker"))[
"source"
] in [
"primary",
*ARCHIVE_SOURCES,