forked from CMLPlatform/Core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_JSON_MS_US_FLOWS_materials.py
193 lines (154 loc) · 6.08 KB
/
create_JSON_MS_US_FLOWS_materials.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
import json
def getfile():
#open the file
#*** > give the path to the file.
f=open('../data/US_GAS_materials.csv', 'r')
#get the content
F=f.read()
#split (make an array where each element is determined by an enter)
U = F.split('\n')
#Create empty list !!!!!!! THIS IS THE WORKING LIST OF LIST WE NEED FOR EVERYTHING !!
L = []
#fill the empty list with the data (this time split even further by tabs)
for line in U:
L.append(line.split('#'))
#data cleaning
L.pop(-1)
L.pop(0)
L.pop(0)
return L
def getfile2():
#open the file
#*** > give the path to the file.
f=open('../data/US_RS_materials.csv', 'r')
#get the content
F=f.read()
#split (make an array where each element is determined by an enter)
U = F.split('\n')
#Create empty list !!!!!!! THIS IS THE WORKING LIST OF LIST WE NEED FOR EVERYTHING !!
L = []
#fill the empty list with the data (this time split even further by tabs)
for line in U:
L.append(line.split('#'))
#data cleaning
L.pop(-1)
L.pop(0)
L.pop(0)
return L
def create_output(input):
id = 0
id2 = 121
id3 = 167
json_string = "{"
data_list = []
#parse over the lines
for line in input:
years = line[0]
timber_hist = line[1]
timber_sc1 = line[2]
timber_sc2 = line[3]
timber_sc3 = line[4]
iron_hist = line[6]
iron_sc1 = line[7]
iron_sc2 = line[8]
iron_sc3 = line[9]
other_hist = line[11]
other_sc1 = line[12]
other_sc2 = line[13]
other_sc3 = line[14]
minerals_hist = line[16]
minerals_sc1 = line[17]
minerals_sc2 = line[18]
minerals_sc3 = line[19]
#as we know the dataset by heart we can manipulate in a simple manner
#we only take historical data now which is all years untill 2005
if years <= '2005':
#use id to identify the highest hierachy of the json files
id = id + 1
json_string += '"'+str(id)+'":{"scenarios":"GAS_Historical_Series","year":'+years+\
',"materials":[["timber",'+timber_hist+'],["iron",'+iron_hist+'],["other metals",'\
+other_hist+'],["minerals",'+minerals_hist+']]},'
#after 2005 we have two scenarios we want to use
else:
id = id + 1
#be careful with parsing here
json_string += '"' + str(id) + '":{"scenarios":"GAS_sc1","year":' + years + \
',"materials":[["timber",' + timber_sc1 + '],["iron",' + iron_sc1 + '],["other metals",' \
+ other_sc1 + '],["minerals",' + minerals_sc1 + ']]},'
id2 = id2 + 1
json_string += '"' + str(id2) + '":{"scenarios":"GAS_sc2","year":' + years + \
',"materials":[["timber",' + timber_sc2 + '],["iron",' + iron_sc2 + '],["other metals",' \
+ other_sc2 + '],["minerals",' + minerals_sc2 + ']]},'
id3 = id3 + 1
json_string += '"' + str(id3) + '":{"scenarios":"GAS_sc3","year":' + years + \
',"materials":[["timber",' + timber_sc3 + '],["iron",' + iron_sc3 + '],["other metals",' \
+ other_sc3 + '],["minerals",' + minerals_sc3 + ']]},'
#remove last character of string
#json_string = json_string[:-1]
#json_string += '}'
print(json_string)
output = data_list
return output
def create_output2(input):
id = 212
id2 = 333
id3 = 500
json_string = ""
data_list = []
#parse over the lines
for line in input:
years = line[0]
timber_hist = line[1]
timber_sc1 = line[2]
timber_sc2 = line[3]
timber_sc3 = line[4]
iron_hist = line[6]
iron_sc1 = line[7]
iron_sc2 = line[8]
iron_sc3 = line[9]
other_hist = line[11]
other_sc1 = line[12]
other_sc2 = line[13]
other_sc3 = line[14]
minerals_hist = line[16]
minerals_sc1 = line[17]
minerals_sc2 = line[18]
minerals_sc3 = line[19]
#as we know the dataset by heart we can manipulate in a simple manner
#we only take historical data now which is all years untill 2005
if years <= '2005':
#use id to identify the highest hierachy of the json files
id = id + 1
json_string += '"'+str(id)+'":{"scenarios":"RS_Historical_Series","year":'+years+\
',"materials":[["timber",'+timber_hist+'],["iron",'+iron_hist+'],["other metals",'\
+other_hist+'],["minerals",'+minerals_hist+']]},'
#after 2005 we have two scenarios we want to use
else:
id = id + 1
#be careful with parsing here
json_string += '"' + str(id) + '":{"scenarios":"RS_sc1","year":' + years + \
',"materials":[["timber",' + timber_sc1 + '],["iron",' + iron_sc1 + '],["other metals",' \
+ other_sc1 + '],["minerals",' + minerals_sc1 + ']]},'
id2 = id2 + 1
json_string += '"' + str(id2) + '":{"scenarios":"RS_sc2","year":' + years + \
',"materials":[["timber",' + timber_sc2 + '],["iron",' + iron_sc2 + '],["other metals",' \
+ other_sc2 + '],["minerals",' + minerals_sc2 + ']]},'
id3 = id3 + 1
json_string += '"' + str(id3) + '":{"scenarios":"RS_sc3","year":' + years + \
',"materials":[["timber",' + timber_sc3 + '],["iron",' + iron_sc3 + '],["other metals",' \
+ other_sc3 + '],["minerals",' + minerals_sc3 + ']]},'
#remove last character of string
json_string = json_string[:-1]
json_string += '}'
print(json_string)
output = data_list
return output
# Start execution here!
if __name__ == '__main__':
print ("Starting JSON data viz creation script...")
data = getfile()
data2 = getfile2()
output = create_output(data)
create_output2(data2)
#for line in output:
# print(line)