-
Notifications
You must be signed in to change notification settings - Fork 0
/
ei_LCIA_MF_populate.py
223 lines (197 loc) · 7.21 KB
/
ei_LCIA_MF_populate.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
# -*- coding: utf-8 -*-
"""
Last modified June 2021
@author: pauliuk
see: https://github.com/IndEcol/openLCA_ecoinvent_Material_Footprint_LCIA
"""
# Script ei_LCIA_MF_populate.py
# Import required libraries:
#%%
import openpyxl
import numpy as np
import os
import uuid
import json
import mf_Paths
#############################
# Functions & Constants #
#############################
def CF_generate(mli,Val,dnames,duuid,dunit,ei_version_string):
# create dictionary with characterisation factor
if ei_version_string == '_ei_3_7_1' or ei_version_string == '_ei_3_8': # unit defintions have not changed, are the same for both ei versions.
U_Mass = {
"@type": "Unit",
"@id": "20aadc24-a391-41cf-b340-3e4529f44bde",
"name": "kg"}
U_Energy = {
"@type": "Unit",
"@id": "52765a6c-3896-43c2-b2f4-c679acf13efe",
"name": "MJ"}
U_Volume = {
"@type": "Unit",
"@id": "1c3a9695-398d-4b1f-b07e-a8715b610f70",
"name": "m3"}
FP_Mass = {
"@type": "FlowProperty",
"@id": "93a60a56-a3c8-11da-a746-0800200b9a66",
"name": "Mass",
"categoryPath": [
"Technical flow properties"]}
FP_Energy = {
"@type": "FlowProperty",
"@id": "f6811440-ee37-11de-8a39-0800200c9a66",
"name": "Energy",
"categoryPath": [
"Technical flow properties"]}
FP_Volumne = {
"@type": "FlowProperty",
"@id": "93a60a56-a3c8-22da-a746-0800200c9a66",
"name": "Volume",
"categoryPath": [
"Technical flow properties"]}
CF = {}
CF["@type"] = "ImpactFactor"
CF["value"] = Val[mli]
CF["flow"] = {"@type": "Flow", "@id": duuid[mli],
"name": dnames[mli],
"categoryPath": [
"Elementary flows",
"Resource",
"in ground"],
"flowType": "ELEMENTARY_FLOW",
"refUnit": dunit[mli]}
if dunit[mli] == 'kg':
CF["unit"] = U_Mass
CF["flowProperty"] = FP_Mass
elif dunit[mli] == 'MJ':
CF["unit"] = U_Energy
CF["flowProperty"] = FP_Energy
elif dunit[mli] == 'm3':
CF["unit"] = U_Volume
CF["flowProperty"] = FP_Volumne
else:
None
return CF
#################
# MAIN #
#################
# Set configuration data
#ei_version_string = '_ei_3_7_1'
ei_version_string = '_ei_3_8'
#%%
if ei_version_string == '_ei_3_7_1':
tp = mf_Paths.data_path_ei371
MSn = 'LCIA_Define_ecoinvent_3_7'
MDn = 'ecoinvent_3_7_Match'
DN = 414
if ei_version_string == '_ei_3_8':
tp = mf_Paths.data_path_ei38
MSn = 'LCIA_Define_ecoinvent_3_8'
MDn = 'ecoinvent_3_8_Match'
DN = 419
ScriptConfig = {}
ScriptConfig['Current_UUID'] = str(uuid.uuid4())
###################################################################################
# Import data from masterfile #
###################################################################################
# open master file
MasterFile = openpyxl.load_workbook(os.path.join(mf_Paths.data_path_main,'Material_Footprint_LCIA_Master_V1.xlsx'),data_only=True)
# read LCIA indicator method uuids
MS = MasterFile[MSn]
mf_uuid = []
wf_uuid = []
for m in range(10,22):
mf_uuid.append(MS.cell(m, 5).value)
for m in range(10,14):
wf_uuid.append(MS.cell(m, 15).value)
# read master data
MD = MasterFile[MDn]
dnames = []
duuid = []
dselect = []
dunit = []
drmi = []
dtmr = []
for m in range(2,2+DN):
dnames.append( MD.cell(m, 2).value)
duuid.append( MD.cell(m, 4).value)
dselect.append(MD.cell(m,11).value)
dunit.append( MD.cell(m,15).value)
drmi.append( MD.cell(m,16).value)
dtmr.append( MD.cell(m,21).value)
# Tables with 1/0 flags to select individual factor for a given indicator
MFSel = np.zeros((DN,6))
TFSel = np.zeros((DN,6))
WFSel = np.zeros((DN,4))
for m in range(2,2+DN):
for n in range(24,30):
MFSel[m-2,n-24] = MD.cell(m,n).value
for n in range(30,36):
TFSel[m-2,n-30] = MD.cell(m,n).value
for n in range(36,40):
WFSel[m-2,n-36] = MD.cell(m,n).value
###################################################################################
# Sort data into json files #
###################################################################################
#%%
# loop over RMI files
for m in range(0,6):
f_in = os.path.join(tp,'lcia_categories',mf_uuid[m]+'.json')
with open(f_in, 'r+') as f:
thisd = json.load(f)
del thisd['impactFactors'][0] # delete the two factors that are still there from copying the files
del thisd['impactFactors'][0]
# add new impact factors from master data
for mli in range(0,DN):
if dselect[mli] != 1 and MFSel[mli,m] == 1: # add this value as impact/characterisation factor
CF = CF_generate(mli,drmi,dnames,duuid,dunit,ei_version_string)
# add new CF to json file:
thisd['impactFactors'].append(CF)
# wrap up and save
f.seek(0) # reset file position to the beginning.
json.dump(thisd, f, indent=4)
f.truncate() # remove remaining part
f.close()
# loop over TMR files
for m in range(0,6):
f_in = os.path.join(tp,'lcia_categories',mf_uuid[m+6]+'.json')
with open(f_in, 'r+') as f:
thisd = json.load(f)
del thisd['impactFactors'][0] # delete the two factors that are still there from copying the files
del thisd['impactFactors'][0]
# add new impact factors from master data
for mli in range(0,DN):
if dselect[mli] != 1 and TFSel[mli,m] == 1: # add this value as impact/characterisation factor
CF = CF_generate(mli,dtmr,dnames,duuid,dunit,ei_version_string)
# add new CF to json file:
thisd['impactFactors'].append(CF)
# wrap up and save
f.seek(0) # reset file position to the beginning.
json.dump(thisd, f, indent=4)
f.truncate() # remove remaining part
f.close()
# loop over WF files
for m in range(0,4):
f_in = os.path.join(tp,'lcia_categories',wf_uuid[m]+'.json')
with open(f_in, 'r+') as f:
thisd = json.load(f)
del thisd['impactFactors'][0] # delete the two factors that are still there from copying the files
del thisd['impactFactors'][0]
# add new impact factors from master data
for mli in range(0,DN):
if dselect[mli] != 1 and WFSel[mli,m] == 1: # add this value as impact/characterisation factor
CF = CF_generate(mli,drmi,dnames,duuid,dunit,ei_version_string)
# add new CF to json file:
thisd['impactFactors'].append(CF)
# wrap up and save
f.seek(0) # reset file position to the beginning.
json.dump(thisd, f, indent=4)
f.truncate() # remove remaining part
f.close()
#%% Sandbox
#
#
#
# The End
#
#