int32_t SDDS_CopyPage(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source) { /* int32_t i, index, column_count, j;*/ /* SDDS_LAYOUT target_layout, source_layout;*/ if (!SDDS_CheckDataset(SDDS_target, "SDDS_CopyPage")) return(0); if (!SDDS_CheckDataset(SDDS_source, "SDDS_CopyPage")) return(0); if (!SDDS_StartPage(SDDS_target, SDDS_target->layout.n_columns?SDDS_source->n_rows:0)) { SDDS_SetError("Unable to copy page (SDDS_CopyPage)"); return(0); } if (!SDDS_CopyParameters(SDDS_target, SDDS_source)) return(0); if (!SDDS_CopyArrays(SDDS_target, SDDS_source)) return(0); if (!SDDS_CopyColumns(SDDS_target, SDDS_source)) return(0); return(1); }
int main(int argc, char **argv) { SDDS_DATASET SDDSnew, SDDSold; long i, j, iArg; SCANNED_ARG *scArg; char *input, *output, *columnName; long mode, matchCode, rows, rowsMinus1, tmpfile_used; double gapAmount, *columnData, gapFactor; char *matchPattern; long matchPatternAfter = 0; double changeAmount, changeBase; long retval, newStart, rowLimit, breakNext; int32_t dataType, overlap=0; unsigned long flags, pipeFlags, changeFlags; char **stringData; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&scArg, argc, argv); if (argc<2) { fprintf(stderr, "%s", USAGE); return(1); } columnData = NULL; stringData = NULL; input = output = columnName = NULL; mode = -1; pipeFlags = flags = 0; gapAmount = changeAmount = rowLimit = gapFactor = 0; matchPattern = NULL; for (iArg=1; iArg<argc; iArg++) { if (scArg[iArg].arg_type==OPTION) { switch (matchCode=match_string(scArg[iArg].list[0], option, N_OPTIONS, 0)) { case SET_GAPIN: if ((scArg[iArg].n_items-=2)<0 || !scanItemList(&flags, scArg[iArg].list+2, &scArg[iArg].n_items, 0, "amount", SDDS_DOUBLE, &gapAmount, 1, GAPIN_AMOUNT, "factor", SDDS_DOUBLE, &gapFactor, 1, GAPIN_FACTOR, NULL) || (flags&GAPIN_AMOUNT && gapAmount<=0) || (flags&GAPIN_FACTOR && gapFactor<=0)) { fprintf(stderr, "Error: invalid -gapin syntax/values\n"); return(1); } columnName = scArg[iArg].list[1]; mode = matchCode; break; case SET_INCREASEOF: case SET_DECREASEOF: if (scArg[iArg].n_items!=2) { fprintf(stderr, "Error: invalid option syntax---specify column-name with -increaseof and -decreaseof\n"); return(1); } columnName = scArg[iArg].list[1]; mode = matchCode; break; case SET_CHANGEOF: if ((scArg[iArg].n_items-=2)<0 || !scanItemList(&changeFlags, scArg[iArg].list+2, &scArg[iArg].n_items, 0, "amount", SDDS_DOUBLE, &changeAmount, 1, CHANGEOF_AMOUNT, "base", SDDS_DOUBLE, &changeBase, 1, CHANGEOF_BASE, NULL) || (changeFlags&CHANGEOF_AMOUNT && changeAmount<=0)) { fprintf(stderr, "Error: invalid -changeof syntax/values\n"); return(1); } columnName = scArg[iArg].list[1]; mode = matchCode; break; case SET_ROWLIMIT: if (scArg[iArg].n_items<2) { fprintf(stderr, "Error: invalid -rowlimit syntax\n"); return(1); } if (sscanf(scArg[iArg].list[1], "%ld", &rowLimit)!=1 || rowLimit<=0) { fprintf(stderr, "Error: invalid -rowlimit syntax\n"); return(1); } if (scArg[iArg].n_items>2) { scArg[iArg].n_items-=2; if (!scanItemList(&flags, scArg[iArg].list+2, &scArg[iArg].n_items, 0, "overlap", SDDS_LONG, &overlap, NULL) || overlap<0) { fprintf(stderr, "Error: invalid overlap given in -rowlimit syntax\n"); return(1); } } mode = matchCode; break; case SET_PIPE: if (!processPipeOption(scArg[iArg].list+1, scArg[iArg].n_items-1, &pipeFlags)) { fprintf(stderr, "Error: invalid -pipe syntax\n"); return(1); } break; case SET_MATCHTO: if ((scArg[iArg].n_items!=3 && scArg[iArg].n_items!=4) || strlen(columnName=scArg[iArg].list[1])==0 || strlen(matchPattern=scArg[iArg].list[2])==0) { fprintf(stderr, "Error: invalid -matchTo syntax\n"); return(1); } if (scArg[iArg].n_items==4) { if (strncmp(scArg[iArg].list[3], "after", strlen(scArg[iArg].list[3]))==0) matchPatternAfter = 1; else { fprintf(stderr, "Error: invalid -matchTo syntax\n"); return(1); } } mode = matchCode; break; default: fprintf(stderr, "Error: unknown switch: %s\n", scArg[iArg].list[0]); fprintf(stderr, "%s", USAGE); return(1); } } else { if (input==NULL) input = scArg[iArg].list[0]; else if (output==NULL) output = scArg[iArg].list[0]; else { fprintf(stderr, "Error: too many filenames given\n"); return(1); } } } processFilenames("sddsbreak", &input, &output, pipeFlags, 0, &tmpfile_used); if (mode==-1) { fprintf(stderr, "Error: no break mode specified\n"); return(1); } if (!SDDS_InitializeInput(&SDDSold, input) || !SDDS_InitializeCopy(&SDDSnew, &SDDSold, output, "w")) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } SDDSnew.layout.data_mode.no_row_counts = 0; if (!SDDS_WriteLayout(&SDDSnew)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } if (mode!=SET_ROWLIMIT) { if (SDDS_GetColumnInformation(&SDDSold, "type", &dataType, SDDS_BY_NAME, columnName)!=SDDS_LONG) { SDDS_SetError("problem getting type information on given column"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } if (mode==SET_MATCHTO) { if (!(dataType==SDDS_STRING)) { fprintf(stderr, "Error: given column does not contain string data\n"); return(1); } } else if (!SDDS_NUMERIC_TYPE(dataType)) { if (!(mode==SET_CHANGEOF && !(changeFlags&CHANGEOF_AMOUNT) && !(changeFlags&CHANGEOF_BASE))) { fprintf(stderr, "Error: given column does not contain numeric data\n"); return(1); } } } while ((retval=SDDS_ReadPage(&SDDSold))>0) { if ((rows = SDDS_CountRowsOfInterest(&SDDSold))<0) { SDDS_SetError("Problem getting number of rows of tabular data"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } rowsMinus1 = rows-1; if (!SDDS_StartPage(&SDDSnew, rows) || !SDDS_CopyParameters(&SDDSnew, &SDDSold) || !SDDS_CopyArrays(&SDDSnew, &SDDSold)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } if (rows==0) { if (!SDDS_WritePage(&SDDSnew)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } continue; } switch (mode) { case SET_GAPIN: if (!(columnData=SDDS_GetColumnInDoubles(&SDDSold, columnName))) { SDDS_SetError("unable to read specified column"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } if (!gapAmount && rows>1) { double *gap; gap = tmalloc(sizeof(*gap)*rows); for (i=1; i<rows; i++) gap[i-1] = fabs(columnData[i]-columnData[i-1]); if (!compute_average(&gapAmount, gap, rows-1)) { fprintf(stderr, "Error: unable to determine default gap amount--couldn't find median gap\n"); return(1); } gapAmount *= (gapFactor?gapFactor:2); free(gap); } newStart = 0; for (i=1; i<=rows; i++) { if (i!=rows && fabs(columnData[i]-columnData[i-1])<gapAmount) continue; if (!SDDS_SetRowFlags(&SDDSold, 0) || !SDDS_AssertRowFlags(&SDDSold, SDDS_INDEX_LIMITS, newStart, i-1, 1) || !SDDS_CopyRowsOfInterest(&SDDSnew, &SDDSold) || !SDDS_WritePage(&SDDSnew)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } newStart = i; } free(columnData); break; case SET_INCREASEOF: if (!(columnData=SDDS_GetColumnInDoubles(&SDDSold, columnName))) { SDDS_SetError("unable to read specified column"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } newStart = 0; for (i=1; i<=rows; i++) { if (i!=rows && columnData[i]<=columnData[i-1]) continue; if (!SDDS_SetRowFlags(&SDDSold, 0) || !SDDS_AssertRowFlags(&SDDSold, SDDS_INDEX_LIMITS, newStart, i-1, 1) || !SDDS_CopyRowsOfInterest(&SDDSnew, &SDDSold) || !SDDS_WritePage(&SDDSnew)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } newStart = i; } free(columnData); break; case SET_DECREASEOF: if (!(columnData=SDDS_GetColumnInDoubles(&SDDSold, columnName))) { SDDS_SetError("unable to read specified column"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } newStart = 0; for (i=1; i<=rows; i++) { if (i!=rows && columnData[i]>=columnData[i-1]) continue; if (!SDDS_SetRowFlags(&SDDSold, 0) || !SDDS_AssertRowFlags(&SDDSold, SDDS_INDEX_LIMITS, newStart, i-1, 1) || !SDDS_CopyRowsOfInterest(&SDDSnew, &SDDSold) || !SDDS_WritePage(&SDDSnew)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } newStart = i; } free(columnData); break; case SET_CHANGEOF: if (dataType!=SDDS_STRING) { if (!(columnData=SDDS_GetColumnInDoubles(&SDDSold, columnName))) { SDDS_SetError("unable to read specified column"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } } else { if (!(stringData=SDDS_GetColumn(&SDDSold, columnName))) { SDDS_SetError("unable to read specified column"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } } newStart = 0; if (dataType==SDDS_STRING || !changeAmount) { for (i=1; i<=rows; i++) { if (i!=rows && ((dataType==SDDS_STRING && strcmp(stringData[i], stringData[i-1])==0) || (dataType!=SDDS_STRING && columnData[i]==columnData[i-1]))) continue; if (!SDDS_SetRowFlags(&SDDSold, 0) || !SDDS_AssertRowFlags(&SDDSold, SDDS_INDEX_LIMITS, newStart, i-1, 1) || !SDDS_CopyRowsOfInterest(&SDDSnew, &SDDSold) || !SDDS_WritePage(&SDDSnew)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } newStart = i; } } else { long region, lastRegion; region = lastRegion = 0; if (!(changeFlags&CHANGEOF_BASE) && rows>=1) changeBase = columnData[0]; if (rows>1) lastRegion = (columnData[0]-changeBase)/changeAmount; #ifdef DEBUG fprintf(stderr, "change base=%e, lastRegion=%ld\n", changeBase, lastRegion); fprintf(stderr, "start value = %e\n", columnData[0]); #endif newStart = 0; for (i=1; i<=rows; i++) { if (i!=rows) region = (columnData[i]-changeBase)/changeAmount; if (i!=rows && region==lastRegion) continue; #ifdef DEBUG fprintf(stderr, "split after %e, before %e, region = %d\n", columnData[i-1], columnData[i], region); #endif if (!SDDS_SetRowFlags(&SDDSold, 0) || !SDDS_AssertRowFlags(&SDDSold, SDDS_INDEX_LIMITS, newStart, i-1, 1) || !SDDS_CopyRowsOfInterest(&SDDSnew, &SDDSold) || !SDDS_WritePage(&SDDSnew)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } newStart = i; lastRegion = region; #ifdef DEBUG fprintf(stderr, "start value = %e\n", columnData[i]); #endif } } if (dataType!=SDDS_STRING) free(columnData); else SDDS_FreeStringArray(stringData, rows); break; case SET_MATCHTO: if (!(stringData=SDDS_GetColumn(&SDDSold, columnName))) { SDDS_SetError("unable to read specified column"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } newStart = 0; breakNext = 0; for (i=1; i<=rows; i++) { if (i!=rows && !breakNext) { if (wild_match(stringData[i], matchPattern)) { if (matchPatternAfter) { breakNext = 1; continue; } } else continue; } if (!SDDS_SetRowFlags(&SDDSold, 0) || !SDDS_AssertRowFlags(&SDDSold, SDDS_INDEX_LIMITS, newStart, i-1, 1) || !SDDS_CopyRowsOfInterest(&SDDSnew, &SDDSold) || !SDDS_WritePage(&SDDSnew)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } breakNext = 0; newStart = i; } SDDS_FreeStringArray(stringData, rows); break; case SET_ROWLIMIT: for (i=0; i<rows; i+=rowLimit-overlap) { if ((j=i+rowLimit-1)>=rows) j = rows-1; if (!SDDS_SetRowFlags(&SDDSold, 0) || !SDDS_AssertRowFlags(&SDDSold, SDDS_INDEX_LIMITS, i, j, 1) || !SDDS_CopyRowsOfInterest(&SDDSnew, &SDDSold) || !SDDS_WritePage(&SDDSnew)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } if (j==rows-1) break; } break; default: fprintf(stderr, "Error: unknown break mode code seen---this can't happen\n"); return(1); } } if (retval==0) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } if (!SDDS_Terminate(&SDDSold)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } if (tmpfile_used && !replaceFileAndBackUp(input, output)) { return(1); } return(0); }
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; }
int main(int argc, char **argv) { double *xData, *yData, *xError, *yError, *derivative, *derivativeError, *derivativePosition; char *input, *output, *xName, *xErrorName, **yName, **yErrorName, **yOutputName, **yOutputErrorName, *ptr; char **yOutputUnits, **yExcludeName; char *mainTemplate[3] = {NULL, NULL, NULL}; char *errorTemplate[3] = {NULL, NULL, NULL}; long i, iArg, yNames, yExcludeNames, rows; int32_t interval; SDDS_DATASET SDDSin, SDDSout; SCANNED_ARG *scanned; unsigned long flags, pipeFlags; long SGLeft, SGRight, SGOrder, SGDerivOrder, intervalGiven, yErrorsSeen; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&scanned, argc, argv); if (argc==1) bomb(NULL, USAGE); input = output = xName = xErrorName = NULL; yName = yErrorName = yExcludeName = NULL; derivative = derivativeError = derivativePosition = yError = yData = xData = xError = NULL; yNames = yExcludeNames = 0; pipeFlags = 0; interval = 2; SGOrder = -1; SGDerivOrder = 1; intervalGiven = 0; yErrorsSeen = 0; 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_DIFFERENTIATE: if (scanned[iArg].n_items!=2 && scanned[iArg].n_items!=3) SDDS_Bomb("invalid -differentiate syntax"); yName = SDDS_Realloc(yName, sizeof(*yName)*(yNames+1)); yErrorName = SDDS_Realloc(yErrorName, sizeof(*yErrorName)*(yNames+1)); yName[yNames] = scanned[iArg].list[1]; if (scanned[iArg].n_items==3) { yErrorsSeen = 1; yErrorName[yNames] = scanned[iArg].list[2]; } else yErrorName[yNames] = NULL; yNames++; break; case CLO_EXCLUDE: if (scanned[iArg].n_items<2) SDDS_Bomb("invalid -exclude syntax"); moveToStringArray(&yExcludeName, &yExcludeNames, scanned[iArg].list+1, scanned[iArg].n_items-1); break; case CLO_VERSUS: if (xName) SDDS_Bomb("give -versus only once"); if (scanned[iArg].n_items!=2) SDDS_Bomb("invalid -versus syntax"); xName = scanned[iArg].list[1]; xErrorName = NULL; break; case CLO_MAINTEMPLATE: if (scanned[iArg].n_items<2) SDDS_Bomb("invalid -mainTemplate syntax"); scanned[iArg].n_items--; if (!scanItemList(&flags, scanned[iArg].list+1, &scanned[iArg].n_items, 0, "name", SDDS_STRING, mainTemplate+0, 1, 0, "description", SDDS_STRING, mainTemplate+1, 1, 0, "symbol", SDDS_STRING, mainTemplate+2, 1, 0, NULL)) SDDS_Bomb("invalid -mainTemplate syntax"); break; case CLO_ERRORTEMPLATE: if (scanned[iArg].n_items<2) SDDS_Bomb("invalid -errorTemplate syntax"); scanned[iArg].n_items--; if (!scanItemList(&flags, scanned[iArg].list+1, &scanned[iArg].n_items, 0, "name", SDDS_STRING, errorTemplate+0, 1, 0, "description", SDDS_STRING, errorTemplate+1, 1, 0, "symbol", SDDS_STRING, errorTemplate+2, 1, 0, NULL)) SDDS_Bomb("invalid -errorTemplate syntax"); break; case CLO_PIPE: if (!processPipeOption(scanned[iArg].list+1, scanned[iArg].n_items-1, &pipeFlags)) SDDS_Bomb("invalid -pipe syntax"); break; case CLO_INTERVAL: if (scanned[iArg].n_items!=2 || sscanf(scanned[iArg].list[1], "%" SCNd32, &interval)!=1 || interval<=0) SDDS_Bomb("invalid -interval syntax/value"); intervalGiven = 1; break; case CLO_SAVITZKYGOLAY: if ((scanned[iArg].n_items!=4 && scanned[iArg].n_items!=5) || sscanf(scanned[iArg].list[1], "%ld", &SGLeft)!=1 || sscanf(scanned[iArg].list[2], "%ld", &SGRight)!=1 || sscanf(scanned[iArg].list[3], "%ld", &SGOrder)!=1 || (scanned[iArg].n_items==5 && sscanf(scanned[iArg].list[4], "%ld", &SGDerivOrder)!=1) || SGLeft<0 || SGRight<0 || (SGLeft+SGRight)<SGOrder || SGOrder<0 || SGDerivOrder<0) SDDS_Bomb("invalid -SavitzkyGolay syntax/values"); break; default: fprintf(stderr, "invalid option seen: %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"); } } if (intervalGiven && SGOrder>=0) SDDS_Bomb("-interval and -SavitzkyGolay options are incompatible"); if (SGOrder>=0 && (xErrorName || yErrorsSeen)) SDDS_Bomb("Savitzky-Golay method does not support errors in data"); processFilenames("sddsderiv", &input, &output, pipeFlags, 0, NULL); if (!yNames) SDDS_Bomb("-differentiate option must be given at least once"); if (!checkErrorNames(yErrorName, yNames)) SDDS_Bomb("either all -differentiate quantities must have errors, or none"); if (!SDDS_InitializeInput(&SDDSin, input)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!(ptr=SDDS_FindColumn(&SDDSin, FIND_NUMERIC_TYPE, xName, NULL))) { fprintf(stderr, "error: column %s doesn't exist\n", xName); exit(1); } free(xName); xName = ptr; if (xErrorName) { if (!(ptr=SDDS_FindColumn(&SDDSin, FIND_NUMERIC_TYPE, xErrorName, NULL))) { fprintf(stderr, "error: column %s doesn't exist\n", xErrorName); exit(1); } else { free(xErrorName); xErrorName = ptr; } } if (!(yNames=expandColumnPairNames(&SDDSin, &yName, &yErrorName, yNames, yExcludeName, yExcludeNames, FIND_NUMERIC_TYPE, 0))) { fprintf(stderr, "error: no quantities to differentiate found in file\n"); exit(1); } setupOutputFile(&SDDSout, &SDDSin, output, &yOutputName, &yOutputErrorName, &yOutputUnits, xName, xErrorName, yName, yErrorName, yNames, mainTemplate, errorTemplate, interval, SGOrder>=0?SGDerivOrder:1); while (SDDS_ReadPage(&SDDSin)>0) { if ((rows = SDDS_CountRowsOfInterest(&SDDSin))<2) SDDS_Bomb("Can't compute derivatives: too little data."); derivative = SDDS_Realloc(derivative, sizeof(*derivative)*rows); derivativeError = SDDS_Realloc(derivativeError, sizeof(*derivativeError)*rows); derivativePosition = SDDS_Realloc(derivativePosition, sizeof(*derivativePosition)*rows); if (!SDDS_StartPage(&SDDSout, rows) || !SDDS_CopyParameters(&SDDSout, &SDDSin)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); xError = NULL; if (!(xData = SDDS_GetColumnInDoubles(&SDDSin, xName)) || (xErrorName && !(xError=SDDS_GetColumnInDoubles(&SDDSin, xErrorName)))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (i=0; i<yNames; i++) { yError = NULL; if (!(yData = SDDS_GetColumnInDoubles(&SDDSin, yName[i])) || (yErrorName && yErrorName[i] && !(yError=SDDS_GetColumnInDoubles(&SDDSin, yErrorName[i])))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (SGOrder>=0) takeSGDerivative(xData, yData, rows, derivative, derivativePosition, SGLeft, SGRight, SGOrder, SGDerivOrder); else takeDerivative(xData, yData, yError, rows, derivative, derivativeError, derivativePosition, interval); if (!SDDS_SetColumnFromDoubles(&SDDSout, SDDS_BY_NAME, derivative, rows, yOutputName[i]) || (yOutputErrorName && yOutputErrorName[i] && !SDDS_SetColumnFromDoubles(&SDDSout, SDDS_BY_NAME, derivativeError, rows, yOutputErrorName[i]))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (yData) free(yData); if (yError) free(yError); yData = yError = NULL; } if (!SDDS_SetColumnFromDoubles(&SDDSout, SDDS_BY_NAME, derivativePosition, rows, xName) || (xErrorName && !SDDS_SetColumnFromDoubles(&SDDSout, SDDS_BY_NAME, xError, rows, xErrorName))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!SDDS_WritePage(&SDDSout)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (xData) free(xData); if (xError) free(xError); xData = xError = NULL; } if (!SDDS_Terminate(&SDDSin) || !SDDS_Terminate(&SDDSout)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (derivative) free(derivative); if (derivativeError) free(derivativeError); if (derivativePosition) free(derivativePosition); return(0); }
int main(int argc, char **argv) { int iArg; char *input, *output,*meanPar, *sigmaPar, *maxPar, *minPar; long i, j, mainInputOpened, haltonID=0, requireInput=0; unsigned long pipeFlags; SCANNED_ARG *scanned; SDDS_DATASET SDDSin, SDDSout, *SDDSptr; long randomNumberSeed = 0; SEQ_REQUEST *seqRequest; long samples, values, seqRequests, randomizationGroups=0; double *sample, *IVValue, *CDFValue; char msgBuffer[1000]; RANDOMIZED_ORDER *randomizationData = NULL; long verbose, optimalHalton=0; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&scanned, argc, argv); if (argc<2) { fprintf(stderr, "%s%s%s\n", USAGE1, USAGE2, USAGE3); return(1); } seqRequest = NULL; seqRequests = 0; output = input = NULL; pipeFlags = 0; samples = values = 0; sample = IVValue = CDFValue = NULL; verbose = 0; maxPar = minPar = meanPar = sigmaPar = 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_COLUMNS: if (scanned[iArg].n_items<3) SDDS_Bomb("invalid -columns syntax"); if (!(seqRequest = SDDS_Realloc(seqRequest, sizeof(*seqRequest)*(seqRequests+1)))) SDDS_Bomb("memory allocation failure"); scanned[iArg].n_items -= 1; memset(seqRequest+seqRequests, 0, sizeof(*seqRequest)); /*remove following pointer initialization because memset already initialize them */ seqRequest[seqRequests].randomizationGroup = -1; seqRequest[seqRequests].factor = 1; seqRequest[seqRequests].offset = 0; if (!scanItemList(&seqRequest[seqRequests].flags, scanned[iArg].list+1, &scanned[iArg].n_items, 0, "datafile", SDDS_STRING, &seqRequest[seqRequests].dataFileName, 1, SEQ_DATAFILE, "independentvariable", SDDS_STRING, &seqRequest[seqRequests].indepName, 1, SEQ_INDEPNAME, "cdf", SDDS_STRING, &seqRequest[seqRequests].CDFName, 1, SEQ_CDFNAME, "df", SDDS_STRING, &seqRequest[seqRequests].DFName, 1, SEQ_DFNAME, "output", SDDS_STRING, &seqRequest[seqRequests].outputName, 1, SEQ_OUTPUTNAME, "units", SDDS_STRING, &seqRequest[seqRequests].units, 1, SEQ_UNITSGIVEN, "haltonradix", SDDS_LONG, &seqRequest[seqRequests].haltonRadix, 1, SEQ_HALTONRADIX, "haltonoffset", SDDS_LONG, &seqRequest[seqRequests].haltonOffset, 1, SEQ_HALTONOFFSET, "randomize", -1, NULL, 0, SEQ_RANDOMIZE, "group", SDDS_LONG, &seqRequest[seqRequests].randomizationGroup, 1, SEQ_RANDOMGROUP, "factor", SDDS_DOUBLE, &seqRequest[seqRequests].factor, 1, 0, "offset", SDDS_DOUBLE, &seqRequest[seqRequests].offset, 1, 0, NULL) || bitsSet(seqRequest[seqRequests].flags&(SEQ_INDEPNAME+SEQ_CDFNAME+SEQ_DFNAME))!=2) SDDS_Bomb("invalid -columns syntax"); if (seqRequest[seqRequests].flags&SEQ_RANDOMGROUP && seqRequest[seqRequests].randomizationGroup<=0) SDDS_Bomb("use a positive integer for the randomization group ID"); if (seqRequest[seqRequests].flags&SEQ_CDFNAME && seqRequest[seqRequests].flags&SEQ_DFNAME) SDDS_Bomb("give df or cdf for -columns, not both"); if (seqRequest[seqRequests].flags&SEQ_HALTONRADIX && !is_prime(seqRequest[seqRequests].haltonRadix)) SDDS_Bomb("halton radix must be a prime number"); seqRequests ++; scanned[iArg].n_items += 1; break; case CLO_GAUSSIAN: if (scanned[iArg].n_items<2) SDDS_Bomb("invalid -gaussian syntax"); if (!(seqRequest = SDDS_Realloc(seqRequest, sizeof(*seqRequest)*(seqRequests+1)))) SDDS_Bomb("memory allocation failure"); memset(seqRequest+seqRequests, 0, sizeof(*seqRequest)); scanned[iArg].n_items -= 1; seqRequest[seqRequests].randomizationGroup = -1; seqRequest[seqRequests].mean = 0; seqRequest[seqRequests].sigma = 1; if (!scanItemList(&seqRequest[seqRequests].flags, scanned[iArg].list+1, &scanned[iArg].n_items, 0, "columnName", SDDS_STRING, &seqRequest[seqRequests].outputName, 1, SEQ_OUTPUTNAME, "meanValue", SDDS_STRING, &meanPar, 1, 0, "sigmaValue", SDDS_STRING, &sigmaPar, 1, 0, "units", SDDS_STRING, &seqRequest[seqRequests].units, 1, SEQ_UNITSGIVEN, NULL)) SDDS_Bomb("invalid -gaussian syntax"); seqRequest[seqRequests].flags |= SEQ_DIRECT_GAUSSIAN; if (!(seqRequest[seqRequests].flags&SEQ_OUTPUTNAME) || !(seqRequest[seqRequests].outputName)) SDDS_Bomb("columnName is not provided for gaussian distribution/"); if (meanPar) { if (wild_match(meanPar, "@*")) SDDS_CopyString(&seqRequest[seqRequests].meanPar, meanPar+1); else if (!get_double(&seqRequest[seqRequests].mean, meanPar)) SDDS_Bomb("Invalid value given for mean value of -gaussian distribution."); free(meanPar); meanPar = NULL; } if (sigmaPar) { if (wild_match(sigmaPar, "@*")) SDDS_CopyString(&seqRequest[seqRequests].sigmaPar, sigmaPar+1); else if (!get_double(&seqRequest[seqRequests].sigma, sigmaPar)) SDDS_Bomb("Invalid value given for sigma value of -gaussian distribution."); free(sigmaPar); sigmaPar = NULL; } seqRequests ++; scanned[iArg].n_items += 1; break; case CLO_UNIFORM: if (scanned[iArg].n_items<2) SDDS_Bomb("invalid -uniform syntax"); if (!(seqRequest = SDDS_Realloc(seqRequest, sizeof(*seqRequest)*(seqRequests+1)))) SDDS_Bomb("memory allocation failure"); memset(seqRequest+seqRequests, 0, sizeof(*seqRequest)); scanned[iArg].n_items -= 1; memset(seqRequest+seqRequests, 0, sizeof(*seqRequest)); seqRequest[seqRequests].randomizationGroup = -1; seqRequest[seqRequests].min = 0; seqRequest[seqRequests].max = 1; if (!scanItemList(&seqRequest[seqRequests].flags, scanned[iArg].list+1, &scanned[iArg].n_items, 0, "columnName", SDDS_STRING, &seqRequest[seqRequests].outputName, 1, SEQ_OUTPUTNAME, "minimumValue", SDDS_STRING, &minPar, 1, 0, "maximumValue", SDDS_STRING, &maxPar, 1, 0, "units", SDDS_STRING, &seqRequest[seqRequests].units, 1, SEQ_UNITSGIVEN, NULL)) SDDS_Bomb("invalid -uniform syntax"); seqRequest[seqRequests].flags |= SEQ_DIRECT_UNIFORM; if (!(seqRequest[seqRequests].flags&SEQ_OUTPUTNAME) || !(seqRequest[seqRequests].outputName)) SDDS_Bomb("columnName is not provided for uniform distribution/"); if (minPar) { if (wild_match(minPar, "@*")) SDDS_CopyString(&seqRequest[seqRequests].minPar, minPar+1); else if (!get_double(&seqRequest[seqRequests].min, minPar)) SDDS_Bomb("Invalid value given for minimum value of -uniform distribution."); free(minPar); minPar = NULL; } if (maxPar) { if (wild_match(maxPar, "@*")) SDDS_CopyString(&seqRequest[seqRequests].maxPar, maxPar+1); else if (!get_double(&seqRequest[seqRequests].max, maxPar)) SDDS_Bomb("Invalid value given for maximum value of -uniform distribution."); free(maxPar); maxPar = NULL; } seqRequests ++; scanned[iArg].n_items += 1; break; case CLO_POISSON: if (scanned[iArg].n_items<2) SDDS_Bomb("invalid -poisson syntax"); if (!(seqRequest = SDDS_Realloc(seqRequest, sizeof(*seqRequest)*(seqRequests+1)))) SDDS_Bomb("memory allocation failure"); memset(seqRequest+seqRequests, 0, sizeof(*seqRequest)); scanned[iArg].n_items -= 1; memset(seqRequest+seqRequests, 0, sizeof(*seqRequest)); seqRequest[seqRequests].randomizationGroup = -1; seqRequest[seqRequests].mean = 1; if (!scanItemList(&seqRequest[seqRequests].flags, scanned[iArg].list+1, &scanned[iArg].n_items, 0, "columnName", SDDS_STRING, &seqRequest[seqRequests].outputName, 1, SEQ_OUTPUTNAME, "meanValue", SDDS_STRING, &meanPar, 1, 0, "units", SDDS_STRING, &seqRequest[seqRequests].units, 1, SEQ_UNITSGIVEN, NULL)) SDDS_Bomb("invalid -poisson syntax"); seqRequest[seqRequests].flags |= SEQ_DIRECT_POISSON; if (!(seqRequest[seqRequests].flags&SEQ_OUTPUTNAME) || !(seqRequest[seqRequests].outputName)) SDDS_Bomb("columnName is not provided for uniform distribution/"); if (meanPar) { if (wild_match(meanPar, "@*")) SDDS_CopyString(&seqRequest[seqRequests].meanPar, meanPar+1); else if (!get_double(&seqRequest[seqRequests].mean, meanPar)) SDDS_Bomb("Invalid value given for mean value of -poisson distribution."); free(meanPar); meanPar = NULL; } seqRequests ++; scanned[iArg].n_items += 1; break; case CLO_SAMPLES: if (scanned[iArg].n_items!=2 || sscanf(scanned[iArg].list[1], "%ld", &samples)!=1 || samples<=0) SDDS_Bomb("invalid -samples syntax"); break; case CLO_SEED: if (scanned[iArg].n_items!=2 || sscanf(scanned[iArg].list[1], "%ld", &randomNumberSeed)!=1 || randomNumberSeed<=0) SDDS_Bomb("invalid -seed syntax"); break; case CLO_PIPE: if (!processPipeOption(scanned[iArg].list+1, scanned[iArg].n_items-1, &pipeFlags)) SDDS_Bomb("invalid -pipe syntax"); break; case CLO_VERBOSE: verbose = 1; break; case CLO_OPTIMAL_HALTON: optimalHalton = 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"); } } if (!seqRequests) SDDS_Bomb("give one or more -columns options"); if (samples<1) SDDS_Bomb("-samples option not given"); for (i=0; i<seqRequests; i++) { if (!(seqRequest[i].flags& (SEQ_DATAFILE|SEQ_DIRECT_GAUSSIAN|SEQ_DIRECT_UNIFORM|SEQ_DIRECT_POISSON))) break; } if (i==seqRequests) { /* all columns options have either their own input files or else use * one of the "direct" distributions. Hence, we don't expect an input * file. */ if (!input) pipeFlags |= USE_STDIN; /* not really, but fakes out processFilenames */ if (input && !output) { output = input; input = NULL; pipeFlags |= USE_STDIN; if (fexists(output)) { sprintf(msgBuffer, "%s exists already (sddssampledist)", output); SDDS_Bomb(msgBuffer); } } } processFilenames("sddssampledist", &input, &output, pipeFlags, 0, NULL); if (!SDDS_InitializeOutput(&SDDSout, SDDS_BINARY, 0, NULL, NULL, output)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (verbose) fprintf(stderr, "Initialized output file %s\n", output); /* open and check input files */ for (i=mainInputOpened=0; i<seqRequests; i++) { if (seqRequest[i].flags&SEQ_DIRECT_GAUSSIAN) { if (seqRequest[i].meanPar || seqRequest[i].sigmaPar) { if (!mainInputOpened) { if (!SDDS_InitializeInput(&SDDSin, input) || !SDDS_TransferAllParameterDefinitions(&SDDSout, &SDDSin, 0)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); mainInputOpened = 1; } requireInput = 1; SDDSptr = &SDDSin; if ((seqRequest[i].meanPar && SDDS_CheckParameter(SDDSptr, seqRequest[i].meanPar, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK) || (seqRequest[i].sigmaPar && SDDS_CheckParameter(SDDSptr, seqRequest[i].sigmaPar, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } } if (!SDDS_DefineSimpleColumn(&SDDSout, seqRequest[i].outputName, NULL, SDDS_DOUBLE)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } else if (seqRequest[i].flags&SEQ_DIRECT_UNIFORM) { if (seqRequest[i].minPar || seqRequest[i].maxPar) { if (!mainInputOpened) { if (!SDDS_InitializeInput(&SDDSin, input) || !SDDS_TransferAllParameterDefinitions(&SDDSout, &SDDSin, 0)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); mainInputOpened = 1; } requireInput = 1; SDDSptr = &SDDSin; if ((seqRequest[i].minPar && SDDS_CheckParameter(SDDSptr, seqRequest[i].minPar, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK) || (seqRequest[i].maxPar && SDDS_CheckParameter(SDDSptr, seqRequest[i].maxPar, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } } if (!SDDS_DefineSimpleColumn(&SDDSout, seqRequest[i].outputName, NULL, SDDS_DOUBLE)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } else if (seqRequest[i].flags&SEQ_DIRECT_POISSON) { if (seqRequest[i].meanPar) { if (!mainInputOpened) { if (!SDDS_InitializeInput(&SDDSin, input) || !SDDS_TransferAllParameterDefinitions(&SDDSout, &SDDSin, 0)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); mainInputOpened = 1; } requireInput = 1; SDDSptr = &SDDSin; if ( SDDS_CheckParameter(SDDSptr, seqRequest[i].meanPar, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } } if (!SDDS_DefineSimpleColumn(&SDDSout, seqRequest[i].outputName, NULL, SDDS_LONG)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } else { if (seqRequest[i].flags&SEQ_RANDOMIZE) { long newGroupID=0; /* define randomization groups */ if (seqRequest[i].flags&SEQ_RANDOMGROUP) { newGroupID = seqRequest[i].randomizationGroup; for (j=0; j<randomizationGroups; j++) if (randomizationData[j].group==newGroupID) { newGroupID = 0; break; } } else { seqRequest[i].randomizationGroup = newGroupID = -(i+1); } if (newGroupID!=0) { if (!(randomizationData = SDDS_Realloc(randomizationData, sizeof(*randomizationData)*(randomizationGroups+1)))) SDDS_Bomb("memory allocation failure"); randomizationData[randomizationGroups].group = newGroupID; randomizationData[randomizationGroups].order = NULL; randomizationGroups ++; } } if (seqRequest[i].flags&SEQ_DATAFILE) { if (!SDDS_InitializeInput(&seqRequest[i].SDDSin, seqRequest[i].dataFileName)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); SDDSptr = &seqRequest[i].SDDSin; } else { if (!mainInputOpened) { if (!SDDS_InitializeInput(&SDDSin, input) || !SDDS_TransferAllParameterDefinitions(&SDDSout, &SDDSin, 0)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); mainInputOpened = 1; } requireInput = 1; SDDSptr = &SDDSin; } if (SDDS_CheckColumn(SDDSptr, seqRequest[i].indepName, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK || ((seqRequest[i].flags&SEQ_CDFNAME) && SDDS_CheckColumn(SDDSptr, seqRequest[i].CDFName, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK) || ((seqRequest[i].flags&SEQ_DFNAME) && SDDS_CheckColumn(SDDSptr, seqRequest[i].DFName, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK) || !SDDS_TransferColumnDefinition(&SDDSout, SDDSptr, seqRequest[i].indepName, seqRequest[i].outputName)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } } if (seqRequest[i].flags&SEQ_UNITSGIVEN && !SDDS_ChangeColumnInformation(&SDDSout, "units", seqRequest[i].units, SDDS_SET_BY_NAME, seqRequest[i].outputName)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (verbose) fprintf(stderr, "Initialized input files\n"); if (!SDDS_WriteLayout(&SDDSout)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (randomNumberSeed==0) { randomNumberSeed = (long)time((time_t *) NULL); randomNumberSeed = 2*(randomNumberSeed/2) + 1; #if defined(_WIN32) random_1(-labs(randomNumberSeed)); #else random_1(-FABS(randomNumberSeed)); #endif } else random_1(-randomNumberSeed); if (!((sample = calloc(sizeof(*sample), samples)))) SDDS_Bomb("memory allocation failure"); while (1) { if (verbose) fprintf(stderr, "Beginning page loop\n"); if (input && SDDS_ReadPage(&SDDSin)<=0) break; for (i=0; i<seqRequests; i++) { if (seqRequest[i].flags&SEQ_DATAFILE && SDDS_ReadPage(&seqRequest[i].SDDSin)<=0) break; } if (i!=seqRequests) break; if (!SDDS_StartPage(&SDDSout, samples) || (input && !SDDS_CopyParameters(&SDDSout, &SDDSin))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (verbose) fprintf(stderr, "Defining randomization tables\n"); /* define randomization tables */ for (i=0; i<randomizationGroups; i++) { if (!(randomizationData[i].order= SDDS_Malloc(sizeof(*randomizationData[i].order)*samples))) SDDS_Bomb("memory allocation failure"); for (j=0; j<samples; j++) randomizationData[i].order[j] = j; randomizeOrder((char*)randomizationData[i].order, sizeof(*randomizationData[i].order), samples, 0, random_1); } if (verbose) fprintf(stderr, "Beginning loop over sequence requests\n"); for (i=0; i<seqRequests; i++) { if (verbose) fprintf(stderr, "Processing sequence request %ld\n", i); if (seqRequest[i].flags&SEQ_DIRECT_GAUSSIAN) { if ((seqRequest[i].meanPar && !SDDS_GetParameterAsDouble(&SDDSin, seqRequest[i].meanPar, &seqRequest[i].mean)) || (seqRequest[i].sigmaPar && !SDDS_GetParameterAsDouble(&SDDSin, seqRequest[i].sigmaPar, &seqRequest[i].sigma))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (j=0; j<samples; j++) sample[j] = gauss_rn_lim(seqRequest[i].mean, seqRequest[i].sigma, -1, random_1); } else if (seqRequest[i].flags&SEQ_DIRECT_UNIFORM) { if ((seqRequest[i].minPar && !SDDS_GetParameterAsDouble(&SDDSin, seqRequest[i].minPar, &seqRequest[i].min)) || (seqRequest[i].maxPar && !SDDS_GetParameterAsDouble(&SDDSin, seqRequest[i].maxPar, &seqRequest[i].max))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (j=0; j<samples; j++) sample[j] = seqRequest[i].min + (seqRequest[i].max - seqRequest[i].min) * random_1(1); } else if (seqRequest[i].flags&SEQ_DIRECT_POISSON) { double *pos_x, *pos_cdf, CDF; long pos_points, code; pos_x = pos_cdf = NULL; if ((seqRequest[i].meanPar && !SDDS_GetParameterAsDouble(&SDDSin, seqRequest[i].meanPar, &seqRequest[i].mean))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); pos_points = CreatePoissonDistributionTable(&pos_x, &pos_cdf, seqRequest[i].mean); for (j=0; j<samples; j++) { CDF = random_1(1); sample[j]= (int)(interp(pos_x, pos_cdf, pos_points, CDF, 0, 1, &code)); /* fprintf(stderr, "%d, cdf=%f, sample=%f\n", j, CDF, sample[j]);*/ } free(pos_x); free(pos_cdf); } else { if (input && !(seqRequest[i].flags&SEQ_DATAFILE)) SDDSptr = &SDDSin; else SDDSptr = &seqRequest[i].SDDSin; if ((values = SDDS_CountRowsOfInterest(SDDSptr))) { if (!(IVValue = SDDS_GetColumnInDoubles(SDDSptr, seqRequest[i].indepName)) || (seqRequest[i].flags&SEQ_CDFNAME && !(CDFValue = SDDS_GetColumnInDoubles(SDDSptr, seqRequest[i].CDFName))) || (seqRequest[i].flags&SEQ_DFNAME && !(CDFValue = SDDS_GetColumnInDoubles(SDDSptr, seqRequest[i].DFName)))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } else { sprintf(msgBuffer, "empty page for file %s\n", seqRequest[i].flags&SEQ_DATAFILE? seqRequest[i].dataFileName:input); SDDS_Bomb(msgBuffer); } if (verbose) fprintf(stderr, "Checking and converting CDF/DF values\n"); /* check/convert CDF/DF values */ for (j=1; j<values; j++) { if (IVValue[j-1]>IVValue[j]) { sprintf(msgBuffer, "random variate values not monotonically increasing for %s", seqRequest[i].flags&SEQ_DATAFILE? seqRequest[i].dataFileName:input); SDDS_Bomb(msgBuffer); } if (seqRequest[i].flags&SEQ_DFNAME) /* convert DF to CDF */ CDFValue[j] += CDFValue[j-1]; if (CDFValue[j] < CDFValue[j-1]) { sprintf(msgBuffer, "CDF values decreasing for %s", seqRequest[i].flags&SEQ_DATAFILE? seqRequest[i].dataFileName:input); SDDS_Bomb(msgBuffer); } } if (verbose) fprintf(stderr, "Normalizing CDF\n"); /* normalize the CDF */ if (CDFValue[values-1]<=0) { sprintf(msgBuffer, "CDF not valid for %s\n", seqRequest[i].dataFileName); SDDS_Bomb(msgBuffer); } for (j=0; j<values; j++) CDFValue[j] /= CDFValue[values-1]; if (seqRequest[i].flags&SEQ_HALTONRADIX) { if (verbose) fprintf(stderr, "Starting halton sequence, offset=%" PRId32 "\n", seqRequest[i].haltonOffset); if (!optimalHalton) haltonID = startHaltonSequence(&seqRequest[i].haltonRadix, 0.5); else haltonID = startModHaltonSequence(&seqRequest[i].haltonRadix, 0); while (seqRequest[i].haltonOffset-- >0) { if (!optimalHalton) nextHaltonSequencePoint(haltonID); else nextModHaltonSequencePoint(haltonID); } } if (verbose) fprintf(stderr, "Generating samples\n"); for (j=0; j<samples; j++) { double CDF; long code; while (1) { if (seqRequest[i].flags&SEQ_HALTONRADIX) { if (!optimalHalton) CDF = nextHaltonSequencePoint(haltonID); else CDF = nextModHaltonSequencePoint(haltonID); } else CDF = random_1(1); if (CDF<=CDFValue[values-1] && CDF>=CDFValue[0]) break; } sample[j] = seqRequest[i].factor*interp(IVValue, CDFValue, values, CDF, 0, 1, &code) + seqRequest[i].offset; } if (seqRequest[i].flags&SEQ_RANDOMIZE) { long k, l; double *sample1; if (verbose) fprintf(stderr, "Randomizing order of values\n"); if (!(sample1 = malloc(sizeof(*sample1)*samples))) SDDS_Bomb("memory allocation failure"); for (l=0; l<randomizationGroups; l++) if (randomizationData[l].group==seqRequest[i].randomizationGroup) break; if (l==randomizationGroups) SDDS_Bomb("problem with construction of randomization groups!"); for (k=0; k<samples; k++) sample1[k] = sample[randomizationData[l].order[k]]; free(sample); sample = sample1; } free(IVValue); free(CDFValue); } if (verbose) fprintf(stderr, "Setting SDDS column values\n"); if (!SDDS_SetColumnFromDoubles(&SDDSout, SDDS_SET_BY_NAME, sample, samples, seqRequest[i].outputName? seqRequest[i].outputName: seqRequest[i].indepName)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (verbose) fprintf(stderr, "Writing data page\n"); if (!SDDS_WritePage(&SDDSout)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!requireInput) break; } if (verbose) fprintf(stderr, "Exited read loop\n"); free(sample); if ((input && !SDDS_Terminate(&SDDSin)) || !SDDS_Terminate(&SDDSout)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (i=0; i<seqRequests; i++) { if (seqRequest[i].dataFileName) free(seqRequest[i].dataFileName); if (seqRequest[i].indepName) free(seqRequest[i].indepName); if (seqRequest[i].outputName) free(seqRequest[i].outputName); if (seqRequest[i].DFName) free(seqRequest[i].DFName); if (seqRequest[i].CDFName) free(seqRequest[i].CDFName); if (seqRequest[i].units) free(seqRequest[i].units); if (seqRequest[i].meanPar) free(seqRequest[i].meanPar); if (seqRequest[i].sigmaPar) free(seqRequest[i].sigmaPar); if (seqRequest[i].minPar) free(seqRequest[i].minPar); if (seqRequest[i].maxPar) free(seqRequest[i].maxPar); if (seqRequest[i].flags&SEQ_DATAFILE && !SDDS_Terminate(&(seqRequest[i].SDDSin))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } free(seqRequest); for (i=0; i<randomizationGroups; i++) free(randomizationData[i].order); if (randomizationData) free(randomizationData); free_scanargs(&scanned, argc); return(0); }
int main(int argc, char **argv) { char *input, *output; char **copyColumnName, **usersCopyColumnName; GROUPS *group; int32_t copyColumns; long usersCopyColumns, groups; long iArg, i, rows, readCode, items; unsigned long flags, pipeFlags; SCANNED_ARG *scArg; SDDS_DATASET SDDSin, SDDSout; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&scArg, argc, argv); if (argc<2) bomb(USAGE, NULL); output = input = NULL; flags = pipeFlags = 0; group = NULL; copyColumnName = usersCopyColumnName = NULL; usersCopyColumns = copyColumns = groups = 0; for (iArg=1; iArg<argc; iArg++) { if (scArg[iArg].arg_type==OPTION) { /* process options here */ switch (match_string(scArg[iArg].list[0], option, N_OPTIONS, 0)) { case SET_PIPE: if (!processPipeOption(scArg[iArg].list+1, scArg[iArg].n_items-1, &pipeFlags)) SDDS_Bomb("invalid -pipe syntax"); break; case SET_GROUP: if ((items = scArg[iArg].n_items-1)<2) SDDS_Bomb("invalid -group syntax"); if (!(group = SDDS_Realloc(group, sizeof(*group)*(groups+1))) || !SDDS_CopyString(&group[groups].newName, scArg[iArg].list[1]) || !(group[groups].usersOldName = SDDS_Malloc(sizeof(*group[groups].usersOldName)* (group[groups].usersOldNames=items-1))) || !SDDS_CopyStringArray(group[groups].usersOldName, scArg[iArg].list+2, group[groups].usersOldNames)) SDDS_Bomb("memory allocation failure"); group[groups].oldName = NULL; group[groups].oldNames = 0; groups++; break; case SET_COPY: if (usersCopyColumns) SDDS_Bomb("give -copy only once"); if ((usersCopyColumns=scArg[iArg].n_items-1)<1) SDDS_Bomb("invalid -copy syntax"); if (!(usersCopyColumnName = SDDS_Malloc(sizeof(*usersCopyColumnName)*usersCopyColumns)) || !SDDS_CopyStringArray(usersCopyColumnName, scArg[iArg].list+1, usersCopyColumns)) SDDS_Bomb("memory allocation failure"); break; default: fprintf(stderr, "error: unknown/ambiguous option: %s\n", scArg[iArg].list[0]); exit(1); break; } } else { if (!input) input = scArg[iArg].list[0]; else if (!output) output = scArg[iArg].list[0]; else SDDS_Bomb("too many filenames seen"); } } if (groups==0) SDDS_Bomb("no groups defined"); processFilenames("sddsseparate", &input, &output, pipeFlags, 0, NULL); if (!SDDS_InitializeInput(&SDDSin, input)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (usersCopyColumns) { SDDS_SetColumnFlags(&SDDSin, 0); for (i=0; i<usersCopyColumns; i++) SDDS_SetColumnsOfInterest(&SDDSin, SDDS_MATCH_STRING, usersCopyColumnName[i], SDDS_OR); if (!(copyColumnName = SDDS_GetColumnNames(&SDDSin, ©Columns)) || copyColumns==0) SDDS_Bomb("no match for copy columns"); } for (i=0; i<groups; i++) { long j, type=0; SDDS_SetColumnFlags(&SDDSin, 0); for (j=0; j<group[i].usersOldNames; j++) SDDS_SetColumnsOfInterest(&SDDSin, SDDS_MATCH_STRING, group[i].usersOldName[j], SDDS_OR); if (!(group[i].oldName = SDDS_GetColumnNames(&SDDSin, &group[i].oldNames))) { fprintf(stderr, "No match for group %s (sddsseparate)\n", group[i].newName); exit(1); } if (i && group[i-1].oldNames!=group[i].oldNames) { fprintf(stderr, "Group %s comprises %" PRId32 " columns, whereas the last group comprises %" PRId32 " (sddsseparate)\n", group[i].newName, group[i].oldNames, group[i-1].oldNames); exit(1); } type = SDDS_GetColumnType(&SDDSin, SDDS_GetColumnIndex(&SDDSin, group[i].oldName[0])); for (j=1; j<group[i].oldNames; j++) { if (type != SDDS_GetColumnType(&SDDSin, SDDS_GetColumnIndex(&SDDSin, group[i].oldName[j]))) { fprintf(stderr, "Inconsistent data types in group %s (sddsseparate)\n", group[i].newName); fprintf(stderr, "First inconsistent column is %s\n", group[i].oldName[j]); exit(1); } } } if (!SDDS_InitializeOutput(&SDDSout, SDDS_BINARY, 0, NULL, NULL, output) || !SDDS_TransferAllParameterDefinitions(&SDDSout, &SDDSin, 0)) SDDS_Bomb("problem initializing output file"); for (i=0; i<copyColumns; i++) if (!SDDS_TransferColumnDefinition(&SDDSout, &SDDSin, copyColumnName[i], NULL)) SDDS_Bomb("problem transferring copy column definitions to output file"); for (i=0; i<groups; i++) { char *name; if (!SDDS_TransferColumnDefinition(&SDDSout, &SDDSin, group[i].oldName[0], group[i].newName)) { fprintf(stderr, "Problem transferring column %s as %s to output file (sddsseparate)\n", group[i].oldName[0], group[i].newName); exit(1); } if (!(group[i].parameterName = SDDS_Malloc(sizeof(*name)*(strlen(group[i].newName)+100)))) SDDS_Bomb("memory allocation failure"); sprintf(group[i].parameterName, "%sSourceColumn", group[i].newName); if (!SDDS_DefineSimpleParameter(&SDDSout, group[i].parameterName, NULL, SDDS_STRING)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_WriteLayout(&SDDSout)) SDDS_Bomb("problem writing layout to output file"); while ((readCode=SDDS_ReadPage(&SDDSin))>0) { if ((rows = SDDS_CountRowsOfInterest(&SDDSin))<0) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!rows) continue; for (i=0; i<group[0].oldNames; i++) { long ic, ig; if (!SDDS_StartPage(&SDDSout, rows) || !SDDS_CopyParameters(&SDDSout, &SDDSin)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (ic=0; ic<copyColumns; ic++) { void *data; if (!(data = SDDS_GetInternalColumn(&SDDSin, copyColumnName[ic])) || !SDDS_SetColumn(&SDDSout, SDDS_SET_BY_NAME, data, rows, copyColumnName[ic])) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } for (ig=0; ig<groups; ig++) { void *data; if (!SDDS_SetParameters(&SDDSout, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, group[ig].parameterName, group[ig].oldName[i], NULL) || !(data = SDDS_GetInternalColumn(&SDDSin, group[ig].oldName[i])) || !SDDS_SetColumn(&SDDSout, SDDS_SET_BY_NAME, data, rows, group[ig].newName)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_WritePage(&SDDSout)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } if (!SDDS_Terminate(&SDDSin)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (!SDDS_Terminate(&SDDSout)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } return 0; }
int main(int argc, char **argv) { char *indepQuantity, **depenQuantity, **exclude, **depenQuantityPair; long depenQuantities, excludes; char *input, *output; long iArg, i, j, rows, readCode, noWarnings, items; long rowsToUse; unsigned long flags, pairFlags, tmpFlags, pipeFlags; SCANNED_ARG *scArg; SDDS_DATASET SDDSin, SDDSout; double *tdata, *data, t0, dt; double fracRMSChangeLimit, fracFreqAccuracyLimit; int32_t frequenciesDesired, maxFrequencies, freqCycleLimit; short truncate; double *frequency, *amplitude=NULL, *phase=NULL, *significance=NULL, *phase1=NULL, *amplitude1=NULL, *significance1=NULL; long frequenciesFound; long *frequencyIndex, *amplitudeIndex, *phaseIndex, *significanceIndex, pairs; long *amplitudeIndex1, *phaseIndex1, *significanceIndex1; SDDS_RegisterProgramName(argv[0]); #ifdef DEBUG if (1) { long code; double x, y; x = 1.1; code = OneDFunctionOptimize(&y, &x, 0.07, -4, 4, trialFn, 50, 1e-6, 0, 1); fprintf(stderr, "code: %ld x=%e, y=%e\n", code, x, y); x = .9; code = OneDFunctionOptimize(&y, &x, 0.15, -4, 4, trialFn, 50, 1e-6, 0, 1); fprintf(stderr, "code: %ld x=%e, y=%e\n", code, x, y); x = .999; code = OneDFunctionOptimize(&y, &x, 0.11, -4, 4, trialFn, 50, 1e-6, 0, 1); fprintf(stderr, "code: %ld x=%e, y=%e\n", code, x, y); exit(0); } #endif argc = scanargs(&scArg, argc, argv); if (argc<3) { fprintf(stderr, "%s%s", USAGE1, USAGE2); exit(1); } output = input = NULL; flags = pipeFlags = excludes = truncate = pairFlags= 0; indepQuantity = NULL; depenQuantity = exclude = depenQuantityPair=NULL; depenQuantities = 0; noWarnings = 0; fracRMSChangeLimit = 0.0; fracFreqAccuracyLimit = 0.00001; frequenciesDesired = 1; maxFrequencies = 4; freqCycleLimit = 100; pairs=0; for (iArg=1; iArg<argc; iArg++) { if (scArg[iArg].arg_type==OPTION) { /* process options here */ switch (match_string(scArg[iArg].list[0], option, N_OPTIONS, 0)) { case SET_TRUNCATE: truncate = 1; break; case SET_PAIR: if (depenQuantities) SDDS_Bomb("Invalid -pair option, the depent-quantity is provided by -column option already."); if (scArg[iArg].n_items!=3) SDDS_Bomb("invalid -pair syntax"); depenQuantity = SDDS_Realloc(depenQuantity, sizeof(*depenQuantity)*(pairs+1)); depenQuantityPair = SDDS_Realloc(depenQuantityPair, sizeof(*depenQuantityPair)*(pairs+1)); depenQuantity[pairs] = scArg[iArg].list[1]; depenQuantityPair[pairs] = scArg[iArg].list[2]; pairs++; break; case SET_COLUMN: if (indepQuantity) SDDS_Bomb("only one -column option may be given"); if (scArg[iArg].n_items<2) SDDS_Bomb("invalid -column syntax"); indepQuantity = scArg[iArg].list[1]; if (scArg[iArg].n_items>=2) { if (pairs) SDDS_Bomb("Invalid -column syntax, the depent-quantity is provided by -pair option already."); depenQuantity = tmalloc(sizeof(*depenQuantity)*(depenQuantities=scArg[iArg].n_items-2)); for (i=0; i<depenQuantities; i++) depenQuantity[i] = scArg[iArg].list[i+2]; } break; case SET_PIPE: if (!processPipeOption(scArg[iArg].list+1, scArg[iArg].n_items-1, &pipeFlags)) SDDS_Bomb("invalid -pipe syntax"); break; case SET_EXCLUDE: if (scArg[iArg].n_items<2) SDDS_Bomb("invalid -exclude syntax"); moveToStringArray(&exclude, &excludes, scArg[iArg].list+1, scArg[iArg].n_items-1); break; case SET_NOWARNINGS: noWarnings = 1; break; case SET_TERM_SEARCH: items = scArg[iArg].n_items-1; flags &= ~(NAFF_RMS_CHANGE_LIMIT|NAFF_FREQS_DESIRED|NAFF_MAX_FREQUENCIES); fracRMSChangeLimit = 0; frequenciesDesired = 0; maxFrequencies = 10; if (!scanItemList(&tmpFlags, scArg[iArg].list+1, &items, 0, "changelimit", SDDS_DOUBLE, &fracRMSChangeLimit, 1, NAFF_RMS_CHANGE_LIMIT, "maxfrequencies", SDDS_LONG, &maxFrequencies, 1, NAFF_MAX_FREQUENCIES, "frequencies", SDDS_LONG, &frequenciesDesired, 1, NAFF_FREQS_DESIRED, NULL) || (tmpFlags&NAFF_RMS_CHANGE_LIMIT && tmpFlags&NAFF_FREQS_DESIRED) || maxFrequencies<1) SDDS_Bomb("invalid -terminateSearch syntax"); flags |= tmpFlags; if (frequenciesDesired) maxFrequencies = frequenciesDesired; break; case SET_ITERATE_FREQ: items = scArg[iArg].n_items - 1; flags &= ~(NAFF_FREQ_CYCLE_LIMIT|NAFF_FREQ_ACCURACY_LIMIT); if (!scanItemList(&tmpFlags, scArg[iArg].list+1, &items, 0, "cyclelimit", SDDS_LONG, &freqCycleLimit, 1, NAFF_FREQ_CYCLE_LIMIT, "accuracylimit", SDDS_DOUBLE, &fracFreqAccuracyLimit, 1, NAFF_FREQ_ACCURACY_LIMIT, NULL) || !bitsSet(tmpFlags) || freqCycleLimit<2) SDDS_Bomb("invalid -iterateFrequency syntax"); flags |= tmpFlags; break; default: fprintf(stderr, "error: unknown/ambiguous option: %s\n", scArg[iArg].list[0]); exit(1); break; } } else { if (!input) input = scArg[iArg].list[0]; else if (!output) output = scArg[iArg].list[0]; else SDDS_Bomb("too many filenames seen"); } } processFilenames("sddsnaff", &input, &output, pipeFlags, 0, NULL); if (!indepQuantity) SDDS_Bomb("supply the independent quantity name with the -columns option"); if (!SDDS_InitializeInput(&SDDSin, input)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (SDDS_CheckColumn(&SDDSin, indepQuantity, NULL, SDDS_ANY_NUMERIC_TYPE, stderr) !=SDDS_CHECK_OKAY) exit(1); excludes = appendToStringArray(&exclude, excludes, indepQuantity); if (pairs) { pairFlags = flags | NAFF_FREQ_FOUND; depenQuantities = pairs; } if (!depenQuantities) depenQuantities = appendToStringArray(&depenQuantity, depenQuantities, "*"); if (!pairs) { 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 quantities selected to fft"); } } if (!SetupNAFFOutput(&SDDSout, output, &SDDSin, indepQuantity, depenQuantities, depenQuantity, &frequencyIndex, &litudeIndex, &phaseIndex, &significanceIndex, depenQuantityPair,&litudeIndex1, &phaseIndex1,&significanceIndex1)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!(frequency = SDDS_Malloc(sizeof(*frequency)*maxFrequencies)) || !(amplitude = SDDS_Malloc(sizeof(*amplitude)*maxFrequencies)) || !(phase = SDDS_Malloc(sizeof(*phase )*maxFrequencies)) || !(significance= SDDS_Malloc(sizeof(*significance)*maxFrequencies))) SDDS_Bomb("memory allocation failure"); if (pairs) { if (!(amplitude1 = SDDS_Malloc(sizeof(*amplitude1)*maxFrequencies)) || !(phase1 = SDDS_Malloc(sizeof(*phase1 )*maxFrequencies)) || !(significance1 = SDDS_Malloc(sizeof(*significance1)*maxFrequencies))) SDDS_Bomb("memory allocation failure"); } while ((readCode=SDDS_ReadPage(&SDDSin))>0) { if ((rows = SDDS_CountRowsOfInterest(&SDDSin))<0) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (rows) { long primeRows; rowsToUse = rows; primeRows = greatestProductOfSmallPrimes(rows); if (rows!=primeRows) { if (truncate) rowsToUse = greatestProductOfSmallPrimes(rows); else if (largest_prime_factor(rows)>100 && !noWarnings) fputs("Warning: number of points has large prime factors.\nThis could take a very long time.\nConsider using the -truncate option.\n", stderr); } if (!SDDS_StartPage(&SDDSout, maxFrequencies) || !SDDS_CopyParameters(&SDDSout, &SDDSin)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!(tdata = SDDS_GetColumnInDoubles(&SDDSin, indepQuantity))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (i=1; i<rowsToUse; i++) if (tdata[i]<=tdata[i-1]) SDDS_Bomb("independent data is not monotonically increasing"); dt = (tdata[rowsToUse-1]-tdata[0])/(rowsToUse-1.0), t0 = tdata[0]; free(tdata); for (i=0; i<depenQuantities; i++) { if (!(data = SDDS_GetColumnInDoubles(&SDDSin, depenQuantity[i]))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (j=0; j<maxFrequencies; j++) frequency[j] = amplitude[j] = phase[j] = significance[j] = -1; frequenciesFound = PerformNAFF(frequency, amplitude, phase, significance, t0, dt, data, rowsToUse, flags, fracRMSChangeLimit, maxFrequencies, freqCycleLimit, fracFreqAccuracyLimit, 0, 0); #ifdef DEBUG fprintf(stderr, "Column %s: ", depenQuantity[i]); fprintf(stderr, "f=%10.3e a=%10.3e p=%10.3e s=%10.3e\n", frequency[0], amplitude[0], phase[0], significance[0]); #endif free(data); data = NULL; if (pairs) { if (!(data = SDDS_GetColumnInDoubles(&SDDSin, depenQuantityPair[i]))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); frequenciesFound = PerformNAFF(frequency, amplitude1, phase1, significance1, t0, dt, data, rowsToUse, pairFlags, fracRMSChangeLimit, maxFrequencies, freqCycleLimit, fracFreqAccuracyLimit, 0, 0); for (j=0; j<maxFrequencies; j++) if (frequency[j]!=-1) frequency[j] = adjustFrequencyHalfPlane(frequency[j], phase[j], phase1[j], dt); free(data); data = NULL; } if (!SDDS_SetColumn(&SDDSout, SDDS_SET_BY_INDEX, frequency, maxFrequencies, frequencyIndex[i]) || !SDDS_SetColumn(&SDDSout, SDDS_SET_BY_INDEX, amplitude, maxFrequencies, amplitudeIndex[i]) || !SDDS_SetColumn(&SDDSout, SDDS_SET_BY_INDEX, phase, maxFrequencies, phaseIndex[i]) || !SDDS_SetColumn(&SDDSout, SDDS_SET_BY_INDEX, significance, maxFrequencies, significanceIndex[i])) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (pairs) { if (!SDDS_SetColumn(&SDDSout, SDDS_SET_BY_INDEX, amplitude1, maxFrequencies, amplitudeIndex1[i]) || !SDDS_SetColumn(&SDDSout, SDDS_SET_BY_INDEX, phase1, maxFrequencies, phaseIndex1[i]) || !SDDS_SetColumn(&SDDSout, SDDS_SET_BY_INDEX, significance1, maxFrequencies, significanceIndex1[i])) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } } else { if (!SDDS_StartPage(&SDDSout, 0) || !SDDS_CopyParameters(&SDDSout, &SDDSin)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_WritePage(&SDDSout)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_Terminate(&SDDSin)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (!SDDS_Terminate(&SDDSout)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } free(frequency); free(amplitude); free(phase); free(significance); if (pairs) { free(amplitude1); free(phase1); free(significance1); free(amplitudeIndex1); free(phaseIndex1); free(significanceIndex1); free(depenQuantityPair); } free(depenQuantity); free(frequencyIndex); free(amplitudeIndex); free(phaseIndex); free(significanceIndex); return 0; }
int main(int argc, char **argv) { SDDS_DATASET SDDS_1, SDDS_2, SDDS_output; long i, j, i_arg, rows1, rows2, reuse, reusePage, i1, i2; SCANNED_ARG *s_arg; char s[200], *ptr; char **match_column, **equate_column; long match_columns, equate_columns; char *input1, *input2, *output; long tmpfile_used, retval1, retval2; long warnings, invert; unsigned long pipeFlags; KEYED_EQUIVALENT **keyGroup=NULL; long keyGroups=0; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&s_arg, argc, argv); if (argc<3) bomb(NULL, USAGE); input1 = input2 = output = NULL; match_column = equate_column = NULL; match_columns = equate_columns = reuse = reusePage = 0; tmpfile_used = invert = 0; warnings = 1; pipeFlags = 0; for (i_arg=1; i_arg<argc; i_arg++) { if (s_arg[i_arg].arg_type==OPTION) { delete_chars(s_arg[i_arg].list[0], "_"); switch (match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) { case SET_MATCH_COLUMN: if (s_arg[i_arg].n_items!=2) SDDS_Bomb("invalid -match syntax"); if (match_columns!=0) SDDS_Bomb("only one -match option may be given"); match_column = tmalloc(sizeof(*match_column)*2); if ((ptr=strchr(s_arg[i_arg].list[1], '='))) *ptr++ = 0; else ptr = s_arg[i_arg].list[1]; match_column[0] = s_arg[i_arg].list[1]; match_column[1] = ptr; match_columns = 1; break; case SET_EQUATE_COLUMN: if (s_arg[i_arg].n_items!=2) SDDS_Bomb("invalid -equate syntax"); if (equate_columns!=0) SDDS_Bomb("only one -equate option may be given"); equate_column = tmalloc(sizeof(*equate_column)*2); if ((ptr=strchr(s_arg[i_arg].list[1], '='))) *ptr++ = 0; else ptr = s_arg[i_arg].list[1]; equate_column[0] = s_arg[i_arg].list[1]; equate_column[1] = ptr; equate_columns = 1; break; case SET_REUSE: if (s_arg[i_arg].n_items==1) reuse = 1; else { char *reuseOptions[2] = {"rows", "page"}; for (i=1; i<s_arg[i_arg].n_items; i++) { switch (match_string(s_arg[i_arg].list[i], reuseOptions, 2, 0)) { case 0: reuse = 1; break; case 1: reusePage = 1; break; default: SDDS_Bomb("unknown reuse keyword"); break; } } } break; case SET_INVERT: invert = 1; break; case SET_NOWARNINGS: warnings = 0; break; case SET_PIPE: if (!processPipeOption(s_arg[i_arg].list+1, s_arg[i_arg].n_items-1, &pipeFlags)) SDDS_Bomb("invalid -pipe syntax"); break; default: fprintf(stderr, "error: unknown switch: %s\n", s_arg[i_arg].list[0]); SDDS_Bomb(NULL); break; } } else { if (input1==NULL) input1 = s_arg[i_arg].list[0]; else if (input2==NULL) input2 = s_arg[i_arg].list[0]; else if (output==NULL) output = s_arg[i_arg].list[0]; else SDDS_Bomb("too many filenames"); } } if (pipeFlags&USE_STDIN && input1) { if (output) SDDS_Bomb("too many filenames (sddsxref)"); output = input2; input2 = input1; input1 = NULL; } processFilenames("sddsselect", &input1, &output, pipeFlags, !warnings, &tmpfile_used); if (!input2) SDDS_Bomb("second input file not specified (sddsxref)"); if (equate_columns && match_columns) SDDS_Bomb("only one of -equate or -match may be given"); if (!equate_columns && !match_columns) SDDS_Bomb("one of -equate or -match must be given"); if (!SDDS_InitializeInput(&SDDS_1, input1)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (!SDDS_InitializeInput(&SDDS_2, input2)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (match_columns) { if ((j=SDDS_GetColumnIndex(&SDDS_1, match_column[0]))<0 || SDDS_GetColumnType(&SDDS_1, j)!=SDDS_STRING) { sprintf(s, "error: column %s not found or not string type in file %s", match_column[0], input1?input1:"stdin"); SDDS_SetError(s); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if ((j=SDDS_GetColumnIndex(&SDDS_2, match_column[1]))<0 || SDDS_GetColumnType(&SDDS_2, j)!=SDDS_STRING) { sprintf(s, "error: column %s not found or not string type in file %s", match_column[1], input2); SDDS_SetError(s); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } if (equate_columns) { if ((j=SDDS_GetColumnIndex(&SDDS_1, equate_column[0]))<0 || !SDDS_NUMERIC_TYPE(SDDS_GetColumnType(&SDDS_1, j))) { sprintf(s, "error: column %s not found or not numeric type in file %s", equate_column[0], input1?input1:"stdin"); SDDS_SetError(s); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if ((j=SDDS_GetColumnIndex(&SDDS_2, equate_column[1]))<0 || !SDDS_NUMERIC_TYPE(SDDS_GetColumnType(&SDDS_2, j))) { sprintf(s, "error: column %s not found or not numeric type in file %s", equate_column[1], input2); SDDS_SetError(s); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } if (output && pipeFlags&USE_STDOUT) SDDS_Bomb("too many filenames with -pipe option"); if (!output && !(pipeFlags&USE_STDOUT)) { if (warnings) fprintf(stderr, "warning: existing file %s will be replaced (sddsselect)\n", input1?input1:"stdin"); tmpfile_used = 1; cp_str(&output, tmpname(NULL)); } if (!SDDS_InitializeCopy(&SDDS_output, &SDDS_1, output, "w")) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (!SDDS_WriteLayout(&SDDS_output)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); while ((retval1=SDDS_ReadPage(&SDDS_1))>0) { if (!reusePage) { if ((retval2=SDDS_ReadPage(&SDDS_2))<=0) { if (warnings) fprintf(stderr, "warning: <input2> ends before <input1>\n"); if (invert) { /* nothing to match, so everything would normally be thrown out */ if (!SDDS_CopyPage(&SDDS_output, &SDDS_1) || !SDDS_WritePage(&SDDS_output)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); continue; } else /* nothing to match, so everything thrown out */ break; } } else { if (retval1==1 && (retval2=SDDS_ReadPage(&SDDS_2))<=0) SDDS_Bomb("<input2> has no data"); SDDS_SetRowFlags(&SDDS_2, 1); } rows1 = SDDS_CountRowsOfInterest(&SDDS_1); rows2 = SDDS_CountRowsOfInterest(&SDDS_2); if (!SDDS_StartPage(&SDDS_output, rows1)) { SDDS_SetError("Problem starting output page"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_CopyParameters(&SDDS_output, &SDDS_2) || !SDDS_CopyArrays(&SDDS_output, &SDDS_2)) { SDDS_SetError("Problem copying parameter or array data from second input file"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_CopyParameters(&SDDS_output, &SDDS_1) || !SDDS_CopyArrays(&SDDS_output, &SDDS_1)) { SDDS_SetError("Problem copying parameter or array data from first input file"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (match_columns) { char **string1, **string2; long matched; string2 = NULL; if (!(string1 = SDDS_GetColumn(&SDDS_1, match_column[0]))) { fprintf(stderr, "Error: problem getting column %s from file %s\n", match_column[0], input1?input1:"stdin"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (rows2 && !(string2 = SDDS_GetColumn(&SDDS_2, match_column[1]))) { fprintf(stderr, "Error: problem getting column %s from file %s\n", match_column[1], input2); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (rows2) keyGroup = MakeSortedKeyGroups(&keyGroups, SDDS_STRING, string2, rows2); for (i1=0; i1<rows1; i1++) { if (!SDDS_CopyRowDirect(&SDDS_output, i1, &SDDS_1, i1)) { sprintf(s, "Problem copying row %ld of first data set", i1); SDDS_SetError(s); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } matched = 0; if (rows2 &&(i2 = FindMatchingKeyGroup(keyGroup, keyGroups, SDDS_STRING, string1+i1, reuse))>=0) { matched = 1; } if ((!matched && !invert) || (matched && invert)) { if (!SDDS_AssertRowFlags(&SDDS_output, SDDS_INDEX_LIMITS, i1, i1, 0)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } if (string1) { for (i=0;i<rows1;i++) free(string1[i]); free(string1); string1 = NULL; } if (string2) { for (i=0;i<rows2;i++) free(string2[i]); free(string2); string2 = NULL; } for (i=0;i<keyGroups;i++) { if (keyGroup[i]) { if (keyGroup[i]->equivalent) free(keyGroup[i]->equivalent); free(keyGroup[i]); keyGroup[i] = NULL; } } if (keyGroups) { free(keyGroup); keyGroup = NULL; keyGroups = 0; } } else if (equate_columns) { double *value1, *value2; long equated; value2 = NULL; if (!(value1 = SDDS_GetColumnInDoubles(&SDDS_1, equate_column[0]))) { fprintf(stderr, "Error: problem getting column %s from file %s\n", equate_column[0], input1?input1:"stdin"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (rows2 && !(value2 = SDDS_GetColumnInDoubles(&SDDS_2, equate_column[1]))) { fprintf(stderr, "Error: problem getting column %s from file %s\n", equate_column[1], input2); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (rows2) keyGroup = MakeSortedKeyGroups(&keyGroups, SDDS_DOUBLE, value2, rows2); for (i1=0; i1<rows1; i1++) { if (!SDDS_CopyRowDirect(&SDDS_output, i1, &SDDS_1, i1)) { sprintf(s, "Problem copying row %ld of first data set", i1); SDDS_SetError(s); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } equated = 0; if (rows2 && (i2 = FindMatchingKeyGroup(keyGroup, keyGroups, SDDS_DOUBLE, value1+i1, reuse))>=0) { equated = 1; } if ((!equated && !invert) || (equated && invert)) { if (!SDDS_AssertRowFlags(&SDDS_output, SDDS_INDEX_LIMITS, i1, i1, 0)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } if (value1) free(value1); value1 = NULL; if (rows2 && value2) free(value2); value2 = NULL; for (i=0;i<keyGroups;i++) { if (keyGroup[i]) { if (keyGroup[i]->equivalent) free(keyGroup[i]->equivalent); free(keyGroup[i]); keyGroup[i] = NULL; } } if (keyGroups) { free(keyGroup); keyGroup = NULL; keyGroups = 0; } } if (!SDDS_WritePage(&SDDS_output)) { SDDS_SetError("Problem writing data to output file"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } if (!SDDS_Terminate(&SDDS_1) || !SDDS_Terminate(&SDDS_2) || !SDDS_Terminate(&SDDS_output)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (tmpfile_used && !replaceFileAndBackUp(input1, output)) exit(1); free_scanargs(&s_arg,argc); if (match_columns) free(match_column); return(0); }