SDDSlib
Loading...
Searching...
No Matches
sddscollapse.c
Go to the documentation of this file.
1/**
2 * @file sddscollapse.c
3 * @brief Converts SDDS file parameters into columns.
4 *
5 * This program processes an input SDDS file to produce an output SDDS file
6 * where each data page contains parameters represented as columns of tabular data.
7 * The rows in the output file correspond to different pages in the input file.
8 * Original file columns are ignored in this transformation.
9 *
10 * @details
11 * The program provides options to control input/output via pipes, suppress warnings,
12 * and specify major order (row or column). It handles parameter definitions and manages
13 * memory dynamically to accommodate varying numbers of parameters and data pages.
14 *
15 * @usage
16 * sddscollapse [<SDDSinputfile>] [<SDDSoutputfile>]
17 * [-pipe=[input][,output]] [-majorOrder=row|column]
18 * [-noWarnings]
19 *
20 * @copyright
21 * - (c) 2002 The University of Chicago, as Operator of Argonne National Laboratory.
22 * - (c) 2002 The Regents of the University of California, as Operator of Los Alamos National Laboratory.
23 *
24 * @license
25 * This file is distributed under the terms of the Software License Agreement
26 * found in the file LICENSE included with this distribution.
27 *
28 * @author M. Borland, C. Saunders, R. Soliday, H. Shang
29 */
30
31#include "mdb.h"
32#include "scan.h"
33#include "SDDS.h"
34
35typedef enum {
36 SET_PIPE, /* Option for setting up input/output pipes. */
37 SET_NOWARNINGS, /* Option to suppress warnings during execution. */
38 SET_MAJOR_ORDER, /* Option to specify row or column major order. */
39 N_OPTIONS /* Total number of options. */
41
42/* Array of option names corresponding to OptionType. */
43char *option[N_OPTIONS] =
44 {
45 "pipe",
46 "nowarnings",
47 "majorOrder",
48 };
49
50/* Usage message displayed for help. */
51char *usage =
52 "sddscollapse [<SDDSinputfile>] [<SDDSoutputfile>]\n"
53 "[-pipe=[input][,output]] [-majorOrder=row|column] \n"
54 "[-noWarnings]\n\n"
55 "sddscollapse reads data pages from a SDDS file and writes a new SDDS file \n"
56 "containing a single data page. This data page contains the parameters, \n"
57 "with each parameter forming a column of the tabular data.\n\n"
58 "Program by Michael Borland. (" __DATE__ " " __TIME__ ", SVN revision: " SVN_VERSION ")\n";
59
60#define ROW_INCREMENT 100 /* Number of rows to increment memory allocation for output. */
61
62int main(int argc, char **argv) {
63 SDDS_DATASET SDDS_input, SDDS_output;
64 char *inputfile = NULL, *outputfile = NULL, **column;
65 long i, i_arg;
66 long page_number, no_warnings = 0, set_page_number;
67 int64_t allocated_rows;
68 int32_t columns;
69 SCANNED_ARG *s_arg;
70 char s[SDDS_MAXLINE];
71 unsigned long pipe_flags = 0, major_order_flag;
72 long buffer[16];
73 short column_major_order = -1;
74
75 /* Register the program name for error messages. */
77
78 /* Parse command-line arguments. */
79 argc = scanargs(&s_arg, argc, argv);
80 if (argc < 2)
81 bomb(NULL, usage);
82
83 /* Process each argument. */
84 for (i_arg = 1; i_arg < argc; i_arg++) {
85 if (s_arg[i_arg].arg_type == OPTION) {
86 /* Match the option type and process accordingly. */
87 switch (match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
88 case SET_MAJOR_ORDER:
89 major_order_flag = 0;
90 s_arg[i_arg].n_items -= 1;
91 if (s_arg[i_arg].n_items > 0 &&
92 (!scanItemList(&major_order_flag, s_arg[i_arg].list + 1,
93 &s_arg[i_arg].n_items, 0,
94 "row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER,
95 "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER,
96 NULL)))
97 SDDS_Bomb("invalid -majorOrder syntax/values");
98 if (major_order_flag & SDDS_COLUMN_MAJOR_ORDER)
99 column_major_order = 1;
100 else if (major_order_flag & SDDS_ROW_MAJOR_ORDER)
101 column_major_order = 0;
102 break;
103 case SET_PIPE:
104 if (!processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1,
105 &pipe_flags))
106 SDDS_Bomb("invalid -pipe syntax");
107 break;
108 case SET_NOWARNINGS:
109 no_warnings = 1;
110 break;
111 default:
112 fprintf(stderr, "error: unknown switch: %s\n", s_arg[i_arg].list[0]);
113 exit(1);
114 break;
115 }
116 } else {
117 /* Process input and output filenames. */
118 if (inputfile == NULL)
119 inputfile = s_arg[i_arg].list[0];
120 else if (outputfile == NULL)
121 outputfile = s_arg[i_arg].list[0];
122 else
123 SDDS_Bomb("too many filenames");
124 }
125 }
126
127 /* Validate and prepare filenames. */
128 processFilenames("sddscollapse", &inputfile, &outputfile, pipe_flags, no_warnings, NULL);
129
130 /* Initialize input SDDS file. */
131 if (!SDDS_InitializeInput(&SDDS_input, inputfile)) {
132 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
133 exit(1);
134 }
135
136 /* Initialize output SDDS file. */
137 if (!SDDS_InitializeOutput(&SDDS_output, SDDS_input.layout.data_mode.mode, 1,
138 NULL, NULL, outputfile)) {
139 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
140 exit(1);
141 }
142
143 /* Set major order for output. */
144 if (column_major_order != -1)
145 SDDS_output.layout.data_mode.column_major = column_major_order;
146 else
147 SDDS_output.layout.data_mode.column_major = SDDS_input.layout.data_mode.column_major;
148
149 /* Get parameter names from the input file. */
150 if (!(column = SDDS_GetParameterNames(&SDDS_input, &columns))) {
151 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
152 exit(1);
153 }
154
155 /* Define output columns corresponding to input parameters. */
156 for (i = 0; i < columns; i++) {
157 if (!SDDS_DefineColumnLikeParameter(&SDDS_output, &SDDS_input, column[i], NULL)) {
158 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
159 exit(1);
160 }
161 }
162
163 /* Define a PageNumber column if it doesn't exist. */
164 sprintf(s, "corresponding page number of %s for this row",
165 inputfile ? inputfile : "stdin");
166 if (SDDS_GetColumnIndex(&SDDS_output, "PageNumber") < 0) {
167 if (SDDS_DefineColumn(&SDDS_output, "PageNumber", NULL, NULL, s, NULL,
168 SDDS_LONG, 0) < 0) {
169 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
170 exit(1);
171 }
172 set_page_number = 1;
173 } else {
174 set_page_number = 0;
175 }
176
177 /* Start writing the layout and allocate rows for output. */
178 if (!SDDS_WriteLayout(&SDDS_output) ||
179 !SDDS_StartPage(&SDDS_output, allocated_rows = ROW_INCREMENT)) {
180 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
181 exit(1);
182 }
183
184 /* Process each data page in the input file. */
185 while ((page_number = SDDS_ReadPageSparse(&SDDS_input, 0, INT32_MAX - 1, 0, 0)) > 0) {
186 /* Expand memory if necessary. */
187 if (page_number > allocated_rows) {
188 if (!SDDS_LengthenTable(&SDDS_output, ROW_INCREMENT)) {
189 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
190 exit(1);
191 }
192 allocated_rows += ROW_INCREMENT;
193 }
194
195 /* Copy parameters into corresponding columns. */
196 for (i = 0; i < columns; i++) {
197 if (!SDDS_GetParameter(&SDDS_input, column[i], buffer)) {
198 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
199 exit(1);
200 }
201 if (!SDDS_SetRowValues(&SDDS_output,
202 SDDS_SET_BY_NAME | SDDS_PASS_BY_REFERENCE,
203 page_number - 1, column[i], buffer, NULL)) {
204 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
205 exit(1);
206 }
207 }
208
209 /* Set the PageNumber column if required. */
210 if (set_page_number &&
211 !SDDS_SetRowValues(&SDDS_output,
212 SDDS_SET_BY_NAME | SDDS_PASS_BY_VALUE,
213 page_number - 1, "PageNumber", page_number, NULL)) {
214 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
215 exit(1);
216 }
217 }
218
219 /* Free allocated memory for column names. */
220 for (i = 0; i < columns; i++)
221 free(column[i]);
222 free(column);
223
224 /* Finalize the output SDDS file. */
225 if (!SDDS_WritePage(&SDDS_output)) {
226 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
227 exit(1);
228 }
229 if (page_number == 0) {
230 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
231 exit(1);
232 }
233 if (!SDDS_Terminate(&SDDS_input) || !SDDS_Terminate(&SDDS_output)) {
234 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
235 exit(1);
236 }
237 return 0;
238}
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
int32_t SDDS_LengthenTable(SDDS_DATASET *SDDS_dataset, int64_t n_additional_rows)
int32_t SDDS_SetRowValues(SDDS_DATASET *SDDS_dataset, int32_t mode, int64_t row,...)
int32_t SDDS_StartPage(SDDS_DATASET *SDDS_dataset, int64_t expected_n_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_ReadPageSparse(SDDS_DATASET *SDDS_dataset, uint32_t mode, int64_t sparse_interval, int64_t sparse_offset, int32_t sparse_statistics)
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_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_DefineColumnLikeParameter(SDDS_DATASET *target, SDDS_DATASET *source, char *name, char *newName)
Defines a column in the target dataset based on a parameter definition from the source dataset.
char ** SDDS_GetParameterNames(SDDS_DATASET *SDDS_dataset, int32_t *number)
Retrieves the names of all parameters in the SDDS dataset.
int32_t SDDS_GetColumnIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named column 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
#define SDDS_LONG
Identifier for the signed 32-bit integer data type.
Definition SDDStypes.h:61
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
Definition bomb.c:26
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.
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
long scanItemList(unsigned long *flags, char **item, long *items, unsigned long mode,...)
Scans a list of items and assigns values based on provided keywords and types.
OptionType
Enumeration for command-line options.