141 {
142 int iArg;
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;
149 int32_t *rowFlag;
150 double sequenceStart, sequenceEnd;
151 double sequenceSpacing;
152 unsigned long flags, interpCode, printFlags, forceMonotonic;
153 SCANNED_ARG *scanned;
155 OUTRANGE_CONTROL aboveRange, belowRange;
156 double *atValue;
157 long atValues, doNotRead, parallelPages;
158 double *indepValue, **depenValue, *interpPoint, **outputData;
159 unsigned long pipeFlags, majorOrderFlag;
160 FILE *fpPrint;
161 short interpShort = 0, interpShortOrder = -1, *shortValue = NULL, columnMajorOrder = -1;
162 long nextPos;
163
165 argc =
scanargs(&scanned, argc, argv);
166 if (argc < 3 || argc > (3 + N_OPTIONS))
168
169 atValue = NULL;
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;
177 order = 1;
178 readCode = interpPoints = 0;
179 fileValuesFile = fileValuesQuantity = NULL;
180 sequenceStart = sequenceEnd = sequenceSpacing = 0;
181 printFlags = pipeFlags = 0;
182 forceMonotonic = 0;
183 indepValue = interpPoint = NULL;
184 depenValue = outputData = NULL;
185
186 for (iArg = 1; iArg < argc; iArg++) {
187 if (scanned[iArg].arg_type == OPTION) {
188
189 switch (
match_string(scanned[iArg].list[0], option, N_OPTIONS, 0)) {
190 case CLO_MAJOR_ORDER:
191 majorOrderFlag = 0;
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;
202 break;
203 case CLO_ORDER:
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");
207 break;
208 case CLO_ATVALUES:
209 if (scanned[iArg].n_items < 2)
211 if (atValue)
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)
217 break;
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");
223 }
224 interpShort = 1;
225 break;
226 case CLO_SEQUENCE:
227 if ((scanned[iArg].n_items != 2 && scanned[iArg].n_items != 4) ||
228 sscanf(scanned[iArg].list[1], "%ld", &sequencePoints) != 1 ||
229 sequencePoints < 2)
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");
235 if (sequenceSpacing)
236 SDDS_Bomb(
"give only one of -sequence and -equispaced");
237 break;
238 case CLO_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");
247 if (sequencePoints)
248 SDDS_Bomb(
"give only one of -sequence and -equispaced");
249 break;
250 case CLO_COLUMNS:
251 if (indepQuantity)
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];
260 }
261 break;
262 case CLO_PRINTOUT:
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))
268 }
269 if (!(printFlags & BARE_PRINTOUT))
270 printFlags |= NORMAL_PRINTOUT;
271 break;
272 case CLO_FILEVALUES:
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)
282 parallelPages = 1;
283 break;
284 case CLO_COMBINEDUPLICATES:
285 SDDS_Bomb(
"-combineDuplicates option not implemented yet--send email to borland@aps.anl.gov");
286 combineDuplicates = 1;
287 break;
288 case CLO_BRANCH:
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");
292 break;
293 case CLO_BELOWRANGE:
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");
306 if (i != 1)
307 belowRange.flags |= OUTRANGE_SATURATE;
308 break;
309 case CLO_ABOVERANGE:
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");
322 if (i != 1)
323 aboveRange.flags |= OUTRANGE_SATURATE;
324 break;
325 case CLO_PIPE:
326 if (!
processPipeOption(scanned[iArg].list + 1, scanned[iArg].n_items - 1, &pipeFlags))
328 break;
329 case CLO_EXCLUDE:
330 if (scanned[iArg].n_items < 2)
332 moveToStringArray(&exclude, &excludes, scanned[iArg].list + 1, scanned[iArg].n_items - 1);
333 break;
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");
341 } else
342 forceMonotonic = FORCE_MONOTONIC;
343 break;
344 case CLO_FILLIN:
345 fillIn = 1;
346 break;
347 default:
348 fprintf(stderr, "error: unknown/ambiguous option: %s\n", scanned[iArg].list[0]);
349 exit(EXIT_FAILURE);
350 break;
351 }
352 } else {
353 if (!input)
354 input = scanned[iArg].list[0];
355 else if (!output)
356 output = scanned[iArg].list[0];
357 else
359 }
360 }
361
363
364 fpPrint = stderr;
365 if (printFlags & STDOUT_PRINTOUT)
366 fpPrint = stdout;
367
368 if (!indepQuantity)
369 SDDS_Bomb(
"supply the independent quantity name with the -columns option");
370
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");
373
376
377 excludes = appendToStringArray(&exclude, excludes, indepQuantity);
378 if (!depenQuantities)
379 depenQuantities = appendToStringArray(&depenQuantity, depenQuantities, "*");
380
381 if ((depenQuantities = expandColumnPairNames(&SDDSin, &depenQuantity, NULL, depenQuantities, exclude, excludes, FIND_NUMERIC_TYPE, 0)) <= 0) {
383 SDDS_Bomb(
"no dependent quantities selected for interpolation");
384 }
385
390 if (columnMajorOrder != -1)
391 SDDSout.layout.data_mode.column_major = columnMajorOrder;
392 else
393 SDDSout.layout.data_mode.column_major = SDDSin.layout.data_mode.column_major;
396
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]);
405 }
409
410 doNotRead = 0;
411 interpPoint = NULL;
412 outputData =
tmalloc(
sizeof(*outputData) * (depenQuantities));
413 depenValue =
tmalloc(
sizeof(*depenValue) * (depenQuantities));
414 rowFlag = NULL;
415 valuesReadCode = 0;
416
417 while (doNotRead || (readCode =
SDDS_ReadPage(&SDDSin)) > 0) {
421 if (atValues) {
422 interpPoint = atValue;
423 interpPoints = atValues;
424 } else if (fileValuesFile) {
425 if (interpPoint)
426 free(interpPoint);
429 else if (valuesReadCode == -1) {
430 if (parallelPages) {
431 fprintf(stderr, "warning: file %s ends before file %s\n", fileValuesFile, input);
432 break;
433 } else {
434
439 fprintf(stderr, "error: unable to (re)read file %s\n", fileValuesFile);
441 exit(EXIT_FAILURE);
442 }
443
445 if (readCode == -1)
446 break;
448 }
450 if (indepValue)
451 free(indepValue);
454 }
455 }
456
457 if (!parallelPages)
458 doNotRead = 1;
463 } else if (sequencePoints || sequenceSpacing) {
464 if (interpPoint)
465 free(interpPoint);
466 interpPoints = sequencePoints;
467 if (!(interpPoint = makeSequence(&interpPoints, sequenceStart, sequenceEnd, sequenceSpacing, indepValue, rows)))
468 exit(EXIT_FAILURE);
469 } else {
470
471 if (interpPoint)
472 free(interpPoint);
473 if (!(interpPoint = makeFillInSequence(indepValue, rows, &interpPoints)))
474 exit(EXIT_FAILURE);
475 }
476
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++)
481 rowFlag[j] = 1;
482 for (i = 0; i < depenQuantities; i++) {
485 }
486 if (forceMonotonic)
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");
492 if (interpShort)
493 shortValue =
tmalloc(
sizeof(*shortValue) * rows);
494
495 for (i = 0; i < depenQuantities; i++) {
496 if (interpShort) {
497 for (row = 0; row < rows; row++) {
498 shortValue[row] = (short)depenValue[i][row];
499 }
500 }
501 for (j = 0; j < interpPoints; j++) {
502 if (!interpShort) {
503 outputData[i][j] =
interpolate(depenValue[i], indepValue, rows, interpPoint[j], &belowRange, &aboveRange, order, &interpCode, monotonicity);
504 } else {
505 outputData[i][j] = (double)
interp_short(shortValue, indepValue, rows, interpPoint[j], 0, interpShortOrder, &interpCode, &nextPos);
506 }
507 if (interpCode) {
508 if (interpCode & OUTRANGE_ABORT) {
509 fprintf(stderr, "error: value %e is out of range for column %s\n", interpPoint[j], depenQuantity[i]);
510 exit(EXIT_FAILURE);
511 }
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)
515 rowFlag[j] = 0;
516 }
517 }
518 }
519 if (interpShort)
520 free(shortValue);
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++)
534 if (rowFlag[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);
539 }
540 } else if (printFlags & NORMAL_PRINTOUT) {
541 for (j = 0; j < interpPoints; j++)
542 if (rowFlag[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);
547 }
548 }
549 if (indepValue)
550 free(indepValue);
551 indepValue = NULL;
552 for (i = 0; i < depenQuantities; i++) {
553 if (outputData[i])
554 free(outputData[i]);
555 outputData[i] = NULL;
556 if (depenValue[i])
557 free(depenValue[i]);
558 depenValue[i] = NULL;
559 }
560 if (fileValuesFile) {
561 if (interpPoint)
562 free(interpPoint);
563 interpPoint = NULL;
564 }
565 if (rowFlag)
566 free(rowFlag);
567 rowFlag = NULL;
568 }
569
572 exit(EXIT_FAILURE);
573 }
576 exit(EXIT_FAILURE);
577 }
578 if (fileValuesFile) {
581 exit(EXIT_FAILURE);
582 }
583 }
584
585 return EXIT_SUCCESS;
586}
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.