SDDSlib
Loading...
Searching...
No Matches
sdds_read_demo.c
Go to the documentation of this file.
1/**
2 * @file sdds_read_demo.c
3 * @brief A program to read and process SDDS files, displaying parameters, columns, and arrays.
4 *
5 * This program reads an SDDS (Self Describing Data Set) file specified as a command-line argument,
6 * and processes its pages, parameters, columns, and arrays, printing their values to the console.
7 *
8 * It demonstrates how to use the SDDS library to access different types of data within an SDDS file.
9 */
10
11#include "SDDS.h"
12#include <stdio.h>
13#include <stdlib.h>
14#include <stdint.h>
15#include <float.h>
16
17/**
18 * @brief Main function that processes the SDDS file.
19 *
20 * @param argc The number of command-line arguments.
21 * @param argv The array of command-line argument strings.
22 * @return Returns 0 on successful execution, non-zero otherwise.
23 *
24 * The main function checks the command-line arguments, initializes the SDDS dataset,
25 * and iterates over the pages in the SDDS file. For each page, it processes the parameters,
26 * columns, and arrays, printing their names and values to the console.
27 */
28int main(int argc, char **argv) {
29 SDDS_DATASET SDDS_dataset; /**< SDDS dataset structure */
30 int32_t page; /**< Current page number */
31 int32_t n_params; /**< Number of parameters */
32 int32_t n_columns; /**< Number of columns */
33 int32_t n_arrays; /**< Number of arrays */
34 int64_t n_rows; /**< Number of rows in the current page */
35 char **parameter_names; /**< Array of parameter names */
36 char **column_names; /**< Array of column names */
37 char **array_names; /**< Array of array names */
38 int32_t type; /**< Data type identifier */
39 void *data; /**< Generic data pointer */
40
41 /** Check command-line arguments */
42 if (argc != 2) {
43 fprintf(stderr, "Usage: %s filename.sdds\n", argv[0]);
44 exit(1);
45 }
46
47 /** Initialize SDDS input */
48 if (!SDDS_InitializeInput(&SDDS_dataset, argv[1])) {
49 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
50 exit(1);
51 }
52
53 /** Get parameter, column, and array names */
54 parameter_names = SDDS_GetParameterNames(&SDDS_dataset, &n_params);
55 column_names = SDDS_GetColumnNames(&SDDS_dataset, &n_columns);
56 array_names = SDDS_GetArrayNames(&SDDS_dataset, &n_arrays);
57
58 page = 0;
59 /** Iterate over pages in the SDDS file */
60 while ((page = SDDS_ReadPage(&SDDS_dataset)) > 0) {
61 printf("Page %d\n", page);
62
63 n_rows = SDDS_CountRowsOfInterest(&SDDS_dataset);
64
65 /** Process parameters */
66 printf("Parameters:\n");
67 for (int32_t i = 0; i < n_params; i++) {
68 type = SDDS_GetParameterType(&SDDS_dataset, i);
69 printf(" %s = ", parameter_names[i]);
70 switch (type) {
71 case SDDS_SHORT: {
72 short value;
73 /** Get parameter value */
74 if (SDDS_GetParameter(&SDDS_dataset, parameter_names[i], &value) == NULL) {
75 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
76 continue;
77 }
78 printf("%hd\n", value);
79 break;
80 }
81 case SDDS_USHORT: {
82 unsigned short value;
83 if (SDDS_GetParameter(&SDDS_dataset, parameter_names[i], &value) == NULL) {
84 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
85 continue;
86 }
87 printf("%hu\n", value);
88 break;
89 }
90 case SDDS_LONG: {
91 int32_t value;
92 if (SDDS_GetParameter(&SDDS_dataset, parameter_names[i], &value) == NULL) {
93 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
94 continue;
95 }
96 printf("%" PRId32 "\n", value);
97 break;
98 }
99 case SDDS_ULONG: {
100 uint32_t value;
101 if (SDDS_GetParameter(&SDDS_dataset, parameter_names[i], &value) == NULL) {
102 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
103 continue;
104 }
105 printf("%" PRIu32 "\n", value);
106 break;
107 }
108 case SDDS_LONG64: {
109 int64_t value;
110 if (SDDS_GetParameter(&SDDS_dataset, parameter_names[i], &value) == NULL) {
111 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
112 continue;
113 }
114 printf("%" PRId64 "\n", value);
115 break;
116 }
117 case SDDS_ULONG64: {
118 uint64_t value;
119 if (SDDS_GetParameter(&SDDS_dataset, parameter_names[i], &value) == NULL) {
120 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
121 continue;
122 }
123 printf("%" PRIu64 "\n", value);
124 break;
125 }
126 case SDDS_FLOAT: {
127 float value;
128 if (SDDS_GetParameter(&SDDS_dataset, parameter_names[i], &value) == NULL) {
129 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
130 continue;
131 }
132 printf("%15.6e\n", value);
133 break;
134 }
135 case SDDS_DOUBLE: {
136 double value;
137 if (SDDS_GetParameter(&SDDS_dataset, parameter_names[i], &value) == NULL) {
138 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
139 continue;
140 }
141 printf("%21.14e\n", value);
142 break;
143 }
144 case SDDS_LONGDOUBLE: {
145 long double value;
146 if (SDDS_GetParameter(&SDDS_dataset, parameter_names[i], &value) == NULL) {
147 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
148 continue;
149 }
150 if (LDBL_DIG == 18) {
151 printf("%21.18Le\n", value);
152 } else {
153 printf("%21.14Le\n", value);
154 }
155 break;
156 }
157 case SDDS_STRING: {
158 char *value;
159 if (SDDS_GetParameter(&SDDS_dataset, parameter_names[i], &value) == NULL) {
160 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
161 continue;
162 }
163 printf("%s\n", value);
164 free(value); /**< Free the allocated string */
165 break;
166 }
167 case SDDS_CHARACTER: {
168 char value;
169 if (SDDS_GetParameter(&SDDS_dataset, parameter_names[i], &value) == NULL) {
170 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
171 continue;
172 }
173 printf("%c\n", value);
174 break;
175 }
176 default:
177 printf("Unknown type\n");
178 break;
179 }
180 }
181
182 /** Process columns */
183 printf("Columns (%" PRId64 " rows):\n", n_rows);
184 for (int32_t i = 0; i < n_columns; i++) {
185 type = SDDS_GetColumnType(&SDDS_dataset, i);
186 printf(" Column: %s\n", column_names[i]);
187 data = SDDS_GetColumn(&SDDS_dataset, column_names[i]);
188 if (data == NULL) {
189 fprintf(stderr, "Error getting column %s\n", column_names[i]);
190 continue;
191 }
192 switch (type) {
193 case SDDS_SHORT: {
194 short *values = (short *)data;
195 for (int64_t row = 0; row < n_rows; row++) {
196 printf(" %hd\n", values[row]);
197 }
198 break;
199 }
200 case SDDS_USHORT: {
201 unsigned short *values = (unsigned short *)data;
202 for (int64_t row = 0; row < n_rows; row++) {
203 printf(" %hu\n", values[row]);
204 }
205 break;
206 }
207 case SDDS_LONG: {
208 int32_t *values = (int32_t *)data;
209 for (int64_t row = 0; row < n_rows; row++) {
210 printf(" %" PRId32 "\n", values[row]);
211 }
212 break;
213 }
214 case SDDS_ULONG: {
215 uint32_t *values = (uint32_t *)data;
216 for (int64_t row = 0; row < n_rows; row++) {
217 printf(" %" PRIu32 "\n", values[row]);
218 }
219 break;
220 }
221 case SDDS_LONG64: {
222 int64_t *values = (int64_t *)data;
223 for (int64_t row = 0; row < n_rows; row++) {
224 printf(" %" PRId64 "\n", values[row]);
225 }
226 break;
227 }
228 case SDDS_ULONG64: {
229 uint64_t *values = (uint64_t *)data;
230 for (int64_t row = 0; row < n_rows; row++) {
231 printf(" %" PRIu64 "\n", values[row]);
232 }
233 break;
234 }
235 case SDDS_FLOAT: {
236 float *values = (float *)data;
237 for (int64_t row = 0; row < n_rows; row++) {
238 printf(" %15.6e\n", values[row]);
239 }
240 break;
241 }
242 case SDDS_DOUBLE: {
243 double *values = (double *)data;
244 for (int64_t row = 0; row < n_rows; row++) {
245 printf(" %21.14e\n", values[row]);
246 }
247 break;
248 }
249 case SDDS_LONGDOUBLE: {
250 long double *values = (long double *)data;
251 if (LDBL_DIG == 18) {
252 for (int64_t row = 0; row < n_rows; row++) {
253 printf(" %21.18Le\n", values[row]);
254 }
255 } else {
256 for (int64_t row = 0; row < n_rows; row++) {
257 printf(" %21.14Le\n", values[row]);
258 }
259 }
260 break;
261 }
262 case SDDS_STRING: {
263 char **values = (char **)data;
264 for (int64_t row = 0; row < n_rows; row++) {
265 printf(" %s\n", values[row]);
266 free(values[row]); /**< Free each string */
267 }
268 break;
269 }
270 case SDDS_CHARACTER: {
271 char *values = (char *)data;
272 for (int64_t row = 0; row < n_rows; row++) {
273 printf(" %c\n", values[row]);
274 }
275 break;
276 }
277 default:
278 printf("Unknown type\n");
279 break;
280 }
281 /** Free data */
282 SDDS_Free(data);
283 data = NULL;
284 }
285
286 /** Process arrays */
287 printf("Arrays:\n");
288 for (int32_t i = 0; i < n_arrays; i++) {
289 type = SDDS_GetArrayType(&SDDS_dataset, i);
290 data = SDDS_GetArray(&SDDS_dataset, array_names[i], NULL);
291 if (!data) {
292 fprintf(stderr, "Error getting array %s\n", array_names[i]);
293 continue;
294 }
295 printf(" Array: %s (dimensions: ", array_names[i]);
296
297 SDDS_ARRAY *array = (SDDS_ARRAY *)data;
298 for (int32_t d = 0; d < array->definition->dimensions; d++) {
299 printf("%d", array->dimension[d]);
300 if (d < array->definition->dimensions - 1)
301 printf(" x ");
302 }
303 printf(")\n");
304
305 switch (type) {
306 case SDDS_SHORT: {
307 short *values = (short *)array->data;
308 for (int32_t idx = 0; idx < array->elements; idx++) {
309 printf(" %hd\n", values[idx]);
310 }
311 break;
312 }
313 case SDDS_USHORT: {
314 unsigned short *values = (unsigned short *)array->data;
315 for (int32_t idx = 0; idx < array->elements; idx++) {
316 printf(" %hu\n", values[idx]);
317 }
318 break;
319 }
320 case SDDS_LONG: {
321 int32_t *values = (int32_t *)array->data;
322 for (int32_t idx = 0; idx < array->elements; idx++) {
323 printf(" %" PRId32 "\n", values[idx]);
324 }
325 break;
326 }
327 case SDDS_ULONG: {
328 uint32_t *values = (uint32_t *)array->data;
329 for (int32_t idx = 0; idx < array->elements; idx++) {
330 printf(" %" PRIu32 "\n", values[idx]);
331 }
332 break;
333 }
334 case SDDS_LONG64: {
335 int64_t *values = (int64_t *)array->data;
336 for (int32_t idx = 0; idx < array->elements; idx++) {
337 printf(" %" PRId64 "\n", values[idx]);
338 }
339 break;
340 }
341 case SDDS_ULONG64: {
342 uint64_t *values = (uint64_t *)array->data;
343 for (int32_t idx = 0; idx < array->elements; idx++) {
344 printf(" %" PRIu64 "\n", values[idx]);
345 }
346 break;
347 }
348 case SDDS_FLOAT: {
349 float *values = (float *)array->data;
350 for (int32_t idx = 0; idx < array->elements; idx++) {
351 printf(" %15.6e\n", values[idx]);
352 }
353 break;
354 }
355 case SDDS_DOUBLE: {
356 double *values = (double *)array->data;
357 for (int32_t idx = 0; idx < array->elements; idx++) {
358 printf(" %21.14e\n", values[idx]);
359 }
360 break;
361 }
362 case SDDS_LONGDOUBLE: {
363 long double *values = (long double *)array->data;
364 if (LDBL_DIG == 18) {
365 for (int32_t idx = 0; idx < array->elements; idx++) {
366 printf(" %21.18Le\n", values[idx]);
367 }
368 } else {
369 for (int32_t idx = 0; idx < array->elements; idx++) {
370 printf(" %21.14Le\n", values[idx]);
371 }
372 }
373 break;
374 }
375 case SDDS_STRING: {
376 char **values = (char **)array->data;
377 for (int32_t idx = 0; idx < array->elements; idx++) {
378 printf(" %s\n", values[idx]);
379 }
380 break;
381 }
382 case SDDS_CHARACTER: {
383 char *values = (char *)array->data;
384 for (int32_t idx = 0; idx < array->elements; idx++) {
385 printf(" %c\n", values[idx]);
386 }
387 break;
388 }
389 default:
390 printf("Unknown type\n");
391 break;
392 }
393 /** Free array data */
394 SDDS_FreeArray(data);
395 }
396 }
397
398 /** Free parameter names */
399 SDDS_FreeStringArray(parameter_names, n_params);
400 SDDS_Free(parameter_names);
401
402 /** Free column names */
403 SDDS_FreeStringArray(column_names, n_columns);
404 SDDS_Free(column_names);
405
406 /** Free array names */
407 SDDS_FreeStringArray(array_names, n_arrays);
408 SDDS_Free(array_names);
409
410 /** Terminate SDDS */
411 SDDS_Terminate(&SDDS_dataset);
412 return 0;
413}
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
void * SDDS_GetColumn(SDDS_DATASET *SDDS_dataset, char *column_name)
Retrieves a copy of the data for a specified column, including only rows marked as "of interest".
int64_t SDDS_CountRowsOfInterest(SDDS_DATASET *SDDS_dataset)
Counts the number of rows marked as "of interest" in the current data table.
SDDS_ARRAY * SDDS_GetArray(SDDS_DATASET *SDDS_dataset, char *array_name, SDDS_ARRAY *memory)
Retrieves an array from the current data table of an SDDS dataset.
void * SDDS_GetParameter(SDDS_DATASET *SDDS_dataset, char *parameter_name, void *memory)
Retrieves the value of a specified parameter from the current data table of a data set.
int32_t SDDS_InitializeInput(SDDS_DATASET *SDDS_dataset, char *filename)
Definition SDDS_input.c:49
int32_t SDDS_Terminate(SDDS_DATASET *SDDS_dataset)
int32_t SDDS_ReadPage(SDDS_DATASET *SDDS_dataset)
void SDDS_FreeArray(SDDS_ARRAY *array)
Frees memory allocated for an SDDS array structure.
int32_t SDDS_FreeStringArray(char **string, int64_t strings)
Frees an array of strings by deallocating each individual string.
int32_t SDDS_GetParameterType(SDDS_DATASET *SDDS_dataset, int32_t index)
Retrieves the data type of a parameter in the SDDS dataset by its index.
char ** SDDS_GetParameterNames(SDDS_DATASET *SDDS_dataset, int32_t *number)
Retrieves the names of all parameters in the SDDS dataset.
char ** SDDS_GetColumnNames(SDDS_DATASET *SDDS_dataset, int32_t *number)
Retrieves the names of all columns in the SDDS dataset.
int32_t SDDS_GetArrayType(SDDS_DATASET *SDDS_dataset, int32_t index)
Retrieves the data type of an array in the SDDS dataset by its index.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
Definition SDDS_utils.c:432
int32_t SDDS_GetColumnType(SDDS_DATASET *SDDS_dataset, int32_t index)
Retrieves the data type of a column in the SDDS dataset by its index.
char ** SDDS_GetArrayNames(SDDS_DATASET *SDDS_dataset, int32_t *number)
Retrieves the names of all arrays in the SDDS dataset.
void SDDS_Free(void *mem)
Free memory previously allocated by SDDS_Malloc.
Definition SDDS_utils.c:655
#define SDDS_ULONG
Identifier for the unsigned 32-bit integer data type.
Definition SDDStypes.h:67
#define SDDS_FLOAT
Identifier for the float data type.
Definition SDDStypes.h:43
#define SDDS_STRING
Identifier for the string data type.
Definition SDDStypes.h:85
#define SDDS_ULONG64
Identifier for the unsigned 64-bit integer data type.
Definition SDDStypes.h:55
#define SDDS_LONG
Identifier for the signed 32-bit integer data type.
Definition SDDStypes.h:61
#define SDDS_SHORT
Identifier for the signed short integer data type.
Definition SDDStypes.h:73
#define SDDS_CHARACTER
Identifier for the character data type.
Definition SDDStypes.h:91
#define SDDS_USHORT
Identifier for the unsigned short integer data type.
Definition SDDStypes.h:79
#define SDDS_DOUBLE
Identifier for the double data type.
Definition SDDStypes.h:37
#define SDDS_LONGDOUBLE
Identifier for the long double data type.
Definition SDDStypes.h:31
#define SDDS_LONG64
Identifier for the signed 64-bit integer data type.
Definition SDDStypes.h:49
int main(int argc, char **argv)
Main function that processes the SDDS file.