SDDS ToolKit Programs and Libraries for C and Python
All Classes Files Functions Variables Macros Pages
example_save_with_sddsdata.py File Reference

Detailed Description

Demonstrates writing an SDDS file using the sddsdata module directly.

This script illustrates how to utilize the sddsdata module to create an SDDS (Self Describing Data Set) file. The file includes parameters, columns, and arrays covering all supported SDDS data types.

The script performs the following operations:

  1. Initializes an SDDS dataset.
  2. Defines parameters, columns, and arrays for each supported SDDS data type.
  3. Populates the defined parameters, columns, and arrays with sample data across multiple pages.
  4. Saves the populated data to an SDDS file.

Supported data types:

  • SDDS_SHORT
  • SDDS_USHORT
  • SDDS_LONG
  • SDDS_ULONG
  • SDDS_LONG64
  • SDDS_ULONG64
  • SDDS_FLOAT
  • SDDS_DOUBLE
  • SDDS_STRING
  • SDDS_CHARACTER

Definition in file example_save_with_sddsdata.py.

Go to the source code of this file.

Functions

 example_save_with_sddsdata.main ()
 

Function Documentation

◆ main()

example_save_with_sddsdata.main ( )

Definition at line 36 of file example_save_with_sddsdata.py.

36def main():
37 fileIndex = 0 # Dataset index for this SDDS file
38 output_file = "output_all_data_types.sdds"
39
40 # Initialize SDDS output file
41 if InitializeOutput(fileIndex, sdds.SDDS_ASCII, 1, "Example output file for demonstration purposes", "Includes parameters, columns, and arrays for every supported datatype.", output_file) != 1:
42 raise ValueError("Failed to initialize SDDS output file.")
43
44 # Define parameters
45 for name, dtype in [
46 ("param_short", sdds.SDDS_SHORT),
47 ("param_ushort", sdds.SDDS_USHORT),
48 ("param_long", sdds.SDDS_LONG),
49 ("param_ulong", sdds.SDDS_ULONG),
50 ("param_long64", sdds.SDDS_LONG64),
51 ("param_ulong64", sdds.SDDS_ULONG64),
52 ("param_float", sdds.SDDS_FLOAT),
53 ("param_double", sdds.SDDS_DOUBLE),
54 ("param_string", sdds.SDDS_STRING),
55 ("param_character", sdds.SDDS_CHARACTER),
56 ]:
57 if DefineSimpleParameter(fileIndex, name, "", dtype) != 1:
58 raise ValueError(f"Failed to define parameter {name}.")
59
60 # Define columns
61 for name, dtype in [
62 ("col_short", sdds.SDDS_SHORT),
63 ("col_ushort", sdds.SDDS_USHORT),
64 ("col_long", sdds.SDDS_LONG),
65 ("col_ulong", sdds.SDDS_ULONG),
66 ("col_long64", sdds.SDDS_LONG64),
67 ("col_ulong64", sdds.SDDS_ULONG64),
68 ("col_float", sdds.SDDS_FLOAT),
69 ("col_double", sdds.SDDS_DOUBLE),
70 ("col_string", sdds.SDDS_STRING),
71 ("col_character", sdds.SDDS_CHARACTER),
72 ]:
73 if DefineSimpleColumn(fileIndex, name, "", dtype) != 1:
74 raise ValueError(f"Failed to define column {name}.")
75
76 # Define arrays
77 for name, dtype, dimensions in [
78 ("array_short", sdds.SDDS_SHORT, 1),
79 ("array_ushort", sdds.SDDS_USHORT, 1),
80 ("array_long", sdds.SDDS_LONG, 2),
81 ("array_ulong", sdds.SDDS_ULONG, 1),
82 ("array_long64", sdds.SDDS_LONG64, 1),
83 ("array_ulong64", sdds.SDDS_ULONG64, 1),
84 ("array_float", sdds.SDDS_FLOAT, 1),
85 ("array_double", sdds.SDDS_DOUBLE, 1),
86 ("array_string", sdds.SDDS_STRING, 1),
87 ("array_character", sdds.SDDS_CHARACTER, 1),
88 ]:
89 if DefineSimpleArray(fileIndex, name, "", dtype, dimensions) != 1:
90 raise ValueError(f"Failed to define array {name}.")
91
92 # Write layout
93 if WriteLayout(fileIndex) != 1:
94 raise ValueError("Failed to write layout.")
95
96 # Populate Page 1
97 if StartPage(fileIndex, 2) != 1:
98 raise ValueError("Failed to start page 1.")
99
100 # Set parameter values
101 for name, data in [
102 ("param_short", 1),
103 ("param_ushort", 2),
104 ("param_long", 3),
105 ("param_ulong", 4),
106 ("param_long64", 5),
107 ("param_ulong64", 6),
108 ("param_float", 7.7),
109 ("param_double", 8.8),
110 ("param_string", "Page 1 String"),
111 ("param_character", "A"),
112 ]:
113 if SetParameter(fileIndex, name, data) != 1:
114 raise ValueError(f"Failed to set parameter {name}.")
115
116 # Set column values
117 for name, data, in [
118 ("col_short", [1, 2]),
119 ("col_ushort", [3, 4]),
120 ("col_long", [5, 6]),
121 ("col_ulong", [7, 8]),
122 ("col_long64", [9, 10]),
123 ("col_ulong64", [11, 12]),
124 ("col_float", [13.1, 14.2]),
125 ("col_double", [15.3, 16.4]),
126 ("col_string", ["String1", "String2"]),
127 ("col_character", ["X", "Y"]),
128 ]:
129 if SetColumn(fileIndex, name, data) != 1:
130 raise ValueError(f"Failed to set column {name}.")
131
132 # Set array values
133 for name, data, dims in [
134 ("array_short", [1, 2, 3], [3]),
135 ("array_ushort", [4, 5, 6], [3]),
136 ("array_long", [7, 8, 9, 17, 18, 19], [2, 3]),
137 ("array_ulong", [10, 11, 12], [3]),
138 ("array_long64", [13, 14, 15], [3]),
139 ("array_ulong64", [16, 17, 18], [3]),
140 ("array_float", [19.1, 20.2, 21.3], [3]),
141 ("array_double", [22.4, 23.5, 24.6], [3]),
142 ("array_string", ["Array1", "Array2", "Array3"], [3]),
143 ("array_character", ["M", "N", "O"], [3]),
144 ]:
145 if SetArray(fileIndex, name, data, dims) != 1:
146 raise ValueError(f"Failed to set array {name}.")
147
148 # Write page
149 if WritePage(fileIndex) != 1:
150 raise ValueError("Failed to write page 1.")
151
152 # Populate Page 2
153 if StartPage(fileIndex, 2) != 1:
154 raise ValueError("Failed to start page 2.")
155
156 # Set parameter values
157 for name, data in [
158 ("param_short", 10),
159 ("param_ushort", 20),
160 ("param_long", 30),
161 ("param_ulong", 40),
162 ("param_long64", 50),
163 ("param_ulong64", 60),
164 ("param_float", 70.7),
165 ("param_double", 80.8),
166 ("param_string", "Page 2 String"),
167 ("param_character", "B"),
168 ]:
169 if SetParameter(fileIndex, name, data) != 1:
170 raise ValueError(f"Failed to set parameter {name}.")
171
172 # Set column values
173 for name, data, in [
174 ("col_short", [21, 22]),
175 ("col_ushort", [23, 24]),
176 ("col_long", [25, 26]),
177 ("col_ulong", [27, 28]),
178 ("col_long64", [29, 30]),
179 ("col_ulong64", [31, 32]),
180 ("col_float", [33.1, 34.2]),
181 ("col_double", [35.3, 36.4]),
182 ("col_string", ["String3", "String4"]),
183 ("col_character", ["Z", "W"]),
184 ]:
185 if SetColumn(fileIndex, name, data) != 1:
186 raise ValueError(f"Failed to set column {name}.")
187
188 # Set array values
189 for name, data, dims in [
190 ("array_short", [101, 102, 103], [3]),
191 ("array_ushort", [104, 105, 106], [3]),
192 ("array_long", [107, 108, 109, 207, 208, 209], [2, 3]),
193 ("array_ulong", [110, 111, 112], [3]),
194 ("array_long64", [113, 114, 115], [3]),
195 ("array_ulong64", [116, 117, 118], [3]),
196 ("array_float", [119.1, 120.2, 121.3], [3]),
197 ("array_double", [122.4, 123.5, 124.6], [3]),
198 ("array_string", ["Array4", "Array5", "Array6"], [3]),
199 ("array_character", ["P", "Q", "R"], [3]),
200 ]:
201 if SetArray(fileIndex, name, data, dims) != 1:
202 raise ValueError(f"Failed to set array {name}.")
203
204 # Write page
205 if WritePage(fileIndex) != 1:
206 raise ValueError("Failed to write page 1.")
207
208 # Terminate SDDS dataset
209 if Terminate(fileIndex) != 1:
210 raise ValueError("Failed to terminate SDDS dataset.")
211