90char *option[N_OPTIONS] = {
100static const char *USAGE =
101 "Usage: sddsinterpset [<input>] [<output>] \n"
102 " [-pipe=[input][,output]] \n"
103 " [-order=<number>] \n"
105 " [-data=fileColumn=<colName>,interpolate=<colName>,functionof=<colName>,\n"
106 " column=<colName> | atValue=<value>] \n"
107 " [-majorOrder=row|column] \n"
108 " [-belowRange={value=<value>|skip|saturate|extrapolate|wrap}[,{abort|warn}]] \n"
109 " [-aboveRange={value=<value>|skip|saturate|extrapolate|wrap}[,{abort|warn}]]\n\n"
111 " -verbose Print detailed processing messages.\n"
112 " -pipe Use standard SDDS Toolkit pipe options for input and output.\n"
113 " -order Specify the order of the polynomials used for interpolation.\n"
114 " Default is 1 (linear interpolation).\n"
115 " -data Define data interpolation parameters:\n"
116 " - fileColumn=<colName> : Column with data file names.\n"
117 " - interpolate=<colName> : Column to interpolate.\n"
118 " - functionof=<colName> : Independent variable column name.\n"
119 " - column=<colName> : Specify interpolation point as a column.\n"
121 " - atValue=<value> : Specify a fixed interpolation value.\n"
122 " -majorOrder Specify data ordering for output: 'row' or 'column'.\n"
123 " Default inherits from input file.\n"
124 " -belowRange Define behavior for interpolation points below data range:\n"
125 " Options: value=<value>, skip, saturate, extrapolate, wrap, abort, warn.\n"
126 " -aboveRange Define behavior for interpolation points above data range:\n"
127 " Options: value=<value>, skip, saturate, extrapolate, wrap, abort, warn.\n\n"
128 "Program by Hairong Shang. ("__DATE__
130 ", SVN revision: " SVN_VERSION
")\n";
132#define AT_COLUMN 0x00000001
133#define AT_VALUE 0x00000002
144 unsigned long hasdata;
148long checkMonotonicity(
double *indepValue, int64_t rows);
149void freedatacontrol(
DATA_CONTROL *data_control,
long dataControls);
151int main(
int argc,
char **argv) {
153 char *input = NULL, *output = NULL, **interpCol = NULL, **funcOf = NULL;
154 long order = 1, dataControls = 0, valid_option = 1, monotonicity;
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;
169 argc =
scanargs(&s_arg, argc, argv);
171 fprintf(stderr,
"%s", USAGE);
175 aboveRange.flags = belowRange.flags = OUTRANGE_SATURATE;
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:
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");
189 if (majorOrderFlag & SDDS_COLUMN_MAJOR_ORDER)
190 columnMajorOrder = 1;
191 else if (majorOrderFlag & SDDS_ROW_MAJOR_ORDER)
192 columnMajorOrder = 0;
196 if (s_arg[i_arg].n_items != 2 ||
197 sscanf(s_arg[i_arg].list[1],
"%ld", &order) != 1 ||
199 SDDS_Bomb(
"invalid -order syntax/value");
204 if (!
processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipeFlags)) {
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");
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");
227 aboveRange.flags |= OUTRANGE_SATURATE;
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");
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");
252 belowRange.flags |= OUTRANGE_SATURATE;
256 s_arg[i_arg].n_items -= 1;
257 if (s_arg[i_arg].n_items < 4) {
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;
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) {
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.");
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.");
294 if (
match_string(data_control[dataControls].funcOfCol, funcOf, dataControls, EXACT_MATCH) > 0) {
295 fprintf(stderr,
"Multiple independent columns provided!\n");
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);
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;
314 fprintf(stderr,
"Error: Unknown or ambiguous option '%s'\n", s_arg[i_arg].list[0]);
320 input = s_arg[i_arg].list[0];
322 output = s_arg[i_arg].list[0];
324 SDDS_Bomb(
"Too many filenames provided.");
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);
340 fprintf(stderr,
"Error: Column '%s' in input file '%s' is not a string column.\n",
341 data_control[i].fileColumn, input);
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);
351 fprintf(stderr,
"Error: Column '%s' in input file '%s' is not a numeric column.\n",
352 data_control[i].atCol, input);
356 data_control[i].hasdata = 1;
361 fprintf(stderr,
"Error: No valid -data options provided for processing.\n");
368 if (columnMajorOrder != -1)
369 SDDSout.layout.data_mode.column_major = columnMajorOrder;
371 SDDSout.layout.data_mode.column_major = SDDSin.layout.data_mode.column_major;
375 out_depenValue =
SDDS_Realloc(out_depenValue,
sizeof(*out_depenValue) * (pages + 1));
376 rowFlag =
SDDS_Realloc(rowFlag,
sizeof(*rowFlag) * (pages + 1));
379 fprintf(stderr,
"Error: No data found in input file '%s'.\n", input);
383 rowFlag[pages] = (int32_t *)malloc(
sizeof(**rowFlag) * rows[pages]);
384 out_depenValue[pages] = (
double *)malloc(
sizeof(**out_depenValue) * rows[pages]);
386 for (i = 0; i < rows[pages]; i++)
387 rowFlag[pages][i] = 1;
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))) {
396 if (data_control[i].atCol) {
402 if (!data_control[i].atCol)
403 atValue = data_control[i].atValue;
405 for (j = 0; j < rows[pages]; j++) {
410 case SDDS_CHECK_OKAY:
411 if (j == (rows[pages] - 1)) {
419 fprintf(stderr,
"Error: Column '%s' missing or invalid in file '%s'.\n",
420 data_control[i].interpCol, data_control[i].file[j]);
426 case SDDS_CHECK_OKAY:
427 if (j == (rows[pages] - 1)) {
428 if ((!pages) && !(data_control[i].atCol)) {
435 fprintf(stderr,
"Error: Column '%s' missing or invalid in file '%s'.\n",
436 data_control[i].funcOfCol, data_control[i].file[j]);
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]);
462 if (data_control[i].atCol)
463 atValue = data_control[i].colValue[j];
465 out_depenValue[pages][j] =
interpolate(depenValue, indepValue, datarows, atValue,
466 &belowRange, &aboveRange, order, &interpCode, monotonicity);
469 if (interpCode & OUTRANGE_ABORT) {
470 fprintf(stderr,
"Error: Value %e out of range for column '%s'.\n",
471 atValue, data_control[i].interpCol);
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;
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;
491 if (data_control[i].colValue) {
492 free(data_control[i].colValue);
493 data_control[i].colValue = NULL;
502 if (!SDDS_StartTable(&SDDSout, rows[pages]))
511 for (i = 0; i < dataControls; i++) {
512 if (data_control[i].hasdata) {
514 rows[pages], data_control[i].interpCol)) {
517 if (!(data_control[i].atCol)) {
518 for (row = 0; row < rows[pages]; row++) {
520 data_control[i].funcOfCol, data_control[i].atValue, NULL)) {
545 freedatacontrol(data_control, dataControls);
547 if (out_depenValue) {
548 for (i = 0; i < pages; i++) {
549 free(out_depenValue[i]);
552 free(out_depenValue);
567long checkMonotonicity(
double *indepValue, int64_t rows) {
571 if (indepValue[rows - 1] > indepValue[0]) {
573 if (indepValue[rows] < indepValue[rows - 1])
578 if (indepValue[rows] > indepValue[rows - 1])
584void freedatacontrol(
DATA_CONTROL *data_control,
long dataControls) {
586 for (i = 0; i < dataControls; i++) {
587 free(data_control[i].interpCol);
588 free(data_control[i].funcOfCol);
589 free(data_control[i].fileColumn);
590 if (data_control[i].atCol)
591 free(data_control[i].atCol);
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
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.
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)
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.