-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdb_setup.py
More file actions
287 lines (220 loc) · 8.43 KB
/
Copy pathdb_setup.py
File metadata and controls
287 lines (220 loc) · 8.43 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
# -*- coding: utf-8 -*-
"""
RIVM note: Script copied from EMPA dpmfa-db package:
https://github.com/empa-tsl/dpmfa-db
dpmfa-db/example_data/db_setup.py
Original:
Created on Tue Oct 8 11:36:27 2019
@author: dew
Altered by Anne Hids (RIVM)
To run this script, SQLite needs to be installed on the computer.
"""
import os
import config
import pandas as pd
import sqlite3
import shutil
# Fetch configurations
reg = config.reg
sellist = config.sellist
# Steps to find or create folders
if config.OS_env == 'win':
inputfolder = ".\\input\\" + reg + "\\"
else:
inputfolder = "./input/" + reg + "/"
db_name = reg + ".db"
os.chdir(inputfolder)
source_pathtoDB = os.path.abspath(db_name)
# if the database already exist, remove and start new
if os.path.isfile(db_name) == True:
os.remove(db_name)
#%%
# create or open database
connection = sqlite3.connect(db_name,timeout=10)
cursor = connection.cursor()
### COMPARTMENTS ##############################################################
# create a table for compartments
cursor.execute("""
CREATE TABLE IF NOT EXISTS compartments (
name TEXT,
fulllabel TEXT,
type TEXT,
PRIMARY KEY(fulllabel)
);""")
# import table
df = pd.read_csv('Compartments.csv', sep = ";", decimal = ",")
# append data to database
df.to_sql('compartments', connection, if_exists='append', index = False)
### MATERIALS #################################################################
# create a table for materials
cursor.execute("""
CREATE TABLE IF NOT EXISTS materials (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT
);""")
# import table
df = pd.read_csv('Materials.csv', sep = ";", decimal = ",")
# append data to database
df.to_sql('materials', connection, if_exists='append', index = False)
### TRANSFER COEFFICIENTS #####################################################
# create a table for transfer coefficients
cursor.execute("""
CREATE TABLE IF NOT EXISTS transfercoefficients (
id INTEGER PRIMARY KEY AUTOINCREMENT,
comp1 INTEGER NOT NULL,
comp2 INTEGER NOT NULL,
year INTEGER,
mat INTEGER NOT NULL,
value DOUBLE,
priority INTEGER NOT NULL,
dqisgeo INTEGER NOT NULL,
dqistemp INTEGER NOT NULL,
dqismat INTEGER NOT NULL,
dqistech INTEGER NOT NULL,
dqisrel INTEGER NOT NULL,
source TEXT,
FOREIGN KEY(comp1) REFERENCES compartments(name),
FOREIGN KEY(comp2) REFERENCES compartments(name),
FOREIGN KEY(mat) REFERENCES materials(name)
);""")
# import table
df = pd.read_csv('TC.csv', sep = ";", decimal = ",")
# append data to database
df.to_sql('transfercoefficients', connection, if_exists='append', index = False)
### INPUT #####################################################################
# create a table for input
cursor.execute("""
CREATE TABLE IF NOT EXISTS input (
id INTEGER PRIMARY KEY AUTOINCREMENT,
comp TEXT,
year INTEGER,
mat TEXT,
value DOUBLE,
dqisgeo INTEGER NOT NULL,
dqistemp INTEGER NOT NULL,
dqismat INTEGER NOT NULL,
dqistech INTEGER NOT NULL,
dqisrel INTEGER NOT NULL,
source TEXT,
FOREIGN KEY(comp) REFERENCES compartments(name),
FOREIGN KEY(mat) REFERENCES materials(id)
);""")
# explore directory for files
for root, dirs, files in os.walk(".", topdown=False):
for name in files:
# test if "Input" is in file name, if not, skip
if not "Input" in name:
continue
# else import table
df = pd.read_csv(name, sep = ";", decimal = ",")
# append data to database
df.to_sql('input', connection, if_exists='append', index = False)
### LIFETIMES #################################################################
# create a table for lifetimes
cursor.execute("""
CREATE TABLE IF NOT EXISTS lifetimes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
comp TEXT,
year INTEGER,
value DOUBLE,
FOREIGN KEY(comp) REFERENCES compartments(name)
);""")
# import table
df = pd.read_csv('Lifetimes.csv', sep = ";", decimal = ",")
# append data to database
df.to_sql('lifetimes', connection, if_exists='append', index = False)
# commit changes
connection.commit()
# close connection
connection.close()
print('Converting csv files to '+ reg +' database done...\n')
#%%
#########################################
# Create selection databases
#########################################
## Create a new database for sel[i], containing only the input rows for sel[i]
selmatdf = pd.DataFrame()
for sel in sellist:
# Create a folder for the new input database (if it does not exist)
if config.OS_env == 'win':
selfolder = ".\\"
else:
selfolder = "./"
# Change working directory to selfolder
os.chdir(selfolder)
# Create a path for the copied db file
db_name = reg + "_" + sel + ".db"
pathtoDB = os.path.abspath(db_name)
# if the database already exist, remove and start new
if os.path.isfile(db_name) == True:
os.remove(db_name)
# Copy the database to the new folder
shutil.copy(source_pathtoDB, pathtoDB)
# Open a connection to the new database
connection = sqlite3.connect(pathtoDB)
cursor = connection.cursor()
if sel == "Clothing" and reg == "NL":
comps = ["Import of clothing (EU)", "Import of clothing (Global)"]
elif sel == "Clothing" and reg == "EU":
comps = ["Import of clothing (Global)", "Import of plastic sheets", "Import of yarn", 'Domestic primary plastic production', 'Import of primary plastics']
else:
comps = [sel]
query = f"SELECT * FROM input WHERE comp IN ({','.join(['?'] * len(comps))})"
# Execute the query with the list of `comps` as parameters
cursor.execute(query, comps)
# Convert the results to a DataFrame
input_selection = pd.DataFrame(cursor.fetchall())
# Rename columns of dataframe
col = {
0: 'id',
1: 'comp',
2: 'year',
3: 'mat',
4: 'value',
5: 'dqisgeo',
6: 'dqistemp',
7: 'dqismat',
8: 'dqistech',
9: 'dqisrel',
10: 'source'
}
input_selection.rename(columns = col, inplace = True)
# Remove the original input table from the database
cursor.execute("DROP TABLE input")
## Insert input_selection as the new input table in the database
# Create a table for input
cursor.execute("""
CREATE TABLE IF NOT EXISTS input (
id INTEGER PRIMARY KEY AUTOINCREMENT,
comp TEXT,
year INTEGER,
mat TEXT,
value DOUBLE,
dqisgeo INTEGER NOT NULL,
dqistemp INTEGER NOT NULL,
dqismat INTEGER NOT NULL,
dqistech INTEGER NOT NULL,
dqisrel INTEGER NOT NULL,
source TEXT,
FOREIGN KEY(comp) REFERENCES compartments(name),
FOREIGN KEY(mat) REFERENCES materials(id)
);""")
# Append data to database
input_selection.to_sql('input', connection, if_exists='append', index = False)
# Check which materials occur in selection and add these to a selmatcombis
cursor.execute("SELECT DISTINCT mat FROM input")
materials = []
materials = cursor.fetchall()
materials_series = pd.Series()
materials_series = pd.Series([mat[0] for mat in materials])
selmatcombis = pd.DataFrame()
selmatcombis['mat'] = materials_series
selmatcombis['sel'] = sel
selmatdf = pd.concat([selmatdf, selmatcombis], axis = 0)
# Commit changes
connection.commit()
# Close connection
connection.close()
selmatdf.to_csv('selection_material_combinations.csv', sep = ";", index = True, header = True)
os.chdir(mainfolder)
print('Creating selection databases done...')