SDDSlib
Loading...
Searching...
No Matches
sddsarray2column.c File Reference

Converts SDDS arrays to SDDS columns. More...

#include "mdb.h"
#include "SDDS.h"
#include "scan.h"

Go to the source code of this file.

Classes

struct  CONVERTED_ARRAY
 

Macros

#define DIM_0   0
 
#define DIM_1   1
 
#define DIM_2   2
 
#define DIM_OPTIONS   3
 

Enumerations

enum  option_type { SET_NOWARNINGS , SET_CONVERT , SET_PIPE , N_OPTIONS }
 

Functions

int main (int argc, char **argv)
 

Variables

static char * option [N_OPTIONS]
 
static char * dim_option [DIM_OPTIONS]
 
static char * USAGE
 

Detailed Description

Converts SDDS arrays to SDDS columns.

This program reads an SDDS file, converts specified arrays into columns, and writes the result to a target SDDS file. The number of elements in the converted arrays must match the number of rows in existing columns or the number of elements in other converted arrays.

Usage

sddsarray2column [<source-file>] [<target-file>]
[-pipe=[input][,output]]
[-nowarnings]
[-convert=<array-name>[,<column-name>][,d<dimension>=<indexValue>]...]

Options

  • -pipe=[input][,output] Use standard input and/or output for file operations.
  • -nowarnings Suppress warning messages.
  • -convert=<array-name>[,<column-name>][,d<dimension>=<indexValue>]... Convert the specified SDDS array to a column.
    • <array-name>: Name of the array to convert.
    • <column-name> (optional): Name of the new column. If not specified, the array name is used.
    • d<dimension>=<indexValue> (optional): Specify indices for multi-dimensional arrays.
License
This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
Author
R. Soliday, M. Borland

Definition in file sddsarray2column.c.

Macro Definition Documentation

◆ DIM_0

#define DIM_0   0

Definition at line 62 of file sddsarray2column.c.

◆ DIM_1

#define DIM_1   1

Definition at line 63 of file sddsarray2column.c.

◆ DIM_2

#define DIM_2   2

Definition at line 64 of file sddsarray2column.c.

◆ DIM_OPTIONS

#define DIM_OPTIONS   3

Definition at line 65 of file sddsarray2column.c.

Enumeration Type Documentation

◆ option_type

enum option_type

Definition at line 49 of file sddsarray2column.c.

49 {
50 SET_NOWARNINGS,
51 SET_CONVERT,
52 SET_PIPE,
53 N_OPTIONS
54};

Function Documentation

◆ main()

int main ( int argc,
char ** argv )

Definition at line 98 of file sddsarray2column.c.

98 {
99 CONVERTED_ARRAY *ca = NULL;
100 SDDS_DATASET SDDS_dataset, SDDS_orig;
101 ARRAY_DEFINITION *ardef = NULL;
102 long i, i_arg, j, found, k, m, n;
103 SCANNED_ARG *s_arg;
104 char *input = NULL, *output = NULL, *ptr = NULL, *buffer = NULL;
105 char *description_text = NULL, *description_contents = NULL;
106 unsigned long pipeFlags = 0;
107 long noWarnings = 0, tmpfile_used = 1;
108 long virtual_rows, max_size, pageNumber, vrows;
109 int64_t rows;
110
111 char **orig_column_name = NULL, **orig_parameter_name = NULL, **orig_array_name = NULL;
112 int32_t orig_column_names = 0, orig_parameter_names = 0, orig_array_names = 0;
113 char **new_array_name = NULL;
114 long new_array_names = 0;
115
116 long converted_array_names = 0;
117
119 argc = scanargs(&s_arg, argc, argv);
120 if (argc < 3) {
121 bomb(NULL, USAGE);
122 }
123
124 for (i_arg = 1; i_arg < argc; i_arg++) {
125 if (s_arg[i_arg].arg_type == OPTION) {
126 delete_chars(s_arg[i_arg].list[0], "_");
127 switch (match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
128 case SET_CONVERT:
129 if (s_arg[i_arg].n_items < 2) {
130 SDDS_Bomb("Invalid -convert syntax");
131 }
132 ca = trealloc(ca, sizeof(*ca) * (converted_array_names + 1));
133
134 ca[converted_array_names].name = s_arg[i_arg].list[1];
135
136 if ((s_arg[i_arg].n_items > 2) && (strchr(s_arg[i_arg].list[2], '=') == NULL)) {
137 ca[converted_array_names].new_name = s_arg[i_arg].list[2];
138 i = 3;
139 } else {
140 ca[converted_array_names].new_name = s_arg[i_arg].list[1];
141 i = 2;
142 }
143
144 ca[converted_array_names].d[0] = NULL;
145 ca[converted_array_names].d[1] = NULL;
146 ca[converted_array_names].d[2] = NULL;
147 while (i < s_arg[i_arg].n_items) {
148 if (!(ptr = strchr(s_arg[i_arg].list[i], '='))) {
149 SDDS_Bomb("Invalid -convert syntax");
150 }
151 *ptr++ = 0;
152 switch (match_string(s_arg[i_arg].list[i], dim_option, DIM_OPTIONS, 0)) {
153 case DIM_0:
154 ca[converted_array_names].d[0] = ptr;
155 break;
156 case DIM_1:
157 ca[converted_array_names].d[1] = ptr;
158 break;
159 case DIM_2:
160 ca[converted_array_names].d[2] = ptr;
161 break;
162 default:
163 SDDS_Bomb("Invalid -convert syntax");
164 }
165 i++;
166 }
167 converted_array_names++;
168 break;
169
170 case SET_NOWARNINGS:
171 if (s_arg[i_arg].n_items != 1) {
172 SDDS_Bomb("Invalid -nowarnings syntax");
173 }
174 noWarnings = 1;
175 break;
176
177 case SET_PIPE:
178 if (!processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipeFlags)) {
179 SDDS_Bomb("Invalid -pipe syntax");
180 }
181 break;
182 }
183 } else {
184 if (input == NULL) {
185 input = s_arg[i_arg].list[0];
186 } else if (output == NULL) {
187 output = s_arg[i_arg].list[0];
188 } else {
189 SDDS_Bomb("Too many filenames");
190 }
191 }
192 }
193
194 processFilenames("sddsarray2column", &input, &output, pipeFlags, noWarnings, &tmpfile_used);
195
196 if (!SDDS_InitializeInput(&SDDS_orig, input)) {
197 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
198 exit(EXIT_FAILURE);
199 }
200
201 if (!description_text) {
202 SDDS_GetDescription(&SDDS_orig, &description_text, &description_contents);
203 }
204
205 if (!SDDS_InitializeOutput(&SDDS_dataset, SDDS_orig.layout.data_mode.mode, 1, description_text, description_contents, output)) {
206 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
207 exit(EXIT_FAILURE);
208 }
209
210 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
211
212 /* Read in names of parameters, columns, and arrays */
213 orig_parameter_name = SDDS_GetParameterNames(&SDDS_orig, &orig_parameter_names);
214 if (!orig_parameter_name) {
215 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
216 exit(EXIT_FAILURE);
217 }
218
219 orig_column_name = SDDS_GetColumnNames(&SDDS_orig, &orig_column_names);
220 if (!orig_column_name) {
221 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
222 exit(EXIT_FAILURE);
223 }
224
225 orig_array_name = SDDS_GetArrayNames(&SDDS_orig, &orig_array_names);
226 if (!orig_array_name) {
227 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
228 exit(EXIT_FAILURE);
229 }
230
231 /* Check for problems with the names of the arrays that are to be converted */
232 for (j = 0; j < converted_array_names; j++) {
233 for (i = 0; i < orig_column_names; i++) {
234 if (strcmp(orig_column_name[i], ca[j].new_name) == 0) {
235 fprintf(stderr, "Error: Column '%s' already exists.\n", orig_column_name[i]);
236 exit(EXIT_FAILURE);
237 }
238 }
239 for (i = 0; i < converted_array_names; i++) {
240 if ((i != j) && (strcmp(ca[i].new_name, ca[j].new_name) == 0)) {
241 fprintf(stderr, "Error: Cannot convert two arrays to the same column name '%s'.\n", ca[j].new_name);
242 exit(EXIT_FAILURE);
243 }
244 }
245 found = 0;
246 for (i = 0; i < orig_array_names; i++) {
247 if (strcmp(orig_array_name[i], ca[j].name) == 0) {
248 found = 1;
249 break;
250 }
251 }
252 if (!found) {
253 fprintf(stderr, "Error: Array '%s' does not exist.\n", ca[j].name);
254 exit(EXIT_FAILURE);
255 }
256 }
257
258 /* Find array names that we will not be converting */
259 for (i = 0; i < orig_array_names; i++) {
260 found = 0;
261 for (j = 0; j < converted_array_names; j++) {
262 if (strcmp(orig_array_name[i], ca[j].name) == 0) {
263 found = 1;
264 break;
265 }
266 }
267 if (!found) {
268 new_array_name = trealloc(new_array_name, sizeof(*new_array_name) * (new_array_names + 1));
269 new_array_name[new_array_names] = orig_array_name[i];
270 new_array_names++;
271 }
272 }
273
274 /* Write the header of the SDDS file */
275 for (i = 0; i < orig_parameter_names; i++) {
276 if (!SDDS_TransferParameterDefinition(&SDDS_dataset, &SDDS_orig, orig_parameter_name[i], orig_parameter_name[i])) {
277 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
278 exit(EXIT_FAILURE);
279 }
280 }
281
282 for (i = 0; i < orig_column_names; i++) {
283 if (!SDDS_TransferColumnDefinition(&SDDS_dataset, &SDDS_orig, orig_column_name[i], orig_column_name[i])) {
284 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
285 exit(EXIT_FAILURE);
286 }
287 }
288
289 for (i = 0; i < new_array_names; i++) {
290 if (!SDDS_TransferArrayDefinition(&SDDS_dataset, &SDDS_orig, new_array_name[i], new_array_name[i])) {
291 fprintf(stderr, "Unable to transfer array '%s' to '%s'.\n", new_array_name[i], new_array_name[i]);
292 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
293 exit(EXIT_FAILURE);
294 }
295 }
296
297 for (i = 0; i < converted_array_names; i++) {
298 ardef = SDDS_GetArrayDefinition(&SDDS_orig, ca[i].name);
299 if (!ardef) {
300 fprintf(stderr, "Error: Unknown array named '%s'.\n", ca[i].name);
301 exit(EXIT_FAILURE);
302 }
303 ca[i].type = ardef->type;
304 if (SDDS_DefineColumn(&SDDS_dataset, ca[i].new_name, ardef->symbol, ardef->units, ardef->description, ardef->format_string, ardef->type, ardef->field_length) < 0) {
306 fprintf(stderr, "Error: Unable to define new column '%s'.\n", ca[i].new_name);
307 exit(EXIT_FAILURE);
308 }
310 }
311
312 if (!SDDS_WriteLayout(&SDDS_dataset)) {
313 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
314 }
315
316 /* Read the data and write the new file */
317 max_size = 0;
318 for (i = 0; i < SDDS_NUM_TYPES; i++) {
319 if (max_size < SDDS_type_size[i]) {
320 max_size = SDDS_type_size[i];
321 }
322 }
323 buffer = tmalloc(max_size * sizeof(char));
324
325 while ((pageNumber = SDDS_ReadPage(&SDDS_orig)) >= 0) {
326 if (pageNumber == 0) {
327 fprintf(stderr, "Error: SDDS data garbled.\n");
328 fprintf(stderr, "Warning: One or more data pages may be missing.\n");
329 break;
330 }
331 rows = SDDS_RowCount(&SDDS_orig);
332 if (rows < 0) {
333 fprintf(stderr, "Error: Problem counting rows in input page.\n");
334 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
335 }
336 if (!SDDS_StartPage(&SDDS_dataset, rows)) {
337 fprintf(stderr, "Error: Problem starting output page.\n");
338 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
339 }
340
341 for (i = 0; i < orig_parameter_names; i++) {
342 if (!SDDS_GetParameter(&SDDS_orig, orig_parameter_name[i], buffer)) {
343 fprintf(stderr, "Error: Problem getting parameter '%s'.\n", orig_parameter_name[i]);
344 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
345 }
346 if (!SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_NAME | SDDS_PASS_BY_REFERENCE, orig_parameter_name[i], buffer, NULL)) {
347 fprintf(stderr, "Error: Problem setting parameter '%s'.\n", orig_parameter_name[i]);
348 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
349 }
350 }
351
352 if (rows) {
353 for (i = 0; i < orig_column_names; i++) {
354 ptr = SDDS_GetInternalColumn(&SDDS_orig, orig_column_name[i]);
355 if (!ptr) {
356 fprintf(stderr, "Error: Problem getting column '%s'.\n", orig_column_name[i]);
357 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
358 }
359 if (!SDDS_SetColumn(&SDDS_dataset, SDDS_SET_BY_NAME, ptr, rows, orig_column_name[i])) {
360 fprintf(stderr, "Error: Problem setting column '%s'.\n", orig_column_name[i]);
361 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
362 }
363 }
364 }
365
366 for (i = 0; i < new_array_names; i++) {
367 SDDS_ARRAY *array;
368 array = SDDS_GetArray(&SDDS_orig, new_array_name[i], NULL);
369 if (!array) {
370 fprintf(stderr, "Error: Problem getting array '%s'.\n", new_array_name[i]);
371 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
372 }
373 if (!SDDS_SetArray(&SDDS_dataset, new_array_name[i], SDDS_CONTIGUOUS_DATA, array->data, array->dimension)) {
374 fprintf(stderr, "Error: Problem setting array '%s'.\n", new_array_name[i]);
375 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
376 }
377 SDDS_FreeArray(array);
378 }
379
380 virtual_rows = -1;
381 for (i = 0; i < converted_array_names; i++) {
382 SDDS_ARRAY *array;
383 array = SDDS_GetArray(&SDDS_orig, ca[i].name, NULL);
384 if (!array) {
385 fprintf(stderr, "Error: Problem getting array '%s'.\n", ca[i].name);
386 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
387 }
388
389 if ((ca[i].d[0] == NULL) && (ca[i].d[1] == NULL) && (ca[i].d[2] == NULL)) {
390 if ((orig_column_names) && (array->elements != rows)) {
391 fprintf(stderr, "Error: Cannot convert '%s' because existing columns have a different number of rows.\n", ca[i].name);
392 exit(EXIT_FAILURE);
393 }
394 if ((virtual_rows >= 0) && (array->elements != virtual_rows)) {
395 fprintf(stderr, "Error: The number of array elements are not the same.\n");
396 exit(EXIT_FAILURE);
397 }
398 if ((!orig_column_names) && (virtual_rows == -1)) {
399 if (!SDDS_LengthenTable(&SDDS_dataset, array->elements)) {
400 SDDS_SetError("Unable to lengthen table");
401 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
402 }
403 }
404 virtual_rows = array->elements;
405 if (!SDDS_SetColumn(&SDDS_dataset, SDDS_SET_BY_NAME, array->data, virtual_rows, ca[i].new_name)) {
406 fprintf(stderr, "Error: Problem setting column '%s'.\n", ca[i].new_name);
407 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
408 }
409 } else {
410 vrows = 0;
411 ca[i].dims[0] = ca[i].dims[1] = ca[i].dims[2] = 0;
412 for (j = 0; j < array->definition->dimensions; j++) {
413 ca[i].dim[j] = NULL;
414 if (ca[i].d[j] == NULL) {
415 ca[i].dims[j] = array->dimension[j];
416 ca[i].dim[j] = malloc(sizeof(long) * ca[i].dims[j]);
417 for (k = 0; k < ca[i].dims[j]; k++) {
418 ca[i].dim[j][k] = k;
419 }
420 } else {
421 ptr = strtok(ca[i].d[j], ",");
422 while (ptr != NULL) {
423 ca[i].dim[j] = realloc(ca[i].dim[j], sizeof(long) * (ca[i].dims[j] + 1));
424 if (!ca[i].dim[j]) {
425 fprintf(stderr, "Error: Memory allocation failed for dimension indices.\n");
426 exit(EXIT_FAILURE);
427 }
428
429 if ((sscanf(ptr, "%ld", &ca[i].dim[j][ca[i].dims[j]]) != 1) ||
430 (ca[i].dim[j][ca[i].dims[j]] < 0) ||
431 (ca[i].dim[j][ca[i].dims[j]] >= array->dimension[j])) {
432 fprintf(stderr, "Error: Invalid value for d%ld: '%s'.\n", j + 1, ptr);
433 exit(EXIT_FAILURE);
434 }
435 ca[i].dims[j]++;
436 ptr = strtok(NULL, ",");
437 }
438 }
439 if (j == 0) {
440 vrows = ca[i].dims[j];
441 } else {
442 vrows *= ca[i].dims[j];
443 }
444 }
445
446 if ((orig_column_names) && (vrows != rows)) {
447 fprintf(stderr, "Error: Cannot convert '%s' because existing columns have a different number of rows.\n", ca[i].name);
448 exit(EXIT_FAILURE);
449 }
450 if ((virtual_rows >= 0) && (vrows != virtual_rows)) {
451 fprintf(stderr, "Error: The number of array elements are not the same.\n");
452 exit(EXIT_FAILURE);
453 }
454 if ((!orig_column_names) && (virtual_rows == -1)) {
455 if (!SDDS_LengthenTable(&SDDS_dataset, vrows)) {
456 SDDS_SetError("Unable to lengthen table");
457 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
458 }
459 }
460 virtual_rows = vrows;
461
462 n = 0;
463 switch (ca[i].type) {
464 case SDDS_SHORT:
465 ca[i].data = malloc(sizeof(short) * vrows);
466 if (ca[i].dims[1] == 0) {
467 for (j = 0; j < ca[i].dims[0]; j++) {
468 ((short *)ca[i].data)[n++] = ((short *)array->data)[ca[i].dim[0][j]];
469 }
470 } else if (ca[i].dims[2] == 0) {
471 for (j = 0; j < ca[i].dims[0]; j++) {
472 for (k = 0; k < ca[i].dims[1]; k++) {
473 ((short *)ca[i].data)[n++] = ((short *)array->data)[ca[i].dim[0][j] * array->dimension[1] + ca[i].dim[1][k]];
474 }
475 }
476 } else {
477 for (j = 0; j < ca[i].dims[0]; j++) {
478 for (k = 0; k < ca[i].dims[1]; k++) {
479 for (m = 0; m < ca[i].dims[2]; m++) {
480 ((short *)ca[i].data)[n++] = ((short *)array->data)[ca[i].dim[0][j] * (array->dimension[1] * array->dimension[2]) +
481 ca[i].dim[1][k] * array->dimension[2] +
482 ca[i].dim[2][m]];
483 }
484 }
485 }
486 }
487 break;
488
489 case SDDS_USHORT:
490 ca[i].data = malloc(sizeof(unsigned short) * vrows);
491 if (ca[i].dims[1] == 0) {
492 for (j = 0; j < ca[i].dims[0]; j++) {
493 ((unsigned short *)ca[i].data)[n++] = ((unsigned short *)array->data)[ca[i].dim[0][j]];
494 }
495 } else if (ca[i].dims[2] == 0) {
496 for (j = 0; j < ca[i].dims[0]; j++) {
497 for (k = 0; k < ca[i].dims[1]; k++) {
498 ((unsigned short *)ca[i].data)[n++] = ((unsigned short *)array->data)[ca[i].dim[0][j] * array->dimension[1] + ca[i].dim[1][k]];
499 }
500 }
501 } else {
502 for (j = 0; j < ca[i].dims[0]; j++) {
503 for (k = 0; k < ca[i].dims[1]; k++) {
504 for (m = 0; m < ca[i].dims[2]; m++) {
505 ((unsigned short *)ca[i].data)[n++] = ((unsigned short *)array->data)[ca[i].dim[0][j] * (array->dimension[1] * array->dimension[2]) +
506 ca[i].dim[1][k] * array->dimension[2] +
507 ca[i].dim[2][m]];
508 }
509 }
510 }
511 }
512 break;
513
514 case SDDS_LONG:
515 ca[i].data = malloc(sizeof(int32_t) * vrows);
516 if (ca[i].dims[1] == 0) {
517 for (j = 0; j < ca[i].dims[0]; j++) {
518 ((int32_t *)ca[i].data)[n++] = ((int32_t *)array->data)[ca[i].dim[0][j]];
519 }
520 } else if (ca[i].dims[2] == 0) {
521 for (j = 0; j < ca[i].dims[0]; j++) {
522 for (k = 0; k < ca[i].dims[1]; k++) {
523 ((int32_t *)ca[i].data)[n++] = ((int32_t *)array->data)[ca[i].dim[0][j] * array->dimension[1] + ca[i].dim[1][k]];
524 }
525 }
526 } else {
527 for (j = 0; j < ca[i].dims[0]; j++) {
528 for (k = 0; k < ca[i].dims[1]; k++) {
529 for (m = 0; m < ca[i].dims[2]; m++) {
530 ((int32_t *)ca[i].data)[n++] = ((int32_t *)array->data)[ca[i].dim[0][j] * (array->dimension[1] * array->dimension[2]) +
531 ca[i].dim[1][k] * array->dimension[2] +
532 ca[i].dim[2][m]];
533 }
534 }
535 }
536 }
537 break;
538
539 case SDDS_ULONG:
540 ca[i].data = malloc(sizeof(uint32_t) * vrows);
541 if (ca[i].dims[1] == 0) {
542 for (j = 0; j < ca[i].dims[0]; j++) {
543 ((uint32_t *)ca[i].data)[n++] = ((uint32_t *)array->data)[ca[i].dim[0][j]];
544 }
545 } else if (ca[i].dims[2] == 0) {
546 for (j = 0; j < ca[i].dims[0]; j++) {
547 for (k = 0; k < ca[i].dims[1]; k++) {
548 ((uint32_t *)ca[i].data)[n++] = ((uint32_t *)array->data)[ca[i].dim[0][j] * array->dimension[1] + ca[i].dim[1][k]];
549 }
550 }
551 } else {
552 for (j = 0; j < ca[i].dims[0]; j++) {
553 for (k = 0; k < ca[i].dims[1]; k++) {
554 for (m = 0; m < ca[i].dims[2]; m++) {
555 ((uint32_t *)ca[i].data)[n++] = ((uint32_t *)array->data)[ca[i].dim[0][j] * (array->dimension[1] * array->dimension[2]) +
556 ca[i].dim[1][k] * array->dimension[2] +
557 ca[i].dim[2][m]];
558 }
559 }
560 }
561 }
562 break;
563
564 case SDDS_LONG64:
565 ca[i].data = malloc(sizeof(int64_t) * vrows);
566 if (ca[i].dims[1] == 0) {
567 for (j = 0; j < ca[i].dims[0]; j++) {
568 ((int64_t *)ca[i].data)[n++] = ((int64_t *)array->data)[ca[i].dim[0][j]];
569 }
570 } else if (ca[i].dims[2] == 0) {
571 for (j = 0; j < ca[i].dims[0]; j++) {
572 for (k = 0; k < ca[i].dims[1]; k++) {
573 ((int64_t *)ca[i].data)[n++] = ((int64_t *)array->data)[ca[i].dim[0][j] * array->dimension[1] + ca[i].dim[1][k]];
574 }
575 }
576 } else {
577 for (j = 0; j < ca[i].dims[0]; j++) {
578 for (k = 0; k < ca[i].dims[1]; k++) {
579 for (m = 0; m < ca[i].dims[2]; m++) {
580 ((int64_t *)ca[i].data)[n++] = ((int64_t *)array->data)[ca[i].dim[0][j] * (array->dimension[1] * array->dimension[2]) +
581 ca[i].dim[1][k] * array->dimension[2] +
582 ca[i].dim[2][m]];
583 }
584 }
585 }
586 }
587 break;
588
589 case SDDS_ULONG64:
590 ca[i].data = malloc(sizeof(uint64_t) * vrows);
591 if (ca[i].dims[1] == 0) {
592 for (j = 0; j < ca[i].dims[0]; j++) {
593 ((uint64_t *)ca[i].data)[n++] = ((uint64_t *)array->data)[ca[i].dim[0][j]];
594 }
595 } else if (ca[i].dims[2] == 0) {
596 for (j = 0; j < ca[i].dims[0]; j++) {
597 for (k = 0; k < ca[i].dims[1]; k++) {
598 ((uint64_t *)ca[i].data)[n++] = ((uint64_t *)array->data)[ca[i].dim[0][j] * array->dimension[1] + ca[i].dim[1][k]];
599 }
600 }
601 } else {
602 for (j = 0; j < ca[i].dims[0]; j++) {
603 for (k = 0; k < ca[i].dims[1]; k++) {
604 for (m = 0; m < ca[i].dims[2]; m++) {
605 ((uint64_t *)ca[i].data)[n++] = ((uint64_t *)array->data)[ca[i].dim[0][j] * (array->dimension[1] * array->dimension[2]) +
606 ca[i].dim[1][k] * array->dimension[2] +
607 ca[i].dim[2][m]];
608 }
609 }
610 }
611 }
612 break;
613
614 case SDDS_FLOAT:
615 ca[i].data = malloc(sizeof(float) * vrows);
616 if (ca[i].dims[1] == 0) {
617 for (j = 0; j < ca[i].dims[0]; j++) {
618 ((float *)ca[i].data)[n++] = ((float *)array->data)[ca[i].dim[0][j]];
619 }
620 } else if (ca[i].dims[2] == 0) {
621 for (j = 0; j < ca[i].dims[0]; j++) {
622 for (k = 0; k < ca[i].dims[1]; k++) {
623 ((float *)ca[i].data)[n++] = ((float *)array->data)[ca[i].dim[0][j] * array->dimension[1] + ca[i].dim[1][k]];
624 }
625 }
626 } else {
627 for (j = 0; j < ca[i].dims[0]; j++) {
628 for (k = 0; k < ca[i].dims[1]; k++) {
629 for (m = 0; m < ca[i].dims[2]; m++) {
630 ((float *)ca[i].data)[n++] = ((float *)array->data)[ca[i].dim[0][j] * (array->dimension[1] * array->dimension[2]) +
631 ca[i].dim[1][k] * array->dimension[2] +
632 ca[i].dim[2][m]];
633 }
634 }
635 }
636 }
637 break;
638
639 case SDDS_DOUBLE:
640 ca[i].data = malloc(sizeof(double) * vrows);
641 if (ca[i].dims[1] == 0) {
642 for (j = 0; j < ca[i].dims[0]; j++) {
643 ((double *)ca[i].data)[n++] = ((double *)array->data)[ca[i].dim[0][j]];
644 }
645 } else if (ca[i].dims[2] == 0) {
646 for (j = 0; j < ca[i].dims[0]; j++) {
647 for (k = 0; k < ca[i].dims[1]; k++) {
648 ((double *)ca[i].data)[n++] = ((double *)array->data)[ca[i].dim[0][j] * array->dimension[1] + ca[i].dim[1][k]];
649 }
650 }
651 } else {
652 for (j = 0; j < ca[i].dims[0]; j++) {
653 for (k = 0; k < ca[i].dims[1]; k++) {
654 for (m = 0; m < ca[i].dims[2]; m++) {
655 ((double *)ca[i].data)[n++] = ((double *)array->data)[ca[i].dim[0][j] * (array->dimension[1] * array->dimension[2]) +
656 ca[i].dim[1][k] * array->dimension[2] +
657 ca[i].dim[2][m]];
658 }
659 }
660 }
661 }
662 break;
663
664 case SDDS_STRING:
665 ca[i].data = malloc(sizeof(char *) * vrows);
666 if (ca[i].dims[1] == 0) {
667 for (j = 0; j < ca[i].dims[0]; j++) {
668 ((char **)ca[i].data)[n++] = ((char **)array->data)[ca[i].dim[0][j]];
669 }
670 } else if (ca[i].dims[2] == 0) {
671 for (j = 0; j < ca[i].dims[0]; j++) {
672 for (k = 0; k < ca[i].dims[1]; k++) {
673 ((char **)ca[i].data)[n++] = ((char **)array->data)[ca[i].dim[0][j] * array->dimension[1] + ca[i].dim[1][k]];
674 }
675 }
676 } else {
677 for (j = 0; j < ca[i].dims[0]; j++) {
678 for (k = 0; k < ca[i].dims[1]; k++) {
679 for (m = 0; m < ca[i].dims[2]; m++) {
680 ((char **)ca[i].data)[n++] = ((char **)array->data)[ca[i].dim[0][j] * (array->dimension[1] * array->dimension[2]) +
681 ca[i].dim[1][k] * array->dimension[2] +
682 ca[i].dim[2][m]];
683 }
684 }
685 }
686 }
687 break;
688
689 case SDDS_CHARACTER:
690 ca[i].data = malloc(sizeof(char) * vrows);
691 if (ca[i].dims[1] == 0) {
692 for (j = 0; j < ca[i].dims[0]; j++) {
693 ((char *)ca[i].data)[n++] = ((char *)array->data)[ca[i].dim[0][j]];
694 }
695 } else if (ca[i].dims[2] == 0) {
696 for (j = 0; j < ca[i].dims[0]; j++) {
697 for (k = 0; k < ca[i].dims[1]; k++) {
698 ((char *)ca[i].data)[n++] = ((char *)array->data)[ca[i].dim[0][j] * array->dimension[1] + ca[i].dim[1][k]];
699 }
700 }
701 } else {
702 for (j = 0; j < ca[i].dims[0]; j++) {
703 for (k = 0; k < ca[i].dims[1]; k++) {
704 for (m = 0; m < ca[i].dims[2]; m++) {
705 ((char *)ca[i].data)[n++] = ((char *)array->data)[ca[i].dim[0][j] * (array->dimension[1] * array->dimension[2]) +
706 ca[i].dim[1][k] * array->dimension[2] +
707 ca[i].dim[2][m]];
708 }
709 }
710 }
711 }
712 break;
713
714 default:
715 fprintf(stderr, "Error: Unsupported data type for array '%s'.\n", ca[i].name);
716 exit(EXIT_FAILURE);
717 }
718
719 if (!SDDS_SetColumn(&SDDS_dataset, SDDS_SET_BY_NAME, ca[i].data, virtual_rows, ca[i].new_name)) {
720 fprintf(stderr, "Error: Problem setting column '%s'.\n", ca[i].new_name);
721 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
722 }
723
724 for (j = 0; j < array->definition->dimensions; j++) {
725 if (ca[i].dim[j] != NULL) {
726 free(ca[i].dim[j]);
727 }
728 }
729 if (ca[i].data != NULL) {
730 free(ca[i].data);
731 }
732 }
733 }
734
735 if (!SDDS_WritePage(&SDDS_dataset)) {
736 fprintf(stderr, "Error: Problem writing page to file '%s'.\n", output);
737 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
738 }
739 }
740
741 if (!SDDS_Terminate(&SDDS_orig) || !SDDS_Terminate(&SDDS_dataset)) {
742 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
743 exit(EXIT_FAILURE);
744 }
745
746 if (tmpfile_used && !replaceFileAndBackUp(input, output)) {
747 exit(EXIT_FAILURE);
748 }
749
750 if (ca != NULL) {
751 free(ca);
752 }
753 if (buffer != NULL) {
754 free(buffer);
755 }
756 for (i = 0; i < orig_array_names; i++) {
757 if (orig_array_name[i] != NULL) {
758 free(orig_array_name[i]);
759 }
760 }
761 if (orig_array_name != NULL) {
762 free(orig_array_name);
763 }
764 for (i = 0; i < orig_parameter_names; i++) {
765 if (orig_parameter_name[i] != NULL) {
766 free(orig_parameter_name[i]);
767 }
768 }
769 if (orig_parameter_name != NULL) {
770 free(orig_parameter_name);
771 }
772 for (i = 0; i < orig_column_names; i++) {
773 if (orig_column_name[i] != NULL) {
774 free(orig_column_name[i]);
775 }
776 }
777 if (orig_column_name != NULL) {
778 free(orig_column_name);
779 }
780 if (description_text != NULL) {
781 free(description_text);
782 }
783 if (description_contents != NULL) {
784 free(description_contents);
785 }
786
787 return EXIT_SUCCESS;
788}
int32_t SDDS_type_size[SDDS_NUM_TYPES]
Array of sizes for each supported data type.
Definition SDDS_data.c:62
int32_t SDDS_LengthenTable(SDDS_DATASET *SDDS_dataset, int64_t n_additional_rows)
int32_t SDDS_StartPage(SDDS_DATASET *SDDS_dataset, int64_t expected_n_rows)
int32_t SDDS_SetParameters(SDDS_DATASET *SDDS_dataset, int32_t mode,...)
int32_t SDDS_SetColumn(SDDS_DATASET *SDDS_dataset, int32_t mode, void *data, int64_t rows,...)
Sets the values for one data column in the current data table of an SDDS dataset.
int32_t SDDS_SetArray(SDDS_DATASET *SDDS_dataset, char *array_name, int32_t mode, void *data_pointer, int32_t *dimension)
Sets the values of an array variable in the SDDS dataset using specified dimensions.
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_GetInternalColumn(SDDS_DATASET *SDDS_dataset, char *column_name)
Retrieves an internal pointer to the data of a specified column, including all rows.
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_GetDescription(SDDS_DATASET *SDDS_dataset, char **text, char **contents)
Retrieves the text and contents descriptions from an SDDS dataset.
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)
int32_t SDDS_InitializeOutput(SDDS_DATASET *SDDS_dataset, int32_t data_mode, int32_t lines_per_row, const char *description, const char *contents, const char *filename)
Initializes the SDDS output dataset.
int32_t SDDS_WritePage(SDDS_DATASET *SDDS_dataset)
Writes the current data table to the output file.
int32_t SDDS_DefineColumn(SDDS_DATASET *SDDS_dataset, const char *name, const char *symbol, const char *units, const char *description, const char *format_string, int32_t type, int32_t field_length)
Defines a data column within the SDDS dataset.
int32_t SDDS_WriteLayout(SDDS_DATASET *SDDS_dataset)
Writes the SDDS layout header to the output file.
int32_t SDDS_TransferColumnDefinition(SDDS_DATASET *target, SDDS_DATASET *source, char *name, char *newName)
Transfers a column definition from a source dataset to a target dataset.
int32_t SDDS_TransferArrayDefinition(SDDS_DATASET *target, SDDS_DATASET *source, char *name, char *newName)
Transfers an array definition from a source dataset to a target dataset.
int32_t SDDS_TransferParameterDefinition(SDDS_DATASET *target, SDDS_DATASET *source, char *name, char *newName)
Transfers a parameter definition from a source dataset to a target dataset.
void SDDS_FreeArray(SDDS_ARRAY *array)
Frees memory allocated for an SDDS array structure.
void SDDS_SetError(char *error_text)
Records an error message in the SDDS error stack.
Definition SDDS_utils.c:379
ARRAY_DEFINITION * SDDS_GetArrayDefinition(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the definition of a specified array from the SDDS dataset.
int32_t SDDS_FreeArrayDefinition(ARRAY_DEFINITION *source)
Frees memory allocated for an array definition.
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.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
Definition SDDS_utils.c:432
void SDDS_RegisterProgramName(const char *name)
Registers the executable program name for use in error messages.
Definition SDDS_utils.c:288
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
Definition SDDS_utils.c:342
char ** SDDS_GetArrayNames(SDDS_DATASET *SDDS_dataset, int32_t *number)
Retrieves the names of all arrays in the SDDS dataset.
#define SDDS_NUM_TYPES
Total number of defined SDDS data types.
Definition SDDStypes.h:97
#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_LONG64
Identifier for the signed 64-bit integer data type.
Definition SDDStypes.h:49
void * trealloc(void *old_ptr, uint64_t size_of_block)
Reallocates a memory block to a new size.
Definition array.c:181
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
Definition array.c:59
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
Definition bomb.c:26
char * delete_chars(char *s, char *t)
Removes all occurrences of characters found in string t from string s.
long match_string(char *string, char **option, long n_options, long mode)
Matches a given string against an array of option strings based on specified modes.
long replaceFileAndBackUp(char *file, char *replacement)
Replaces a file with a replacement file and creates a backup of the original.
Definition replacefile.c:75
int scanargs(SCANNED_ARG **scanned, int argc, char **argv)
Definition scanargs.c:36
long processPipeOption(char **item, long items, unsigned long *flags)
Definition scanargs.c:356
void processFilenames(char *programName, char **input, char **output, unsigned long pipeFlags, long noWarnings, long *tmpOutputUsed)
Definition scanargs.c:390

Variable Documentation

◆ dim_option

char* dim_option[DIM_OPTIONS]
static
Initial value:
= {
"d0",
"d1",
"d2",
}

Definition at line 67 of file sddsarray2column.c.

67 {
68 "d0",
69 "d1",
70 "d2",
71};

◆ option

char* option[N_OPTIONS]
static
Initial value:
= {
"nowarnings",
"convert",
"pipe",
}

Definition at line 56 of file sddsarray2column.c.

56 {
57 "nowarnings",
58 "convert",
59 "pipe",
60};

◆ USAGE

char* USAGE
static
Initial value:
=
"Usage: sddsarray2column [<source-file>] [<target-file>]\n"
" [-pipe=[input][,output]]\n"
" [-nowarnings]\n"
" [-convert=<array-name>[,<column-name>][,d<dimension>=<indexValue>]...]\n\n"
"sddsarray2column converts SDDS arrays to SDDS columns.\n"
"The number of elements in the converted arrays must equal\n"
"the number of rows if there are columns in the file and\n"
"the number of elements in other converted arrays.\n\n"
"Examples:\n"
" sddsarray2column in out -convert=A,A_out\n"
" sddsarray2column in out -convert=A,A_out,d0=0\n"
" sddsarray2column in out \"-convert=A,A_out,d2=(1,3)\"\n\n"
"Program by Robert Soliday. (Compiled on " __DATE__ " at " __TIME__ ", SVN revision: " SVN_VERSION ")\n"

Definition at line 73 of file sddsarray2column.c.