4@file example_save_with_sddsdata.py
5@brief Demonstrates writing an SDDS file using the sddsdata module directly.
7This script illustrates how to utilize the sddsdata module to create an SDDS (Self Describing Data Set) file.
8The file includes parameters, columns, and arrays covering all supported SDDS data types.
10The script performs the following operations:
111. Initializes an SDDS dataset.
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.
33 from sddsdata
import *
38 output_file =
"output_all_data_types.sdds"
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.")
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),
57 if DefineSimpleParameter(fileIndex, name,
"", dtype) != 1:
58 raise ValueError(f
"Failed to define parameter {name}.")
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),
73 if DefineSimpleColumn(fileIndex, name,
"", dtype) != 1:
74 raise ValueError(f
"Failed to define column {name}.")
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),
89 if DefineSimpleArray(fileIndex, name,
"", dtype, dimensions) != 1:
90 raise ValueError(f
"Failed to define array {name}.")
93 if WriteLayout(fileIndex) != 1:
94 raise ValueError(
"Failed to write layout.")
97 if StartPage(fileIndex, 2) != 1:
98 raise ValueError(
"Failed to start page 1.")
107 (
"param_ulong64", 6),
108 (
"param_float", 7.7),
109 (
"param_double", 8.8),
110 (
"param_string",
"Page 1 String"),
111 (
"param_character",
"A"),
113 if SetParameter(fileIndex, name, data) != 1:
114 raise ValueError(f
"Failed to set parameter {name}.")
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"]),
129 if SetColumn(fileIndex, name, data) != 1:
130 raise ValueError(f
"Failed to set column {name}.")
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]),
145 if SetArray(fileIndex, name, data, dims) != 1:
146 raise ValueError(f
"Failed to set array {name}.")
149 if WritePage(fileIndex) != 1:
150 raise ValueError(
"Failed to write page 1.")
153 if StartPage(fileIndex, 2) != 1:
154 raise ValueError(
"Failed to start page 2.")
159 (
"param_ushort", 20),
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"),
169 if SetParameter(fileIndex, name, data) != 1:
170 raise ValueError(f
"Failed to set parameter {name}.")
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"]),
185 if SetColumn(fileIndex, name, data) != 1:
186 raise ValueError(f
"Failed to set column {name}.")
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]),
201 if SetArray(fileIndex, name, data, dims) != 1:
202 raise ValueError(f
"Failed to set array {name}.")
205 if WritePage(fileIndex) != 1:
206 raise ValueError(
"Failed to write page 1.")
209 if Terminate(fileIndex) != 1:
210 raise ValueError(
"Failed to terminate SDDS dataset.")
212if __name__ ==
"__main__":