-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdata_get.py
431 lines (313 loc) · 11.8 KB
/
data_get.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
def data_get(file_name,key,keynum,*positional_parameters,**keyword_parameters):
import num_check
keycount=0
file1 = open(str(file_name))
preamble=[]
if ('preamble' in keyword_parameters):
preadd=1
else:
preadd=0
if preadd==1:
read_line=file1.readline()
preamble.append(read_line)
split_line=[]
while keycount < keynum:
read_line=file1.readline()
split_line=read_line.split()
# print("split= ", split_line)
if len(split_line)>0:
if split_line[0]==key:
keycount+=1
if preadd==1: del preamble[-1]
# print("vars= ", split_line)
var_name=[i for i in split_line]
# print "var_name= ", var_name
# print len(var_name)
variables=[[] for i in range(len(var_name))]
Nvar=len(variables)
# print("Nvar= ", Nvar)
while split_line != []:
read_line=file1.readline()
split_line=read_line.split()
# print("split_line= ", split_line)
if split_line!=[]:
skip=0
if len(split_line) < Nvar:
skip=1
# print("skip")
else:
for i in split_line:
if num_check.num_check(i) == 0:
skip=1
if skip==0:
for i in range(len(split_line)):
# print("i= ", i)
variables[i].append(split_line[i])
output=[]
for i in range(len(var_name)):
output.append(variables[i])
output.append(var_name)
if preadd==1: output=[output,preamble]
file1.close()
return output
#------------------------------------------------------------------------------------------------------------------------------------------------------
def data_get_every(file_name,key,keynum,*positional_parameters,**keyword_parameters):
import num_check
keycount=0
file1 = open(str(file_name))
preamble=[]
if ('preamble' in keyword_parameters):
preadd=1
else:
preadd=0
if preadd==1:
read_line=file1.readline()
preamble.append(read_line)
split_line=[]
while keycount < keynum:
read_line=file1.readline()
split_line=read_line.split()
# print("split= ", split_line)
if len(split_line)>0:
if split_line[0]==key:
keycount+=1
if preadd==1: del preamble[-1]
# print("vars= ", split_line)
var_name=[i for i in split_line]
# print "var_name= ", var_name
# print len(var_name)
variables=[[] for i in range(len(var_name))]
Nvar=len(variables)
# print("Nvar= ", Nvar)
empty_count=0
while emptycount<100:
read_line=file1.readline()
split_line=read_line.split()
# print("split_line= ", split_line)
if split_line!=[]:
skip=0
if len(split_line) < Nvar:
skip=1
# print("skip")
empty_count+=1
else:
for i in split_line:
if num_check.num_check(i) == 0:
skip=1
empty_count+=1
if skip==0:
empty_count=0
for i in range(len(split_line)):
# print("i= ", i)
variables[i].append(split_line[i])
output=[]
for i in range(len(var_name)):
output.append(variables[i])
output.append(var_name)
if preadd==1: output=[output,preamble]
file1.close()
return output
#------------------------------------------------------------------------------------------------------------------------------------------------------
def data_get_ncn(file_name,key,keynum,*positional_parameters,**keyword_parameters):
import num_check
keycount=0
file1 = open(str(file_name))
read_line=file1.readline()
split_line=read_line.split()
Nvar=len(split_line)
for i in range(Nvar):
globals()['Var%s'%i]=[]
globals()['Var%s'%i].append(split_line[i])
while split_line != []:
read_line=file1.readline()
split_line=read_line.split()
if split_line==[]:
v=2
# print 'read complete'
else:
skip=0
if len(split_line) < Nvar:
skip=1
else:
for i in split_line:
if num_check.num_check(i) == 0:
skip=1
skip=0
if skip==0:
for i in range(Nvar):
globals()['Var%s'%i].append(split_line[i])
#here since we cant put the return command in a loop we need to create a loop to create the
# 'output' list that is a list of the lists we extracted
output=[]
for i in range(Nvar):
output.append(globals()['Var%s'%i])
output.append([i+1 for i in range(Nvar)])
file1.close()
return output
def data_get_T(file_name,key,keynum):
print(file_name)
print(key)
print(keynum)
keycount=0
# Here we read in the file and read and split the first line to initiate the loop
file1 = open(str(file_name))
read_line=file1.readline()
split_line=read_line.split()
# print 'first split entry =',split_line[0]
# Here the Entry Index is the line at which we encounter the key phrase
# We assume that all Data files will some kind of keyword that symbolizes the
# start of the actual data so we can ignore the rest
entry_index=1
# This block of commands is to help navigate through the file until we find the key
# which is a keyword that signifies the next line is actual data
# We read and split each line succesively looking for the first entry in a line to be key
while keycount < keynum:
while split_line == [] or split_line[0] != key:
read_line=file1.readline()
split_line=read_line.split()
entry_index = entry_index + 1
# print 'entry_index=', entry_index
Nvar=len(split_line)
if split_line[0] == key:
keycount=keycount+1
if keycount < keynum:
read_line=file1.readline()
split_line=read_line.split()
entry_index = entry_index + 1
# print keycount
# print 'entry_index=', entry_index
output=[]
read_line=file1.readline()
split_line=read_line.split()
names=[]
while split_line != []:
output.append([split_line[i+1] for i in range(len(split_line)-1)])
names.append(split_line[0])
read_line=file1.readline()
split_line=read_line.split()
output.append(names)
#here since we cant put the return command in a loop we need to create a loop to create the
# 'output' list that is a list of the lists we extracted
file1.close()
return output
def data_get_single_line(file_name,key,keynum):
print(file_name)
print(key)
print(keynum)
keycount=0
# Here we read in the file and read and split the first line to initiate the loop
file1 = open(str(file_name))
read_line=file1.readline()
split_line=read_line.split()
# print 'first split entry =',split_line[0]
# Here the Entry Index is the line at which we encounter the key phrase
# We assume that all Data files will some kind of keyword that symbolizes the
# start of the actual data so we can ignore the rest
entry_index=1
# This block of commands is to help navigate through the file until we find the key
# which is a keyword that signifies the next line is actual data
# We read and split each line succesively looking for the first entry in a line to be key
while keycount < keynum:
while split_line == [] or split_line[0] != key:
read_line=file1.readline()
split_line=read_line.split()
entry_index = entry_index + 1
# print 'entry_index=', entry_index
Nvar=len(split_line)
if split_line[0] == key:
keycount=keycount+1
if keycount < keynum:
read_line=file1.readline()
split_line=read_line.split()
entry_index = entry_index + 1
# print keycount
# print 'entry_index=', entry_index
#here since we cant put the return command in a loop we need to create a loop to create the
# 'output' list that is a list of the lists we extracted
file1.close()
return split_line
def data_get_string(file_name,key,keynum,*positional_parameters,**keyword_parameters):
import num_check
keycount=0
# Here we read in the file and read and split the first line to initiate the loop
file1 = open(str(file_name))
preamble=[]
if ('preamble' in keyword_parameters):
# print "getting preamble"
preadd=1
else:
preadd=0
read_line=file1.readline()
if preadd==1:
preamble.append(read_line)
split_line=read_line.split()
# print split_line
# print 'first split entry =',split_line[0]
# Here the Entry Index is the line at which we encounter the key phrase
# We assume that all Data files will some kind of keyword that symbolizes the
# start of the actual data so we can ignore the rest
entry_index=1
# This block of commands is to help navigate through the file until we find the key
# which is a keyword that signifies the next line is actual data
# We read and split each line succesively looking for the first entry in a line to be key
while keycount < keynum:
while split_line == [] or split_line[0] != key:
read_line=file1.readline()
if preadd==1: preamble.append(read_line)
split_line=read_line.split()
# print "split_line= ", split_line
entry_index = entry_index + 1
# print 'entry_index=', entry_index
Nvar=len(split_line)
if split_line[0] == key:
keycount=keycount+1
if keycount < keynum:
read_line=file1.readline()
if preadd==1: preamble.append(read_line)
split_line=read_line.split()
# print "split_line= ", split_line
entry_index = entry_index + 1
if preadd==1: del preamble[-1]
# print keycount
# print 'entry_index=', entry_index
Nvar=len(split_line)
var_name=split_line
# print "Nvar= ", Nvar
# These commands are now to create lists for each value
# We simply keep appending the entries until we get to the end
# The point of the len check is that if we encounter the phrase: "End Run", "Error"
# or some other phrase which is shorter than the number of vars we
# have then we can fill in those empty spots
# with the 'empty'
# the point of the phrase globals()['Var%s'%i] is so that we
# can create the appropriate amount of lists to fix the issue
# of a non static amount of variable we may encounter
for i in range(Nvar):
globals()['Var%s'%i]=[]
while split_line != []:
read_line=file1.readline()
split_line=read_line.split()
if split_line==[]:
v=2
# print 'read complete'
else:
skip=0
if len(split_line) < Nvar:
skip=1
# try:
# float(split_line[0])
# except ValueError:
# skip=1
if skip==0:
for i in range(Nvar):
globals()['Var%s'%i].append(split_line[i])
#here since we cant put the return command in a loop we need to create a loop to create the
# 'output' list that is a list of the lists we extracted
output=[]
for i in range(Nvar):
del globals()['Var%s'%i][-1]
output.append(globals()['Var%s'%i])
output.append(var_name)
if preadd==1: output=[output,preamble]
file1.close()
return output