Skip to content

Commit 8aabdae

Browse files
committed
add try catch
1 parent 272ee99 commit 8aabdae

File tree

1 file changed

+62
-59
lines changed

1 file changed

+62
-59
lines changed

exemplary_workflow/source/postprocessing.py

Lines changed: 62 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -30,68 +30,71 @@ def main(args):
3030
point_data = mesh.GetPointData()
3131
cell_data = mesh.GetCellData()
3232

33-
point_arrays = [point_data.GetArrayName(i) for i in range(point_data.GetNumberOfArrays())]
34-
cell_arrays = [cell_data.GetArrayName(i) for i in range(cell_data.GetNumberOfArrays())]
35-
36-
if args.field in point_arrays:
37-
array_type = "point"
38-
elif args.field in cell_arrays:
39-
array_type = "cell"
40-
else:
41-
print(f"Error: Field '{args.field}' not found in point or cell arrays.")
42-
print(f"Available point arrays: {point_arrays}")
43-
print(f"Available cell arrays: {cell_arrays}")
44-
sys.exit(1)
45-
46-
47-
# Create points along a line using vtkLineSource (2D: z=0)
48-
resolution = 101
49-
line_source = vtk.vtkLineSource()
50-
line_source.SetPoint1(0.0, 0.0, 0.0)
51-
line_source.SetPoint2(1.0, 1.0, 0.0)
52-
line_source.SetResolution(resolution - 1)
53-
line_source.Update()
54-
probe_polydata = line_source.GetOutput()
55-
56-
# Create VTK probe filter
57-
probe_filter = vtk.vtkProbeFilter()
58-
probe_filter.SetSourceData(mesh)
59-
probe_filter.SetInputData(probe_polydata)
60-
probe_filter.Update()
61-
62-
# Get results
63-
result = probe_filter.GetOutput()
64-
sampled_points = vtk_to_numpy(result.GetPoints().GetData())
65-
66-
# Get field data
67-
if args.field in point_arrays:
68-
field_array = result.GetPointData().GetArray(args.field)
69-
else:
70-
field_array = result.GetCellData().GetArray(args.field)
71-
72-
if field_array is None:
73-
print(f"Error: Field '{args.field}' not found in sampled data.")
74-
sys.exit(1)
75-
76-
field_data = vtk_to_numpy(field_array)
33+
try:
34+
point_arrays = [point_data.GetArrayName(i) for i in range(point_data.GetNumberOfArrays())]
35+
cell_arrays = [cell_data.GetArrayName(i) for i in range(cell_data.GetNumberOfArrays())]
36+
37+
if args.field in point_arrays:
38+
array_type = "point"
39+
elif args.field in cell_arrays:
40+
array_type = "cell"
41+
else:
42+
print(f"Error: Field '{args.field}' not found in point or cell arrays.")
43+
print(f"Available point arrays: {point_arrays}")
44+
print(f"Available cell arrays: {cell_arrays}")
45+
sys.exit(1)
7746

78-
# Prepare data for CSV using numpy instead of pandas
79-
arc_length = np.linalg.norm(sampled_points - sampled_points[0], axis=1)
80-
81-
# Check for valid mask in VTK results
82-
valid_mask_array = result.GetPointData().GetArray("vtkValidPointMask")
83-
if valid_mask_array is not None:
84-
valid_mask = vtk_to_numpy(valid_mask_array)
85-
if not np.all(valid_mask == 1):
86-
print("Error: Not all probe points are valid (some are outside the mesh).")
47+
# Create points along a line using vtkLineSource (2D: z=0)
48+
resolution = 101
49+
line_source = vtk.vtkLineSource()
50+
line_source.SetPoint1(0.0, 0.0, 0.0)
51+
line_source.SetPoint2(1.0, 1.0, 0.0)
52+
line_source.SetResolution(resolution - 1)
53+
line_source.Update()
54+
probe_polydata = line_source.GetOutput()
55+
56+
# Create VTK probe filter
57+
probe_filter = vtk.vtkProbeFilter()
58+
probe_filter.SetSourceData(mesh)
59+
probe_filter.SetInputData(probe_polydata)
60+
probe_filter.Update()
61+
62+
# Get results
63+
result = probe_filter.GetOutput()
64+
sampled_points = vtk_to_numpy(result.GetPoints().GetData())
65+
66+
# Get field data
67+
if args.field in point_arrays:
68+
field_array = result.GetPointData().GetArray(args.field)
69+
else:
70+
field_array = result.GetCellData().GetArray(args.field)
71+
72+
if field_array is None:
73+
print(f"Error: Field '{args.field}' not found in sampled data.")
8774
sys.exit(1)
75+
76+
field_data = vtk_to_numpy(field_array)
8877

89-
# Write CSV manually using numpy
90-
header = ["arc_length", args.field]
91-
data_array = np.column_stack([arc_length, field_data])
92-
93-
np.savetxt(args.csv, data_array, delimiter=',', header=','.join(header), comments='')
94-
print(f"Data successfully written to {args.csv}")
78+
# Prepare data for CSV using numpy instead of pandas
79+
arc_length = np.linalg.norm(sampled_points - sampled_points[0], axis=1)
80+
81+
# Check for valid mask in VTK results
82+
valid_mask_array = result.GetPointData().GetArray("vtkValidPointMask")
83+
if valid_mask_array is not None:
84+
valid_mask = vtk_to_numpy(valid_mask_array)
85+
if not np.all(valid_mask == 1):
86+
print("Error: Not all probe points are valid (some are outside the mesh).")
87+
sys.exit(1)
88+
89+
# Write CSV manually using numpy
90+
header = ["arc_length", args.field]
91+
data_array = np.column_stack([arc_length, field_data])
92+
93+
np.savetxt(args.csv, data_array, delimiter=',', header=','.join(header), comments='')
94+
print(f"Data successfully written to {args.csv}")
95+
except Exception as e:
96+
print(f"Postprocessing error: {e}")
97+
sys.exit(1)
9598

9699

97100
if __name__ == "__main__":

0 commit comments

Comments
 (0)