Coverage for src / sdynpy / fileio / sdynpy_uff_datasets / sdynpy_uff_dataset_2420.py: 25%

53 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-03-11 16:22 +0000

1# -*- coding: utf-8 -*- 

2""" 

3Coordinate Systems 

4 

5This dataset defines the coordinate systems in a test geometry 

6""" 

7""" 

8Copyright 2022 National Technology & Engineering Solutions of Sandia, 

9LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S. 

10Government retains certain rights in this software. 

11 

12This program is free software: you can redistribute it and/or modify 

13it under the terms of the GNU General Public License as published by 

14the Free Software Foundation, either version 3 of the License, or 

15(at your option) any later version. 

16 

17This program is distributed in the hope that it will be useful, 

18but WITHOUT ANY WARRANTY; without even the implied warranty of 

19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

20GNU General Public License for more details. 

21 

22You should have received a copy of the GNU General Public License 

23along with this program. If not, see <https://www.gnu.org/licenses/>. 

24""" 

25 

26from ..sdynpy_uff import UFFReadError, parse_uff_line, parse_uff_lines, write_uff_line 

27from ...core import sdynpy_colors 

28import numpy as np 

29 

30 

31class Sdynpy_UFF_Dataset_2420: 

32 def __init__(self, part_uid=1, part_name='', 

33 cs_labels=[], cs_types=[], cs_colors=[], 

34 cs_names=[], cs_matrices=np.zeros((0, 4, 3))): 

35 self.part_uid = part_uid 

36 self.part_name = part_name 

37 self.cs_labels = cs_labels 

38 self.cs_types = cs_types 

39 self.cs_colors = cs_colors 

40 self.cs_names = cs_names 

41 self.cs_matrices = cs_matrices 

42 

43 @property 

44 def dataset_number(self): 

45 return 2420 

46 

47 @classmethod 

48 def from_uff_data_array(cls, data): 

49 # Transform from binary to ascii 

50 data = [line.decode() for line in data] 

51 # Record 1: FORMAT (1I10) 

52 # Field 1 -- Part UID 

53 part_uid, = parse_uff_line(data[0], ['I10']) 

54# Record 2: FORMAT (40A2) 

55# Field 1 -- Part Name 

56 part_name, = parse_uff_line(data[1], ['A80']) 

57 cs_labels = [] 

58 cs_types = [] 

59 cs_colors = [] 

60 cs_matrices = [] 

61 cs_names = [] 

62 index = 2 

63 while index < len(data): 

64 # Record 3: FORMAT (3I10) 

65 # Field 1 -- Coordinate System Label 

66 # Field 2 -- Coordinate System Type 

67 # = 0, Cartesian 

68 # = 1, Cylindrical 

69 # = 2, Spherical 

70 # Field 3 -- Coordinate System Color 

71 cs_label, cs_type, cs_color = parse_uff_line(data[index], 3 * ['I10']) 

72# Record 4: FORMAT (40A2) 

73# Field 1 -- Coordinate System Name 

74 cs_name, = parse_uff_line(data[index + 1], ['A80']) 

75# Record 5: FORMAT (1P3D25.16) 

76# Field 1-3 -- Transformation Matrix Row 1 

77# 

78# Record 6: FORMAT (1P3D25.16) 

79# Field 1-3 -- Transformation Matrix Row 2 

80# 

81# Record 7: FORMAT (1P3D25.16) 

82# Field 1-3 -- Transformation Matrix Row 3 

83# 

84# Record 8: FORMAT (1P3D25.16) 

85# Field 1-3 -- Transformation Matrix Row 4 

86 cs_matrix, read_lines = parse_uff_lines(data[index + 2:], 3 * ['D25.16'], 12) 

87 cs_matrix = np.array(cs_matrix).reshape(4, 3) 

88 index += 2 + read_lines 

89 cs_labels.append(cs_label) 

90 cs_types.append(cs_type) 

91 cs_colors.append(cs_color) 

92 cs_matrices.append(cs_matrix) 

93 cs_names.append(cs_name) 

94 # Make sure matrices are the right size 

95 matrix_array = np.array(cs_matrices) 

96 ds_2420 = cls(part_uid, part_name, cs_labels, cs_types, cs_colors, cs_names, 

97 matrix_array) 

98 return ds_2420 

99 

100 def __repr__(self): 

101 return 'Sdynpy_UFF_Dataset_2420<{} coordinate systems(s)>'.format(len(self.cs_labels)) 

102 

103 def write_string(self): 

104 return_string = write_uff_line([self.part_uid, self.part_name], ['I10', '\n', 'A80']) 

105 

106 for cs_label, cs_type, cs_color, cs_name, cs_matrix in zip(self.cs_labels, self.cs_types, 

107 self.cs_colors, self.cs_names, 

108 self.cs_matrices): 

109 return_string += (write_uff_line([cs_label, cs_type, cs_color], 3 * ['I10']) + 

110 write_uff_line([cs_name], ['A80']) + 

111 write_uff_line(cs_matrix.flatten(), 3 * ['D25.16'])) 

112 return return_string 

113 

114 def __str__(self): 

115 lines = self.write_string().split('\n') 

116 if len(lines) > 8: 

117 return 'Dataset 2420: Coordinate Systems\n ' + '\n '.join(lines[0:5] + ['.', '.', '.']) 

118 else: 

119 return 'Dataset 2420: Coordinate Systems\n ' + '\n '.join(lines) 

120 

121 

122def read(data): 

123 return Sdynpy_UFF_Dataset_2420.from_uff_data_array(data)