64 CLO_COMBINEDUPLICATES,
78char *option[N_OPTIONS] = {
99 "Usage: sddsinterp [<inputfile>] [<outputfile>]\n"
101 " -pipe=[input][,output] Use pipe for input and/or output.\n"
102 " -columns=<independent>,<dependent1>[,...] Specify the independent and dependent columns.\n"
103 " -exclude=<name>[,...] Exclude specified columns from processing.\n"
104 " -atValues=<values-list> Interpolate at the specified list of values.\n"
105 " -sequence=<points>[,<start>,<end>] Generate a sequence of interpolation points.\n"
106 " -equispaced=<spacing>[,<start>,<end>] Generate equispaced interpolation points.\n"
107 " -fillIn Automatically fill in interpolation points based on data.\n"
108 " -fileValues=<valuesfile>[,column=<name>][,parallelPages]\n"
109 " Use values from a file for interpolation.\n"
110 " -interpShort=-1|-2 Interpolate short columns with order -1 or -2.\n"
111 " Order -1 inherits value from the previous point;\n"
112 " Order -2 inherits value from the next point.\n"
113 " -order=<number> Set the interpolation order (default is 1).\n"
114 " -printout[=bare][,stdout] Output interpolated data in bare format and/or to stdout.\n"
115 " -forceMonotonic[={increasing|decreasing}] Enforce monotonicity in the data.\n"
116 " -belowRange={value=<v>|skip|saturate|extrapolate|wrap}[,{abort|warn}]\n"
117 " Handle values below the interpolation range.\n"
118 " -aboveRange={value=<v>|skip|saturate|extrapolate|wrap}[,{abort|warn}]\n"
119 " Handle values above the interpolation range.\n"
120 " -majorOrder=row|column Set the major order of data storage.\n\n"
122 " sddsinterp input.sdds output.sdds -columns=energy,flux -atValues=1.0,2.0,3.0\n\n"
123 " Program by Michael Borland. (" __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
")\n";
125double *makeSequence(int64_t *points,
double start,
double end,
double spacing,
double *data, int64_t dataRows);
126double *makeFillInSequence(
double *x, int64_t dataRows, int64_t *nPointsReturn);
127long checkMonotonicity(
double *indepValue, int64_t rows);
128int64_t forceMonotonicity(
double *indepValue,
double **y,
long columns, int64_t rows,
unsigned long direction);
129int64_t combineDuplicatePoints(
double *x,
double **y,
long columns, int64_t rows,
double tolerance);
131#define FILEVALUES_PARALLEL_PAGES 0x0001U
133#define NORMAL_PRINTOUT 1
134#define BARE_PRINTOUT 2
135#define STDOUT_PRINTOUT 4
137#define FORCE_MONOTONIC 0x0001U
138#define FORCE_INCREASING 0x0002U
139#define FORCE_DECREASING 0x0004U
141int main(
int argc,
char **argv) {
143 char *indepQuantity, **depenQuantity, *fileValuesQuantity, *fileValuesFile, **exclude;
144 long depenQuantities, monotonicity, excludes;
145 char *input, *output;
146 long i, readCode, order, valuesReadCode, fillIn;
147 int64_t rows, row, interpPoints, j;
148 long sequencePoints, combineDuplicates, branch;
150 double sequenceStart, sequenceEnd;
151 double sequenceSpacing;
152 unsigned long flags, interpCode, printFlags, forceMonotonic;
153 SCANNED_ARG *scanned;
155 OUTRANGE_CONTROL aboveRange, belowRange;
157 long atValues, doNotRead, parallelPages;
158 double *indepValue, **depenValue, *interpPoint, **outputData;
159 unsigned long pipeFlags, majorOrderFlag;
161 short interpShort = 0, interpShortOrder = -1, *shortValue = NULL, columnMajorOrder = -1;
165 argc =
scanargs(&scanned, argc, argv);
166 if (argc < 3 || argc > (3 + N_OPTIONS))
170 atValues = fillIn = 0;
171 output = input = NULL;
172 combineDuplicates = branch = sequencePoints = parallelPages = 0;
173 indepQuantity = NULL;
174 depenQuantity = exclude = NULL;
175 depenQuantities = excludes = 0;
176 aboveRange.flags = belowRange.flags = OUTRANGE_SATURATE;
178 readCode = interpPoints = 0;
179 fileValuesFile = fileValuesQuantity = NULL;
180 sequenceStart = sequenceEnd = sequenceSpacing = 0;
181 printFlags = pipeFlags = 0;
183 indepValue = interpPoint = NULL;
184 depenValue = outputData = NULL;
186 for (iArg = 1; iArg < argc; iArg++) {
187 if (scanned[iArg].arg_type == OPTION) {
189 switch (
match_string(scanned[iArg].list[0], option, N_OPTIONS, 0)) {
190 case CLO_MAJOR_ORDER:
192 scanned[iArg].n_items--;
193 if (scanned[iArg].n_items > 0 &&
194 (!
scanItemList(&majorOrderFlag, scanned[iArg].list + 1, &scanned[iArg].n_items, 0,
195 "row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER,
196 "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER, NULL)))
197 SDDS_Bomb(
"invalid -majorOrder syntax/values");
198 if (majorOrderFlag & SDDS_COLUMN_MAJOR_ORDER)
199 columnMajorOrder = 1;
200 else if (majorOrderFlag & SDDS_ROW_MAJOR_ORDER)
201 columnMajorOrder = 0;
204 if (scanned[iArg].n_items != 2 ||
205 sscanf(scanned[iArg].list[1],
"%ld", &order) != 1 || order < 0)
206 SDDS_Bomb(
"invalid -order syntax/value");
209 if (scanned[iArg].n_items < 2)
213 atValue =
tmalloc(
sizeof(*atValue) * (atValues = scanned[iArg].n_items - 1));
214 for (i = 0; i < atValues; i++)
215 if (sscanf(scanned[iArg].list[i + 1],
"%lf", &atValue[i]) != 1)
218 case CLO_INTERP_SHORT:
219 if (scanned[iArg].n_items == 2) {
220 if (sscanf(scanned[iArg].list[1],
"%hd", &interpShortOrder) != 1 ||
221 (interpShortOrder != -1 && interpShortOrder != -2))
222 SDDS_Bomb(
"invalid -interpShort value; must be -1 or -2");
227 if ((scanned[iArg].n_items != 2 && scanned[iArg].n_items != 4) ||
228 sscanf(scanned[iArg].list[1],
"%ld", &sequencePoints) != 1 ||
230 SDDS_Bomb(
"invalid -sequence syntax/value");
231 if (scanned[iArg].n_items == 4 &&
232 (sscanf(scanned[iArg].list[2],
"%lf", &sequenceStart) != 1 ||
233 sscanf(scanned[iArg].list[3],
"%lf", &sequenceEnd) != 1))
234 SDDS_Bomb(
"invalid -sequence syntax/value");
236 SDDS_Bomb(
"give only one of -sequence and -equispaced");
239 if ((scanned[iArg].n_items != 2 && scanned[iArg].n_items != 4) ||
240 sscanf(scanned[iArg].list[1],
"%lf", &sequenceSpacing) != 1 ||
241 sequenceSpacing <= 0)
242 SDDS_Bomb(
"invalid -equispaced syntax/value");
243 if (scanned[iArg].n_items == 4 &&
244 (sscanf(scanned[iArg].list[2],
"%lf", &sequenceStart) != 1 ||
245 sscanf(scanned[iArg].list[3],
"%lf", &sequenceEnd) != 1))
246 SDDS_Bomb(
"invalid -equispaced syntax/values");
248 SDDS_Bomb(
"give only one of -sequence and -equispaced");
252 SDDS_Bomb(
"only one -columns option may be given");
253 if (scanned[iArg].n_items < 2)
255 indepQuantity = scanned[iArg].list[1];
256 if (scanned[iArg].n_items >= 2) {
257 depenQuantity =
tmalloc(
sizeof(*depenQuantity) * (depenQuantities = scanned[iArg].n_items - 2));
258 for (i = 0; i < depenQuantities; i++)
259 depenQuantity[i] = scanned[iArg].list[i + 2];
263 if ((scanned[iArg].n_items -= 1) >= 1) {
264 if (!
scanItemList(&printFlags, scanned[iArg].list + 1, &scanned[iArg].n_items, 0,
265 "bare", -1, NULL, 0, BARE_PRINTOUT,
266 "stdout", -1, NULL, 0, STDOUT_PRINTOUT, NULL))
269 if (!(printFlags & BARE_PRINTOUT))
270 printFlags |= NORMAL_PRINTOUT;
273 if (scanned[iArg].n_items < 2)
275 fileValuesFile = scanned[iArg].list[1];
276 scanned[iArg].n_items -= 2;
277 if (!
scanItemList(&flags, scanned[iArg].list + 2, &scanned[iArg].n_items, 0,
279 "parallelpages", -1, NULL, 0, FILEVALUES_PARALLEL_PAGES, NULL))
281 if (flags & FILEVALUES_PARALLEL_PAGES)
284 case CLO_COMBINEDUPLICATES:
285 SDDS_Bomb(
"-combineDuplicates option not implemented yet--send email to borland@aps.anl.gov");
286 combineDuplicates = 1;
289 SDDS_Bomb(
"-branch option not implemented yet--send email to borland@aps.anl.gov");
290 if (scanned[iArg].n_items != 2 || sscanf(scanned[iArg].list[1],
"%ld", &branch) != 1 || branch < 1)
291 SDDS_Bomb(
"invalid -branch syntax/value");
294 if ((scanned[iArg].n_items -= 1) < 1 ||
295 !
scanItemList(&belowRange.flags, scanned[iArg].list + 1, &scanned[iArg].n_items, 0,
296 "value",
SDDS_DOUBLE, &belowRange.value, 1, OUTRANGE_VALUE,
297 "skip", -1, NULL, 0, OUTRANGE_SKIP,
298 "saturate", -1, NULL, 0, OUTRANGE_SATURATE,
299 "extrapolate", -1, NULL, 0, OUTRANGE_EXTRAPOLATE,
300 "wrap", -1, NULL, 0, OUTRANGE_WRAP,
301 "abort", -1, NULL, 0, OUTRANGE_ABORT,
302 "warn", -1, NULL, 0, OUTRANGE_WARN, NULL))
303 SDDS_Bomb(
"invalid -belowRange syntax/value");
304 if ((i =
bitsSet(belowRange.flags & (OUTRANGE_VALUE | OUTRANGE_SKIP | OUTRANGE_SATURATE | OUTRANGE_EXTRAPOLATE | OUTRANGE_WRAP | OUTRANGE_ABORT))) > 1)
305 SDDS_Bomb(
"incompatible keywords given for -belowRange");
307 belowRange.flags |= OUTRANGE_SATURATE;
310 if ((scanned[iArg].n_items -= 1) < 1 ||
311 !
scanItemList(&aboveRange.flags, scanned[iArg].list + 1, &scanned[iArg].n_items, 0,
312 "value",
SDDS_DOUBLE, &aboveRange.value, 1, OUTRANGE_VALUE,
313 "skip", -1, NULL, 0, OUTRANGE_SKIP,
314 "saturate", -1, NULL, 0, OUTRANGE_SATURATE,
315 "extrapolate", -1, NULL, 0, OUTRANGE_EXTRAPOLATE,
316 "wrap", -1, NULL, 0, OUTRANGE_WRAP,
317 "abort", -1, NULL, 0, OUTRANGE_ABORT,
318 "warn", -1, NULL, 0, OUTRANGE_WARN, NULL))
319 SDDS_Bomb(
"invalid -aboveRange syntax/value");
320 if ((i =
bitsSet(aboveRange.flags & (OUTRANGE_VALUE | OUTRANGE_SKIP | OUTRANGE_SATURATE | OUTRANGE_EXTRAPOLATE | OUTRANGE_WRAP | OUTRANGE_ABORT))) > 1)
321 SDDS_Bomb(
"incompatible keywords given for -aboveRange");
323 aboveRange.flags |= OUTRANGE_SATURATE;
326 if (!
processPipeOption(scanned[iArg].list + 1, scanned[iArg].n_items - 1, &pipeFlags))
330 if (scanned[iArg].n_items < 2)
332 moveToStringArray(&exclude, &excludes, scanned[iArg].list + 1, scanned[iArg].n_items - 1);
334 case CLO_FORCEMONOTONIC:
335 if ((scanned[iArg].n_items -= 1) > 0) {
336 if (!
scanItemList(&forceMonotonic, scanned[iArg].list + 1, &scanned[iArg].n_items, 0,
337 "increasing", -1, NULL, 0, FORCE_INCREASING,
338 "decreasing", -1, NULL, 0, FORCE_DECREASING, NULL) ||
340 SDDS_Bomb(
"invalid -forceMonotonic syntax/value");
342 forceMonotonic = FORCE_MONOTONIC;
348 fprintf(stderr,
"error: unknown/ambiguous option: %s\n", scanned[iArg].list[0]);
354 input = scanned[iArg].list[0];
356 output = scanned[iArg].list[0];
365 if (printFlags & STDOUT_PRINTOUT)
369 SDDS_Bomb(
"supply the independent quantity name with the -columns option");
371 if ((atValues ? 1 : 0) + (fileValuesFile ? 1 : 0) + (sequencePoints ? 1 : 0) + fillIn + (sequenceSpacing > 0 ? 1 : 0) != 1)
372 SDDS_Bomb(
"you must give one and only one of -atValues, -fileValues, -sequence, -equispaced, and -fillIn");
377 excludes = appendToStringArray(&exclude, excludes, indepQuantity);
378 if (!depenQuantities)
379 depenQuantities = appendToStringArray(&depenQuantity, depenQuantities,
"*");
381 if ((depenQuantities = expandColumnPairNames(&SDDSin, &depenQuantity, NULL, depenQuantities, exclude, excludes, FIND_NUMERIC_TYPE, 0)) <= 0) {
383 SDDS_Bomb(
"no dependent quantities selected for interpolation");
390 if (columnMajorOrder != -1)
391 SDDSout.layout.data_mode.column_major = columnMajorOrder;
393 SDDSout.layout.data_mode.column_major = SDDSin.layout.data_mode.column_major;
398 "Page of interpolation data file used to create this page", NULL,
SDDS_LONG, NULL) < 0 ||
399 SDDS_DefineParameter(&SDDSout,
"InterpPointsPage", NULL, NULL,
"Page of interpolation points file used to create this page", NULL,
SDDS_LONG, NULL) < 0)
401 for (i = 0; i < depenQuantities; i++)
403 fprintf(stderr,
"problem creating interpolated-output column %s\n", depenQuantity[i]);
412 outputData =
tmalloc(
sizeof(*outputData) * (depenQuantities));
413 depenValue =
tmalloc(
sizeof(*depenValue) * (depenQuantities));
417 while (doNotRead || (readCode =
SDDS_ReadPage(&SDDSin)) > 0) {
422 interpPoint = atValue;
423 interpPoints = atValues;
424 }
else if (fileValuesFile) {
429 else if (valuesReadCode == -1) {
431 fprintf(stderr,
"warning: file %s ends before file %s\n", fileValuesFile, input);
439 fprintf(stderr,
"error: unable to (re)read file %s\n", fileValuesFile);
463 }
else if (sequencePoints || sequenceSpacing) {
466 interpPoints = sequencePoints;
467 if (!(interpPoint = makeSequence(&interpPoints, sequenceStart, sequenceEnd, sequenceSpacing, indepValue, rows)))
473 if (!(interpPoint = makeFillInSequence(indepValue, rows, &interpPoints)))
477 for (i = 0; i < depenQuantities; i++)
478 outputData[i] =
tmalloc(
sizeof(*outputData[i]) * interpPoints);
479 rowFlag =
trealloc(rowFlag,
sizeof(*rowFlag) * interpPoints);
480 for (j = 0; j < interpPoints; j++)
482 for (i = 0; i < depenQuantities; i++) {
487 rows = forceMonotonicity(indepValue, depenValue, depenQuantities, rows, forceMonotonic);
488 else if (combineDuplicates)
489 rows = combineDuplicatePoints(indepValue, depenValue, depenQuantities, rows, 0.0);
490 if ((monotonicity = checkMonotonicity(indepValue, rows)) == 0)
491 SDDS_Bomb(
"independent data values do not change monotonically or repeated independent values exist");
493 shortValue =
tmalloc(
sizeof(*shortValue) * rows);
495 for (i = 0; i < depenQuantities; i++) {
497 for (row = 0; row < rows; row++) {
498 shortValue[row] = (short)depenValue[i][row];
501 for (j = 0; j < interpPoints; j++) {
503 outputData[i][j] =
interpolate(depenValue[i], indepValue, rows, interpPoint[j], &belowRange, &aboveRange, order, &interpCode, monotonicity);
505 outputData[i][j] = (double)
interp_short(shortValue, indepValue, rows, interpPoint[j], 0, interpShortOrder, &interpCode, &nextPos);
508 if (interpCode & OUTRANGE_ABORT) {
509 fprintf(stderr,
"error: value %e is out of range for column %s\n", interpPoint[j], depenQuantity[i]);
512 if (interpCode & OUTRANGE_WARN)
513 fprintf(stderr,
"warning: value %e is out of range for column %s\n", interpPoint[j], depenQuantity[i]);
514 if (interpCode & OUTRANGE_SKIP)
524 if (!
SDDS_SetParameters(&SDDSout, SDDS_BY_NAME | SDDS_PASS_BY_VALUE,
"InterpDataPage", readCode,
"InterpPointsPage", valuesReadCode, NULL) ||
527 for (i = 0; i < depenQuantities; i++)
532 if (printFlags & BARE_PRINTOUT) {
533 for (j = 0; j < interpPoints; j++)
535 fprintf(fpPrint,
"%21.15e ", interpPoint[j]);
536 for (i = 0; i < depenQuantities; i++)
537 fprintf(fpPrint,
"%21.15e ", outputData[i][j]);
538 fputc(
'\n', fpPrint);
540 }
else if (printFlags & NORMAL_PRINTOUT) {
541 for (j = 0; j < interpPoints; j++)
543 fprintf(fpPrint,
"%s=%21.15e ", indepQuantity, interpPoint[j]);
544 for (i = 0; i < depenQuantities; i++)
545 fprintf(fpPrint,
"%s=%21.15e ", depenQuantity[i], outputData[i][j]);
546 fputc(
'\n', fpPrint);
552 for (i = 0; i < depenQuantities; i++) {
555 outputData[i] = NULL;
558 depenValue[i] = NULL;
560 if (fileValuesFile) {
578 if (fileValuesFile) {
588double *makeFillInSequence(
double *x, int64_t dataRows, int64_t *nPointsReturn) {
590 double dxMin, dx, xMin, xMax;
594 for (i = 1; i < dataRows; i++) {
595 if ((dx = fabs(x[i] - x[i - 1])) < dxMin && dx > 0)
602 *nPointsReturn = (int64_t)((xMax - xMin) / dxMin + 1);
603 return makeSequence(nPointsReturn, xMin, xMax, 0.0, x, dataRows);
606double *makeSequence(int64_t *points,
double start,
double end,
double spacing,
double *data, int64_t dataRows) {
608 double delta, *sequence;
614 delta = (end - start) / (*points - 1);
615 }
else if (spacing >= 0) {
617 *points = (int64_t)((end - start) / delta + 1.5);
622 sequence =
tmalloc(
sizeof(*sequence) * (*points));
623 for (i = 0; i < *points; i++)
624 sequence[i] = start + delta * i;
628long checkMonotonicity(
double *indepValue, int64_t rows) {
631 if (indepValue[rows - 1] > indepValue[0]) {
633 if (indepValue[rows] <= indepValue[rows - 1])
638 if (indepValue[rows] >= indepValue[rows - 1])
644int64_t combineDuplicatePoints(
double *x,
double **y,
long columns, int64_t rows,
double tolerance) {
651 SDDS_Bomb(
"interpolation data is invalid--no range in independent variable");
652 tolerance *= xmax - xmin;
653 for (i = 1; i < rows; i++) {
654 if (fabs(x[i] - x[i - 1]) <= tolerance) {
655 x[i - 1] = (x[i] + x[i - 1]) / 2;
656 for (column = 0; column < columns; column++)
657 y[column][i - 1] = (y[column][i] + y[column][i - 1]) / 2;
658 for (j = i + 1; j < rows; j++) {
660 for (column = 0; column < columns; column++)
661 y[column][j - 1] = y[column][j];
670int64_t forceMonotonicity(
double *x,
double **y,
long columns, int64_t rows,
unsigned long mode) {
672 long column, direction;
679 if (mode & FORCE_INCREASING)
681 else if (mode & FORCE_DECREASING)
684 direction = x[1] > x[0] ? 1 : -1;
686 keep =
tmalloc(
sizeof(*keep) * rows);
689 for (i = 1; i < rows; i++) {
690 if (direction * (x[i] - reference) > 0) {
697 for (i = j = 1; i < rows; i++) {
700 for (column = 0; column < columns; column++)
701 y[column][j] = y[column][i];
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
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.
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
#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.
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
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.
int find_min_max(double *min, double *max, double *list, int64_t n)
Finds the minimum and maximum values in a list of doubles.
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.