-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfile_parser.py
More file actions
128 lines (101 loc) · 3.95 KB
/
file_parser.py
File metadata and controls
128 lines (101 loc) · 3.95 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
# parse output file from CST
# add data to database
import re
import numpy as np
import database
class SuperParser(object):
def __init__(self, path, filename):
super(SuperParser, self).__init__()
self.path = path
self.file_name = filename
self.inputfile = open(path + filename, "r")
class CSTFileParser(SuperParser):
def __init__(self, path, filename):
super(CSTFileParser, self).__init__(path, filename)
def parse_file(self, database):
i = 0
for line in self.inputfile:
caption_match = re.findall('([A-Za-z]+\s)', line)
digit_match = re.findall('-?\d+\.?\d*', line)
if caption_match:
for component in caption_match:
database.captions.append(component)
database[component] = []
if digit_match:
for component in database.captions:
database[component].append(float(digit_match[database.captions.index(component)]))
class NewCSTFileParser(SuperParser):
def __init__(self, path, filename):
super(NewCSTFileParser, self).__init__(path, filename)
def parse_file(self, database):
row1 = next(self.inputfile)
caption_match = re.findall('([A-Za-z]+\s)', row1)
if caption_match:
for component in caption_match:
database.captions.append(component)
database[component] = []
next(self.inputfile)
for line in self.inputfile:
for component in database.captions:
database[component].append(float(line.split()[database.captions.index(component)]))
class CSVNerveShapeParser(SuperParser):
def __init__(self, path, filename):
super(CSVNerveShapeParser, self).__init__(path, filename)
def parse_file(self, database):
x = []
y = []
z = []
with open(self.path + self.file_name) as f:
# lines = f.readlines()
next(f)
for line in f:
line_elements = line.split(',')
x.append(float(line_elements[0]))
y.append(float(line_elements[1]))
z.append(float(line_elements[2]))
database["x "] = x
database["y "] = y
database["z "] = z
database["ExRe "] = []
database["EyRe "] = []
database["EzRe "] = []
database["ExIm "] = []
database["EyIm "] = []
database["EzIm "] = []
class S4LFileParser(SuperParser):
def __init__(self, path, filename):
super(S4LFileParser, self).__init__(path, filename)
def parse_file(self, database):
x, y, z = [], [], []
Ex_Re, Ex_Im = [], [] # Electric field intensity in Sim4Life
Ey_Re, Ey_Im = [], []
Ez_Re, Ez_Im = [], []
i = 0
inputfile = open(self.file_name, "r")
for line in inputfile:
if i == 0: # [0] headers
str_list = line.split() # separated by the symbol inside ''
elif i == 1: # [1] dash line
str_list = line.split()
elif i > 19: # line after headers [0] and dash line [1]
str_list = line.split() # separated by the symbol
x.append((float(str_list[0])))
y.append((float(str_list[1])))
z.append((float(str_list[2])))
Ex_Re.append(float(str_list[3]))
Ex_Im.append(float(str_list[4]))
Ey_Re.append(float(str_list[5]))
Ey_Im.append(float(str_list[6]))
Ez_Re.append(float(str_list[7]))
Ez_Im.append(float(str_list[8]))
i = i + 1
print('Parsing done')
database["x "] = x
database["y "] = y
database["z "] = z
database["ExRe "] = Ex_Re
database["EyRe "] = Ey_Re
database["EzRe "] = Ez_Re
database["ExIm "] = Ex_Im
database["EyIm "] = Ey_Im
database["EzIm "] = Ez_Im