175 {
176 int iArg;
177 char *indepQuantity, **depenQuantity, *fileValuesQuantity, *fileValuesFile, **exclude;
178 long depenQuantities, monotonicity, excludes;
179 char *input, *output;
180 long i, readCode, order, valuesReadCode, fillIn;
181 int64_t rows, row, interpPoints, j;
182 long sequencePoints, combineDuplicates, branch;
183 int32_t *rowFlag;
184 double sequenceStart, sequenceEnd;
185 double sequenceSpacing;
186 unsigned long flags, interpCode, printFlags, forceMonotonic;
187 SCANNED_ARG *scanned;
189 OUTRANGE_CONTROL aboveRange, belowRange;
190 double *atValue;
191 long atValues, doNotRead, parallelPages;
192 double *indepValue, **depenValue, *interpPoint, **outputData;
193 unsigned long pipeFlags, majorOrderFlag;
194 FILE *fpPrint;
195 short interpShort = 0, interpShortOrder = -1, *shortValue = NULL, columnMajorOrder = -1;
196 long nextPos;
197
199 argc =
scanargs(&scanned, argc, argv);
200 if (argc < 3 || argc > (3 + N_OPTIONS))
202
203 atValue = NULL;
204 atValues = fillIn = 0;
205 output = input = NULL;
206 combineDuplicates = branch = sequencePoints = parallelPages = 0;
207 indepQuantity = NULL;
208 depenQuantity = exclude = NULL;
209 depenQuantities = excludes = 0;
210 aboveRange.flags = belowRange.flags = OUTRANGE_SATURATE;
211 order = 1;
212 readCode = interpPoints = 0;
213 fileValuesFile = fileValuesQuantity = NULL;
214 sequenceStart = sequenceEnd = sequenceSpacing = 0;
215 printFlags = pipeFlags = 0;
216 forceMonotonic = 0;
217 indepValue = interpPoint = NULL;
218 depenValue = outputData = NULL;
219
220 for (iArg = 1; iArg < argc; iArg++) {
221 if (scanned[iArg].arg_type == OPTION) {
222
223 switch (
match_string(scanned[iArg].list[0], option, N_OPTIONS, 0)) {
224 case CLO_MAJOR_ORDER:
225 majorOrderFlag = 0;
226 scanned[iArg].n_items--;
227 if (scanned[iArg].n_items > 0 &&
228 (!
scanItemList(&majorOrderFlag, scanned[iArg].list + 1, &scanned[iArg].n_items, 0,
229 "row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER,
230 "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER, NULL)))
231 SDDS_Bomb(
"invalid -majorOrder syntax/values");
232 if (majorOrderFlag & SDDS_COLUMN_MAJOR_ORDER)
233 columnMajorOrder = 1;
234 else if (majorOrderFlag & SDDS_ROW_MAJOR_ORDER)
235 columnMajorOrder = 0;
236 break;
237 case CLO_ORDER:
238 if (scanned[iArg].n_items != 2 ||
239 sscanf(scanned[iArg].list[1], "%ld", &order) != 1 || order < 0)
240 SDDS_Bomb(
"invalid -order syntax/value");
241 break;
242 case CLO_ATVALUES:
243 if (scanned[iArg].n_items < 2)
245 if (atValue)
247 atValue =
tmalloc(
sizeof(*atValue) * (atValues = scanned[iArg].n_items - 1));
248 for (i = 0; i < atValues; i++)
249 if (sscanf(scanned[iArg].list[i + 1], "%lf", &atValue[i]) != 1)
251 break;
252 case CLO_INTERP_SHORT:
253 if (scanned[iArg].n_items == 2) {
254 if (sscanf(scanned[iArg].list[1], "%hd", &interpShortOrder) != 1 ||
255 (interpShortOrder != -1 && interpShortOrder != -2))
256 SDDS_Bomb(
"invalid -interpShort value; must be -1 or -2");
257 }
258 interpShort = 1;
259 break;
260 case CLO_SEQUENCE:
261 if ((scanned[iArg].n_items != 2 && scanned[iArg].n_items != 4) ||
262 sscanf(scanned[iArg].list[1], "%ld", &sequencePoints) != 1 ||
263 sequencePoints < 2)
264 SDDS_Bomb(
"invalid -sequence syntax/value");
265 if (scanned[iArg].n_items == 4 &&
266 (sscanf(scanned[iArg].list[2], "%lf", &sequenceStart) != 1 ||
267 sscanf(scanned[iArg].list[3], "%lf", &sequenceEnd) != 1))
268 SDDS_Bomb(
"invalid -sequence syntax/value");
269 if (sequenceSpacing)
270 SDDS_Bomb(
"give only one of -sequence and -equispaced");
271 break;
272 case CLO_EQUISPACED:
273 if ((scanned[iArg].n_items != 2 && scanned[iArg].n_items != 4) ||
274 sscanf(scanned[iArg].list[1], "%lf", &sequenceSpacing) != 1 ||
275 sequenceSpacing <= 0)
276 SDDS_Bomb(
"invalid -equispaced syntax/value");
277 if (scanned[iArg].n_items == 4 &&
278 (sscanf(scanned[iArg].list[2], "%lf", &sequenceStart) != 1 ||
279 sscanf(scanned[iArg].list[3], "%lf", &sequenceEnd) != 1))
280 SDDS_Bomb(
"invalid -equispaced syntax/values");
281 if (sequencePoints)
282 SDDS_Bomb(
"give only one of -sequence and -equispaced");
283 break;
284 case CLO_COLUMNS:
285 if (indepQuantity)
286 SDDS_Bomb(
"only one -columns option may be given");
287 if (scanned[iArg].n_items < 2)
289 indepQuantity = scanned[iArg].list[1];
290 if (scanned[iArg].n_items >= 2) {
291 depenQuantity =
tmalloc(
sizeof(*depenQuantity) * (depenQuantities = scanned[iArg].n_items - 2));
292 for (i = 0; i < depenQuantities; i++)
293 depenQuantity[i] = scanned[iArg].list[i + 2];
294 }
295 break;
296 case CLO_PRINTOUT:
297 if ((scanned[iArg].n_items -= 1) >= 1) {
298 if (!
scanItemList(&printFlags, scanned[iArg].list + 1, &scanned[iArg].n_items, 0,
299 "bare", -1, NULL, 0, BARE_PRINTOUT,
300 "stdout", -1, NULL, 0, STDOUT_PRINTOUT, NULL))
302 }
303 if (!(printFlags & BARE_PRINTOUT))
304 printFlags |= NORMAL_PRINTOUT;
305 break;
306 case CLO_FILEVALUES:
307 if (scanned[iArg].n_items < 2)
309 fileValuesFile = scanned[iArg].list[1];
310 scanned[iArg].n_items -= 2;
311 if (!
scanItemList(&flags, scanned[iArg].list + 2, &scanned[iArg].n_items, 0,
313 "parallelpages", -1, NULL, 0, FILEVALUES_PARALLEL_PAGES, NULL))
315 if (flags & FILEVALUES_PARALLEL_PAGES)
316 parallelPages = 1;
317 break;
318 case CLO_COMBINEDUPLICATES:
319 SDDS_Bomb(
"-combineDuplicates option not implemented yet--send email to borland@aps.anl.gov");
320 combineDuplicates = 1;
321 break;
322 case CLO_BRANCH:
323 SDDS_Bomb(
"-branch option not implemented yet--send email to borland@aps.anl.gov");
324 if (scanned[iArg].n_items != 2 || sscanf(scanned[iArg].list[1], "%ld", &branch) != 1 || branch < 1)
325 SDDS_Bomb(
"invalid -branch syntax/value");
326 break;
327 case CLO_BELOWRANGE:
328 if ((scanned[iArg].n_items -= 1) < 1 ||
329 !
scanItemList(&belowRange.flags, scanned[iArg].list + 1, &scanned[iArg].n_items, 0,
330 "value",
SDDS_DOUBLE, &belowRange.value, 1, OUTRANGE_VALUE,
331 "skip", -1, NULL, 0, OUTRANGE_SKIP,
332 "saturate", -1, NULL, 0, OUTRANGE_SATURATE,
333 "extrapolate", -1, NULL, 0, OUTRANGE_EXTRAPOLATE,
334 "wrap", -1, NULL, 0, OUTRANGE_WRAP,
335 "abort", -1, NULL, 0, OUTRANGE_ABORT,
336 "warn", -1, NULL, 0, OUTRANGE_WARN, NULL))
337 SDDS_Bomb(
"invalid -belowRange syntax/value");
338 if ((i =
bitsSet(belowRange.flags & (OUTRANGE_VALUE | OUTRANGE_SKIP | OUTRANGE_SATURATE | OUTRANGE_EXTRAPOLATE | OUTRANGE_WRAP | OUTRANGE_ABORT))) > 1)
339 SDDS_Bomb(
"incompatible keywords given for -belowRange");
340 if (i != 1)
341 belowRange.flags |= OUTRANGE_SATURATE;
342 break;
343 case CLO_ABOVERANGE:
344 if ((scanned[iArg].n_items -= 1) < 1 ||
345 !
scanItemList(&aboveRange.flags, scanned[iArg].list + 1, &scanned[iArg].n_items, 0,
346 "value",
SDDS_DOUBLE, &aboveRange.value, 1, OUTRANGE_VALUE,
347 "skip", -1, NULL, 0, OUTRANGE_SKIP,
348 "saturate", -1, NULL, 0, OUTRANGE_SATURATE,
349 "extrapolate", -1, NULL, 0, OUTRANGE_EXTRAPOLATE,
350 "wrap", -1, NULL, 0, OUTRANGE_WRAP,
351 "abort", -1, NULL, 0, OUTRANGE_ABORT,
352 "warn", -1, NULL, 0, OUTRANGE_WARN, NULL))
353 SDDS_Bomb(
"invalid -aboveRange syntax/value");
354 if ((i =
bitsSet(aboveRange.flags & (OUTRANGE_VALUE | OUTRANGE_SKIP | OUTRANGE_SATURATE | OUTRANGE_EXTRAPOLATE | OUTRANGE_WRAP | OUTRANGE_ABORT))) > 1)
355 SDDS_Bomb(
"incompatible keywords given for -aboveRange");
356 if (i != 1)
357 aboveRange.flags |= OUTRANGE_SATURATE;
358 break;
359 case CLO_PIPE:
360 if (!
processPipeOption(scanned[iArg].list + 1, scanned[iArg].n_items - 1, &pipeFlags))
362 break;
363 case CLO_EXCLUDE:
364 if (scanned[iArg].n_items < 2)
366 moveToStringArray(&exclude, &excludes, scanned[iArg].list + 1, scanned[iArg].n_items - 1);
367 break;
368 case CLO_FORCEMONOTONIC:
369 if ((scanned[iArg].n_items -= 1) > 0) {
370 if (!
scanItemList(&forceMonotonic, scanned[iArg].list + 1, &scanned[iArg].n_items, 0,
371 "increasing", -1, NULL, 0, FORCE_INCREASING,
372 "decreasing", -1, NULL, 0, FORCE_DECREASING, NULL) ||
374 SDDS_Bomb(
"invalid -forceMonotonic syntax/value");
375 } else
376 forceMonotonic = FORCE_MONOTONIC;
377 break;
378 case CLO_FILLIN:
379 fillIn = 1;
380 break;
381 default:
382 fprintf(stderr, "error: unknown/ambiguous option: %s\n", scanned[iArg].list[0]);
383 exit(EXIT_FAILURE);
384 break;
385 }
386 } else {
387 if (!input)
388 input = scanned[iArg].list[0];
389 else if (!output)
390 output = scanned[iArg].list[0];
391 else
393 }
394 }
395
397
398 fpPrint = stderr;
399 if (printFlags & STDOUT_PRINTOUT)
400 fpPrint = stdout;
401
402 if (!indepQuantity)
403 SDDS_Bomb(
"supply the independent quantity name with the -columns option");
404
405 if ((atValues ? 1 : 0) + (fileValuesFile ? 1 : 0) + (sequencePoints ? 1 : 0) + fillIn + (sequenceSpacing > 0 ? 1 : 0) != 1)
406 SDDS_Bomb(
"you must give one and only one of -atValues, -fileValues, -sequence, -equispaced, and -fillIn");
407
410
411 excludes = appendToStringArray(&exclude, excludes, indepQuantity);
412 if (!depenQuantities)
413 depenQuantities = appendToStringArray(&depenQuantity, depenQuantities, "*");
414
415 if ((depenQuantities = expandColumnPairNames(&SDDSin, &depenQuantity, NULL, depenQuantities, exclude, excludes, FIND_NUMERIC_TYPE, 0)) <= 0) {
417 SDDS_Bomb(
"no dependent quantities selected for interpolation");
418 }
419
424 if (columnMajorOrder != -1)
425 SDDSout.layout.data_mode.column_major = columnMajorOrder;
426 else
427 SDDSout.layout.data_mode.column_major = SDDSin.layout.data_mode.column_major;
430
432 "Page of interpolation data file used to create this page", NULL,
SDDS_LONG, NULL) < 0 ||
433 SDDS_DefineParameter(&SDDSout,
"InterpPointsPage", NULL, NULL,
"Page of interpolation points file used to create this page", NULL,
SDDS_LONG, NULL) < 0)
435 for (i = 0; i < depenQuantities; i++)
437 fprintf(stderr, "problem creating interpolated-output column %s\n", depenQuantity[i]);
439 }
443
444 doNotRead = 0;
445 interpPoint = NULL;
446 outputData =
tmalloc(
sizeof(*outputData) * (depenQuantities));
447 depenValue =
tmalloc(
sizeof(*depenValue) * (depenQuantities));
448 rowFlag = NULL;
449 valuesReadCode = 0;
450
451 while (doNotRead || (readCode =
SDDS_ReadPage(&SDDSin)) > 0) {
455 if (atValues) {
456 interpPoint = atValue;
457 interpPoints = atValues;
458 } else if (fileValuesFile) {
459 if (interpPoint)
460 free(interpPoint);
463 else if (valuesReadCode == -1) {
464 if (parallelPages) {
465 fprintf(stderr, "warning: file %s ends before file %s\n", fileValuesFile, input);
466 break;
467 } else {
468
473 fprintf(stderr, "error: unable to (re)read file %s\n", fileValuesFile);
475 exit(EXIT_FAILURE);
476 }
477
479 if (readCode == -1)
480 break;
482 }
484 if (indepValue)
485 free(indepValue);
488 }
489 }
490
491 if (!parallelPages)
492 doNotRead = 1;
497 } else if (sequencePoints || sequenceSpacing) {
498 if (interpPoint)
499 free(interpPoint);
500 interpPoints = sequencePoints;
501 if (!(interpPoint = makeSequence(&interpPoints, sequenceStart, sequenceEnd, sequenceSpacing, indepValue, rows)))
502 exit(EXIT_FAILURE);
503 } else {
504
505 if (interpPoint)
506 free(interpPoint);
507 if (!(interpPoint = makeFillInSequence(indepValue, rows, &interpPoints)))
508 exit(EXIT_FAILURE);
509 }
510
511 for (i = 0; i < depenQuantities; i++)
512 outputData[i] =
tmalloc(
sizeof(*outputData[i]) * interpPoints);
513 rowFlag =
trealloc(rowFlag,
sizeof(*rowFlag) * interpPoints);
514 for (j = 0; j < interpPoints; j++)
515 rowFlag[j] = 1;
516 for (i = 0; i < depenQuantities; i++) {
519 }
520 if (forceMonotonic)
521 rows = forceMonotonicity(indepValue, depenValue, depenQuantities, rows, forceMonotonic);
522 else if (combineDuplicates)
523 rows = combineDuplicatePoints(indepValue, depenValue, depenQuantities, rows, 0.0);
524 if ((monotonicity = checkMonotonicity(indepValue, rows)) == 0)
525 SDDS_Bomb(
"independent data values do not change monotonically or repeated independent values exist");
526 if (interpShort)
527 shortValue =
tmalloc(
sizeof(*shortValue) * rows);
528
529 for (i = 0; i < depenQuantities; i++) {
530 if (interpShort) {
531 for (row = 0; row < rows; row++) {
532 shortValue[row] = (short)depenValue[i][row];
533 }
534 }
535 for (j = 0; j < interpPoints; j++) {
536 if (!interpShort) {
537 outputData[i][j] =
interpolate(depenValue[i], indepValue, rows, interpPoint[j], &belowRange, &aboveRange, order, &interpCode, monotonicity);
538 } else {
539 outputData[i][j] = (double)
interp_short(shortValue, indepValue, rows, interpPoint[j], 0, interpShortOrder, &interpCode, &nextPos);
540 }
541 if (interpCode) {
542 if (interpCode & OUTRANGE_ABORT) {
543 fprintf(stderr, "error: value %e is out of range for column %s\n", interpPoint[j], depenQuantity[i]);
544 exit(EXIT_FAILURE);
545 }
546 if (interpCode & OUTRANGE_WARN)
547 fprintf(stderr, "warning: value %e is out of range for column %s\n", interpPoint[j], depenQuantity[i]);
548 if (interpCode & OUTRANGE_SKIP)
549 rowFlag[j] = 0;
550 }
551 }
552 }
553 if (interpShort)
554 free(shortValue);
558 if (!
SDDS_SetParameters(&SDDSout, SDDS_BY_NAME | SDDS_PASS_BY_VALUE,
"InterpDataPage", readCode,
"InterpPointsPage", valuesReadCode, NULL) ||
561 for (i = 0; i < depenQuantities; i++)
566 if (printFlags & BARE_PRINTOUT) {
567 for (j = 0; j < interpPoints; j++)
568 if (rowFlag[j]) {
569 fprintf(fpPrint, "%21.15e ", interpPoint[j]);
570 for (i = 0; i < depenQuantities; i++)
571 fprintf(fpPrint, "%21.15e ", outputData[i][j]);
572 fputc('\n', fpPrint);
573 }
574 } else if (printFlags & NORMAL_PRINTOUT) {
575 for (j = 0; j < interpPoints; j++)
576 if (rowFlag[j]) {
577 fprintf(fpPrint, "%s=%21.15e ", indepQuantity, interpPoint[j]);
578 for (i = 0; i < depenQuantities; i++)
579 fprintf(fpPrint, "%s=%21.15e ", depenQuantity[i], outputData[i][j]);
580 fputc('\n', fpPrint);
581 }
582 }
583 if (indepValue)
584 free(indepValue);
585 indepValue = NULL;
586 for (i = 0; i < depenQuantities; i++) {
587 if (outputData[i])
588 free(outputData[i]);
589 outputData[i] = NULL;
590 if (depenValue[i])
591 free(depenValue[i]);
592 depenValue[i] = NULL;
593 }
594 if (fileValuesFile) {
595 if (interpPoint)
596 free(interpPoint);
597 interpPoint = NULL;
598 }
599 if (rowFlag)
600 free(rowFlag);
601 rowFlag = NULL;
602 }
603
606 exit(EXIT_FAILURE);
607 }
610 exit(EXIT_FAILURE);
611 }
612 if (fileValuesFile) {
615 exit(EXIT_FAILURE);
616 }
617 }
618
619 return EXIT_SUCCESS;
620}
int32_t SDDS_CopyParameters(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
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_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_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.
int32_t SDDS_DefineParameter(SDDS_DATASET *SDDS_dataset, const char *name, const char *symbol, const char *units, const char *description, const char *format_string, int32_t type, char *fixed_value)
Defines a data parameter with a fixed string value.
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_TransferAllParameterDefinitions(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source, uint32_t mode)
Transfers all parameter definitions from a source dataset to a target dataset.
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_NumberOfErrors()
Retrieves the number of errors recorded by SDDS library routines.
#define SDDS_STRING
Identifier for the string data type.
#define SDDS_LONG
Identifier for the signed 32-bit integer data type.
#define SDDS_DOUBLE
Identifier for the double data type.
void * trealloc(void *old_ptr, uint64_t size_of_block)
Reallocates a memory block to a new size.
long bitsSet(unsigned long data)
Counts the number of set bits (1s) in the given data.
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
short interp_short(short *f, double *x, int64_t n, double xo, long warnings, short order, unsigned long *returnCode, long *next_start_pos)
Performs interpolation for short integer data types.
double interpolate(double *f, double *x, int64_t n, double xo, OUTRANGE_CONTROL *belowRange, OUTRANGE_CONTROL *aboveRange, long order, unsigned long *returnCode, long M)
Performs interpolation with range control options.
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)
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.