Node *Sequence_append(Node *a, Node *e) { assert(a); a= makeSequence(a); assert(a->sequence.last); assert(e); a->sequence.last->any.next= e; a->sequence.last= e; return a; }
double *makeFillInSequence(double *x, long dataRows, long *nPointsReturn) { long i; double dxMin, dx, xMin, xMax; dxMin = DBL_MAX; xMin = xMax = x[0]; for (i=1; i<dataRows; i++) { if ((dx=fabs(x[i]-x[i-1]))<dxMin && dx>0) dxMin = dx; if (x[i]<xMin) xMin = x[i]; if (x[i]>xMax) xMax = x[i]; } *nPointsReturn = (xMax-xMin)/dxMin+1; return makeSequence(nPointsReturn, xMin, xMax, 0.0, x, dataRows); }
int main(int argc, char **argv) { int iArg; char *indepQuantity, **depenQuantity, *fileValuesQuantity, *fileValuesFile, **exclude; long depenQuantities, monotonicity, excludes; char *input, *output; long i, j, rows, readCode, order, valuesReadCode, fillIn, row; long sequencePoints, combineDuplicates, branch; int32_t *rowFlag; double sequenceStart, sequenceEnd; double sequenceSpacing; unsigned long flags, interpCode, printFlags, forceMonotonic; SCANNED_ARG *scanned; SDDS_DATASET SDDSin, SDDSout, SDDSvalues; OUTRANGE_CONTROL aboveRange, belowRange; double *atValue; long atValues, interpPoints, doNotRead, parallelPages; double *indepValue, **depenValue, *interpPoint, **outputData; unsigned long pipeFlags; FILE *fpPrint; short interpShort=0, interpShortOrder=-1, *shortValue=NULL; long nextPos; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&scanned, argc, argv); if (argc<3 || argc>(3+CLO_OPTIONS)) bomb(NULL, USAGE); atValue = NULL; atValues = fillIn = 0; output = input = NULL; combineDuplicates = branch = sequencePoints = parallelPages = 0; indepQuantity = NULL; depenQuantity = exclude = NULL; depenQuantities = excludes = 0; aboveRange.flags = belowRange.flags = OUTRANGE_SATURATE; order = 1; readCode = interpPoints = 0; fileValuesFile = fileValuesQuantity = NULL; sequenceStart = sequenceEnd = sequenceSpacing = 0; printFlags = pipeFlags = 0; forceMonotonic = 0; indepValue = interpPoint = NULL; depenValue = outputData = NULL; for (iArg=1; iArg<argc; iArg++) { if (scanned[iArg].arg_type==OPTION) { /* process options here */ switch (match_string(scanned[iArg].list[0], option, CLO_OPTIONS, 0)) { case CLO_ORDER: if (scanned[iArg].n_items!=2 || sscanf(scanned[iArg].list[1], "%ld", &order)!=1 || order<1) SDDS_Bomb("invalid -order syntax/value"); break; case CLO_ATVALUES: if (scanned[iArg].n_items<2) SDDS_Bomb("invalid -atValues syntax"); if (atValue) SDDS_Bomb("give -atValues only once"); atValue = tmalloc(sizeof(*atValue)*(atValues=scanned[iArg].n_items-1)); for (i=0; i<atValues; i++) if (sscanf(scanned[iArg].list[i+1], "%lf", &atValue[i])!=1) SDDS_Bomb("invalid -atValues value"); break; case CLO_INTERP_SHORT: if (scanned[iArg].n_items==2) { if (sscanf(scanned[iArg].list[1], "%hd", &interpShortOrder)!=1) SDDS_Bomb("invalid -interpShort value"); } interpShort = 1; break; case CLO_SEQUENCE: if ((scanned[iArg].n_items!=2 && scanned[iArg].n_items!=4) || sscanf(scanned[iArg].list[1], "%ld", &sequencePoints)!=1 || sequencePoints<2) SDDS_Bomb("invalid -sequence syntax/value"); if (scanned[iArg].n_items==4 && (sscanf(scanned[iArg].list[2], "%lf", &sequenceStart)!=1 || sscanf(scanned[iArg].list[3], "%lf", &sequenceEnd)!=1)) SDDS_Bomb("invalid -sequence syntax/value"); if (sequenceSpacing) SDDS_Bomb("give only one of -sequence and -equispaced"); break; case CLO_EQUISPACED: if ((scanned[iArg].n_items!=2 && scanned[iArg].n_items!=4) || sscanf(scanned[iArg].list[1], "%lf", &sequenceSpacing)!=1 || sequenceSpacing<=0) SDDS_Bomb("invalid -equispaced syntax/value"); if (scanned[iArg].n_items==4 && (sscanf(scanned[iArg].list[2], "%lf", &sequenceStart)!=1 || sscanf(scanned[iArg].list[3], "%lf", &sequenceEnd)!=1)) SDDS_Bomb("invalid -equispaced syntax/values"); if (sequencePoints) SDDS_Bomb("give only one of -sequence and -equispaced"); break; case CLO_COLUMNS: if (indepQuantity) SDDS_Bomb("only one -columns option may be given"); if (scanned[iArg].n_items<2) SDDS_Bomb("invalid -columns syntax"); indepQuantity = scanned[iArg].list[1]; if (scanned[iArg].n_items>=2) { depenQuantity = tmalloc(sizeof(*depenQuantity)*(depenQuantities=scanned[iArg].n_items-2)); for (i=0; i<depenQuantities; i++) depenQuantity[i] = scanned[iArg].list[i+2]; } break; case CLO_PRINTOUT: if ((scanned[iArg].n_items-=1)>=1) { if (!scanItemList(&printFlags, scanned[iArg].list+1, &scanned[iArg].n_items, 0, "bare", -1, NULL, 0, BARE_PRINTOUT, "stdout", -1, NULL, 0, STDOUT_PRINTOUT, NULL)) SDDS_Bomb("invalid -printout syntax"); } if (!(printFlags&BARE_PRINTOUT)) printFlags |= NORMAL_PRINTOUT; break; case CLO_FILEVALUES: if (scanned[iArg].n_items<2) SDDS_Bomb("invalid -fileValues syntax"); fileValuesFile = scanned[iArg].list[1]; scanned[iArg].n_items -= 2; if (!scanItemList(&flags, scanned[iArg].list+2, &scanned[iArg].n_items, 0, "column", SDDS_STRING, &fileValuesQuantity, 1, 0, "parallelpages", -1, NULL, 0, FILEVALUES_PARALLEL_PAGES, NULL)) SDDS_Bomb("invalid -fileValues syntax"); if (flags&FILEVALUES_PARALLEL_PAGES) parallelPages = 1; break; case CLO_COMBINEDUPLICATES: SDDS_Bomb("-combineDuplicates option not implemented yet--send email to [email protected]"); combineDuplicates = 1; break; case CLO_BRANCH: SDDS_Bomb("-branch option not implemented yet--send email to [email protected]"); if (scanned[iArg].n_items!=2 || sscanf(scanned[iArg].list[1], "%ld", &branch)!=1 || branch<1) SDDS_Bomb("invalid -branch syntax/value"); break; case CLO_BELOWRANGE: if ((scanned[iArg].n_items-=1)<1 || !scanItemList(&belowRange.flags, scanned[iArg].list+1, &scanned[iArg].n_items, 0, "value", SDDS_DOUBLE, &belowRange.value, 1, OUTRANGE_VALUE, "skip", -1, NULL, 0, OUTRANGE_SKIP, "saturate", -1, NULL, 0, OUTRANGE_SATURATE, "extrapolate", -1, NULL, 0, OUTRANGE_EXTRAPOLATE, "wrap", -1, NULL, 0, OUTRANGE_WRAP, "abort", -1, NULL, 0, OUTRANGE_ABORT, "warn", -1, NULL, 0, OUTRANGE_WARN, NULL)) SDDS_Bomb("invalid -belowRange syntax/value"); if ((i=bitsSet(belowRange.flags& (OUTRANGE_VALUE|OUTRANGE_SKIP|OUTRANGE_SATURATE|OUTRANGE_EXTRAPOLATE|OUTRANGE_WRAP|OUTRANGE_ABORT)))>1) SDDS_Bomb("incompatible keywords given for -belowRange"); if (i!=1) belowRange.flags |= OUTRANGE_SATURATE; break; case CLO_ABOVERANGE: if ((scanned[iArg].n_items-=1)<1 || !scanItemList(&aboveRange.flags, scanned[iArg].list+1, &scanned[iArg].n_items, 0, "value", SDDS_DOUBLE, &aboveRange.value, 1, OUTRANGE_VALUE, "skip", -1, NULL, 0, OUTRANGE_SKIP, "saturate", -1, NULL, 0, OUTRANGE_SATURATE, "extrapolate", -1, NULL, 0, OUTRANGE_EXTRAPOLATE, "wrap", -1, NULL, 0, OUTRANGE_WRAP, "abort", -1, NULL, 0, OUTRANGE_ABORT, "warn", -1, NULL, 0, OUTRANGE_WARN, NULL)) SDDS_Bomb("invalid -aboveRange syntax/value"); if ((i=bitsSet(aboveRange.flags& (OUTRANGE_VALUE|OUTRANGE_SKIP|OUTRANGE_SATURATE|OUTRANGE_EXTRAPOLATE|OUTRANGE_WRAP|OUTRANGE_ABORT)))>1) SDDS_Bomb("incompatible keywords given for -aboveRange"); if (i!=1) aboveRange.flags |= OUTRANGE_SATURATE; break; case CLO_PIPE: if (!processPipeOption(scanned[iArg].list+1, scanned[iArg].n_items-1, &pipeFlags)) SDDS_Bomb("invalid -pipe syntax"); break; case CLO_EXCLUDE: if (scanned[iArg].n_items<2) SDDS_Bomb("invalid -exclude syntax"); moveToStringArray(&exclude, &excludes, scanned[iArg].list+1, scanned[iArg].n_items-1); break; case CLO_FORCEMONOTONIC: if ((scanned[iArg].n_items-=1)>0) { if (!scanItemList(&forceMonotonic, scanned[iArg].list+1, &scanned[iArg].n_items, 0, "increasing", -1, NULL, 0, FORCE_INCREASING, "decreasing", -1, NULL, 0, FORCE_DECREASING, NULL) || bitsSet(forceMonotonic)!=1) SDDS_Bomb("invalid -forceMonotonic syntax/value"); } else forceMonotonic = FORCE_MONOTONIC; break; case CLO_FILLIN: fillIn = 1; break; default: fprintf(stderr, "error: unknown/ambiguous option: %s\n", scanned[iArg].list[0]); exit(1); break; } } else { if (!input) input = scanned[iArg].list[0]; else if (!output) output = scanned[iArg].list[0]; else SDDS_Bomb("too many filenames seen"); } } processFilenames("sddsinterp", &input, &output, pipeFlags, 0, NULL); fpPrint = stderr; if (printFlags&STDOUT_PRINTOUT) fpPrint = stdout; if (!indepQuantity) SDDS_Bomb("supply the independent quantity name with the -columns option"); if ((atValues?1:0)+(fileValuesFile?1:0)+(sequencePoints?1:0)+fillIn+(sequenceSpacing>0?1:0) != 1) SDDS_Bomb("you must give one and only one of -atValues, -fileValues, -sequence, -equispaced, and -fillIn"); if (!SDDS_InitializeInput(&SDDSin, input)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); excludes = appendToStringArray(&exclude, excludes, indepQuantity); if (!depenQuantities) depenQuantities = appendToStringArray(&depenQuantity, depenQuantities, "*"); if ((depenQuantities=expandColumnPairNames(&SDDSin, &depenQuantity, NULL, depenQuantities, exclude, excludes, FIND_NUMERIC_TYPE, 0))<=0) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); SDDS_Bomb("no dependent quantities selected for interpolation"); } if (fileValuesFile && !SDDS_InitializeInput(&SDDSvalues, fileValuesFile)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!SDDS_InitializeOutput(&SDDSout, SDDS_BINARY, 0, NULL, "sddsinterp output", output)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!SDDS_TransferColumnDefinition(&SDDSout, &SDDSin, indepQuantity, NULL)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); /* if (fileValuesQuantity && strcmp(fileValuesQuantity, indepQuantity)!=0 && !SDDS_TransferColumnDefinition(&SDDSout, &SDDSvalues, fileValuesQuantity, NULL)) { fprintf(stderr, "problem creating -fileValues column %s\n", fileValuesQuantity); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } */ if (SDDS_DefineParameter(&SDDSout, "InterpDataPage", NULL, NULL, "Page of interpolation data file used to create this page", NULL, SDDS_LONG, NULL)<0 || SDDS_DefineParameter(&SDDSout, "InterpPointsPage", NULL, NULL, "Page of interpolation points file used to create this page", NULL, SDDS_LONG, NULL)<0) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (i=0; i<depenQuantities; i++) if (!SDDS_TransferColumnDefinition(&SDDSout, &SDDSin, depenQuantity[i], NULL)) { fprintf(stderr, "problem creating interpolated-output column %s\n", depenQuantity[i]); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_TransferAllParameterDefinitions(&SDDSout, &SDDSin, SDDS_TRANSFER_KEEPOLD) || !SDDS_WriteLayout(&SDDSout)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); doNotRead = 0; interpPoint = NULL; outputData = tmalloc(sizeof(*outputData)*(depenQuantities)); depenValue = tmalloc(sizeof(*depenValue)*(depenQuantities)); rowFlag = NULL; valuesReadCode = 0; while (doNotRead || (readCode=SDDS_ReadPage(&SDDSin))>0) { rows = SDDS_CountRowsOfInterest(&SDDSin); if (!(indepValue = SDDS_GetColumnInDoubles(&SDDSin, indepQuantity))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (atValues) { interpPoint = atValue; interpPoints = atValues; } else if (fileValuesFile) { if (interpPoint) free(interpPoint); if ((valuesReadCode=SDDS_ReadPage(&SDDSvalues))==0) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); else if (valuesReadCode==-1) { if (parallelPages) { fprintf(stderr, "warning: file %s ends before file %s\n", fileValuesFile, input); break; } else { /* "rewind" the values file */ if (!SDDS_Terminate(&SDDSvalues) || !SDDS_InitializeInput(&SDDSvalues, fileValuesFile)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if ((valuesReadCode=SDDS_ReadPage(&SDDSvalues))<1) { fprintf(stderr, "error: unable to (re)read file %s\n", fileValuesFile); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } /* read the next page of the interpolation data file */ if ((readCode=SDDS_ReadPage(&SDDSin))<1) { if (readCode==-1) break; SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } rows = SDDS_CountRowsOfInterest(&SDDSin); if (indepValue) free(indepValue); if (!(indepValue = SDDS_GetColumnInDoubles(&SDDSin, indepQuantity))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } if (!parallelPages) doNotRead = 1; interpPoints = SDDS_CountRowsOfInterest(&SDDSvalues); interpPoint = SDDS_GetColumnInDoubles(&SDDSvalues, fileValuesQuantity); if (SDDS_NumberOfErrors()) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } else if (sequencePoints || sequenceSpacing) { if (interpPoint) free(interpPoint); interpPoints = sequencePoints; if (!(interpPoint = makeSequence(&interpPoints, sequenceStart, sequenceEnd, sequenceSpacing, indepValue, rows))) exit(1); } else { /* fillIn interpolation */ if (interpPoint) free(interpPoint); if (!(interpPoint = makeFillInSequence(indepValue, rows, &interpPoints))) exit(1); } for (i=0; i<depenQuantities; i++) outputData[i] = tmalloc(sizeof(*outputData[i])*interpPoints); rowFlag = trealloc(rowFlag, sizeof(*rowFlag)*interpPoints); for (j=0; j<interpPoints; j++) rowFlag[j] = 1; for (i=0; i<depenQuantities; i++) { if (!(depenValue[i] = SDDS_GetColumnInDoubles(&SDDSin, depenQuantity[i]))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (forceMonotonic) rows = forceMonotonicity(indepValue, depenValue, depenQuantities, rows, forceMonotonic); else if (combineDuplicates) rows = combineDuplicatePoints(indepValue, depenValue, depenQuantities, rows, 0.0); if ((monotonicity=checkMonotonicity(indepValue, rows))==0) SDDS_Bomb("independent data values do not change monotonically or repeated independent values exist"); if (interpShort) shortValue = malloc(sizeof(*shortValue)*rows); for (i=0; i<depenQuantities; i++) { if (interpShort) { for (row=0; row<rows; row++) { shortValue[row] = (short)depenValue[i][row]; } } for (j=0; j<interpPoints; j++) { if (!interpShort) { outputData[i][j] = interpolate(depenValue[i], indepValue, rows, interpPoint[j], &belowRange, &aboveRange, order, &interpCode, monotonicity); } else { outputData[i][j] = (double)interp_short(shortValue, indepValue, rows, interpPoint[j], 0, -1, &interpCode, &nextPos); } if (interpCode) { if (interpCode&OUTRANGE_ABORT) { fprintf(stderr, "error: value %e is out of range for column %s\n", interpPoint[j], depenQuantity[i]); exit(1); } if (interpCode&OUTRANGE_WARN) fprintf(stderr, "warning: value %e is out of range for column %s\n", interpPoint[j], depenQuantity[i]); if (interpCode&OUTRANGE_SKIP) rowFlag[j] = 0; } } } if (interpShort) free(shortValue); if (!SDDS_StartPage(&SDDSout, interpPoints) || !SDDS_SetColumnFromDoubles(&SDDSout, SDDS_SET_BY_NAME, interpPoint, interpPoints, indepQuantity)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!SDDS_SetParameters(&SDDSout, SDDS_BY_NAME|SDDS_PASS_BY_VALUE, "InterpDataPage", readCode, "InterpPointsPage", valuesReadCode, NULL) || !SDDS_CopyParameters(&SDDSout, &SDDSin)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (i=0; i<depenQuantities; i++) if (!SDDS_SetColumnFromDoubles(&SDDSout, SDDS_SET_BY_NAME, outputData[i], interpPoints, depenQuantity[i])) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!SDDS_AssertRowFlags(&SDDSout, SDDS_FLAG_ARRAY, rowFlag, rows) || !SDDS_WritePage(&SDDSout)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (printFlags&BARE_PRINTOUT) { for (j=0; j<interpPoints; j++) if (rowFlag[j]) { fprintf(fpPrint, "%21.15e ", interpPoint[j]); for (i=0; i<depenQuantities; i++) fprintf(fpPrint, "%21.15e ", outputData[i][j]); fputc('\n', fpPrint); } } else if (printFlags&NORMAL_PRINTOUT) { for (j=0; j<interpPoints; j++) if (rowFlag[j]) { fprintf(fpPrint, "%s=%21.15e ", indepQuantity, interpPoint[j]); for (i=0; i<depenQuantities; i++) fprintf(fpPrint, "%s=%21.15e ", depenQuantity[i], outputData[i][j]); fputc('\n', fpPrint); } } if (indepValue) free(indepValue); indepValue = NULL; for (i=0; i<depenQuantities; i++) { if (outputData[i]) free(outputData[i]); outputData[i] = NULL; if (depenValue[i]) free(depenValue[i]); depenValue[i] = NULL; } if (fileValuesFile) { if (interpPoint) free(interpPoint); interpPoint = NULL; } if (rowFlag) free(rowFlag); rowFlag = NULL; } if (!SDDS_Terminate(&SDDSin)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (!SDDS_Terminate(&SDDSout)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (fileValuesFile) { if (!SDDS_Terminate(&SDDSvalues)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } } return 0; }