SDDSlib
Loading...
Searching...
No Matches
example_save.py
Go to the documentation of this file.
1#! /usr/bin/env oagpython3
2
3"""
4@file example_save.py
5@brief Demonstrates writing an SDDS file using the SDDS Python module
6@details
7This script illustrates how to utilize the SDDS Python module to create an SDDS (Self Describing Data Set) file.
8The file includes parameters, columns, and arrays covering all supported SDDS data types.
9
10The script performs the following operations:
111. Initializes an SDDS object.
122. Defines parameters, columns, and arrays for each supported SDDS data type.
133. Populates the defined parameters, columns, and arrays with sample data across multiple pages.
144. Saves the populated data to an SDDS file.
15
16Supported data types:
17 - SDDS_SHORT
18 - SDDS_USHORT
19 - SDDS_LONG
20 - SDDS_ULONG
21 - SDDS_LONG64
22 - SDDS_ULONG64
23 - SDDS_FLOAT
24 - SDDS_DOUBLE
25 - SDDS_STRING
26 - SDDS_CHARACTER
27"""
28
29from sdds import SDDS
30
31def main():
32 # Initialize the SDDS object with an index of 0
33 sdds_obj = SDDS(0)
34
35 # Specify the name of the output SDDS file
36 output_file = "output_all_data_types.sdds"
37
38 # Set the description of the SDDS file with a brief text and detailed contents
39 sdds_obj.setDescription(
40 "Example output file for demonstration purposes",
41 "Includes parameters, columns, and arrays for every supported datatype."
42 )
43
44 # Define parameters for all supported SDDS data types
45 sdds_obj.defineSimpleParameter("param_short", SDDS.SDDS_SHORT)
46 sdds_obj.defineSimpleParameter("param_ushort", SDDS.SDDS_USHORT)
47 sdds_obj.defineSimpleParameter("param_long", SDDS.SDDS_LONG)
48 sdds_obj.defineSimpleParameter("param_ulong", SDDS.SDDS_ULONG)
49 sdds_obj.defineSimpleParameter("param_long64", SDDS.SDDS_LONG64)
50 sdds_obj.defineSimpleParameter("param_ulong64", SDDS.SDDS_ULONG64)
51 sdds_obj.defineSimpleParameter("param_float", SDDS.SDDS_FLOAT)
52 sdds_obj.defineSimpleParameter("param_double", SDDS.SDDS_DOUBLE)
53 sdds_obj.defineSimpleParameter("param_string", SDDS.SDDS_STRING)
54 sdds_obj.defineSimpleParameter("param_character", SDDS.SDDS_CHARACTER)
55
56 # Define columns for all supported SDDS data types
57 sdds_obj.defineSimpleColumn("col_short", SDDS.SDDS_SHORT)
58 sdds_obj.defineSimpleColumn("col_ushort", SDDS.SDDS_USHORT)
59 sdds_obj.defineSimpleColumn("col_long", SDDS.SDDS_LONG)
60 sdds_obj.defineSimpleColumn("col_ulong", SDDS.SDDS_ULONG)
61 sdds_obj.defineSimpleColumn("col_long64", SDDS.SDDS_LONG64)
62 sdds_obj.defineSimpleColumn("col_ulong64", SDDS.SDDS_ULONG64)
63 sdds_obj.defineSimpleColumn("col_float", SDDS.SDDS_FLOAT)
64 sdds_obj.defineSimpleColumn("col_double", SDDS.SDDS_DOUBLE)
65 sdds_obj.defineSimpleColumn("col_string", SDDS.SDDS_STRING)
66 sdds_obj.defineSimpleColumn("col_character", SDDS.SDDS_CHARACTER)
67
68 # Define arrays for all supported SDDS data types
69 # The third parameter specifies the dimensionality of the array
70 sdds_obj.defineSimpleArray("array_short", SDDS.SDDS_SHORT, 1)
71 sdds_obj.defineSimpleArray("array_ushort", SDDS.SDDS_USHORT, 1)
72 sdds_obj.defineSimpleArray("array_long", SDDS.SDDS_LONG, 2)
73 sdds_obj.defineSimpleArray("array_ulong", SDDS.SDDS_ULONG, 1)
74 sdds_obj.defineSimpleArray("array_long64", SDDS.SDDS_LONG64, 1)
75 sdds_obj.defineSimpleArray("array_ulong64", SDDS.SDDS_ULONG64, 1)
76 sdds_obj.defineSimpleArray("array_float", SDDS.SDDS_FLOAT, 1)
77 sdds_obj.defineSimpleArray("array_double", SDDS.SDDS_DOUBLE, 1)
78 sdds_obj.defineSimpleArray("array_string", SDDS.SDDS_STRING, 1)
79 sdds_obj.defineSimpleArray("array_character", SDDS.SDDS_CHARACTER, 1)
80
81 # -------------------------
82 # Populate Page 1 with Data
83 # -------------------------
84
85 # Set parameter values for Page 1
86 sdds_obj.setParameterValue("param_short", 1, page=1)
87 sdds_obj.setParameterValue("param_ushort", 2, page=1)
88 sdds_obj.setParameterValue("param_long", 3, page=1)
89 sdds_obj.setParameterValue("param_ulong", 4, page=1)
90 sdds_obj.setParameterValue("param_long64", 5, page=1)
91 sdds_obj.setParameterValue("param_ulong64", 6, page=1)
92 sdds_obj.setParameterValue("param_float", 7.7, page=1)
93 sdds_obj.setParameterValue("param_double", 8.8, page=1)
94 sdds_obj.setParameterValue("param_string", "Page 1 String", page=1)
95 sdds_obj.setParameterValue("param_character", "A", page=1)
96
97 # Define column data for Page 1
98 columns_page1 = {
99 "col_short": [1, 2],
100 "col_ushort": [3, 4],
101 "col_long": [5, 6],
102 "col_ulong": [7, 8],
103 "col_long64": [9, 10],
104 "col_ulong64": [11, 12],
105 "col_float": [13.1, 14.2],
106 "col_double": [15.3, 16.4],
107 "col_string": ["String1", "String2"],
108 "col_character": ["X", "Y"],
109 }
110
111 # Populate column data for Page 1
112 for col_name, col_data in columns_page1.items():
113 sdds_obj.setColumnValueList(col_name, col_data, page=1)
114
115 # Define array data for Page 1 (one-dimensional arrays)
116 arrays_page1_1d = {
117 "array_short": [1, 2, 3],
118 "array_ushort": [4, 5, 6],
119 "array_ulong": [10, 11, 12],
120 "array_long64": [13, 14, 15],
121 "array_ulong64": [16, 17, 18],
122 "array_float": [19.1, 20.2, 21.3],
123 "array_double": [22.4, 23.5, 24.6],
124 "array_string": ["Array1", "Array2", "Array3"],
125 "array_character": ["M", "N", "O"],
126 }
127
128 # Populate one-dimensional array data for Page 1
129 for array_name, array_data in arrays_page1_1d.items():
130 sdds_obj.setArrayValueList(array_name, array_data, [3], page=1)
131
132 # Define array data for Page 1 (two-dimensional array)
133 arrays_page1_2d = {
134 "array_long": [7, 8, 9, 17, 18, 19],
135 }
136
137 # Populate two-dimensional array data for Page 1
138 for array_name, array_data in arrays_page1_2d.items():
139 sdds_obj.setArrayValueList(array_name, array_data, [2, 3], page=1)
140
141 # -------------------------
142 # Populate Page 2 with Data
143 # -------------------------
144
145 # Set parameter values for Page 2
146 sdds_obj.setParameterValue("param_short", 10, page=2)
147 sdds_obj.setParameterValue("param_ushort", 20, page=2)
148 sdds_obj.setParameterValue("param_long", 30, page=2)
149 sdds_obj.setParameterValue("param_ulong", 40, page=2)
150 sdds_obj.setParameterValue("param_long64", 50, page=2)
151 sdds_obj.setParameterValue("param_ulong64", 60, page=2)
152 sdds_obj.setParameterValue("param_float", 70.7, page=2)
153 sdds_obj.setParameterValue("param_double", 80.8, page=2)
154 sdds_obj.setParameterValue("param_string", "Page 2 String", page=2)
155 sdds_obj.setParameterValue("param_character", "B", page=2)
156
157 # Define column data for Page 2
158 columns_page2 = {
159 "col_short": [21, 22],
160 "col_ushort": [23, 24],
161 "col_long": [25, 26],
162 "col_ulong": [27, 28],
163 "col_long64": [29, 30],
164 "col_ulong64": [31, 32],
165 "col_float": [33.1, 34.2],
166 "col_double": [35.3, 36.4],
167 "col_string": ["String3", "String4"],
168 "col_character": ["Z", "W"],
169 }
170
171 # Populate column data for Page 2
172 for col_name, col_data in columns_page2.items():
173 sdds_obj.setColumnValueList(col_name, col_data, page=2)
174
175 # Define array data for Page 2 (one-dimensional arrays)
176 arrays_page2_1d = {
177 "array_short": [101, 102, 103],
178 "array_ushort": [104, 105, 106],
179 "array_ulong": [110, 111, 112],
180 "array_long64": [113, 114, 115],
181 "array_ulong64": [116, 117, 118],
182 "array_float": [119.1, 120.2, 121.3],
183 "array_double": [122.4, 123.5, 124.6],
184 "array_string": ["Array4", "Array5", "Array6"],
185 "array_character": ["P", "Q", "R"],
186 }
187
188 # Populate one-dimensional array data for Page 2
189 for array_name, array_data in arrays_page2_1d.items():
190 sdds_obj.setArrayValueList(array_name, array_data, [3], page=2)
191
192 # Define array data for Page 2 (two-dimensional array)
193 arrays_page2_2d = {
194 "array_long": [107, 108, 109, 207, 208, 209],
195 }
196
197 # Populate two-dimensional array data for Page 2
198 for array_name, array_data in arrays_page2_2d.items():
199 sdds_obj.setArrayValueList(array_name, array_data, [2, 3], page=2)
200
201 # -------------------------
202 # Save the SDDS File
203 # -------------------------
204
205 # Save all the defined data into the specified SDDS file
206 sdds_obj.save(output_file)
207
208if __name__ == "__main__":
209 main()