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

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

Required Description
-convert Convert the specified SDDS array to a column.
Optional Description
-pipe Use standard input and/or output for file operations.
-nowarnings Suppress warning messages.

Incompatibilities

  • -convert:
    • Converted arrays cannot have the same column-name as existing columns.
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.

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

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Function Documentation

◆ main()

int main ( int argc,
char ** argv )

Definition at line 99 of file sddsarray2column.c.

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