81 {
83 TABLE *mpl_data;
84 SCANNED_ARG *scanned;
85 char **input;
86 long i, j, i_arg, inputs;
87 char *output;
88 long erase, rows, new_columns, SDDS_rows;
89 double **data;
90 long *index;
91 long binary;
92
94 argc =
scanargs(&scanned, argc, argv);
95 if (argc < 3) {
97 }
98
99 input = NULL;
100 output = NULL;
101 inputs = erase = binary = 0;
102
103 for (i_arg = 1; i_arg < argc; i_arg++) {
104 if (scanned[i_arg].arg_type == OPTION) {
105
106 switch (
match_string(scanned[i_arg].list[0], option, N_OPTIONS, 0)) {
107 case SET_ERASE:
108 erase = 1;
109 break;
110 case SET_OUTPUT:
111 if (scanned[i_arg].n_items != 2) {
112 bomb(
"Invalid syntax for -output option.", USAGE);
113 }
114 output = scanned[i_arg].list[1];
115 break;
116 case SET_BINARY:
117 if (scanned[i_arg].n_items != 1) {
118 bomb(
"Invalid syntax for -binary option.", USAGE);
119 }
120 binary = 1;
121 break;
122 default:
123 bomb(
"Unknown option provided.", USAGE);
124 }
125 } else {
126 input =
trealloc(input, (inputs + 1) *
sizeof(*input));
127 input[inputs++] = scanned[i_arg].list[0];
128 }
129 }
130
131 if (!output) {
132 bomb(
"The -output option must be specified.", USAGE);
133 }
134 if (!input) {
135 bomb(
"No input MPL files provided.", USAGE);
136 }
137
138 if (!erase &&
fexists(output)) {
139
141 fprintf(stderr, "Error: Unable to read SDDS layout from %s.\n", output);
143 return EXIT_FAILURE;
144 }
146 fprintf(stderr, "Error: Unable to read data table from %s.\n", output);
148 return EXIT_FAILURE;
149 }
151 fclose(SDDS_dataset.layout.fp);
152 SDDS_dataset.layout.fp =
fopen_e(output,
"w", 0);
153 } else {
154
155 if (!
SDDS_InitializeOutput(&SDDS_dataset, binary ? SDDS_BINARY : SDDS_ASCII, 1, NULL, NULL, output)) {
156 fprintf(stderr, "Error: Unable to initialize output SDDS structure for %s.\n", output);
158 return EXIT_FAILURE;
159 }
160 SDDS_rows = -1;
161 }
162
163 rows = 0;
164 mpl_data =
tmalloc(
sizeof(*mpl_data) * inputs);
165 data =
tmalloc(
sizeof(*data) * (2 * inputs));
166 index =
tmalloc(
sizeof(*index) * (2 * inputs));
167 new_columns = 0;
168
169 for (i = 0; i < inputs; i++) {
170 if (!
get_table(&mpl_data[i], input[i], 1, 0)) {
171 fprintf(stderr, "Warning: Unable to read data from %s. Continuing with other files.\n", input[i]);
172 continue;
173 }
174
175 if (!rows) {
176 if (!(rows = mpl_data[i].n_data)) {
177 fprintf(stderr, "Warning: No data in file %s. Continuing with other files.\n", input[i]);
178 continue;
179 }
180 } else if (rows != mpl_data[i].n_data) {
181 SDDS_Bomb(
"All MPL files must have the same number of data points.");
182 } else if (SDDS_rows != -1 && rows != SDDS_rows) {
183 SDDS_Bomb(
"Number of data points in MPL files must match the number of rows in the SDDS file.");
184 }
185
186 if ((index[new_columns] = add_definition(&SDDS_dataset, mpl_data[i].xlab, input[i])) < 0) {
187 free(mpl_data[i].c1);
188 } else {
189 data[new_columns++] = mpl_data[i].c1;
190 }
191
192 if ((index[new_columns] = add_definition(&SDDS_dataset, mpl_data[i].ylab, input[i])) < 0) {
193 free(mpl_data[i].c2);
194 } else {
195 data[new_columns++] = mpl_data[i].c2;
196 }
197 }
198
199 if (!rows || !new_columns) {
200 SDDS_Bomb(
"All input files are empty or invalid.");
201 }
202
205 return EXIT_FAILURE;
206 }
209 return EXIT_FAILURE;
210 }
211
212 for (i = 0; i < new_columns; i++) {
213 for (j = 0; j < rows; j++) {
214 SDDS_SetRowValues(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, j, index[i], data[i][j], -1);
215 }
216 }
217
220 return EXIT_FAILURE;
221 }
222
223 return EXIT_SUCCESS;
224}
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)
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_WriteLayout(SDDS_DATASET *SDDS_dataset)
Writes the SDDS layout header to the output file.
void SDDS_RegisterProgramName(const char *name)
Registers the executable program name for use in error messages.
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
void * trealloc(void *old_ptr, uint64_t size_of_block)
Reallocates a memory block to a new size.
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
long fexists(const char *filename)
Checks if a file exists.
FILE * fopen_e(char *file, char *open_mode, long mode)
Opens a file with error checking, messages, and aborts.
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)
long get_table(TABLE *tab, char *file, int64_t sample_interval, long flags)
Gets a table from a file in DPL format.