-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataconvert.py
214 lines (193 loc) · 7.57 KB
/
dataconvert.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
import csv
import json
CSVCHUNK = 1000
def parse(conf_file,data_file):
"""
Read the type field and chose the associated parser
"""
with open(conf_file,'r') as fi:
conf_str = fi.read()
config_f = json.loads(conf_str)
if config_f['type']==1:
parse1(conf_file,data_file,config_f)
elif config_f['type']==2:
parse2(conf_file,data_file,config_f)
elif config_f['type']==3:
#station varies, datetime together
parse3(conf_file,data_file,config_f)
def parse1(conf_file,data_file,config):
with open(data_file,'r') as fi:
with open('temp/all_data.csv','w', newline="") as fo:
# convert the input file to a consistent file with all data
r = csv.reader(fi)
w = csv.writer(fo)
w.writerow(["station","date","time","parameter","value"])
columns = config['columns']
station = config['station']
datecol = columns.index("date")
timecol = columns.index("time")
header = config['header']
for i,ncol in enumerate(columns):
if ncol == "date":
continue
elif ncol == "time":
continue
elif ncol == "":
continue
else:
for j,nrow in enumerate(r):
if j < header:
continue
else:
newrow = []
newrow.append(station) #station
newrow.append(nrow[datecol]) #date
newrow.append(nrow[timecol]) #time
newrow.append(ncol) #parameter
newrow.append(nrow[i]) # value
w.writerow(newrow)
fi.seek(0)
with open('temp/all_data.csv','r') as fi:
r = csv.reader(fi)
filecount = 1
counter = 1
eof = False
next(fi)
while eof == False:
eof = True
filename = "temp/PART_{}.csv".format(filecount)
filecount += 1
with open(filename,'w',newline="") as fo:
w = csv.writer(fo)
w.writerow(["station","date","time","parameter","value"])
for i,line in enumerate(r):
# if i < counter:
# continue
# else:
#print(counter)
counter += 1
w.writerow(line)
eof = False
if counter % CSVCHUNK == 0:
early_break = True
break
def recorddate(date_time):
cleandata = date_time.strip()
rdate = cleandata.split(" ")[0]
rtlist = cleandata.split(" ")[1:]
rtime = " ".join(rtlist)
return rdate, rtime
def parse2(conf_file,data_file,config):
with open(data_file,'r') as fi:
with open('temp/all_data.csv','w', newline="") as fo:
# convert the input file to a consistent file with all data
r = csv.reader(fi)
w = csv.writer(fo)
w.writerow(["station","date","time","parameter","value"])
columns = config['columns']
station = config['station']
datetimecol = columns.index("datetime")
header = config['header']
for i,ncol in enumerate(columns):
if ncol == "datetime":
continue
elif ncol == "":
continue
else:
for j,nrow in enumerate(r):
if j < header:
continue
else:
rdate,rtime = recorddate(nrow[datetimecol])
newrow = []
newrow.append(station) #station
newrow.append(rdate) #date
newrow.append(rtime) #time
newrow.append(ncol) #parameter
newrow.append(nrow[i]) # value
w.writerow(newrow)
fi.seek(0)
with open('temp/all_data.csv','r') as fi:
r = csv.reader(fi)
filecount = 1
counter = 1
eof = False
next(fi)
while eof == False:
eof = True
filename = "temp/PART_{}.csv".format(filecount)
filecount += 1
with open(filename,'w',newline="") as fo:
w = csv.writer(fo)
w.writerow(["station","date","time","parameter","value"])
for i,line in enumerate(r):
# if i < counter:
# continue
# else:
#print(counter)
counter += 1
w.writerow(line)
eof = False
if counter % CSVCHUNK == 0:
early_break = True
break
def parse3(conf_file,data_file,config):
with open(data_file,'r') as fi:
with open('temp/all_data.csv','w', newline="") as fo:
# convert the input file to a consistent file with all data
r = csv.reader(fi)
w = csv.writer(fo)
w.writerow(["station","date","time","parameter","value"])
columns = config['columns']
stationcol = columns.index("station")
datetimecol = columns.index("datetime")
header = config['header']
for i,ncol in enumerate(columns):
if ncol == "datetime":
continue
elif ncol == "":
continue
elif ncol == "station":
continue
else:
for j,nrow in enumerate(r):
if j < header:
continue
else:
rdate,rtime = recorddate(nrow[datetimecol])
station = nrow[stationcol]
newrow = []
newrow.append(station) #station
newrow.append(rdate) #date
newrow.append(rtime) #time
newrow.append(ncol) #parameter
newrow.append(nrow[i]) # value
w.writerow(newrow)
fi.seek(0)
with open('temp/all_data.csv','r') as fi:
r = csv.reader(fi)
filecount = 1
counter = 1
eof = False
next(fi)
while eof == False:
eof = True
filename = "temp/PART_{}.csv".format(filecount)
filecount += 1
with open(filename,'w',newline="") as fo:
w = csv.writer(fo)
w.writerow(["station","date","time","parameter","value"])
for i,line in enumerate(r):
# if i < counter:
# continue
# else:
#print(counter)
counter += 1
w.writerow(line)
eof = False
if counter % CSVCHUNK == 0:
early_break = True
break
if __name__ == "__main__":
#used only for testing
parse("njdep.json","c:/data/njdep.csv")