-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport_food.py
executable file
·329 lines (302 loc) · 10.7 KB
/
import_food.py
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
#!/usr/bin/env python3
import argparse
import copy
import functools
import os
from os.path import join
import bw2data
from common import brightway_patch as brightway_patch
from common.import_ import (
DB_FILES_DIR,
import_simapro_csv,
link_technosphere_by_activity_hash_ref_product,
setup_project,
)
CURRENT_FILE_DIR = os.path.dirname(os.path.realpath(__file__))
PROJECT = "ecobalyse"
AGRIBALYSE31 = "AGB3.1.1.20230306.CSV.zip" # Agribalyse 3.1
AGRIBALYSE32 = "AGB32beta_08082024.CSV.zip" # Agribalyse 3.2
GINKO = "CSV_369p_et_298chapeaux_final.csv.zip" # additional organic processes
PASTOECO = "pastoeco.CSV.zip"
CTCPA = "Export emballages_PACK AGB_CTCPA.CSV.zip"
WFLDB = "WFLDB.CSV.zip"
BIOSPHERE = "biosphere3"
ACTIVITIES = "food/activities.json"
#
ACTIVITIES_TO_CREATE = "food/activities_to_create.json"
# excluded strategies and migrations
EXCLUDED = [
"normalize_biosphere_names",
"fix_localized_water_flows", # both agb and ef31 adapted have localized wf
"simapro-water",
]
GINKO_MIGRATIONS = [
{
"name": "diesel-fix",
"description": "Fix Diesel process name",
"data": {
"fields": ("name",),
"data": [
(
(
"Diesel {GLO}| market group for | Cut-off, S - Copied from ecoinvent",
),
{
"name": "Diesel {GLO}| market group for | Cut-off, S - Copied from Ecoinvent U"
},
)
],
},
}
]
# migrations necessary to link some remaining unlinked technosphere activities
AGRIBALYSE_MIGRATIONS = [
{
"name": "agb-technosphere-fixes",
"description": "Specific technosphere fixes for Agribalyse 3",
"data": {
"fields": ["name", "unit"],
"data": [
(
(
"Wastewater, average {Europe without Switzerland}| market for wastewater, average | Cut-off, S - Copied from Ecoinvent U",
"l",
),
{"unit": "m3", "multiplier": 1e-3},
),
(
(
"Wastewater, from residence {RoW}| market for wastewater, from residence | Cut-off, S - Copied from Ecoinvent U",
"l",
),
{"unit": "m3", "multiplier": 1e-3},
),
(
(
"Heat, central or small-scale, natural gas {Europe without Switzerland}| market for heat, central or small-scale, natural gas | Cut-off, S - Copied from Ecoinvent U",
"kWh",
),
{"unit": "MJ", "multiplier": 3.6},
),
(
(
"Heat, district or industrial, natural gas {Europe without Switzerland}| heat production, natural gas, at industrial furnace >100kW | Cut-off, S - Copied from Ecoinvent U",
"kWh",
),
{"unit": "MJ", "multiplier": 3.6},
),
(
(
"Heat, district or industrial, natural gas {RER}| market group for | Cut-off, S - Copied from Ecoinvent U",
"kWh",
),
{"unit": "MJ", "multiplier": 3.6},
),
(
(
"Heat, district or industrial, natural gas {RoW}| market for heat, district or industrial, natural gas | Cut-off, S - Copied from Ecoinvent U",
"kWh",
),
{"unit": "MJ", "multiplier": 3.6},
),
(
(
"Land use change, perennial crop {BR}| market group for land use change, perennial crop | Cut-off, S - Copied from Ecoinvent U",
"m2",
),
{"unit": "ha", "multiplier": 1e-4},
),
]
+ sum(
[
[
[
(f"Water, river, {country}", "l"),
{"unit": "cubic meter", "multiplier": 0.001},
],
[
(f"Water, well, {country}", "l"),
{"unit": "cubic meter", "multiplier": 0.001},
],
]
# only ES for AGB, all for Ginko
for country in ["ES", "ID", "CO", "CR", "EC", "IN", "BR", "US"]
],
[],
),
},
}
]
PASTOECO_MIGRATIONS = [
{
"name": "pastoeco-technosphere-fixes",
"description": "Fixes to ease linking to agb",
"data": {
"fields": ("name",),
"data": [
(
("Diesel {Europe without Switzerland}| market for | Cut-off, S",),
{
"name": "Diesel {Europe without Switzerland}| market for | Cut-off, S - Copied from Ecoinvent U"
},
),
(
("Petrol, two-stroke blend {GLO}| market for | Cut-off, S",),
{
"name": "Petrol, two-stroke blend {GLO}| market for | Cut-off, S - Copied from Ecoinvent U"
},
),
(
(
"Tap water {Europe without Switzerland}| market for | Cut-off, S",
),
{
"name": "Tap water {Europe without Switzerland}| market for | Cut-off, S - Copied from Ecoinvent U"
},
),
(
("Electricity, low voltage {FR}| market for | Cut-off, S",),
{
"name": "Electricity, low voltage {FR}| market for | Cut-off, S - Copied from Ecoinvent U"
},
),
(
(
"Newborn dairy calf, Conventional, Alsace, at farm gate, FR - MEANS#16615 U",
),
{
"name": "Newborn dairy calf, Conventional, Alsace, at farm gate - MEANS#16615, FR"
},
),
],
},
}
]
def remove_azadirachtine(db):
"""Remove all exchanges with azadirachtine, except for apples"""
new_db = []
for ds in db:
new_ds = copy.deepcopy(ds)
new_ds["exchanges"] = [
exc
for exc in ds["exchanges"]
if (
"azadirachtin" not in exc.get("name", "").lower()
or ds.get("name", "").lower().startswith("apple")
)
]
new_db.append(new_ds)
return new_db
def remove_negative_land_use_on_tomato(db):
"""Remove transformation flows from urban on greenhouses
that cause negative land-use on tomatoes"""
new_db = []
for ds in db:
new_ds = copy.deepcopy(ds)
if ds.get("name", "").lower().startswith("plastic tunnel"):
new_ds["exchanges"] = [
exc
for exc in ds["exchanges"]
if not exc.get("name", "")
.lower()
.startswith("transformation, from urban")
]
else:
pass
new_db.append(new_ds)
return new_db
def remove_some_processes(db):
"""Some processes make the whole import fail
due to inability to parse the Input and Calculated parameters"""
new_db = []
for ds in db:
new_ds = copy.deepcopy(ds)
if ds.get("simapro metadata", {}).get("Process identifier") not in (
"EI3CQUNI000025017103662",
):
new_db.append(new_ds)
return new_db
GINKO_STRATEGIES = [
remove_negative_land_use_on_tomato,
remove_azadirachtine,
functools.partial(
link_technosphere_by_activity_hash_ref_product,
external_db_name="Agribalyse 3.1.1",
fields=("name", "unit"),
),
]
AGB_STRATEGIES = [remove_negative_land_use_on_tomato]
if __name__ == "__main__":
"""Import Agribalyse and additional processes"""
parser = argparse.ArgumentParser()
parser.add_argument(
"--recreate-activities",
action="store_true",
help="Delete and re-create the created activities",
)
args = parser.parse_args()
setup_project()
# AGRIBALYSE 3.1.1
if (db := "Agribalyse 3.1.1") not in bw2data.databases:
import_simapro_csv(
join(DB_FILES_DIR, AGRIBALYSE31),
db,
migrations=AGRIBALYSE_MIGRATIONS,
excluded_strategies=EXCLUDED,
other_strategies=AGB_STRATEGIES,
)
else:
print(f"{db} already imported")
# AGRIBALYSE 3.2
if (db := "Agribalyse 3.2 beta 08/08/2024") not in bw2data.databases:
import_simapro_csv(
join(DB_FILES_DIR, AGRIBALYSE32),
db,
migrations=AGRIBALYSE_MIGRATIONS,
first_strategies=[remove_some_processes],
excluded_strategies=EXCLUDED,
other_strategies=AGB_STRATEGIES,
)
else:
print(f"{db} already imported")
# PASTO ECO
if (db := "PastoEco") not in bw2data.databases:
import_simapro_csv(
join(DB_FILES_DIR, PASTOECO),
db,
migrations=PASTOECO_MIGRATIONS,
excluded_strategies=EXCLUDED,
other_strategies=[
functools.partial(
link_technosphere_by_activity_hash_ref_product,
external_db_name="Agribalyse 3.1.1",
fields=("name", "unit"),
)
],
)
else:
print(f"{db} already imported")
# GINKO
if (db := "Ginko") not in bw2data.databases:
import_simapro_csv(
join(DB_FILES_DIR, GINKO),
db,
excluded_strategies=EXCLUDED,
other_strategies=GINKO_STRATEGIES,
migrations=GINKO_MIGRATIONS + AGRIBALYSE_MIGRATIONS,
)
else:
print(f"{db} already imported")
# CTCPA
if (db := "CTCPA") not in bw2data.databases:
import_simapro_csv(join(DB_FILES_DIR, CTCPA), db, excluded_strategies=EXCLUDED)
else:
print(f"{db} already imported")
# WFLDB
if (db := "WFLDB") not in bw2data.databases:
import_simapro_csv(join(DB_FILES_DIR, WFLDB), db, excluded_strategies=EXCLUDED)
else:
print(f"{db} already imported")
if args.recreate_activities:
if "Ecobalyse" in bw2data.databases:
del bw2data.databases["Ecobalyse"]