151 {
152 int i_arg;
153 char *input = NULL, *output = NULL, **interpCol = NULL, **funcOf = NULL;
154 long order = 1, dataControls = 0, valid_option = 1, monotonicity;
155
156 SCANNED_ARG *s_arg;
157 OUTRANGE_CONTROL aboveRange, belowRange;
159 unsigned long pipeFlags = 0, interpCode = 0, majorOrderFlag;
161 double *indepValue = NULL, *depenValue = NULL, **out_depenValue = NULL, atValue = 0;
162 int32_t **rowFlag = NULL;
163 long valid_data = 0, index, pages = 0;
164 int64_t *rows = NULL;
165 int64_t i, j, datarows, row;
166 short columnMajorOrder = -1;
167
169 argc =
scanargs(&s_arg, argc, argv);
170 if (argc < 3) {
171 fprintf(stderr, "%s", USAGE);
172 exit(EXIT_FAILURE);
173 }
174
175 aboveRange.flags = belowRange.flags = OUTRANGE_SATURATE;
176
177 for (i_arg = 1; i_arg < argc; i_arg++) {
178 if (s_arg[i_arg].arg_type == OPTION) {
179 switch (
match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
180 case CLO_MAJOR_ORDER:
181 majorOrderFlag = 0;
182 s_arg[i_arg].n_items--;
183 if (s_arg[i_arg].n_items > 0 &&
184 !
scanItemList(&majorOrderFlag, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
185 "row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER,
186 "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER, NULL)) {
187 SDDS_Bomb(
"invalid -majorOrder syntax/values");
188 }
189 if (majorOrderFlag & SDDS_COLUMN_MAJOR_ORDER)
190 columnMajorOrder = 1;
191 else if (majorOrderFlag & SDDS_ROW_MAJOR_ORDER)
192 columnMajorOrder = 0;
193 break;
194
195 case CLO_ORDER:
196 if (s_arg[i_arg].n_items != 2 ||
197 sscanf(s_arg[i_arg].list[1], "%ld", &order) != 1 ||
198 order < 1) {
199 SDDS_Bomb(
"invalid -order syntax/value");
200 }
201 break;
202
203 case CLO_PIPE:
204 if (!
processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipeFlags)) {
206 }
207 break;
208
209 case CLO_ABOVERANGE:
210 s_arg[i_arg].n_items -= 1;
211 if (s_arg[i_arg].n_items < 1 ||
212 !
scanItemList(&aboveRange.flags, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
213 "value",
SDDS_DOUBLE, &aboveRange.value, 1, OUTRANGE_VALUE,
214 "skip", -1, NULL, 0, OUTRANGE_SKIP,
215 "saturate", -1, NULL, 0, OUTRANGE_SATURATE,
216 "extrapolate", -1, NULL, 0, OUTRANGE_EXTRAPOLATE,
217 "wrap", -1, NULL, 0, OUTRANGE_WRAP,
218 "abort", -1, NULL, 0, OUTRANGE_ABORT,
219 "warn", -1, NULL, 0, OUTRANGE_WARN, NULL)) {
220 SDDS_Bomb(
"invalid -aboveRange syntax/value");
221 }
222 if ((i =
bitsSet(aboveRange.flags & (OUTRANGE_VALUE | OUTRANGE_SKIP | OUTRANGE_SATURATE |
223 OUTRANGE_EXTRAPOLATE | OUTRANGE_WRAP | OUTRANGE_ABORT))) > 1) {
224 SDDS_Bomb(
"incompatible keywords given for -aboveRange");
225 }
226 if (i != 1)
227 aboveRange.flags |= OUTRANGE_SATURATE;
228 break;
229
230 case CLO_VERBOSE:
231
232 break;
233
234 case CLO_BELOWRANGE:
235 s_arg[i_arg].n_items -= 1;
236 if (s_arg[i_arg].n_items < 1 ||
237 !
scanItemList(&belowRange.flags, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
238 "value",
SDDS_DOUBLE, &belowRange.value, 1, OUTRANGE_VALUE,
239 "skip", -1, NULL, 0, OUTRANGE_SKIP,
240 "saturate", -1, NULL, 0, OUTRANGE_SATURATE,
241 "extrapolate", -1, NULL, 0, OUTRANGE_EXTRAPOLATE,
242 "wrap", -1, NULL, 0, OUTRANGE_WRAP,
243 "abort", -1, NULL, 0, OUTRANGE_ABORT,
244 "warn", -1, NULL, 0, OUTRANGE_WARN, NULL)) {
245 SDDS_Bomb(
"invalid -belowRange syntax/value");
246 }
247 if ((i =
bitsSet(belowRange.flags & (OUTRANGE_VALUE | OUTRANGE_SKIP | OUTRANGE_SATURATE |
248 OUTRANGE_EXTRAPOLATE | OUTRANGE_WRAP | OUTRANGE_ABORT))) > 1) {
249 SDDS_Bomb(
"incompatible keywords given for -belowRange");
250 }
251 if (i != 1)
252 belowRange.flags |= OUTRANGE_SATURATE;
253 break;
254
255 case CLO_DATA:
256 s_arg[i_arg].n_items -= 1;
257 if (s_arg[i_arg].n_items < 4) {
259 }
260 data_control =
SDDS_Realloc(data_control,
sizeof(*data_control) * (dataControls + 1));
261 data_control[dataControls].fileColumn = data_control[dataControls].interpCol =
262 data_control[dataControls].funcOfCol = data_control[dataControls].atCol = NULL;
263 data_control[dataControls].file = NULL;
264 data_control[dataControls].files = 0;
265 data_control[dataControls].hasdata = 0;
266 data_control[dataControls].colValue = NULL;
267 data_control[dataControls].flags = 0;
268
269 if (!
scanItemList(&data_control[dataControls].flags, s_arg[i_arg].list + 1,
270 &s_arg[i_arg].n_items, 0,
271 "fileColumn",
SDDS_STRING, &(data_control[dataControls].fileColumn), 1, 0,
272 "interpolate",
SDDS_STRING, &(data_control[dataControls].interpCol), 1, 0,
273 "functionof",
SDDS_STRING, &(data_control[dataControls].funcOfCol), 1, 0,
274 "column",
SDDS_STRING, &(data_control[dataControls].atCol), 1, AT_COLUMN,
275 "atValue",
SDDS_DOUBLE, &(data_control[dataControls].atValue), 1, AT_VALUE, NULL) ||
276 !data_control[dataControls].fileColumn ||
277 !data_control[dataControls].interpCol ||
278 !data_control[dataControls].funcOfCol) {
280 }
281
282 if (!(data_control[dataControls].flags & AT_COLUMN) &&
283 !(data_control[dataControls].flags & AT_VALUE)) {
284 SDDS_Bomb(
"Invalid -data syntax: either column or atValue option should be given.");
285 }
286
287 if ((data_control[dataControls].flags & AT_COLUMN) &&
288 (data_control[dataControls].flags & AT_VALUE)) {
289 SDDS_Bomb(
"Invalid -data syntax: column and atValue options are not compatible.");
290 }
291
292 valid_option = 1;
293 if (dataControls) {
294 if (
match_string(data_control[dataControls].funcOfCol, funcOf, dataControls, EXACT_MATCH) > 0) {
295 fprintf(stderr, "Multiple independent columns provided!\n");
296 exit(EXIT_FAILURE);
297 }
298 if (
match_string(data_control[dataControls].interpCol, interpCol, dataControls, EXACT_MATCH) > 0) {
299 fprintf(stderr, "Warning: Interpolate column '%s' has been used.\n", data_control[dataControls].interpCol);
300 valid_option = 0;
301 }
302 }
303
304 if (valid_option) {
305 interpCol =
SDDS_Realloc(interpCol,
sizeof(*interpCol) * (dataControls + 1));
306 funcOf =
SDDS_Realloc(funcOf,
sizeof(*funcOf) * (dataControls + 1));
307 interpCol[dataControls] = data_control[dataControls].interpCol;
308 funcOf[dataControls] = data_control[dataControls].funcOfCol;
309 dataControls++;
310 }
311 break;
312
313 default:
314 fprintf(stderr, "Error: Unknown or ambiguous option '%s'\n", s_arg[i_arg].list[0]);
315 exit(EXIT_FAILURE);
316 break;
317 }
318 } else {
319 if (!input)
320 input = s_arg[i_arg].list[0];
321 else if (!output)
322 output = s_arg[i_arg].list[0];
323 else
324 SDDS_Bomb(
"Too many filenames provided.");
325 }
326 }
327
329
332
333 for (i = 0; i < dataControls; i++) {
335 fprintf(stderr, "Warning: Column '%s' does not exist in input file '%s'.\n",
336 data_control[i].fileColumn, input);
337 continue;
338 }
340 fprintf(stderr, "Error: Column '%s' in input file '%s' is not a string column.\n",
341 data_control[i].fileColumn, input);
342 continue;
343 }
344 if (data_control[i].atCol) {
346 fprintf(stderr, "Warning: Column '%s' does not exist in input file '%s'.\n",
347 data_control[i].atCol, input);
348 continue;
349 }
351 fprintf(stderr, "Error: Column '%s' in input file '%s' is not a numeric column.\n",
352 data_control[i].atCol, input);
353 continue;
354 }
355 }
356 data_control[i].hasdata = 1;
357 valid_data++;
358 }
359
360 if (!valid_data) {
361 fprintf(stderr, "Error: No valid -data options provided for processing.\n");
362 exit(EXIT_FAILURE);
363 }
364
367
368 if (columnMajorOrder != -1)
369 SDDSout.layout.data_mode.column_major = columnMajorOrder;
370 else
371 SDDSout.layout.data_mode.column_major = SDDSin.layout.data_mode.column_major;
372
375 out_depenValue =
SDDS_Realloc(out_depenValue,
sizeof(*out_depenValue) * (pages + 1));
376 rowFlag =
SDDS_Realloc(rowFlag,
sizeof(*rowFlag) * (pages + 1));
377
379 fprintf(stderr, "Error: No data found in input file '%s'.\n", input);
380 exit(EXIT_FAILURE);
381 }
382
383 rowFlag[pages] = (int32_t *)malloc(sizeof(**rowFlag) * rows[pages]);
384 out_depenValue[pages] = (double *)malloc(sizeof(**out_depenValue) * rows[pages]);
385
386 for (i = 0; i < rows[pages]; i++)
387 rowFlag[pages][i] = 1;
388
389 for (i = 0; i < dataControls; i++) {
390 if (data_control[i].hasdata) {
391 data_control[i].files = rows[pages];
392 if (!(data_control[i].file = (
char **)
SDDS_GetColumn(&SDDSin, data_control[i].fileColumn))) {
394 }
395
396 if (data_control[i].atCol) {
399 }
400 }
401
402 if (!data_control[i].atCol)
403 atValue = data_control[i].atValue;
404
405 for (j = 0; j < rows[pages]; j++) {
408
410 case SDDS_CHECK_OKAY:
411 if (j == (rows[pages] - 1)) {
412 if (!pages) {
415 }
416 }
417 break;
418 default:
419 fprintf(stderr, "Error: Column '%s' missing or invalid in file '%s'.\n",
420 data_control[i].interpCol, data_control[i].file[j]);
421 exit(EXIT_FAILURE);
422 break;
423 }
424
426 case SDDS_CHECK_OKAY:
427 if (j == (rows[pages] - 1)) {
428 if ((!pages) && !(data_control[i].atCol)) {
431 }
432 }
433 break;
434 default:
435 fprintf(stderr, "Error: Column '%s' missing or invalid in file '%s'.\n",
436 data_control[i].funcOfCol, data_control[i].file[j]);
437 exit(EXIT_FAILURE);
438 break;
439 }
440
443
445
448 }
451 }
452
455
456 if (!(monotonicity = checkMonotonicity(indepValue, datarows))) {
457 fprintf(stderr, "Error: Independent (%s) data in file '%s' is not monotonic.\n",
458 data_control[i].funcOfCol, data_control[i].file[j]);
459 exit(EXIT_FAILURE);
460 }
461
462 if (data_control[i].atCol)
463 atValue = data_control[i].colValue[j];
464
465 out_depenValue[pages][j] =
interpolate(depenValue, indepValue, datarows, atValue,
466 &belowRange, &aboveRange, order, &interpCode, monotonicity);
467
468 if (interpCode) {
469 if (interpCode & OUTRANGE_ABORT) {
470 fprintf(stderr, "Error: Value %e out of range for column '%s'.\n",
471 atValue, data_control[i].interpCol);
472 exit(EXIT_FAILURE);
473 }
474 if (interpCode & OUTRANGE_WARN)
475 fprintf(stderr, "Warning: Value %e out of range for column '%s'.\n",
476 atValue, data_control[i].interpCol);
477 if (interpCode & OUTRANGE_SKIP)
478 rowFlag[pages][j] = 0;
479 }
480
481 free(depenValue);
482 free(indepValue);
483 }
484 }
485
486 for (j = 0; j < data_control[i].files; j++)
487 free(data_control[i].file[j]);
488 free(data_control[i].file);
489 data_control[i].file = NULL;
490
491 if (data_control[i].colValue) {
492 free(data_control[i].colValue);
493 data_control[i].colValue = NULL;
494 }
495 }
496
497 if (!pages) {
500 }
501
502 if (!SDDS_StartTable(&SDDSout, rows[pages]))
504
507
510
511 for (i = 0; i < dataControls; i++) {
512 if (data_control[i].hasdata) {
514 rows[pages], data_control[i].interpCol)) {
516 }
517 if (!(data_control[i].atCol)) {
518 for (row = 0; row < rows[pages]; row++) {
520 data_control[i].funcOfCol, data_control[i].atValue, NULL)) {
522 }
523 }
524 }
525 }
526 }
527
530
533
534 pages++;
535 }
536
539
542 exit(EXIT_FAILURE);
543 }
544
545 freedatacontrol(data_control, dataControls);
546
547 if (out_depenValue) {
548 for (i = 0; i < pages; i++) {
549 free(out_depenValue[i]);
550 free(rowFlag[i]);
551 }
552 free(out_depenValue);
553 free(rowFlag);
554 }
555
556 if (interpCol)
557 free(interpCol);
558 if (funcOf)
559 free(funcOf);
560 if (rows)
561 free(rows);
562
564 return EXIT_SUCCESS;
565}
int32_t SDDS_CopyColumns(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
int32_t SDDS_CopyParameters(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
int32_t SDDS_InitializeCopy(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source, char *filename, char *filemode)
int32_t SDDS_SetRowValues(SDDS_DATASET *SDDS_dataset, int32_t mode, int64_t row,...)
int32_t SDDS_SetColumnFromDoubles(SDDS_DATASET *SDDS_dataset, int32_t mode, double *data, int64_t rows,...)
Sets the values for a single data column using double-precision floating-point numbers.
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.
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_GetColumnIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named column in the SDDS dataset.
int32_t SDDS_CheckColumn(SDDS_DATASET *SDDS_dataset, char *name, char *units, int32_t type, FILE *fp_message)
Checks if a column exists in the SDDS dataset with the specified name, units, and type.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
void SDDS_RegisterProgramName(const char *name)
Registers the executable program name for use in error messages.
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.
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
void * SDDS_Realloc(void *old_ptr, size_t new_size)
Reallocates memory to a new size.
#define SDDS_STRING
Identifier for the string data type.
#define SDDS_ANY_NUMERIC_TYPE
Special identifier used by SDDS_Check*() routines to accept any numeric type.
#define SDDS_DOUBLE
Identifier for the double data type.
#define SDDS_NUMERIC_TYPE(type)
Checks if the given type identifier corresponds to any numeric type.
long bitsSet(unsigned long data)
Counts the number of set bits (1s) in the given data.
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 processPipeOption(char **item, long items, unsigned long *flags)
void processFilenames(char *programName, char **input, char **output, unsigned long pipeFlags, long noWarnings, long *tmpOutputUsed)
void free_scanargs(SCANNED_ARG **scanned, int argc)
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.