Skip to content

Commit c123285

Browse files
committed
of parser for windows
1 parent afe63b5 commit c123285

File tree

5 files changed

+40
-32
lines changed

5 files changed

+40
-32
lines changed

smithers/io/openfoam/mesh_parser.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -187,22 +187,30 @@ def parse_points_content(cls, content, is_binary, skip=10):
187187
:param skip: skip lines
188188
:return: points coordinates as numpy.array
189189
"""
190-
n = skip
191-
while n < len(content):
192-
lc = content[n]
193-
if is_integer(lc):
194-
num = int(lc)
195-
if not is_binary:
196-
data = np.array([ln[1:-2].split() for ln in content[n + 2:n + 2 + num]], dtype=float)
197-
else:
198-
buf = b''.join(content[n+1:])
199-
disp = struct.calcsize('c')
200-
vv = np.array(struct.unpack('{}d'.format(num*3),
201-
buf[disp:num*3*struct.calcsize('d') + disp]))
202-
data = vv.reshape((num, 3))
203-
return data
204-
n += 1
205-
return None
190+
def parse_line(line):
191+
return line.decode('utf-8').strip().replace(')', '').replace('(', '').split()
192+
193+
for idx, row in enumerate(content):
194+
try:
195+
num_points = int(row)
196+
start_idx = idx+2
197+
break
198+
except ValueError:
199+
pass
200+
201+
string_coords = content[start_idx:start_idx+num_points]
202+
203+
data = np.array(list(map(parse_line, string_coords)), dtype=float)
204+
return data
205+
206+
#TODO binary
207+
# if is_binary:
208+
# buf = b''.join(content[n+1:])
209+
# disp = struct.calcsize('c')
210+
# vv = np.array(struct.unpack('{}d'.format(num*3),
211+
# buf[disp:num*3*struct.calcsize('d') + disp]))
212+
# data = vv.reshape((num, 3))
213+
206214

207215

208216
@classmethod

smithers/io/vtkhandler.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ class VTKHandler(BaseVTKHandler):
2525
}
2626

2727
@classmethod
28-
def read(cls, filename, format='polydata'):
28+
def read(cls, filename, fmt='polydata'):
2929

30-
if format not in cls.vtk_format.keys():
31-
raise ValueError('`format` is invalid')
30+
if fmt not in cls.vtk_format.keys():
31+
raise ValueError('`fmt` is invalid')
3232

33-
reader = cls.vtk_format[format]['reader']()
33+
reader = cls.vtk_format[fmt]['reader']()
3434
reader.SetFileName(filename)
3535
reader.Update()
3636
data_dict = cls.vtk2dict(reader.GetOutput())
@@ -57,10 +57,10 @@ def vtk2dict(cls, data):
5757
return result
5858

5959
@classmethod
60-
def dict2vtk(cls, data, format):
60+
def dict2vtk(cls, data, fmt):
6161
""" TODO """
6262

63-
vtkdata = cls.vtk_format[format]['type']()
63+
vtkdata = cls.vtk_format[fmt]['type']()
6464

6565
vtk_points = cls._points_()
6666
vtk_points.SetData(cls._numpy_to_vtk_(data['points']))
@@ -78,13 +78,13 @@ def dict2vtk(cls, data, format):
7878
return vtkdata
7979

8080
@classmethod
81-
def write(cls, filename, data, format='polydata'):
81+
def write(cls, filename, data, fmt='polydata'):
8282

83-
if format not in cls.vtk_format.keys():
84-
raise ValueError('`format` is invalid')
83+
if fmt not in cls.vtk_format.keys():
84+
raise ValueError('`fmt` is invalid')
8585

86-
vtkdata = cls.dict2vtk(data, format)
87-
writer = cls.vtk_format[format]['writer']()
86+
vtkdata = cls.dict2vtk(data, fmt)
87+
writer = cls.vtk_format[fmt]['writer']()
8888
writer.SetFileName(filename)
8989
writer.SetInputData(vtkdata)
9090
writer.Write()

smithers/io/vtphandler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ class VTPHandler(VTKHandler):
77

88
@classmethod
99
def read(cls, filename):
10-
return super().read(filename, format=cls.cls_format)
10+
return super().read(filename, fmt=cls.cls_format)
1111

1212
@classmethod
1313
def write(cls, filename, data):
14-
super().write(filename, data, format=cls.cls_format)
14+
super().write(filename, data, fmt=cls.cls_format)

tests/test_objhandler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ def test_write():
4040
]
4141
x.polygons = [[0, 1, 2], [2, 1, 0]]
4242

43-
save_obj(x, "/var/tmp/data.obj")
44-
assert cmp("/var/tmp/data.obj", "tests/test_datasets/file.obj")
43+
save_obj(x, "data.obj")
44+
assert cmp("data.obj", "tests/test_datasets/file.obj")
4545

4646
def test_boundary():
4747
x = WavefrontOBJ()

tests/test_vtkhandler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def test_polydata():
1414

1515

1616
def test_grid():
17-
data = VTKHandler.read(ugrid_file, format='unstructured')
17+
data = VTKHandler.read(ugrid_file, fmt='unstructured')
1818
np.testing.assert_array_almost_equal(data["points"][-1], [10] * 3)
1919
np.testing.assert_array_almost_equal(data["points"][0], [0] * 3)
2020
np.testing.assert_equal(data["cells"][5], [5, 6, 17, 16, 126, 127, 138, 137])

0 commit comments

Comments
 (0)