long combineDuplicatePoints(double *x, double **y, long columns, long rows, double tolerance) { double xmin, xmax; long i, j, column; find_min_max(&xmin, &xmax, x, rows); if (xmin==xmax) SDDS_Bomb("interpolation data is invalid--no range in independent variable"); tolerance *= xmax-xmin; for (i=1; i<rows; i++) { if (fabs(x[i]-x[i-1])<=tolerance) { x[i-1] = (x[i]+x[i-1])/2; for (column=0; column<columns; column++) y[column][i-1] = (y[column][i]+y[column][i-1])/2; for (j=i+1; j<rows; j++) { x[j-1] = x[j]; for (column=0; column<columns; column++) y[column][j-1] = y[column][j]; } rows--; i--; } } return rows; }
long appendToStringArray(char ***item, long items, char *newItem) { if (!(*item = SDDS_Realloc(*item, sizeof(**item)*(items+1)))) SDDS_Bomb("allocation failure in appendToStringArray"); if (!SDDS_CopyString((*item)+items, newItem)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } return items+1; }
long CreatePoissonDistributionTable(double **x, double **pos_CDF, double mean) { long i, npoints=20, count=0; double *pos=NULL; SDDS_DATASET pos_out; *x = *pos_CDF = NULL; if (!(*x=malloc(sizeof(**x)*npoints)) || !(pos=malloc(sizeof(*pos)*npoints)) || !(*pos_CDF=malloc(sizeof(**pos_CDF)*npoints))) SDDS_Bomb("memeroy allocation failure."); i = count = 0; while (1) { if (count+2>=npoints) { npoints += 20; *x = SDDS_Realloc(*x, sizeof(**x)*npoints); *pos_CDF = SDDS_Realloc(*pos_CDF, sizeof(**pos_CDF)*npoints); pos = SDDS_Realloc(pos, sizeof(*pos)*npoints); } (*x)[count] = i; if (!i) { pos[i] = exp(-mean); (*pos_CDF)[count] = pos[i]; count ++; } else { pos[i] = pos[i-1]*mean/i; (*pos_CDF)[count] = (*pos_CDF)[count-1]; (*pos_CDF)[count+1] = (*pos_CDF)[count-1] + pos[i]; (*x)[count+1] = i; if (1.0-(*pos_CDF)[count+1]<=1.0e-15) break; count +=2; } i++; } /* fprintf(stderr,"lamda=%f\n", mean); if (!SDDS_InitializeOutput(&pos_out, SDDS_BINARY, 0, NULL, NULL, "pos_dist.sdds")) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!SDDS_DefineSimpleColumn(&pos_out, "Count", NULL, SDDS_DOUBLE) || !SDDS_DefineSimpleColumn(&pos_out, "P", NULL, SDDS_DOUBLE)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!SDDS_SaveLayout(&pos_out) || !SDDS_WriteLayout(&pos_out)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!SDDS_StartPage(&pos_out, count) || !SDDS_SetColumnFromDoubles(&pos_out, SDDS_SET_BY_NAME, *x, count, "Count") || !SDDS_SetColumnFromDoubles(&pos_out, SDDS_SET_BY_NAME, *pos_CDF, count, "P")) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!SDDS_WritePage(&pos_out) || !SDDS_Terminate(&pos_out)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); */ free(pos); return count; }
long greatestProductOfSmallPrimes(long rows) /* doesn't really find the greatest product, but simply a large one */ { long bestResult = 0, result; static long prime[MAXPRIMES] = {2, 3, 5, 7, 11, 13, 17, 19}; long nPrimes; for (nPrimes=1; nPrimes<=MAXPRIMES; nPrimes++) { if ((result = greatestProductOfSmallPrimes1(rows, prime, nPrimes))>bestResult && result<=rows) bestResult = result; } if (bestResult==0) SDDS_Bomb("couldn't find acceptable number of rows for truncation/padding"); return bestResult; }
long applyFilters(double *outputData, double *inputData, double *timeData, long rows, FILTER_STAGE *filterStage, long filterStages) { long stage, row, frequencies; double *realimagInput, *realimagOutput; double length, dfrequency, factor; realimagOutput = NULL; if (!(realimagInput = (double*)malloc(sizeof(*realimagInput)*(rows+2))) || !(realimagOutput = (double*)malloc(sizeof(*realimagOutput)*(rows+2))) ) SDDS_Bomb("allocation failure"); /* input data is real and has "rows" rows */ /* result is interleaved real and imaginary */ realFFT2(realimagInput, inputData, rows, 0); frequencies = rows/2 + 1; /* length is the assumed length of the periodic signal, which is one interval longer that the actual data entered */ length = ((double)rows)*(timeData[rows-1]-timeData[0])/((double)rows-1.0); dfrequency = factor = 1.0/length; for (stage=0; stage<filterStages; stage++) { if (!applyFilterStage(realimagOutput, realimagInput, frequencies, dfrequency, filterStage+stage)) return 0; SWAP_PTR(realimagOutput, realimagInput); } /* input is still interleaved real and imaginary. The flag INVERSE_FFT ensures that the array is interpreted correctly. */ /* output is interleaved real and imaginary */ #if DEBUG for (row=0; row<rows; row++; row++) { fprinf(stdout,"Real: %f, Imag: %i\n", realimagInput[row], realimagInput[row+1]); } #endif realFFT2(realimagOutput, realimagInput, rows, INVERSE_FFT); for (row=0; row<rows; row++) outputData[row] = realimagOutput[row]; free(realimagOutput); free(realimagInput); return 1; }
long addStatRequests(STAT_REQUEST **statRequest, long requests, char **item, long items, long code, unsigned long flags) { long i; if (!(*statRequest = SDDS_Realloc(*statRequest, sizeof(**statRequest)*(requests+1))) || !((*statRequest)[requests].sourceColumn = (char**)malloc(sizeof(*(*statRequest)[requests].sourceColumn)* (items-1)))) SDDS_Bomb("memory allocation failure"); for (i=0; i<items-1; i++) SDDS_CopyString(&((*statRequest)[requests].sourceColumn[i]), item[i+1]); (*statRequest)[requests].resultColumn = item[0]; (*statRequest)[requests].sourceColumns = items-1; (*statRequest)[requests].optionCode = code; (*statRequest)[requests].sumPower = 1; (*statRequest)[requests].flags = flags; (*statRequest)[requests].positionColumn = NULL; return requests+1; }
long applyFilterStage(double *outputRI, double *inputRI, long frequencies, double dfrequency, FILTER_STAGE *filterStage) { long i, ifilter; for (i=0; i<2*frequencies; i++) outputRI[i] = 0; for (ifilter=0; ifilter<filterStage->filters; ifilter++) { switch (filterStage->filter[ifilter].filterType) { case SET_CLIPFREQ: addClipFilterOutput(outputRI, inputRI, frequencies, dfrequency, filterStage->filter[ifilter].filter); break; case SET_THRESHOLD: addThresholdFilterOutput(outputRI, inputRI, frequencies, dfrequency, filterStage->filter[ifilter].filter); break; case SET_HIGHPASS: addHighPassFilterOutput(outputRI, inputRI, frequencies, dfrequency, filterStage->filter[ifilter].filter); break; case SET_LOWPASS: addLowPassFilterOutput(outputRI, inputRI, frequencies, dfrequency, filterStage->filter[ifilter].filter); break; case SET_NOTCH: addNotchFilterOutput(outputRI, inputRI, frequencies, dfrequency, filterStage->filter[ifilter].filter); break; case SET_BANDPASS: addBandPassFilterOutput(outputRI, inputRI, frequencies, dfrequency, filterStage->filter[ifilter].filter); break; case SET_FILTERFILE: addFileFilterOutput(outputRI, inputRI, frequencies, dfrequency, filterStage->filter[ifilter].filter); break; default: SDDS_Bomb("unknown filter type in applyFilterStage--this shouldn't happen"); break; } } return(1); }
STAT_DEFINITION *compileStatDefinitions(SDDS_DATASET *inData, STAT_REQUEST *request, long requests, long *stats, long noWarnings) { STAT_DEFINITION *stat; long iReq, iName, iStat; if (!(stat = (STAT_DEFINITION *)malloc(sizeof(*stat)*requests))) SDDS_Bomb("memory allocation failure"); for (iReq=iStat=0; iReq<requests; iReq++) { if ((stat[iStat].sourceColumns = expandColumnPairNames(inData, &request[iReq].sourceColumn, NULL, request[iReq].sourceColumns, NULL, 0, FIND_NUMERIC_TYPE, 0))<=0) { if (!noWarnings) { fprintf(stderr, "Warning: no match for column names (sddsrowstats):\n"); for (iName=0; iName<request[iReq].sourceColumns; iName++) fprintf(stderr, "%s, ", request[iReq].sourceColumn[iName]); fputc('\n', stderr); } } else { stat[iStat].sourceColumn = request[iReq].sourceColumn; stat[iStat].resultColumn = request[iReq].resultColumn; stat[iStat].optionCode = request[iReq].optionCode; stat[iStat].sumPower = request[iReq].sumPower; stat[iStat].flags = request[iReq].flags; stat[iStat].topLimit = request[iReq].topLimit; stat[iStat].bottomLimit = request[iReq].bottomLimit; stat[iStat].positionColumn = request[iReq].positionColumn; stat[iStat].percent = request[iReq].percent; iStat++; } *stats = iStat; } return stat; }
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) { SDDS_DATASET inSet, outSet; SCANNED_ARG *s_arg; long i_arg, pageReturned, rows, row; int32_t *rowFlag; char *input, *output, *columnName, *par_thresholdName; double *data; unsigned long pipeFlags, flags; double threshold, ezoneFraction, changeThreshold; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&s_arg, argc, argv); if (argc<2 || argc>(2+N_OPTIONS)) bomb(NULL, USAGE); flags = pipeFlags = 0; input = output = NULL; columnName = NULL; ezoneFraction = changeThreshold = 0; rowFlag = NULL; par_thresholdName=NULL; for (i_arg=1; i_arg<argc; i_arg++) { if (s_arg[i_arg].arg_type==OPTION) { switch (match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) { case CLO_THRESHOLD: if (s_arg[i_arg].n_items==2) { if (s_arg[i_arg].list[1][0]=='@' ) { SDDS_CopyString(&par_thresholdName, s_arg[i_arg].list[1]+1); flags |= PAR_THRESHOLD; } else { if (sscanf(s_arg[i_arg].list[1], "%lf", &threshold)!=1) SDDS_Bomb("incorrect -threshold syntax"); flags |= THRESHOLD; } } else SDDS_Bomb("incorrect -threshold syntax"); break; case CLO_FIVEPOINT: flags |= FIVEPOINT; break; case CLO_CHANGETHRESHOLD: if (s_arg[i_arg].n_items!=2 || sscanf(s_arg[i_arg].list[1], "%lf", &changeThreshold)!=1 || changeThreshold<=0) SDDS_Bomb("incorrect -changeThreshold syntax or values"); flags |= CHANGETHRES; break; case CLO_PIPE: if (!processPipeOption(s_arg[i_arg].list+1, s_arg[i_arg].n_items-1, &pipeFlags)) SDDS_Bomb("invalid -pipe syntax"); break; case CLO_COLUMN: if (s_arg[i_arg].n_items!=2) SDDS_Bomb("invalid -column syntax"); columnName = s_arg[i_arg].list[1]; break; case CLO_EXCLUSIONZONE: if (s_arg[i_arg].n_items!=2 || sscanf(s_arg[i_arg].list[1], "%lf", &ezoneFraction)!=1 || ezoneFraction<=0) SDDS_Bomb("invalid -exclusionZone syntax or value"); flags |= EZONEFRAC; break; default: fprintf(stderr, "error: unknown/ambiguous option: %s\n", s_arg[i_arg].list[0]); exit(1); break; } } else { if (input==NULL) input = s_arg[i_arg].list[0]; else if (output==NULL) output = s_arg[i_arg].list[0]; else SDDS_Bomb("too many filenames"); } } processFilenames("sddspeakfind", &input, &output, pipeFlags, 0, NULL); if (!columnName) SDDS_Bomb("-column option must be given"); if (!SDDS_InitializeInput(&inSet, input)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!SDDS_FindColumn(&inSet, FIND_NUMERIC_TYPE, columnName, NULL)) SDDS_Bomb("the given column is nonexistent or nonnumeric"); if (!SDDS_InitializeCopy(&outSet, &inSet, output, "w") || !SDDS_WriteLayout(&outSet)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); while ((pageReturned=SDDS_ReadPage(&inSet))>0) { if (!SDDS_CopyPage(&outSet, &inSet)) { SDDS_SetError("Problem copying data for output file"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if ((rows=SDDS_CountRowsOfInterest(&outSet))>1) { if (!(data = SDDS_GetColumnInDoubles(&inSet, columnName))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); rowFlag = SDDS_Realloc(rowFlag, sizeof(*rowFlag)*rows); for (row=0; row<rows; row++) rowFlag[row] = 0; markPeaks(data, rowFlag, rows, flags&FIVEPOINT); if (flags&PAR_THRESHOLD) { if (!SDDS_GetParameter(&inSet,par_thresholdName,&threshold)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (flags&THRESHOLD || flags&PAR_THRESHOLD ) { for (row=0; row<rows; row++) if (rowFlag[row] && data[row]<threshold) rowFlag[row] = 0; } if (flags&CHANGETHRES) unmarkFlatPeaks(data, rowFlag, rows, changeThreshold, flags&FIVEPOINT); if (flags&EZONEFRAC) unmarkExcludedPeaks(data, rowFlag, rows, ezoneFraction); if (!SDDS_AssertRowFlags(&outSet, SDDS_FLAG_ARRAY, rowFlag, rows)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); free(data); } if (!SDDS_WritePage(&outSet)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_Terminate(&inSet) || !SDDS_Terminate(&outSet)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } free_scanargs(&s_arg,argc); if (par_thresholdName) free(par_thresholdName); if (rowFlag) free(rowFlag); return(0); }
int main(int argc, char **argv) { SCANNED_ARG *scanned; SDDS_TABLE inputPage, outputPage, residualPage; char *inputfile, *outputfile; char **column, **excludeColumn; int32_t columns; long excludeColumns; char *indColumnName; long verbose; long iArg, i, j, ipage; double *indVar, *indVarOrig; char *indVarUnits; char **intColumn, **slopeColumn, **slopeSigmaColumn, **interceptSigmaColumn; char *Units,*slopeUnits; double *depVar, *depVarOrig; long order; double *coef, *coefsigma, *weight, *diff, *diffOrig, chi; long iCol, iRow; long rows, rowsOrig; double rmsResidual; double slopeSigma, interceptSigma; char **sigmaColumn, **chiSquaredColumn; long *sigmaColumnExists; long doSlopeSigma, generateSigma, doPreliminaryFit; long validSigmas; double sigmaSum, averageSigma; long ascii; char *residualFile; unsigned long pipeFlags; long tmpfile_used, noWarnings; double xMin, xMax; indVar = indVarOrig = depVar = depVarOrig = coef = coefsigma = weight = diff = NULL; intColumn = slopeColumn = slopeSigmaColumn = interceptSigmaColumn = sigmaColumn = chiSquaredColumn = NULL; slopeUnits = NULL; sigmaColumnExists = NULL; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&scanned, argc, argv); if (argc == 1) bomb(NULL, USAGE); inputfile = outputfile = NULL; columns = excludeColumns = 0; column = excludeColumn = NULL; indColumnName = NULL; verbose = 0; doSlopeSigma = 0; generateSigma = 0; doPreliminaryFit = 0; ascii = 0; pipeFlags = 0; tmpfile_used=0; noWarnings=0; residualFile = NULL; xMin = xMax = 0; for (iArg = 1; iArg<argc; iArg++) { if (scanned[iArg].arg_type == OPTION) { delete_chars(scanned[iArg].list[0], "_"); switch (match_string(scanned[iArg].list[0], commandline_option, COMMANDLINE_OPTIONS, UNIQUE_MATCH)) { case CLO_INDEPENDENT_COLUMN: if (!(indColumnName = scanned[iArg].list[1])) SDDS_Bomb("no string given for option -independentVariable"); break; case CLO_COLUMNS: if (columns) SDDS_Bomb("only one -columns option may be given"); if (scanned[iArg].n_items<2) SDDS_Bomb("invalid -columns syntax"); column = tmalloc(sizeof(*column)*(columns = scanned[iArg].n_items-1)); for (i = 0; i<columns; i++) column[i] = scanned[iArg].list[i+1]; break; case CLO_EXCLUDE: if (excludeColumns) SDDS_Bomb("only one -excludecolumns option may be given"); if (scanned[iArg].n_items<2) SDDS_Bomb("invalid -excludecolumns syntax"); excludeColumn = tmalloc(sizeof(*excludeColumn)*(excludeColumns = scanned[iArg].n_items-1)); for (i = 0; i<excludeColumns; i++) excludeColumn[i] = scanned[iArg].list[i+1]; break; case CLO_VERBOSE: verbose = 1; break; case CLO_ASCII: ascii = 1; break; case CLO_PIPE: if (!processPipeOption(scanned[iArg].list+1, scanned[iArg].n_items-1, &pipeFlags)) SDDS_Bomb("invalid -pipe syntax"); break; case CLO_SIGMA: doSlopeSigma = 1; if (scanned[iArg].n_items > 1 ) { switch (match_string(scanned[iArg].list[1], sigma_option, SIGMA_OPTIONS, UNIQUE_MATCH)) { case SIGMA_GENERATE: generateSigma = 1; break; default: SDDS_Bomb("unrecognized sigma option given"); break; } } break; case CLO_RESIDUAL: if (!(residualFile=scanned[iArg].list[1])){ fprintf(stderr,"No file specified in -residual option.\n"); exit(1); } break; case CLO_RANGE: if (scanned[iArg].n_items!=3 || 1!=sscanf(scanned[iArg].list[1], "%lf", &xMin) || 1!=sscanf(scanned[iArg].list[2], "%lf", &xMax) || xMin>=xMax) SDDS_Bomb("incorrect -range syntax"); break; default: SDDS_Bomb("unrecognized option given"); break; } } else { if (!inputfile) inputfile = scanned[iArg].list[0]; else if (!outputfile) outputfile = scanned[iArg].list[0]; else SDDS_Bomb("too many filenames given"); } } if (residualFile && outputfile) { if (!strcmp( residualFile, outputfile)) { fprintf( stderr, "Residual file can't be the same as the output file.\n"); exit(1); } } processFilenames("sddsslopes", &inputfile, &outputfile, pipeFlags, noWarnings, &tmpfile_used); if (!indColumnName) { fprintf( stderr, "independentVariable not given\n"); exit(1); } if (!excludeColumns) { excludeColumn = defaultExcludedColumn; excludeColumns = DEFAULT_EXCLUDED_COLUMNS; } if (verbose) fprintf(stderr,"Reading file %s.\n",inputfile); if ( !SDDS_InitializeInput( &inputPage, inputfile) ) SDDS_PrintErrors( stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); while (0 < (ipage=SDDS_ReadTable( &inputPage))) { if (verbose) { fprintf(stderr, "working on page %ld\n", ipage); } rows = SDDS_CountRowsOfInterest(&inputPage); rowsOrig = rows; /*************************************\ * make array of independent variable \*************************************/ if (ipage==1) { indVar = (double*) malloc( sizeof(*indVar) * rows); } else { indVar = (double*) realloc( indVar, sizeof(*indVar) * rows); } if (ipage==1) { if (!SDDS_FindColumn(&inputPage, FIND_NUMERIC_TYPE, indColumnName, NULL)){ fprintf(stderr,"Something wrong with column %s.\n", indColumnName); SDDS_CheckColumn(&inputPage, indColumnName, NULL, SDDS_ANY_NUMERIC_TYPE, stderr); exit(1); } } /* filter out the specified range in independent variable */ if (xMin!=xMax) { if (!(indVarOrig = SDDS_GetColumnInDoubles( &inputPage, indColumnName))) SDDS_PrintErrors( stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); for (i=j=0; i<rowsOrig; i++) { if (indVarOrig[i]<=xMax && indVarOrig[i]>=xMin) { indVar[j] = indVarOrig[i]; j++; } } rows = j; } else { if (!(indVar = SDDS_GetColumnInDoubles( &inputPage, indColumnName))) SDDS_PrintErrors( stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); } if ( ipage == 1 ) { if (!SDDS_GetColumnInformation(&inputPage, "units", &indVarUnits, SDDS_GET_BY_NAME, indColumnName)) SDDS_PrintErrors( stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!indVarUnits) { indVarUnits = (char *) malloc(sizeof(*indVarUnits)); indVarUnits[0] = 0; } } /************************************\ * initialize residual file \************************************/ if (residualFile) { if ( ipage == 1 ) { if(!SDDS_InitializeOutput(&residualPage,ascii?SDDS_ASCII:SDDS_BINARY,1, "Residual of 2-term fit",NULL,outputfile) || !SDDS_InitializeCopy(&residualPage, &inputPage, residualFile, "w") || !SDDS_WriteLayout(&residualPage) ) SDDS_PrintErrors(stderr,SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_CopyPage(&residualPage,&inputPage)) SDDS_PrintErrors(stderr,SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } /************************************\ * get columns of interest. use set_multicolumn_flags to simply * return new values for array column. \*************************************/ if (!set_multicolumn_flags(&inputPage, &column, &columns, excludeColumn, excludeColumns)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } /************************************\ * make column names for the output \*************************************/ if (ipage==1) { intColumn = (char**) malloc((sizeof(char*)*columns)); slopeColumn = (char**) malloc((sizeof(char*)*columns)); if (doSlopeSigma) { slopeSigmaColumn = (char**) malloc((sizeof(char*)*columns)); interceptSigmaColumn = (char**) malloc((sizeof(char*)*columns)); chiSquaredColumn = (char**) malloc((sizeof(char*)*columns)); } for (i=0; i<columns; i++) { intColumn[i] = (char*) malloc((sizeof(char)*(strlen(column[i])+strlen("Intercept")+1))); strcat(strcpy(intColumn[i], column[i]), "Intercept"); slopeColumn[i] = (char*) malloc((sizeof(char)*(strlen(column[i])+strlen("Slope")+1))); strcat(strcpy(slopeColumn[i], column[i]), "Slope"); if (doSlopeSigma) { slopeSigmaColumn[i] = (char*) malloc((sizeof(char)*(strlen(column[i])+strlen("SlopeSigma")+1))); strcat(strcpy(slopeSigmaColumn[i], column[i]), "SlopeSigma"); interceptSigmaColumn[i] = (char*) malloc((sizeof(char)*(strlen(column[i])+strlen("InterceptSigma")+1))); strcat(strcpy(interceptSigmaColumn[i], column[i]), "InterceptSigma"); chiSquaredColumn[i] = (char*) malloc((sizeof(char)*(strlen(column[i])+strlen("ChiSquared")+1))); strcat(strcpy(chiSquaredColumn[i], column[i]), "ChiSquared"); } } } /************************************\ * Write layout for output file \*************************************/ if (ipage==1) { if (verbose) fprintf(stderr,"Opening file %s.\n",outputfile); if(!SDDS_InitializeOutput(&outputPage,ascii?SDDS_ASCII:SDDS_BINARY,1, "2-term fit",NULL,outputfile) || 0>SDDS_DefineParameter(&outputPage, "InputFile", "InputFile", NULL, "InputFile", NULL, SDDS_STRING, 0) || 0>SDDS_DefineColumn(&outputPage, "IndependentVariable", NULL, NULL, NULL, NULL, SDDS_STRING,0) ) SDDS_PrintErrors(stderr,SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (iCol=0; iCol<columns; iCol++) { if (!SDDS_GetColumnInformation(&inputPage, "units", &Units, SDDS_GET_BY_NAME, column[iCol])) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!Units) { Units = (char*) malloc(sizeof(*Units)); Units[0] = 0; } if (0>SDDS_DefineColumn(&outputPage, intColumn[iCol], NULL, Units, NULL, NULL, SDDS_DOUBLE,0)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); /* units for slopes columns */ if (strlen(indVarUnits) && strlen(Units) ) { slopeUnits = (char*)malloc(sizeof(*slopeUnits)*(strlen(Units)+strlen(indVarUnits)+2)); strcat( strcat( strcpy(slopeUnits, Units), "/"), indVarUnits); } if (strlen(indVarUnits) && !strlen(Units) ) { slopeUnits = (char*)malloc(sizeof(*slopeUnits)*(strlen(indVarUnits)+2)); strcat( strcpy( slopeUnits, "1/"), indVarUnits); } if (!strlen(indVarUnits) && strlen(Units) ) { slopeUnits = (char*)malloc(sizeof(*slopeUnits)*(strlen(Units)+2)); strcpy( slopeUnits, indVarUnits); } if (!strlen(indVarUnits) && !strlen(Units) ) { slopeUnits = (char*)malloc(sizeof(*slopeUnits)); strcpy( slopeUnits, ""); } if (0>SDDS_DefineColumn(&outputPage, slopeColumn[iCol], NULL, slopeUnits, NULL, NULL, SDDS_DOUBLE,0)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (doSlopeSigma) { if (0>SDDS_DefineColumn(&outputPage, interceptSigmaColumn[iCol], NULL, Units, NULL, NULL, SDDS_DOUBLE,0) || 0>SDDS_DefineColumn(&outputPage, slopeSigmaColumn[iCol], NULL, slopeUnits, NULL, NULL, SDDS_DOUBLE,0) || 0>SDDS_DefineColumn(&outputPage, chiSquaredColumn[iCol], NULL, NULL, NULL, NULL, SDDS_DOUBLE,0)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } free(slopeUnits); } if ( !SDDS_WriteLayout(&outputPage)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if ( !SDDS_StartTable(&outputPage,1) || !SDDS_SetParameters(&outputPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "InputFile",inputfile?inputfile:"pipe",NULL) || !SDDS_SetRowValues(&outputPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, 0, "IndependentVariable", indColumnName, NULL) ) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); /* determine which included columns has a Sigma column defined in the input file */ if ( ipage == 1 ) { sigmaColumn = (char **) malloc( sizeof(*sigmaColumn)*columns); sigmaColumnExists = (long *) malloc(columns*sizeof(*sigmaColumnExists)); for (iCol=0; iCol<columns; iCol++) { sigmaColumn[iCol] = (char *) malloc( sizeof(**sigmaColumn) * (strlen(column[iCol]) + strlen("Sigma") + 1) ); strcat( strcpy(sigmaColumn[iCol], column[iCol]), "Sigma"); switch(SDDS_CheckColumn(&inputPage, sigmaColumn[iCol], NULL, SDDS_DOUBLE, NULL)) { case SDDS_CHECK_WRONGUNITS: case SDDS_CHECK_OKAY: sigmaColumnExists[iCol] = 1; break; default: /* try other possible spelling */ strcat( strcpy(sigmaColumn[iCol] ,"Sigma"), column[iCol]); switch(SDDS_CheckColumn(&inputPage, sigmaColumn[iCol], NULL, SDDS_DOUBLE, NULL)) { case SDDS_CHECK_WRONGUNITS: case SDDS_CHECK_OKAY: sigmaColumnExists[iCol] = 1; break; default: sigmaColumnExists[iCol] = 0; } break; } } } if ( ipage == 1 ) { weight = (double*)malloc(sizeof(*weight)*rows); diff = (double*)malloc(sizeof(*diff)*rows); order=1; coef = (double*)malloc(sizeof(*coef)*(order+1)); coefsigma = (double*)malloc(sizeof(*coefsigma)*(order+1)); } else { weight = (double*)realloc( weight, sizeof(*weight)*rows); diff = (double*)realloc( diff, sizeof(*diff)*rows); order=1; coef = (double*)realloc( coef, sizeof(*coef)*(order+1)); coefsigma = (double*)realloc( coefsigma, sizeof(*coefsigma)*(order+1)); } if (ipage==1) { depVar = (double*) malloc( sizeof(*depVar) * rows); } else { depVar = (double*) realloc( depVar, sizeof(*depVar) * rows); } for (iCol=0; iCol<columns; iCol++) { if (verbose) fprintf(stderr,"Doing column %s.\n", column[iCol]); /* filter out the specified range in independent variable */ if (xMin!=xMax) { if (!(depVarOrig = (double*) SDDS_GetColumnInDoubles(&inputPage, column[iCol]))) SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); for (i=j=0; i<rowsOrig; i++) { if ( xMin <= indVarOrig[i] && indVarOrig[i] <= xMax ) { depVar[j] = depVarOrig[i]; j++; } } } else { if (!(depVar=SDDS_GetColumnInDoubles(&inputPage, column[iCol]))) SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); } /********************* three possibilities: 1) don't do or write slope errors. (doSlopeSigma=0) do one lsf call with all weights = 1 2) calculated slope errors from sigma columns in the input file. (doSlopeSigma=1 && generateSigma=0 && sigmaColumnExists[iCol]=1 ) do one lsf call with weights from sigma columns 3) calculate slope errors from generated sigma from a preliminary fit. (doSlopeSigma=1 && (generateSigma=1 || sigmaColumnExists[iCol]=NULL) do preliminary fit to generate sigma *********************/ for (iRow=0; iRow<rows; iRow++) weight[iRow] = 1; if (doSlopeSigma) { /********************* check validity of sigma column values *********************/ if( !generateSigma && sigmaColumnExists[iCol]) { if (verbose) fprintf(stderr,"\tUsing column %s for sigma.\n",sigmaColumn[iCol]); if(!(weight=SDDS_GetColumnInDoubles(&inputPage, sigmaColumn[iCol]))) SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); /* check for zero weight values which will give lsfn problems */ validSigmas = rows; sigmaSum = 0; for (iRow=0; iRow<rows; iRow++) { sigmaSum += weight[iRow]; if(!weight[iRow]) { validSigmas--; /* fprintf(stderr,"Warning: %s of row number %ld is zero. Using average sigma.\n",sigmaColumn[iCol],iRow); */ } } if (!validSigmas) { fprintf(stderr,"Warning: All sigmas are zero.\n"); doPreliminaryFit = 1; } else if (validSigmas!=rows) { /* fix some sigmas */ averageSigma = sigmaSum/ validSigmas; fprintf(stderr, "Warning: replacing %ld invalid sigmas with average (%e)\n", rows-validSigmas, averageSigma); for (iRow=0; iRow<rows; iRow++) { if(!weight[iRow]) { weight[iRow] = averageSigma; } } } } else { doPreliminaryFit = 1; } } if (doPreliminaryFit) { if (verbose) fprintf(stderr,"\tGenerating sigmas from rms residual of a preliminary fit.\n"); if (!(lsfn(indVar, depVar, weight, rows, order, coef, coefsigma, &chi, diff))){ fprintf(stderr,"Problem with call to lsfn\n."); exit(1); } rmsResidual = 0; /* calculate rms residual */ for (iRow=0; iRow<rows; iRow++) { rmsResidual += sqr(diff[iRow]); } rmsResidual = sqrt(rmsResidual/(rows)); for (iRow=0; iRow<rows; iRow++) { weight[iRow] = rmsResidual; } } if (!(lsfn(indVar, depVar, weight, rows, order, coef, coefsigma, &chi, diff))) { fprintf(stderr,"Problem with call to lsfn\n."); exit(1); } if (!SDDS_SetRowValues(&outputPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, 0, intColumn[iCol], coef[0], slopeColumn[iCol], coef[1], NULL)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (doSlopeSigma) { interceptSigma = coefsigma[0]; slopeSigma = coefsigma[1]; if (!SDDS_SetRowValues(&outputPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, 0, chiSquaredColumn[iCol], chi, interceptSigmaColumn[iCol], interceptSigma, slopeSigmaColumn[iCol], slopeSigma, NULL)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (residualFile) { if (xMin!=xMax) { /* calculate the residuals for the whole column explicitly since there are points outside the range of which the lsf call did not calculate the difference. */ diffOrig = (double*) malloc( rowsOrig * sizeof(double) ); for (i=0; i<rowsOrig; i++) { diffOrig[i] = depVarOrig[i] - coef[0] - coef[1] * indVarOrig[i]; } if( !SDDS_SetColumnFromDoubles(&residualPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_REFERENCE, diffOrig,rowsOrig,column[iCol])) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } else { if( !SDDS_SetColumnFromDoubles(&residualPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_REFERENCE, diff,rows,column[iCol])) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } } if (residualFile) { if (!SDDS_WriteTable(&residualPage)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_WriteTable(&outputPage)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (residualFile) { if (!SDDS_Terminate(&residualPage)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if(!SDDS_Terminate(&inputPage) || !SDDS_Terminate(&outputPage)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (tmpfile_used && !replaceFileAndBackUp(inputfile, outputfile)) exit(1); return(0); }
int main(int argc, char **argv) { FILE *fileID; SDDS_LAYOUT *layout=NULL; SDDS_FILEBUFFER *fBuffer=NULL; SDDS_DATASET SDDS_dataset, SDDS_dummy; SCANNED_ARG *s_arg; long i, j, i_arg, retval, page_number=0, size, columnOrder=0; int32_t rows=0; char *input, *output; unsigned long pipeFlags=0, flags=0; long noWarnings=0, tmpfile_used=0; long *columnType, *columnIndex; void **columnData; char *buffer; char **column, **column_match; long columns=0, column_matches; input = output = NULL; columnType = columnIndex = NULL; columnData = NULL; column_match = NULL; column_matches = 0; buffer = tmalloc(sizeof(char)*16); SDDS_RegisterProgramName(argv[0]); argc = scanargs(&s_arg, argc, argv); if (argc<3) bomb(NULL, USAGE); for (i_arg=1; i_arg<argc; i_arg++) { if (s_arg[i_arg].arg_type==OPTION) { switch (match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) { case SET_ORDER: if (s_arg[i_arg].n_items!=2) SDDS_Bomb("invalid -order syntax"); switch (match_string(s_arg[i_arg].list[1], order_names, ORDERS, 0)) { case ROW_ORDER: columnOrder = 0; break; case COLUMN_ORDER: columnOrder = 1; break; default: SDDS_Bomb("invalid -order syntax"); break; } break; case SET_COLUMN: if ((s_arg[i_arg].n_items<2)) SDDS_Bomb("invalid -column syntax"); column_matches = s_arg[i_arg].n_items - 1; column_match = tmalloc(sizeof(*column_match)*column_matches); for (i=0; i<column_matches; i++) column_match[i] = s_arg[i_arg].list[i+1]; break; default: fprintf(stderr, "error: unknown switch: %s\n", s_arg[i_arg].list[0]); exit(1); break; } } else { if (input == NULL) { input = s_arg[i_arg].list[0]; } else if (output == NULL) { output = s_arg[i_arg].list[0]; } else { fprintf(stderr, "too many filenames"); exit(1); } } } processFilenames("sdds2headlessdata", &input, &output, pipeFlags, noWarnings, &tmpfile_used); if (!column_matches) SDDS_Bomb("you must specify -column options"); if (!SDDS_InitializeInput(&SDDS_dataset, input)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } column = getMatchingSDDSNames(&SDDS_dataset, column_match, column_matches, &columns, SDDS_MATCH_COLUMN); if (!columns) SDDS_Bomb("No columns found in the input file."); columnType = tmalloc(sizeof(*columnType)*columns); columnIndex = tmalloc(sizeof(*columnIndex)*columns); columnData = tmalloc(sizeof(*columnData)*columns); for (i=0; i<columns; i++) { if ((columnIndex[i]=SDDS_GetColumnIndex(&SDDS_dataset, column[i]))<0) { fprintf(stderr, "error: column %s does not exist\n", column[i]); exit(1); } if ((columnType[i]=SDDS_GetColumnType(&SDDS_dataset, columnIndex[i]))<=0) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } } if (!output) { #if defined(_WIN32) if (_setmode(_fileno(stdout), _O_BINARY) == -1) { fprintf(stderr, "error: unable to set stdout to binary mode\n"); exit(1); } #endif fileID = stdout; } else { fileID = fopen(output, "wb"); } if (fileID == NULL) { fprintf(stderr, "unable to open output file for writing\n"); exit(1); } layout = &SDDS_dataset.layout; fBuffer = &SDDS_dummy.fBuffer; fBuffer->buffer = NULL; if (!fBuffer->buffer) { if (!(fBuffer->buffer = fBuffer->data = SDDS_Malloc(sizeof(char)*SDDS_FILEBUFFER_SIZE))) { fprintf(stderr, "Unable to do buffered read--allocation failure\n"); exit(1); } fBuffer->bufferSize = SDDS_FILEBUFFER_SIZE; fBuffer->bytesLeft = SDDS_FILEBUFFER_SIZE; } retval = -1; while (retval!=page_number && (retval=SDDS_ReadPage(&SDDS_dataset))>0) { if (page_number && retval!=page_number) continue; if ((rows=SDDS_CountRowsOfInterest(&SDDS_dataset))<0) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (rows) { if (columnOrder) { for (j=0; j<columns; j++) { if (columnType[j]==SDDS_STRING) { for (i=0; i<rows; i++) { if (!SDDS_WriteBinaryString(*((char**)SDDS_dataset.data[columnIndex[j]]+i), fileID, fBuffer)) { fprintf(stderr, "Unable to write rows--failure writing string\n"); exit(1); } } } else { size = SDDS_type_size[columnType[j]-1]; for (i=0; i<rows; i++) { if (!SDDS_BufferedWrite((char*)SDDS_dataset.data[columnIndex[j]]+i*size, size, fileID, fBuffer)) { fprintf(stderr, "Unable to write rows--failure writing string\n"); exit(1); } } } } } else { for (i=0; i<rows; i++) { for (j=0; j<columns; j++) { if (columnType[j]==SDDS_STRING) { if (!SDDS_WriteBinaryString(*((char**)SDDS_dataset.data[columnIndex[j]]+i), fileID, fBuffer)) { fprintf(stderr, "Unable to write rows--failure writing string\n"); exit(1); } } else { size = SDDS_type_size[columnType[j]-1]; if (!SDDS_BufferedWrite((char*)SDDS_dataset.data[columnIndex[j]]+i*size, size, fileID, fBuffer)) { fprintf(stderr, "Unable to write rows--failure writing string\n"); exit(1); } } } } } } if (retval==0) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } } if (!SDDS_FlushBuffer(fileID, fBuffer)) { SDDS_SetError("Unable to write page--buffer flushing problem (SDDS_WriteBinaryPage)"); return 0; } fclose(fileID); if (!SDDS_Terminate(&SDDS_dataset)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } exit(0); }
void determine_dataset_labels(PLOT_SPEC *plspec, SDDS_TABLE *table, PLOT_DATA *dataset) { PLOT_REQUEST *request; long i; DATA_INFO *info; char printBuffer[SDDS_MAXLINE]; request = plspec->plot_request+dataset->request_index; for (i=0; i<4; i++) { dataset->label[i] = NULL; if (request->label[i].flags&LABEL_STRING_GIVEN) { if (!SDDS_CopyString(&dataset->label[i], request->label[i].label)) SDDS_Bomb("String copy error (determine_dataset_labels)"); } else if (request->label[i].flags&LABEL_PARAMETER_GIVEN) { long type; char *dataBuffer = NULL; char *format =NULL; if ( !(dataBuffer = malloc(sizeof(double)*4))) SDDS_Bomb("Allocation failure in determin_dataset_labels"); if (!SDDS_GetParameter(table,request->label[i].label, dataBuffer)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if(!(type=SDDS_GetParameterType(table,SDDS_GetParameterIndex(table,request->label[i].label)))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if(SDDS_GetParameterInformation(table,"format_string",&format,SDDS_GET_BY_NAME,request->label[i].label) !=SDDS_STRING) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if ( request->label[i].flags&LABEL_FORMAT_GIVEN) { if(!SDDS_VerifyPrintfFormat(request->label[i].format,type)) { fprintf(stderr, "error: given format (\"%s\") for parameter %s is invalid\n", request->label[i].format, request->label[i].label); exit(1); } if(!(SDDS_SprintTypedValue((void*)dataBuffer,0,type,request->label[i].format,printBuffer,SDDS_PRINT_NOQUOTES))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); SDDS_CopyString(&dataset->label[i],printBuffer); } else { if(!SDDS_StringIsBlank(format)) { if(!(SDDS_SprintTypedValue((void*)dataBuffer,0,type,format,printBuffer,SDDS_PRINT_NOQUOTES))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); SDDS_CopyString(&dataset->label[i],printBuffer); } else { if (!(dataset->label[i]=SDDS_GetParameterAsString(table,request->label[i].label,NULL))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } free(format); free(dataBuffer); /*if (SDDS_GetParameterType(table, SDDS_GetParameterIndex(table, request->label[i].label)) != SDDS_STRING) { fprintf(stderr, "labels taken from parameters must be strings\n"); exit(1); } if (!SDDS_GetParameter(table, request->label[i].label, &s)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); dataset->label[i] = s; */ } else if (request->label[i].flags&(LABEL_USE_NAME+LABEL_USE_SYMBOL+LABEL_USE_DESCRIPTION)) { if (i==0) info = &dataset->info[0]; else info = &dataset->info[1]; if (request->label[i].flags&LABEL_USE_NAME) SDDS_CopyString(&dataset->label[i], i==0?request->xname[dataset->dataname_index]:request->yname[dataset->dataname_index]); else if (request->label[i].flags&LABEL_USE_SYMBOL && info->symbol) SDDS_CopyString(&dataset->label[i], info->symbol); else if (request->label[i].flags&LABEL_USE_DESCRIPTION && info->description) SDDS_CopyString(&dataset->label[i], info->description); if (dataset->label[i] && request->label[i].flags&LABEL_INCLUDE_UNITS && info->units && !SDDS_StringIsBlank(info->units)) { if (!(dataset->label[i] = SDDS_Realloc(dataset->label[i], sizeof(*dataset->label[i])*(strlen(dataset->label[i])+strlen(info->units)+4)))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); strcat(dataset->label[i], " ("); strcat(dataset->label[i], info->units); strcat(dataset->label[i], ")"); } } } }
void determine_panel_labels(PLOT_SPEC *plspec, long panel_index) { long i, request, file, first, datasets; long j, plane, scaleCount, group, set, index, ig; static char buffer[SDDS_MAXLINE], s[SDDS_MAXLINE]; PLOT_DATA **dataset; time_t timeValue; static unsigned long ticksetTime[2] = {TICKSET_XTIME, TICKSET_YTIME}, flags; char *newLabel, *editCommand; /* Construct labels for each scale for each plane */ first = 1; dataset = plspec->panel[panel_index].dataset; datasets = plspec->panel[panel_index].datasets; #if defined(DEBUG) fprintf(stderr, "panel label determination: %ld datasets for panel %ld\n", datasets, panel_index); #endif newLabel = NULL; for (plane=0; plane<2; plane++) { scaleCount = 0; for (ig=0; ig<plspec->panel[panel_index].scalesUsed[plane]; ig++) { group = plspec->panel[panel_index].scalesGroupIndex[plane][ig]; /* each scales group has a separate label */ if (plspec->scaleLabelInfo[plane][group].flags!=SCALE_LABEL_USED) continue; plspec->scaleLabelInfo[plane][group].flags = 0; plspec->scaleLabelInfo[plane][group].scaleNumber = -1; plspec->scaleLabelInfo[plane][group].label = newLabel = editCommand = NULL; for (set=0; set<datasets; set++) { /* scan for datasets that belong to this group */ if (dataset[set]->scalesGroupIndex[plane]!=group) continue; request = dataset[set]->request_index; if (plspec->plot_request[request].overlay.flags) continue; /* If we get to this point, then a scale will be made for * this group. */ if (plspec->scaleLabelInfo[plane][group].scaleNumber==-1) plspec->scaleLabelInfo[plane][group].scaleNumber = ++scaleCount; /* For each scales group, the label may have been specified by * a -label option for one request. */ if ((index=plspec->scalesGroupData[plane][group].labelSpecRequestIndex)>=0 && index==request && (flags=plspec->plot_request[index].label[plane].flags)) { if (flags&(LABEL_STRING_GIVEN+LABEL_PARAMETER_GIVEN+ LABEL_USE_NAME+LABEL_USE_SYMBOL+LABEL_USE_DESCRIPTION)) { if (newLabel) free(newLabel); if (!SDDS_CopyString(&newLabel, dataset[set]->label[plane])) SDDS_Bomb("Memory allocation failure (determine_panel_labels)"); } if (flags&LABEL_EDITCOMMAND_GIVEN) editCommand = plspec->plot_request[index].label[plane].edit_command; if (flags&LABEL_SCALE_GIVEN) { plspec->scaleLabelInfo[plane][group].flags |= SCALE_LABEL_SCALEGIVEN; plspec->scaleLabelInfo[plane][group].scale = plspec->plot_request[index].label[plane].scale; } if (flags&LABEL_OFFSET_GIVEN) { plspec->scaleLabelInfo[plane][group].flags |= SCALE_LABEL_OFFSETGIVEN; plspec->scaleLabelInfo[plane][group].offset = plspec->plot_request[index].label[plane].offset; } if (flags&LABEL_THICKNESS_GIVEN) { plspec->scaleLabelInfo[plane][group].flags |= SCALE_LABEL_THICKNESSGIVEN; plspec->scaleLabelInfo[plane][group].thickness = plspec->plot_request[index].label[plane].thickness; } if (flags&LABEL_LINETYPE_GIVEN) { plspec->scaleLabelInfo[plane][group].flags |= SCALE_LABEL_LINETYPEGIVEN; plspec->scaleLabelInfo[plane][group].linetype = plspec->plot_request[index].label[plane].linetype; } break; } } if (plspec->scaleLabelInfo[plane][group].scaleNumber==-1) continue; if (!newLabel) { /* No explicit label was given or generated */ if ((index=plspec->scalesGroupData[plane][group].tickSettingsRequestIndex)>=0 && plspec->plot_request[index].tick_settings.flags&ticksetTime[plane]) { /* Use calendar time scales, per user request. */ timeValue = plspec->scalesGroupData[plane][group].limit[0]; if (!(newLabel = SDDS_Malloc(sizeof(*newLabel)*50))) SDDS_Bomb("Memory allocation failure (determine_panel_labels)"); if (timeValue!=DBL_MAX) sprintf(newLabel, "Time starting %s", ctime(&timeValue)); else sprintf(newLabel, "Undefined time values!"); delete_chars(newLabel, "\n"); } else { /* Make a label using the names of the columns. This is the default * if no commandline options are given. */ if (!(newLabel=makeNameUnitsLabel(plspec, panel_index, plane, group))) SDDS_Bomb("Unable to make label (determine_panel_labels)"); } } if (editCommand) { /* Edit the label per user request. */ strcpy_ss(buffer, newLabel); free(newLabel); edit_string(buffer, editCommand); if (plspec->scaleLabelInfo[plane][group].label) free(plspec->scaleLabelInfo[plane][group].label); if (!SDDS_CopyString(&plspec->scaleLabelInfo[plane][group].label, buffer)) SDDS_Bomb("String copy problem (determine_panel_labels)"); } else { /* Store the label for latter placement on plot. */ if (plspec->scaleLabelInfo[plane][group].label) free(plspec->scaleLabelInfo[plane][group].label); plspec->scaleLabelInfo[plane][group].label = newLabel; newLabel = NULL; } } } /* This section creates labels for the title (0) and topline (1) */ for (j=0; j<2; j++) { if (plspec->panel[panel_index].titleSpec[j].label) SDDS_CopyString(plspec->panel[panel_index].title+j, plspec->panel[panel_index].titleSpec[j].label); else { /* Stuff in some default labels... */ if (j==0 && plspec->plot_request[dataset[0]->request_index].description_text[dataset[0]->file_index]) /* title from description text of first dataset, if there is any */ SDDS_CopyString(plspec->panel[panel_index].title+j, plspec->plot_request[dataset[0]->request_index]. description_text[dataset[0]->file_index]); else SDDS_CopyString(plspec->panel[panel_index].title+j, ""); } if ((editCommand=plspec->panel[panel_index].titleSpec[j].edit_command)) { strcpy_ss(buffer, plspec->panel[panel_index].title[j]); edit_string(buffer, editCommand); free(plspec->panel[panel_index].title[j]); SDDS_CopyString(plspec->panel[panel_index].title+j, buffer); } } if (plspec->panel[panel_index].flags&PLREQ_FNONTOPLINE) { /* User requested filenames on topline */ char **filename; char buffer[1024]; filename = tmalloc(sizeof(*filename)*plspec->panel[panel_index].datasets); for (i=0; i<plspec->panel[panel_index].datasets; i++) { request = plspec->panel[panel_index].dataset[i]->request_index; file = plspec->panel[panel_index].dataset[i]->file_index; filename[i] = plspec->plot_request[request].filename[file]; if (plspec->plot_request[request].filenamesOnToplineEditCmd) { strcpy_ss(buffer, filename[i]); edit_string(buffer, plspec->plot_request[request].filenamesOnToplineEditCmd); SDDS_CopyString(&filename[i], buffer); } } qsort(filename, plspec->panel[panel_index].datasets, sizeof(*filename), string_cmpasc); i = unique(filename, plspec->panel[panel_index].datasets, sizeof(*filename), string_cmpasc, string_copy); if (plspec->panel[panel_index].title[1][0]) sprintf(s, "%s ", plspec->panel[panel_index].title[1]); else s[0] = 0; for (j=0; j<i; j++) { if ((strlen(s)+strlen(filename[j])+3)>1024) break; if (j) strcat(s, ", "); strcat(s, filename[j]); } SDDS_CopyString(plspec->panel[panel_index].title+1, s); } if (plspec->panel[panel_index].flags&PLREQ_YLONTOPLINE) { /* This is probably impossible with the multiple scales... */ } }
int main(int argc, char **argv) { SDDS_DATASET SDDS_orig, SDDS_out; int32_t i, j, k, i_arg, rows, xfilter_provided, yfilter_provided, zfilter_provided, row; long power=1; SCANNED_ARG *s_arg; char *inputFile, *outputRoot, output[1024], tmpcol[256]; double xmin, xmax, ymin, ymax, zmin, zmax, xinterval, yinterval, zinterval; int32_t xdim, ydim, zdim, columnNames, outputColumns, page=0; char **columnName, **outputColumn; double ***Rho, ***Jz, rhoSum, rhoSum1, yRho, zRho, jzRho; double x_min, x_max, y_min, y_max, z_min, z_max, x, y, z; unsigned long dummyFlags = 0; x_min = x_max = y_min = y_max = z_min = z_max = 0; xfilter_provided = yfilter_provided = zfilter_provided = 0; inputFile = outputRoot = NULL; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&s_arg, argc, argv); if (argc<2) bomb(NULL, USAGE); columnNames = outputColumns = 0; columnName = outputColumn = NULL; Rho = Jz = NULL; 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_POWER: if (s_arg[i_arg].n_items!=2) SDDS_Bomb("Invalid -power syntax."); if (!get_long(&power, s_arg[i_arg].list[1])) SDDS_Bomb("Invalid -power value provided."); break; case SET_XFILTER: if (s_arg[i_arg].n_items<2) SDDS_Bomb("Invalid -xfilter syntax."); s_arg[i_arg].n_items--; if (!scanItemList(&dummyFlags, s_arg[i_arg].list+1, &s_arg[i_arg].n_items, 0, "minimum", SDDS_DOUBLE, &x_min, 1, 0, "maximum", SDDS_DOUBLE, &x_max, 1, 0, NULL)) SDDS_Bomb("Invalid -xfilter syntax"); s_arg[i_arg].n_items++; if (x_max<=x_min) { fprintf(stderr, "Invalid -xfilter provided, x_max <= x_min\n"); exit(1); } xfilter_provided =1; break; case SET_YFILTER: if (s_arg[i_arg].n_items<2) SDDS_Bomb("Invalid -yfilter syntax."); s_arg[i_arg].n_items--; if (!scanItemList(&dummyFlags, s_arg[i_arg].list+1, &s_arg[i_arg].n_items, 0, "minimum", SDDS_DOUBLE, &y_min, 1, 0, "maximum", SDDS_DOUBLE, &y_max, 1, 0, NULL)) SDDS_Bomb("Invalid -yfilter syntax"); s_arg[i_arg].n_items++; if (y_max<=y_min) { fprintf(stderr, "Invalid -yfilter provided, y_max <= y_min\n"); exit(1); } yfilter_provided =1; break; case SET_ZFILTER: if (s_arg[i_arg].n_items<2) SDDS_Bomb("Invalid -zfilter syntax."); s_arg[i_arg].n_items--; if (!scanItemList(&dummyFlags, s_arg[i_arg].list+1, &s_arg[i_arg].n_items, 0, "minimum", SDDS_DOUBLE, &z_min, 1, 0, "maximum", SDDS_DOUBLE, &z_max, 1, 0, NULL)) SDDS_Bomb("Invalid -zfilter syntax"); s_arg[i_arg].n_items++; if (z_max<=z_min) { fprintf(stderr, "Invalid -yfilter provided, z_max <= z_min\n"); exit(1); } zfilter_provided =1; break; default: fprintf(stderr, "Unknown option - %s provided.\n", s_arg[i_arg].list[0]); exit(1); } } else { if (!inputFile) inputFile = s_arg[i_arg].list[0]; else if (!outputRoot) outputRoot = s_arg[i_arg].list[0]; else SDDS_Bomb("Too many file names provided."); } } if (!outputRoot) outputRoot = inputFile; sprintf(output, "%s.ave", outputRoot); if (!SDDS_InitializeInput(&SDDS_orig, inputFile)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } xdim = ydim = zdim = 1; zmin = zmax = zinterval = 0; while (SDDS_ReadPage(&SDDS_orig)>0) { if (page==0) { if (!SDDS_GetParameterAsDouble(&SDDS_orig, "origin1", &xmin) || !SDDS_GetParameterAsDouble(&SDDS_orig, "max_ext1", &xmax) || !SDDS_GetParameterAsDouble(&SDDS_orig, "delta1", &xinterval) || !SDDS_GetParameterAsLong(&SDDS_orig, "numPhysCells1", &xdim) || !SDDS_GetParameterAsDouble(&SDDS_orig, "origin2", &ymin) || !SDDS_GetParameterAsDouble(&SDDS_orig, "max_ext2", &ymax) || !SDDS_GetParameterAsDouble(&SDDS_orig, "delta2", &yinterval) || !SDDS_GetParameterAsLong(&SDDS_orig, "numPhysCells2", &ydim)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (SDDS_CheckParameter(&SDDS_orig, "origin3", NULL, SDDS_ANY_NUMERIC_TYPE, NULL)==SDDS_CHECK_OK) { if (!SDDS_GetParameterAsDouble(&SDDS_orig, "origin3", &zmin) || !SDDS_GetParameterAsDouble(&SDDS_orig, "max_ext3", &zmax) || !SDDS_GetParameterAsDouble(&SDDS_orig, "delta3", &zinterval) || !SDDS_GetParameterAsLong(&SDDS_orig, "numPhysCells3", &zdim)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } } if (xfilter_provided) { if (x_min>xmax || x_max<xmin) { fprintf(stderr, "Invalid xfilter provided, it should be between %le and %le\n", xmin, xmax); if (!SDDS_Terminate(&SDDS_orig)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); exit(1); } } else { x_min = xmin; x_max = xmax; } if (yfilter_provided) { if (y_min>ymax || y_max<ymin) { fprintf(stderr, "Invalid yfilter provided, it should be between %le and %le\n", ymin, ymax); if (!SDDS_Terminate(&SDDS_orig)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); exit(1); } } else { y_min = ymin; y_max = ymax; } if (zfilter_provided && zmin!=0 && zmax!=0) { if (z_min>zmax || z_max<zmin) { fprintf(stderr, "Invalid zfilter provided, it should be between %le and %le\n", zmin, zmax); if (!SDDS_Terminate(&SDDS_orig)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); exit(1); } } else { z_min = zmin; z_max = zmax; } Rho = malloc(sizeof(*Rho)*zdim); Jz = malloc(sizeof(*Rho)*zdim); for (i=0; i<zdim; i++) { Rho[i] = malloc(sizeof(**Rho)*ydim); Jz[i] = malloc(sizeof(**Jz)*ydim); } SetupOutputFile(&SDDS_out, output, zdim); } rows = SDDS_CountRowsOfInterest(&SDDS_orig); if (rows!=xdim) { fprintf(stderr, "Row number does not equal xdim size.\n"); exit(1); } for (j=1; j<=ydim; j++) { sprintf(tmpcol, "Rho_%d",j); Rho[page][j-1] = NULL; if (!(Rho[page][j-1]=SDDS_GetColumnInDoubles(&SDDS_orig, tmpcol))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); sprintf(tmpcol, "Jz_%d",j); Jz[page][j-1] = NULL; if (!(Jz[page][j-1]=SDDS_GetColumnInDoubles(&SDDS_orig, tmpcol))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } page ++; } if (!SDDS_Terminate(&SDDS_orig)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (page!=zdim) { free_data_memory(Rho, zdim, ydim); free_data_memory(Jz, zdim, ydim); fprintf(stderr, "Error, the page number does not equal to zdim size.\n"); exit(1); } if (!SDDS_StartPage(&SDDS_out, xdim) || !SDDS_SetParameters(&SDDS_out, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "origin1", xmin, "origin2", ymin, "origin3", zmin, "max_ext1", xmax, "max_ext2", ymax, "delta1", xinterval, "delta2", yinterval, "numPhysCells1", xdim, "numPhysCells2", ydim, "xstart", x_min, "xend", x_max, "ystart", y_min, "yend", y_max, NULL)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (zdim>1) { if (!SDDS_SetParameters(&SDDS_out, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "origin3", zmin, "max_ext3", zmax, "delta3", zinterval, "numPhysCells3", zdim, "zstart", z_min, "zend", z_max, NULL)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } row = 0; x = y = z = 0; for (i=0; i<xdim; i++) { rhoSum = rhoSum1 = 0; yRho = zRho = jzRho = 0; x = i * xinterval + xmin; if (xfilter_provided && (x<x_min || x>x_max)) continue; for (j=0; j<ydim; j++) { y = j * yinterval + ymin; if (yfilter_provided && (y<y_min || y>y_max)) continue; for (k=0; k<zdim; k++) { z = k * zinterval + zmin; if (zfilter_provided && zdim>1 && (z<z_min || z>z_max)) continue; if (power==1) { yRho += fabs(Rho[k][j][i]) * y; zRho += fabs(Rho[k][j][i]) * z; jzRho += Rho[k][j][i] * Jz[k][j][i]; rhoSum += fabs(Rho[k][j][i]); rhoSum1 += Rho[k][j][i]; } else { yRho += pow(fabs(Rho[k][j][i]), power) * y; zRho += pow(fabs(Rho[k][j][i]), power) * z; jzRho += pow(Rho[k][j][i] * Jz[k][j][i], power); rhoSum += pow(fabs(Rho[k][j][i]), power); rhoSum1 += pow(Rho[k][j][i], power); } } } /*set row values */ if (!SDDS_SetRowValues(&SDDS_out, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, row, "x", x, "YAve", yRho/(rhoSum+1.0e-20), "JzAve", jzRho/(rhoSum1 + 1.0e-20), NULL)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (zdim>1 && !SDDS_SetRowValues(&SDDS_out, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, row, "ZAve",zRho/(rhoSum+1.0e-20), NULL)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); row++; } if (!SDDS_WritePage(&SDDS_out) || !SDDS_Terminate(&SDDS_out)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); free_data_memory(Rho, zdim, ydim); free_data_memory(Jz, zdim, ydim); return 0; }
int main(int argc, char **argv) { POLYNOMIAL *poly; long nPoly, iPoly, row, rows, iInput; int iArg; char *input, *output; unsigned long pipeFlags; SCANNED_ARG *scanned; SDDS_DATASET SDDSin, SDDSout; double *outputData ; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&scanned, argc, argv); if (argc<3) bomb(NULL, USAGE); outputData = NULL; input = output = NULL; pipeFlags = 0; poly = NULL; nPoly = 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_EVALUATE: if (!(poly = SDDS_Realloc(poly, sizeof(*poly)*(nPoly+1))) || !(poly[nPoly].inputColumn =SDDS_Malloc(sizeof(*(poly[nPoly].inputColumn))*5)) || !(poly[nPoly].powerColumn =SDDS_Malloc(sizeof(*(poly[nPoly].powerColumn))*5))) SDDS_Bomb("memory allocation failure"); scanned[iArg].n_items -= 1; if (!scanItemList(&poly[nPoly].flags, scanned[iArg].list+1, &scanned[iArg].n_items, 0, "filename", SDDS_STRING, &(poly[nPoly].filename), 1, POLY_FILE_SEEN, "output", SDDS_STRING, &(poly[nPoly].outputColumn), 1, POLY_OUTPUT_SEEN, "coefficients", SDDS_STRING, &(poly[nPoly].coefColumn), 1, POLY_COEF_SEEN, "input0", SDDS_STRING, poly[nPoly].inputColumn+0, 1, POLY_IN0_SEEN, "power0", SDDS_STRING, poly[nPoly].powerColumn+0, 1, POLY_OUT0_SEEN, "input1", SDDS_STRING, poly[nPoly].inputColumn+1, 1, POLY_IN1_SEEN, "power1", SDDS_STRING, poly[nPoly].powerColumn+1, 1, POLY_OUT1_SEEN, "input2", SDDS_STRING, poly[nPoly].inputColumn+2, 1, POLY_IN2_SEEN, "power2", SDDS_STRING, poly[nPoly].powerColumn+2, 1, POLY_OUT2_SEEN, "input3", SDDS_STRING, poly[nPoly].inputColumn+3, 1, POLY_IN3_SEEN, "power3", SDDS_STRING, poly[nPoly].powerColumn+3, 1, POLY_OUT3_SEEN, "input4", SDDS_STRING, poly[nPoly].inputColumn+4, 1, POLY_IN4_SEEN, "power4", SDDS_STRING, poly[nPoly].powerColumn+4, 1, POLY_OUT4_SEEN, NULL) || !(poly[nPoly].flags&POLY_FILE_SEEN) || !(poly[nPoly].flags&POLY_OUTPUT_SEEN) || !(poly[nPoly].flags&POLY_COEF_SEEN) || !(poly[nPoly].flags&POLY_IN0_SEEN) || !(poly[nPoly].flags&POLY_OUT0_SEEN)) SDDS_Bomb("invalid -evaluate syntax"); nPoly++; break; case CLO_PIPE: if (!processPipeOption(scanned[iArg].list+1, scanned[iArg].n_items-1, &pipeFlags)) SDDS_Bomb("invalid -pipe syntax"); 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("sddspoly", &input, &output, pipeFlags, 0, NULL); if (nPoly==0) SDDS_Bomb("give at least one -evaluate option"); if (!SDDS_InitializeInput(&SDDSin, input)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!SDDS_InitializeCopy(&SDDSout, &SDDSin, output, "w")) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (iPoly=0; iPoly<nPoly; iPoly++) initializePolynomial(&poly[iPoly], &SDDSin, &SDDSout); if (!SDDS_WriteLayout(&SDDSout)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); while (SDDS_ReadPage(&SDDSin)>0) { rows = SDDS_CountRowsOfInterest(&SDDSin); if (!SDDS_CopyPage(&SDDSout, &SDDSin)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!(outputData = SDDS_Realloc(outputData, sizeof(*outputData)*rows))) SDDS_Bomb("memory allocation failure"); for (iPoly=0; iPoly<nPoly; iPoly++) { for (iInput=0; iInput<poly[iPoly].nInputs; iInput++) { if (!(poly[iPoly].inputData[iInput]= SDDS_GetColumnInDoubles(&SDDSin, poly[iPoly].inputColumn[iInput]))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } for (row=0; row<rows; row++) { for (iInput=0; iInput<poly[iPoly].nInputs; iInput++) poly[iPoly].input[iInput] = poly[iPoly].inputData[iInput][row]; outputData[row] = evaluatePoly(poly[iPoly].coef, poly[iPoly].power, poly[iPoly].nTerms, poly[iPoly].input, poly[iPoly].nInputs); } if (!SDDS_SetColumn(&SDDSout, SDDS_SET_BY_NAME, outputData, rows, poly[iPoly].outputColumn)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_WritePage(&SDDSout)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } free(outputData); 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_scanargs(&scanned,argc); FreePolynormialMemory(poly,nPoly); free(poly); return 0; }
int main(int argc, char **argv) { int iArg; char **outputColumn, **difColumn; char *indepColumn, **depenColumn, **exclude; long depenColumns, excludes; char *input, *output; long i, rows, readCode, optionCode; unsigned long flags, pipeFlags; SCANNED_ARG *scanned; SDDS_DATASET SDDSin, SDDSout; double *timeData, *inputData, *outputData; FILTER_STAGE *filterStage; long filterStages, totalFilters; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&scanned, argc, argv); if (argc<3 || argc>(3+N_OPTIONS)) bomb(NULL, USAGE); output = input = NULL; flags = pipeFlags = 0; indepColumn = NULL; depenColumn = exclude = NULL; depenColumns = excludes = 0; if (!(filterStage = (FILTER_STAGE*)calloc(1, sizeof(*filterStage)))) SDDS_Bomb("allocation failure"); filterStage->filter = NULL; filterStage->filters = 0; filterStages = 1; totalFilters = 0; for (iArg=1; iArg<argc; iArg++) { if (scanned[iArg].arg_type==OPTION) { /* process options here */ switch (optionCode=match_string(scanned[iArg].list[0], option, N_OPTIONS, 0)) { case SET_PIPE: if (!processPipeOption(scanned[iArg].list+1, scanned[iArg].n_items-1, &pipeFlags)) SDDS_Bomb("invalid -pipe syntax"); break; case SET_COLUMNS: if (indepColumn) SDDS_Bomb("only one -columns option may be given"); if (scanned[iArg].n_items<2) SDDS_Bomb("invalid -columns syntax"); indepColumn = scanned[iArg].list[1]; if (scanned[iArg].n_items>=2) { depenColumn = tmalloc(sizeof(*depenColumn)*(depenColumns=scanned[iArg].n_items-2)); for (i=0; i<depenColumns; i++) depenColumn[i] = scanned[iArg].list[i+2]; } break; case SET_THRESHOLD: case SET_HIGHPASS: case SET_LOWPASS: case SET_NOTCH: case SET_BANDPASS: case SET_FILTERFILE: case SET_CLIPFREQ: addFilter(filterStage+filterStages-1, optionCode, scanned+iArg); totalFilters++; break; case SET_CASCADE: if (filterStages==0) SDDS_Bomb("-cascade option precedes all filter definitions"); if (!(filterStage = SDDS_Realloc(filterStage, (filterStages+1)*sizeof(*filterStage)))) SDDS_Bomb("allocation failure"); filterStage[filterStages].filter = NULL; filterStage[filterStages].filters = 0; filterStages++; break; case SET_NEWCOLUMNS: flags |= FL_NEWCOLUMNS; break; case SET_DIFFERENCECOLUMNS: flags |= FL_DIFCOLUMNS; break; case SET_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; default: fprintf(stderr, "error: unknown/ambiguous option: %s (%s)\n", scanned[iArg].list[0], argv[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("sddsfdfilter", &input, &output, pipeFlags, 0, NULL); if (!totalFilters) fputs("warning: no filters specified (sddsfdfilter)\n", stderr); if (!indepColumn) SDDS_Bomb("supply the independent column name with the -columns option"); if (!SDDS_InitializeInput(&SDDSin, input)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (SDDS_CheckColumn(&SDDSin, indepColumn, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OKAY) exit(1); excludes = appendToStringArray(&exclude, excludes, indepColumn); if (!depenColumns) depenColumns = appendToStringArray(&depenColumn, depenColumns, "*"); if ((depenColumns=expandColumnPairNames(&SDDSin, &depenColumn, NULL, depenColumns, exclude, excludes, FIND_NUMERIC_TYPE, 0))<=0) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); SDDS_Bomb("No quantities selected to filter"); } if (!SDDS_InitializeCopy(&SDDSout, &SDDSin, output, "w")) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (flags&FL_NEWCOLUMNS) { outputColumn = tmalloc(sizeof(*outputColumn)*depenColumns); for (i=0; i<depenColumns; i++) { outputColumn[i] = tmalloc(sizeof(**outputColumn)*(strlen(depenColumn[i])+1+strlen("Filtered"))); sprintf(outputColumn[i], "%sFiltered", depenColumn[i]); if (!SDDS_TransferColumnDefinition(&SDDSout, &SDDSin, depenColumn[i], outputColumn[i])) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } else outputColumn=depenColumn; difColumn = NULL; if (flags&FL_DIFCOLUMNS) { difColumn = tmalloc(sizeof(*difColumn)*depenColumns); for (i=0; i<depenColumns; i++) { difColumn[i] = tmalloc(sizeof(**difColumn)*(strlen(depenColumn[i])+1+strlen("Difference"))); sprintf(difColumn[i], "%sDifference", depenColumn[i]); if (!SDDS_TransferColumnDefinition(&SDDSout, &SDDSin, depenColumn[i], difColumn[i])) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } if (!SDDS_WriteLayout(&SDDSout)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); outputData = NULL; while ((readCode=SDDS_ReadPage(&SDDSin))>0) { if (!SDDS_CopyPage(&SDDSout, &SDDSin)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if ((rows = SDDS_CountRowsOfInterest(&SDDSin))<0) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (rows) { if (!(timeData = SDDS_GetColumnInDoubles(&SDDSin, indepColumn))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!(outputData = SDDS_Realloc(outputData, sizeof(*outputData)*rows))) SDDS_Bomb("allocation failure"); for (i=0; i<depenColumns; i++) { if (!(inputData = SDDS_GetColumnInDoubles(&SDDSin, depenColumn[i]))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!applyFilters(outputData, inputData, timeData, rows, filterStage, filterStages)) exit(1); if (!SDDS_SetColumnFromDoubles(&SDDSout, SDDS_BY_NAME, outputData, rows, outputColumn[i])) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (flags&FL_DIFCOLUMNS) { long j; for (j=0; j<rows; j++) outputData[j] = inputData[j] - outputData[j]; if (!SDDS_SetColumnFromDoubles(&SDDSout, SDDS_BY_NAME, outputData, rows, difColumn[i])) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } free(inputData); } free(timeData); } if (!SDDS_WritePage(&SDDSout)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } free(outputData); for(i=0;i<depenColumns;i++) { free(depenColumn[i]); if (flags&FL_NEWCOLUMNS) free(outputColumn[i]); if (flags&FL_DIFCOLUMNS) free(difColumn[i]); } for(i=0;i<excludes;i++) free(exclude[i]); free(indepColumn); if (flags&FL_NEWCOLUMNS) free(outputColumn); free(depenColumn); if (flags&FL_DIFCOLUMNS) free(difColumn); free(exclude); for(i=0;i<filterStages;i++) { long j; for(j=0;j<filterStage[i].filters;j++) { switch (filterStage[i].filter[j].filterType) { case SET_FILTERFILE : free( ((FILE_FILTER*) (filterStage[i].filter[j].filter))->freqData); free( ((FILE_FILTER*) (filterStage[i].filter[j].filter))->magData); free( ((FILE_FILTER*) (filterStage[i].filter[j].filter))->imagData); free( ((FILE_FILTER*) (filterStage[i].filter[j].filter))->realData); break; default : break; } } } if (!SDDS_Terminate(&SDDSout) || !SDDS_Terminate(&SDDSin)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); return 0; }
int main(int argc, char **argv) { SDDS_DATASET SDDS_input, SDDS_output; char *inputfile, *outputfile, **column; long i, i_arg; long page_number, allocated_rows, noWarnings, setPageNumber; int32_t columns; SCANNED_ARG *s_arg; char s[SDDS_MAXLINE]; unsigned long pipeFlags, majorOrderFlag; long buffer[16]; short columnMajorOrder = -1; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&s_arg, argc, argv); if (argc<2) bomb(NULL, USAGE); inputfile = outputfile = NULL; pipeFlags = noWarnings = 0; for (i_arg=1; i_arg<argc; i_arg++) { if (s_arg[i_arg].arg_type==OPTION) { switch (match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) { case SET_MAJOR_ORDER: majorOrderFlag=0; s_arg[i_arg].n_items -=1; if (s_arg[i_arg].n_items>0 && (!scanItemList(&majorOrderFlag, s_arg[i_arg].list+1, &s_arg[i_arg].n_items, 0, "row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER, "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER, NULL))) SDDS_Bomb("invalid -majorOrder syntax/values"); if (majorOrderFlag&SDDS_COLUMN_MAJOR_ORDER) columnMajorOrder=1; else if (majorOrderFlag&SDDS_ROW_MAJOR_ORDER) columnMajorOrder=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; case SET_NOWARNINGS: noWarnings = 1; break; default: fprintf(stderr, "error: unknown switch: %s\n", s_arg[i_arg].list[0]); exit(1); break; } } else { if (inputfile==NULL) inputfile = s_arg[i_arg].list[0]; else if (outputfile==NULL) outputfile = s_arg[i_arg].list[0]; else SDDS_Bomb("too many filenames"); } } #ifdef DEBUG fprintf(stderr, "About to process filename arguments\n"); #endif processFilenames("sddscollapse", &inputfile, &outputfile, pipeFlags, noWarnings, NULL); #ifdef DEBUG fprintf(stderr, "About to initialize input\n"); #endif if (!SDDS_InitializeInput(&SDDS_input, inputfile)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } #ifdef DEBUG fprintf(stderr, "About to initialize output\n"); #endif if (!SDDS_InitializeOutput(&SDDS_output, SDDS_input.layout.data_mode.mode, 1, NULL, NULL, outputfile) ) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (columnMajorOrder!=-1) SDDS_output.layout.data_mode.column_major = columnMajorOrder; else SDDS_output.layout.data_mode.column_major = SDDS_input.layout.data_mode.column_major; #ifdef DEBUG fprintf(stderr, "About to get parameter names\n"); #endif if (!(column = SDDS_GetParameterNames(&SDDS_input, &columns))) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } #ifdef DEBUG fprintf(stderr, "About to define parameters\n"); #endif for (i=0; i<columns; i++) { if (!SDDS_DefineColumnLikeParameter(&SDDS_output, &SDDS_input, column[i], NULL)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } } sprintf(s, "corresponding page number of %s for this row", inputfile?inputfile:"stdin"); if (SDDS_GetColumnIndex(&SDDS_output, "PageNumber")<0) { if (SDDS_DefineColumn(&SDDS_output, "PageNumber", NULL, NULL, s, NULL, SDDS_LONG, 0)<0) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } setPageNumber = 1; } else { setPageNumber = 0; } if (!SDDS_WriteLayout(&SDDS_output) || !SDDS_StartPage(&SDDS_output, allocated_rows=ROW_INCREMENT)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } while ((page_number=SDDS_ReadPageSparse(&SDDS_input, 0, LONG_MAX-1, 0))>0) { #ifdef DEBUG fprintf(stderr, "working on page %ld\n", page_number); #endif if (page_number>allocated_rows) { if (!SDDS_LengthenTable(&SDDS_output, ROW_INCREMENT)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } allocated_rows += ROW_INCREMENT; } for (i=0; i<columns; i++) { #ifdef DEBUG fprintf(stderr, "Getting param %s\n", column[i]); #endif if (!SDDS_GetParameter(&SDDS_input, column[i], buffer)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } #ifdef DEBUG fprintf(stderr, "Setting column %s\n", column[i]); #endif if (!SDDS_SetRowValues(&SDDS_output, SDDS_SET_BY_NAME|SDDS_PASS_BY_REFERENCE, page_number-1, column[i], buffer, NULL)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } #ifdef DEBUG fprintf(stderr, "Done setting %s\n", column[i]); #endif } #ifdef DEBUG fprintf(stderr, "Setting page number\n"); #endif if (setPageNumber && !SDDS_SetRowValues(&SDDS_output, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, page_number-1, "PageNumber", page_number, NULL)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } } if (!SDDS_WritePage(&SDDS_output)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (page_number==0) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (!SDDS_Terminate(&SDDS_input) || !SDDS_Terminate(&SDDS_output)) { 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) { SCANNED_ARG *s_arg; SDDS_DATASET inputPage, outputPage; char *inputfile, *outputfile; char **inputColumnName, **inputStringColumnName, **inputDoubleColumnName; char **outputStringColumnName, **outputDoubleColumnName, **matchColumn=NULL; long inputRows, inputDoubleColumns, inputStringColumns, indexColumn=0, matchColumns=0, noOldColumnNamesColumn=0; long outputRows, outputDoubleColumns, outputStringColumns; char **inputParameterName; int32_t inputParameters, inputColumns; char *inputDescription, *inputContents; char *outputDescription; long i, i_arg, col; char *buffer; char **columnOfStrings; long buffer_size; #define BUFFER_SIZE_INCREMENT 16384 MATRIX *R, *RInv; long OldStringColumnsDefined; char *inputStringRows, *outputStringRows; char **stringArray, *stringParameter; long token_length; long verbose; char format[32]; long digits; char *Symbol, *Root; void *parameterPointer; long ascii; unsigned long pipeFlags, majorOrderFlag; long tmpfile_used, noWarnings; long ipage=0, columnType; char *oldColumnNames, *newColumnNamesColumn; short columnMajorOrder=-1; inputColumnName = outputStringColumnName = outputDoubleColumnName = inputParameterName = NULL; outputRows = outputDoubleColumns = outputStringColumns = OldStringColumnsDefined = 0; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&s_arg, argc, argv); if (argc==1) bomb(NULL, USAGE); inputfile = outputfile = NULL; verbose = 0; Symbol = Root = NULL; ascii = 0; digits=3; pipeFlags = 0; tmpfile_used = 0; noWarnings = 0; oldColumnNames = NULL; newColumnNamesColumn = NULL; for (i_arg=1; i_arg<argc; i_arg++) { if (s_arg[i_arg].arg_type==OPTION) { switch(match_string(s_arg[i_arg].list[0], commandline_option, COMMANDLINE_OPTIONS, UNIQUE_MATCH)) { case CLO_MAJOR_ORDER: majorOrderFlag=0; s_arg[i_arg].n_items--; if (s_arg[i_arg].n_items>0 && (!scanItemList(&majorOrderFlag, s_arg[i_arg].list+1, &s_arg[i_arg].n_items, 0, "row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER, "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER, NULL))) SDDS_Bomb("invalid -majorOrder syntax/values"); if (majorOrderFlag&SDDS_COLUMN_MAJOR_ORDER) columnMajorOrder=1; else if (majorOrderFlag&SDDS_ROW_MAJOR_ORDER) columnMajorOrder=0; break; case CLO_MATCH_COLUMN: matchColumns = s_arg[i_arg].n_items-1; matchColumn = s_arg[i_arg].list+1; break; case CLO_INDEX_COLUMN: indexColumn = 1; break; case CLO_NO_OLDCOLUMNNAMES: noOldColumnNamesColumn = 1; break; case CLO_VERBOSE: verbose=1; break; case CLO_ASCII: ascii=1; break; case CLO_DIGITS: if (!(get_long(&digits, s_arg[i_arg].list[1]))) bomb("no string given for option -digits", USAGE); break; case CLO_COLUMNROOT: if (!(Root=s_arg[i_arg].list[1])) SDDS_Bomb("No root string given"); break; case CLO_SYMBOL: if (!(Symbol=s_arg[i_arg].list[1])) SDDS_Bomb("No symbol string given"); break; case CLO_PIPE: if (!processPipeOption(s_arg[i_arg].list+1, s_arg[i_arg].n_items-1, &pipeFlags)) SDDS_Bomb("invalid -pipe syntax"); break; case CLO_OLDCOLUMNNAMES: if (!(oldColumnNames=s_arg[i_arg].list[1])) SDDS_Bomb("No oldColumnNames string given"); break; case CLO_NEWCOLUMNNAMES: if (s_arg[i_arg].n_items!=2 || SDDS_StringIsBlank(newColumnNamesColumn = s_arg[i_arg].list[1])) SDDS_Bomb("Invalid -newColumnNames syntax/value"); break; default: bomb("unrecognized option given", USAGE); } } else { if (!inputfile) inputfile = s_arg[i_arg].list[0]; else if (!outputfile) outputfile = s_arg[i_arg].list[0]; else bomb("too many filenames given", USAGE); } } processFilenames("sddstranpose", &inputfile, &outputfile, pipeFlags, noWarnings, &tmpfile_used); if (newColumnNamesColumn && Root) SDDS_Bomb("-root and -newColumnNames are incompatible"); if (!SDDS_InitializeInput(&inputPage, inputfile) || !(inputParameterName=(char**)SDDS_GetParameterNames(&inputPage, &inputParameters)) || !SDDS_GetDescription(&inputPage, &inputDescription, &inputContents)) SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); if (matchColumns) inputColumnName = getMatchingSDDSNames(&inputPage, matchColumn, matchColumns, &inputColumns, SDDS_MATCH_COLUMN); else { if (!(inputColumnName=(char**)SDDS_GetColumnNames(&inputPage, &inputColumns))) SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); } inputDoubleColumns=0; inputStringColumns=0; inputDoubleColumnName=(char**)malloc(inputColumns*sizeof(char*)); inputStringColumnName=(char**)malloc(inputColumns*sizeof(char*)); inputRows = 0; /*********** \ * read data * \***********/ while (0<SDDS_ReadTable(&inputPage)) { ipage ++; #if defined(DEBUG) fprintf(stderr, "working on page %ld\n", ipage); #endif if (ipage==1) { SDDS_DeferSavingLayout(1); if( !SDDS_SetColumnFlags(&inputPage, 0)) SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); /* count the string and numerical columns in the input file */ for (i=0;i<inputColumns;i++) { if ( SDDS_NUMERIC_TYPE( columnType = SDDS_GetColumnType( &inputPage, i))) { inputDoubleColumnName[inputDoubleColumns]=inputColumnName[i]; inputDoubleColumns++; } } for (i=0; i<inputPage.layout.n_columns; i++) { if (inputPage.layout.column_definition[i].type == SDDS_STRING ) { inputStringColumnName[inputStringColumns] = inputPage.layout.column_definition[i].name; inputStringColumns++; } } if( !(inputRows=SDDS_CountRowsOfInterest(&inputPage))) SDDS_Bomb("No rows in dataset."); } else { /* these statements are executed on the subsequent pages */ if (inputRows != SDDS_CountRowsOfInterest(&inputPage)) { SDDS_Bomb("Datasets don't have the same number of rows.\nProcessing stopped before reaching the end of the input file."); } } #if defined(DEBUG) fprintf(stderr, "row flags set\n"); #endif if (inputDoubleColumns == 0) SDDS_Bomb("No numerical columns in file."); if ((ipage==1) && verbose) { fprintf(stderr, "No. of double/float/integer columns: %ld.\n", inputDoubleColumns); fprintf(stderr, "No. of string columns: %ld.\n", inputStringColumns); fprintf(stderr, "No. of rows: %ld.\n", inputRows); } /****************\ * transpose data * \****************/ if (inputDoubleColumns) { if (ipage == 1) { m_alloc(&RInv, inputRows, inputDoubleColumns); m_alloc(&R, inputDoubleColumns, inputRows); } for (col=0;col<inputDoubleColumns;col++){ if (!(R->a[col]=(double*)SDDS_GetColumnInDoubles(&inputPage, inputDoubleColumnName[col]))) { SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } if (verbose) { m_show(R, "%9.6le ", "Transpose of input matrix:\n", stdout); } m_trans(RInv, R); } /***************************\ * determine existence of * * transposed string columns * \***************************/ if (ipage == 1) { OldStringColumnsDefined=0; switch(SDDS_CheckParameter(&inputPage, OLD_STRING_COLUMN_NAMES, NULL, SDDS_STRING, NULL)){ case SDDS_CHECK_OKAY: OldStringColumnsDefined=1; break; case SDDS_CHECK_NONEXISTENT: break; case SDDS_CHECK_WRONGTYPE: case SDDS_CHECK_WRONGUNITS: fprintf(stderr, "Something wrong with parameter OldStringColumns.\n"); exit(1); break; } if (OldStringColumnsDefined){ /* decompose OldStringColumns into names of string columns for the output file */ if (!SDDS_GetParameter(&inputPage, OLD_STRING_COLUMN_NAMES, &inputStringRows)) SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (verbose) { fprintf(stderr, "Parameter OldStringColumns: %s.\n", inputStringRows); } outputStringColumnName=(char**)malloc(sizeof(char*)); outputStringColumns=0; buffer_size=BUFFER_SIZE_INCREMENT; buffer=(char*)malloc(sizeof(char)*buffer_size); while ( 0 <= (token_length = SDDS_GetToken(inputStringRows, buffer, BUFFER_SIZE_INCREMENT))){ if (!token_length) SDDS_Bomb("A null string was detected in parameter OldStringColumns.\n"); if (!SDDS_CopyString(&outputStringColumnName[outputStringColumns], buffer)) SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (verbose) { fprintf(stderr, "Output string column: %s\n", outputStringColumnName[outputStringColumns]); } outputStringColumns++; } } } /*********************\ * define output page * \*********************/ if ( ipage == 1 ) { outputRows = inputDoubleColumns; outputDoubleColumns = inputRows; if (inputDescription){ outputDescription = (char*) malloc( sizeof(char) * (strlen("Transpose of ") + strlen(inputDescription) + 1)); strcat(strcpy(outputDescription, "Transpose of "), inputDescription); if (!SDDS_InitializeOutput(&outputPage, ascii?SDDS_ASCII:SDDS_BINARY, 1, outputDescription, inputContents, outputfile)) SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } else { if (!SDDS_InitializeOutput(&outputPage, ascii?SDDS_ASCII:SDDS_BINARY, 1, NULL, NULL, outputfile)) SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (columnMajorOrder!=-1) outputPage.layout.data_mode.column_major = columnMajorOrder; else outputPage.layout.data_mode.column_major = inputPage.layout.data_mode.column_major; /***********************************\ * define names for double columns * \***********************************/ if (!Root && inputStringColumns ) { /* use specified string column, or first string column encountered */ if (!newColumnNamesColumn) /* first string column encountered */ outputDoubleColumnName = (char**) SDDS_GetColumn(&inputPage, inputStringColumnName[0]); else { /* use specified string column */ if (SDDS_CheckColumn(&inputPage, newColumnNamesColumn, NULL, SDDS_STRING, stderr)!=SDDS_CHECK_OKAY) SDDS_Bomb("column named with -newColumnNames does not exist in input"); outputDoubleColumnName = (char**)SDDS_GetColumn(&inputPage, newColumnNamesColumn); } for (i=1; i<inputRows; i++) { if (match_string(outputDoubleColumnName[i-1], outputDoubleColumnName+i, inputRows-i, EXACT_MATCH)>=0) { fprintf(stderr, "Error, duplicate %s found in input file string column %s, can not be used as output column names\n", outputDoubleColumnName[i-1], newColumnNamesColumn ? newColumnNamesColumn : inputStringColumnName[0]); exit(1); } } } else { /* use command line options to produce column names in the output file */ outputDoubleColumnName = (char**) malloc( outputDoubleColumns * sizeof(char*) ); digits = MAX(digits, log10(inputRows) + 1); if (!Root){ Root = (char*) malloc( sizeof(char) * (strlen("Column")+1) ); strcpy(Root, "Column"); } if (outputDoubleColumns!=1) { for ( i=0; i < outputDoubleColumns; i++){ outputDoubleColumnName[i] = (char*) malloc( sizeof(char) * (strlen(Root)+digits+1)); sprintf(format, "%s%%0%ldld", Root, digits); sprintf(outputDoubleColumnName[i], format, i); } } else {/* only one row to transpose */ outputDoubleColumnName[0] = (char*) malloc( sizeof(char) * (strlen(Root)+1)); strcpy( outputDoubleColumnName[0], Root); } } /*************************\ * define string columns * \*************************/ if (OldStringColumnsDefined) { if (!SDDS_DefineSimpleColumns(&outputPage, outputStringColumns, outputStringColumnName, NULL, SDDS_STRING)) SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } else { /* by default there should be at least one string column, that of the old column names. */ if (!noOldColumnNamesColumn) { outputStringColumns = 1; outputStringColumnName = (char**) malloc( sizeof(char*)); if (oldColumnNames) { /* commanline option specification */ outputStringColumnName[0] = oldColumnNames; } else { outputStringColumnName[0] = (char*) malloc( sizeof(char) * (strlen("OldColumnNames") + 1)); strcpy(outputStringColumnName[0], "OldColumnNames"); } if ( 0 > SDDS_DefineColumn(&outputPage, outputStringColumnName[0], NULL, NULL, NULL, NULL, SDDS_STRING, 0)) SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } if (indexColumn && !SDDS_DefineSimpleColumn(&outputPage, "Index", NULL, SDDS_LONG)) SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); /*************************\ * define double columns * \*************************/ for ( i=0; i < outputDoubleColumns; i++) if (Symbol){ if (0>SDDS_DefineColumn(&outputPage, outputDoubleColumnName[i], Symbol, NULL, NULL, NULL, SDDS_DOUBLE, 0)) SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } else { if (0>SDDS_DefineColumn(&outputPage, outputDoubleColumnName[i], NULL, NULL, NULL, NULL, SDDS_DOUBLE, 0)) SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } /********************************\ * define string parameters * * i.e. transposed string columns * \********************************/ if ( inputStringColumns>1 ) { if (0>SDDS_DefineParameter(&outputPage, OLD_STRING_COLUMN_NAMES, NULL, NULL, "Transposed string columns", NULL, SDDS_STRING, NULL)) SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for ( i=0; i < inputStringColumns; i++){ if (0>SDDS_DefineParameter(&outputPage, inputStringColumnName[i], NULL, NULL, "Transposed string column data", NULL, SDDS_STRING, NULL)) SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } /*************************\ * transfer parameters not * * associated with old * * string columns * \*************************/ if (inputParameters) { for ( i=0; i < inputParameters; i++) { if ( (0 > match_string(inputParameterName[i], outputStringColumnName, outputStringColumns, 0) && strcasecmp(inputParameterName[i], OLD_STRING_COLUMN_NAMES))) if ( 0 > SDDS_TransferParameterDefinition(&outputPage, &inputPage, inputParameterName[i], NULL)) SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } /***************\ * write layout * \***************/ SDDS_DeferSavingLayout(0); /* if InputFile is not already transfered ot the output file, then create it. */ switch( SDDS_CheckParameter(&outputPage, "InputFile", NULL, SDDS_STRING, NULL) ) { case SDDS_CHECK_NONEXISTENT: if (0>SDDS_DefineParameter(&outputPage, "InputFile", NULL, NULL, "Original matrix file", NULL, SDDS_STRING, NULL)) SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); break; default: break; } if (!SDDS_WriteLayout(&outputPage) ) SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } #if defined(DEBUG) fprintf(stderr, "table layout defined\n"); #endif if (!SDDS_StartTable(&outputPage, outputRows) ) SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (ipage == 1) { if (!SDDS_SetParameters(&outputPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "InputFile", inputfile?inputfile:"pipe", NULL)) SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } /***************************************\ * assign string columns from input * * to string parameters in output * \**************************************/ if ( inputStringColumns > 1) { for ( i=0; i < inputStringColumns; i++){ columnOfStrings = (char**) SDDS_GetColumn(&inputPage, inputStringColumnName[i]); stringParameter = JoinStrings(columnOfStrings, inputRows, BUFFER_SIZE_INCREMENT); if ( !SDDS_SetParameters(&outputPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, inputStringColumnName[i], stringParameter, NULL)) SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); free(columnOfStrings); free(stringParameter); } outputStringRows = JoinStrings(inputStringColumnName, inputStringColumns, BUFFER_SIZE_INCREMENT); if (!SDDS_SetParameters(&outputPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, OLD_STRING_COLUMN_NAMES, outputStringRows, NULL)) SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } #if defined(DEBUG) fprintf(stderr, "string parameters assigned\n"); #endif if (inputParameters){ for ( i=0; i < inputParameters; i++){ if ( (0 > match_string(inputParameterName[i], outputStringColumnName, outputStringColumns, 0) && strcasecmp(inputParameterName[i], OLD_STRING_COLUMN_NAMES))) { parameterPointer = (void*) SDDS_GetParameter(&inputPage, inputParameterName[i], NULL); if (!SDDS_SetParameters(&outputPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_REFERENCE, inputParameterName[i], parameterPointer, NULL)) SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); free(parameterPointer); } } } #if defined(DEBUG) fprintf(stderr, "input parameters assigned\n"); #endif /**********************************\ * assign data to * * output table part of data set * \**********************************/ if (outputRows) { /***************************\ * assign string column data * \***************************/ if (OldStringColumnsDefined){ for ( i=0 ; i < outputStringColumns; i++){ if (!SDDS_GetParameter(&inputPage, outputStringColumnName[i], &stringParameter)) SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); stringArray=TokenizeString(stringParameter, outputRows); if (!SDDS_SetColumn(&outputPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_REFERENCE, stringArray, outputRows, outputStringColumnName[i])) SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } else { if (!noOldColumnNamesColumn && !SDDS_SetColumn(&outputPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_REFERENCE, inputDoubleColumnName, outputRows, outputStringColumnName[0])) SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } #if defined(DEBUG) fprintf(stderr, "string data columns assigned\n"); #endif /***************************\ * assign double column data * \***************************/ for ( i=0 ; i < outputDoubleColumns; i++) /* i is the row index */ if (!SDDS_SetColumn(&outputPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_REFERENCE, RInv->a[i], outputRows, outputDoubleColumnName[i])) SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (indexColumn) { for (i=0; i<outputRows; i++) if (!SDDS_SetRowValues(&outputPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, i, "Index", i, NULL)) SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } #if defined(DEBUG) fprintf(stderr, "double data columns assigned\n"); #endif } #if defined(DEBUG) fprintf(stderr, "data assigned\n"); #endif if (!SDDS_WriteTable(&outputPage)) SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); #if defined(DEBUG) fprintf(stderr, "data written out\n"); #endif } if (inputDoubleColumns) { m_free(&RInv); m_free(&R); } if (inputColumnName) { SDDS_FreeStringArray(inputColumnName, inputColumns); free(inputColumnName); } if (inputStringColumns) free(inputStringColumnName); if (inputDescription) free(inputDescription); if (inputParameterName) { SDDS_FreeStringArray(inputParameterName, inputParameters); free(inputParameterName); } if (outputDoubleColumns) { SDDS_FreeStringArray(outputDoubleColumnName, outputDoubleColumns); free(outputDoubleColumnName); } if (!SDDS_Terminate(&inputPage) || !SDDS_Terminate(&outputPage)) SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (tmpfile_used && !replaceFileAndBackUp(inputfile, outputfile)) exit(1); 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); }
void initializePolynomial(POLYNOMIAL *poly, SDDS_DATASET *SDDSin, SDDS_DATASET *SDDSout) { long i; SDDS_DATASET SDDSpoly; char buffer[1024]; for (i=1; i<5; i++) { if ((poly->flags&(POLY_IN0_SEEN<<(2*i))) && !(poly->flags&(POLY_OUT0_SEEN<<(2*i)))) SDDS_Bomb("input qualifier seen without matching output qualifier"); if (!(poly->flags&(POLY_IN0_SEEN<<(2*i))) && (poly->flags&(POLY_OUT0_SEEN<<(2*i)))) SDDS_Bomb("output qualifier seen without matching input qualifier"); if (!(poly->flags&(POLY_IN0_SEEN<<(2*i))) && !(poly->flags&(POLY_OUT0_SEEN<<(2*i)))) break; } poly->nInputs = i; for (i++; i<5; i++) { if ((poly->flags&(POLY_IN0_SEEN<<(2*i))) || (poly->flags&(POLY_OUT0_SEEN<<(2*i)))) SDDS_Bomb("input<n> or output<n> qualifiers skipped"); } if (!SDDS_InitializeInput(&SDDSpoly, poly->filename)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (SDDS_GetColumnIndex(&SDDSpoly,poly->outputColumn)==-1) { if (!SDDS_DefineSimpleColumn(SDDSout, poly->outputColumn, NULL, SDDS_DOUBLE)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } else { if (SDDS_CheckColumn(&SDDSpoly, poly->outputColumn, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OKAY) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (SDDS_CheckColumn(&SDDSpoly, poly->coefColumn, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OKAY) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (i=0; i<poly->nInputs; i++) { if (SDDS_CheckColumn(SDDSin, poly->inputColumn[i], NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OKAY) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (SDDS_CheckColumn(&SDDSpoly, poly->powerColumn[i], NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OKAY) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (SDDS_ReadPage(&SDDSpoly)<=0) { sprintf(buffer, "problem with file %s\n", poly->filename); SDDS_SetError(buffer); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if ((poly->nTerms=SDDS_RowCount(&SDDSpoly))<=0) { sprintf(buffer, "problem with file %s: no rows\n", poly->filename); SDDS_SetError(buffer); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!(poly->coef=SDDS_GetColumnInDoubles(&SDDSpoly, poly->coefColumn))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!(poly->power=SDDS_Malloc(sizeof(*(poly->power))*poly->nInputs))) SDDS_Bomb("memory allocation failure"); for (i=0; i<poly->nInputs; i++) { if (!(poly->power[i]= SDDS_GetColumnInLong(&SDDSpoly, poly->powerColumn[i]))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_Terminate(&SDDSpoly)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!(poly->input = SDDS_Malloc(sizeof(*(poly->input))*poly->nInputs))) SDDS_Bomb("memory allocation failure"); if (!(poly->inputData = SDDS_Malloc(sizeof(*(poly->inputData))*poly->nInputs))) SDDS_Bomb("memory allocation failure"); }
int main(int argc, char **argv) { FILE *outfile; SDDS_TABLE SDDS_table; SDDS_LAYOUT *layout; COLUMN_DEFINITION *coldef; PARAMETER_DEFINITION *pardef; ARRAY_DEFINITION *arraydef; long list_request; char *input, *output; long i, j, i_arg, append_units, nrows, ntable; SCANNED_ARG *s_arg; char *text, *contents, *delimiter; long verbose=0, all=0, nvariableparms=0, excel=0, line=0; void *data; unsigned long pipeFlags; #ifdef USE_XLS workbook *w; worksheet *ws; #endif int ret; char sheet[256]; char buffer[5]; SDDS_RegisterProgramName(argv[0]); list_request = -1; input = output = NULL; append_units = 0; delimiter = DELIMITER; pipeFlags = 0; argc = scanargs(&s_arg, argc, argv); if (argc==1) bomb(NULL, USAGE); for (i_arg=1; i_arg<argc; i_arg++) { if (s_arg[i_arg].arg_type==OPTION) { switch (match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) { case SET_DELIMITER: if (s_arg[i_arg].n_items<2) SDDS_Bomb("Invalid -delimiter syntax"); delimiter = s_arg[i_arg].list[1]; break; case SET_ALL: all = 1; break; case SET_EXCEL: #ifdef USE_XLS excel = 1; #else SDDS_Bomb("-excel option is not available becuase sdds2spreadsheet was not compiled with xlslib support"); #endif break; case SET_VERBOSE: verbose = 1; 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: SDDS_Bomb(strcat("Unknown option: ", s_arg[i_arg].list[0])); break; } } else { if (input==NULL) input = s_arg[i_arg].list[0]; else if (output==NULL) output = s_arg[i_arg].list[0]; else SDDS_Bomb("too many filenames"); } } processFilenames("sdds2spreadsheet", &input, &output, pipeFlags, 0, NULL); if (output) { if (!excel) { outfile=fopen(output,"w"); if(!outfile) SDDS_Bomb(strcat("Can't open output file ", output)); } } else { if (excel) { SDDS_Bomb("-pipe=out and -excel options cannot be used together"); } outfile = stdout; } if (input && !excel) fprintf(outfile,"Created from SDDS file: %s\n", input); if (!SDDS_InitializeInput(&SDDS_table, input)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } layout = &SDDS_table.layout; /* Description */ if(verbose && input) fprintf(stderr, "\nfile %s is in SDDS protocol version %" PRId32 "\n", input, layout->version); if (!SDDS_GetDescription(&SDDS_table, &text, &contents)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (text) if (verbose) fprintf(stderr, "description: %s\n", text); if (!excel) fprintf(outfile,"%s%s\n",text?text:"No description",delimiter); if (contents) if (verbose) fprintf(stderr, "contents: %s\n", contents); if (!excel) fprintf(outfile,"%s%s\n",contents?contents:"No description",delimiter); if (layout->data_mode.mode==SDDS_ASCII) { if (verbose) { fprintf(stderr, "\ndata is ASCII with %" PRId32 " lines per row and %" PRId32 " additional header lines expected.\n", layout->data_mode.lines_per_row, layout->data_mode.additional_header_lines); fprintf(stderr, "row counts: %s \n", layout->data_mode.no_row_counts?"no":"yes"); } } else if (verbose) fprintf(stderr, "\ndata is binary\n"); /* Columns */ if (layout->n_columns) { if(verbose) { fprintf(stderr, "\n%" PRId32 " columns of data:\n", layout->n_columns); fprintf(stderr, "NAME UNITS SYMBOL FORMAT TYPE FIELD DESCRIPTION\n"); fprintf(stderr, " LENGTH\n"); } if (all && !excel) fprintf(outfile, "\nColumns%s\nName%sUnits%sSymbol%sFormat%sType%sField Length%sDescription%s\n", delimiter, delimiter, delimiter, delimiter, delimiter, delimiter, delimiter, delimiter); for (i=0; i<layout->n_columns; i++) { coldef = layout->column_definition+i; if(verbose) fprintf(stderr, "%-15s %-15s %-15s %-15s %-7s %-7" PRId32 " %s\n", coldef->name, coldef->units?coldef->units:"", coldef->symbol?coldef->symbol:"", coldef->format_string?coldef->format_string:"", SDDS_type_name[coldef->type-1], coldef->field_length, coldef->description?coldef->description:""); if(all && !excel) fprintf(outfile,"%s%s%s%s%s%s%s%s%s%s%-7" PRId32 "%s%s%s\n", coldef->name, delimiter, coldef->units?coldef->units:"", delimiter, coldef->symbol?coldef->symbol:"", delimiter, coldef->format_string?coldef->format_string:"", delimiter, SDDS_type_name[coldef->type-1], delimiter, coldef->field_length, delimiter, coldef->description?coldef->description:"", delimiter); } } /* Parameters */ if (layout->n_parameters) { if (verbose) { fprintf(stderr, "\n%" PRId32 " parameters:\n", layout->n_parameters); fprintf(stderr, "NAME UNITS SYMBOL TYPE DESCRIPTION\n"); } if(all && !excel) fprintf(outfile,"\nParameters%s\nName%sFixedValue%sUnits%sSymbol%sType%sDescription%s\n", delimiter, delimiter, delimiter, delimiter, delimiter, delimiter, delimiter); for (i=0; i<layout->n_parameters; i++) { pardef = layout->parameter_definition+i; if(!pardef->fixed_value) { nvariableparms++; if(!all) continue; } if(verbose) fprintf(stderr, "%-19s %-19s %-19s %-19s %s\n", pardef->name, pardef->units?pardef->units:"", pardef->symbol?pardef->symbol:"", SDDS_type_name[pardef->type-1], pardef->description?pardef->description:""); if (!excel) { if(all) fprintf(outfile,"%s%s%s%s%s%s%s%s%s%s%s%s\n", pardef->name, delimiter, pardef->fixed_value?pardef->fixed_value:"", delimiter, pardef->units?pardef->units:"", delimiter, pardef->symbol?pardef->symbol:"", delimiter, SDDS_type_name[pardef->type-1], delimiter, pardef->description?pardef->description:"", delimiter); else fprintf(outfile,"%s%s%s%s%s\n", pardef->name, delimiter, delimiter, pardef->fixed_value?pardef->fixed_value:"", delimiter); } } } /* Arrays */ if (layout->n_arrays && all) { if(verbose) { fprintf(stderr, "\n%" PRId32 " arrays of data:\n", layout->n_arrays); fprintf(stderr, "NAME UNITS SYMBOL" " FORMAT TYPE FIELD GROUP DESCRIPTION\n"); fprintf(stderr, " LENGTH NAME\n"); } if (!excel) { fprintf(outfile,"\nArrays%s\nName%sUnits%sSymbol%sFormat%sType%sField Length%sGroup Name%sDescription%s\n", delimiter, delimiter, delimiter, delimiter, delimiter, delimiter, delimiter, delimiter, delimiter); } for (i=0; i<layout->n_arrays; i++) { arraydef = layout->array_definition+i; if(verbose) fprintf(stderr, "%-15s %-15s %-15s %-7s %-8s*^%-5" PRId32 " %-7" PRId32 " %-15s %s\n", arraydef->name, arraydef->units, arraydef->symbol, arraydef->format_string, SDDS_type_name[arraydef->type-1], arraydef->dimensions, arraydef->field_length, arraydef->group_name, arraydef->description); if (!excel) { fprintf(outfile,"%s%s%s%s%s%s%s%s%s*^%-5" PRId32 "%s%-7" PRId32 "%s%s%s%s%s\n", arraydef->name, delimiter, arraydef->units, delimiter, arraydef->symbol, delimiter, arraydef->format_string, delimiter, SDDS_type_name[arraydef->type-1], arraydef->dimensions, delimiter, arraydef->field_length, delimiter, arraydef->group_name, delimiter, arraydef->description, delimiter); } } } /* Associates */ /* if (layout->n_associates && all) { if(verbose) { fprintf(stderr, "\n%ld associates:\n", layout->n_associates); fprintf(stderr, "SDDS FILENAME PATH CONTENTS DESCRIPTION\n"); } fprintf(outfile,"\nAssociates%s\nType%sFilename%sPath%sContents%sDescription%s\n", delimiter, delimiter, delimiter, delimiter, delimiter, delimiter); for (i=0; i<layout->n_associates; i++) if(verbose) fprintf(stderr, "%-5s %-19s %-29s %-19s %s\n", layout->associate_definition[i].sdds?"SDDS":"Non-SDDS", layout->associate_definition[i].filename, layout->associate_definition[i].path, layout->associate_definition[i].contents, layout->associate_definition[i].description); fprintf(outfile,"%s%s%s%s%s%s%s%s%s%s\n", layout->associate_definition[i].sdds?"yes":"no", delimiter, layout->associate_definition[i].filename, delimiter, layout->associate_definition[i].path, delimiter, layout->associate_definition[i].contents, delimiter, layout->associate_definition[i].description, delimiter); } */ #ifdef USE_XLS w = xlsNewWorkbook(); #ifdef __APPLE__ xlsWorkbookIconvInType(w, "UCS-4-INTERNAL"); #endif #endif /* Process tables */ while ((ntable=SDDS_ReadTable(&SDDS_table))>0) { line=0; #ifdef USE_XLS if (excel) { sprintf(sheet, "Sheet%d", ntable); ws = xlsWorkbookSheet(w, sheet); } else fprintf(outfile,"\nTable %ld\n",ntable); #else fprintf(outfile,"\nTable %ld\n",ntable); #endif /* Variable parameters */ if (nvariableparms) { for (i=0; i<layout->n_parameters; i++) { pardef = layout->parameter_definition+i; if(pardef->fixed_value) continue; if (!(data=SDDS_GetParameter(&SDDS_table, pardef->name, NULL))) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } #ifdef USE_XLS if (excel) { xlsWorksheetLabel(ws, line, 0, pardef->name, NULL); switch(pardef->type) { case SDDS_DOUBLE: xlsWorksheetNumberDbl(ws, line, 1, *((double*)data), NULL); break; case SDDS_FLOAT: xlsWorksheetNumberDbl(ws, line, 1, *((float*)data), NULL); break; case SDDS_ULONG: xlsWorksheetNumberInt(ws, line, 1, *((uint32_t*)data), NULL); break; case SDDS_LONG: xlsWorksheetNumberInt(ws, line, 1, *((int32_t*)data), NULL); break; case SDDS_USHORT: xlsWorksheetNumberInt(ws, line, 1, *((unsigned short*)data), NULL); break; case SDDS_SHORT: xlsWorksheetNumberInt(ws, line, 1, *((short*)data), NULL); break; case SDDS_STRING: xlsWorksheetLabel(ws, line, 1, *((char**)data), NULL); break; case SDDS_CHARACTER: sprintf(buffer, "%c", *((char*)data)); xlsWorksheetLabel(ws, line, 1, buffer, NULL); break; default: break; } line++; } else { #endif fprintf(outfile,"%s%s%s",pardef->name,delimiter,delimiter); SDDS_PrintTypedValue(data, 0, pardef->type, NULL, outfile, 0); fprintf(outfile,"%s\n",delimiter); #ifdef USE_XLS } #endif } line++; } /* Columns */ if (layout->n_columns) { SDDS_SetColumnFlags(&SDDS_table, 1); SDDS_SetRowFlags(&SDDS_table, 1); if ((nrows=SDDS_CountRowsOfInterest(&SDDS_table))<0) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } for (i=0; i<layout->n_columns; i++) { coldef = layout->column_definition+i; #ifdef USE_XLS if (excel) { xlsWorksheetLabel(ws, line, i, coldef->name, NULL); } else fprintf(outfile,"%s%s",coldef->name,delimiter); #else fprintf(outfile,"%s%s",coldef->name,delimiter); #endif } line++; if (!excel) fprintf(outfile,"\n"); if (nrows) { for (j=0; j<nrows; j++) { for (i=0; i<layout->n_columns; i++) { coldef = layout->column_definition+i; if (!(data=SDDS_GetValue(&SDDS_table, coldef->name,j,NULL))) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } #ifdef USE_XLS if (excel) { switch(coldef->type) { case SDDS_DOUBLE: xlsWorksheetNumberDbl(ws, line, i, *((double*)data), NULL); break; case SDDS_FLOAT: xlsWorksheetNumberDbl(ws, line, i, *((float*)data), NULL); break; case SDDS_ULONG: xlsWorksheetNumberInt(ws, line, i, *((uint32_t*)data), NULL); break; case SDDS_LONG: xlsWorksheetNumberInt(ws, line, i, *((int32_t*)data), NULL); break; case SDDS_USHORT: xlsWorksheetNumberInt(ws, line, i, *((unsigned short*)data), NULL); break; case SDDS_SHORT: xlsWorksheetNumberInt(ws, line, i, *((short*)data), NULL); break; case SDDS_STRING: xlsWorksheetLabel(ws, line, i, *((char**)data), NULL); break; case SDDS_CHARACTER: sprintf(buffer, "%c", *((char*)data)); xlsWorksheetLabel(ws, line, i, buffer, NULL); break; default: break; } } else { #endif switch(coldef->type) { case SDDS_DOUBLE: fprintf(outfile, "%.*g", DBL_DIG, *((double*)data)); break; case SDDS_FLOAT: fprintf(outfile, "%.*g", FLT_DIG, *((float*)data)); break; default: SDDS_PrintTypedValue(data, 0, coldef->type, NULL, outfile, 0); break; } fprintf(outfile,delimiter); #ifdef USE_XLS } #endif } if (!excel) fprintf(outfile,"\n"); line++; } } } } #ifdef USE_XLS ret = xlsWorkbookDump(w, output); xlsDeleteWorkbook(w); #endif /* QUIT: */ fflush(stdout); if (!SDDS_Terminate(&SDDS_table)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } return(0); }
void addFilter(FILTER_STAGE *filterStage, long optionCode, SCANNED_ARG *scanned) { THRESHOLD_FILTER *thresholdFilter; HILO_FILTER *hiloFilter; NHBP_FILTER *nhbpFilter; FILE_FILTER *fileFilter; CLIP_FILTER *clipFilter; FILTER *filter; long items, filters; char **item; if (!(filterStage->filter = SDDS_Realloc(filterStage->filter, sizeof(*filterStage->filter)*(filterStage->filters+1)))) SDDS_Bomb("allocation failure adding filter slot"); filter = filterStage->filter; filters = filterStage->filters; filterStage->filters ++; items = scanned->n_items - 1; item = scanned->list + 1; switch (filter[filters].filterType = optionCode) { case SET_THRESHOLD: if (!(thresholdFilter = filter[filters].filter = calloc(1, sizeof(THRESHOLD_FILTER)))) SDDS_Bomb("allocation failure allocating threshold filter"); if (items<1) SDDS_Bomb("invalid -threshold syntax"); if (!scanItemList(&thresholdFilter->flags, item, &items, 0, "level", SDDS_DOUBLE, &thresholdFilter->level, 1, FILT_LEVEL_GIVEN, "fractional", -1, NULL, 0, FILT_FRACTHRES_GIVEN, "start", SDDS_DOUBLE, &thresholdFilter->start, 1, FILT_START_GIVEN, "end", SDDS_DOUBLE, &thresholdFilter->end, 1, FILT_START_GIVEN, NULL)) SDDS_Bomb("invalid -threshold syntax/values"); if (!(thresholdFilter->flags&FILT_LEVEL_GIVEN)) SDDS_Bomb("supply level=<value> qualifier for -threshold"); if ((thresholdFilter->flags&FILT_START_GIVEN && thresholdFilter->flags&FILT_END_GIVEN) && thresholdFilter->start>thresholdFilter->end) SDDS_Bomb("start>end for -threshold filter"); break; case SET_HIGHPASS: case SET_LOWPASS: if (!(hiloFilter = filter[filters].filter = calloc(1, sizeof(HILO_FILTER)))) SDDS_Bomb("allocation failure allocating low-pass or high-pass filter"); if (!scanItemList(&hiloFilter->flags, item, &items, 0, "start", SDDS_DOUBLE, &hiloFilter->start, 1, FILT_START_GIVEN, "end", SDDS_DOUBLE, &hiloFilter->end, 1, FILT_END_GIVEN, NULL)) SDDS_Bomb("invalid -highpass or -lowpass syntax"); if (!(hiloFilter->flags&FILT_START_GIVEN) || !(hiloFilter->flags&FILT_END_GIVEN)) SDDS_Bomb("supply start=<value> and end=<value> qualifiers with -highpass and -lowpass"); break; case SET_NOTCH: case SET_BANDPASS: if (!(nhbpFilter = filter[filters].filter = calloc(1, sizeof(NHBP_FILTER)))) SDDS_Bomb("allocation failure allocating notch or bandpass filter"); if (!scanItemList(&nhbpFilter->flags, item, &items, 0, "center", SDDS_DOUBLE, &nhbpFilter->center, 1, FILT_CENTER_GIVEN, "fullwidth", SDDS_DOUBLE, &nhbpFilter->fullwidth, 1, FILT_FULLWIDTH_GIVEN, "flatwidth", SDDS_DOUBLE, &nhbpFilter->flatwidth, 1, FILT_FLATWIDTH_GIVEN, NULL)) SDDS_Bomb("invalid -notch or -bandpass syntax"); if (!(nhbpFilter->flags&FILT_CENTER_GIVEN) || !(nhbpFilter->flags&FILT_FLATWIDTH_GIVEN) ) SDDS_Bomb("supply center=<value> and flatWidth=<value> qualifiers with -notch and -bandpass"); if (!(nhbpFilter->flags&FILT_FULLWIDTH_GIVEN)) nhbpFilter->fullwidth = nhbpFilter->flatwidth; if (nhbpFilter->fullwidth<nhbpFilter->flatwidth) SDDS_Bomb("full width may not be less that flat width for notch/bandpass filter"); break; case SET_FILTERFILE: if (!(fileFilter = filter[filters].filter = calloc(1, sizeof(FILE_FILTER)))) SDDS_Bomb("allocation failure allocating file filter"); if (!scanItemList(&fileFilter->flags, item, &items, 0, "filename", SDDS_STRING, &fileFilter->file, 1, FILT_FILENAME_GIVEN, "frequency", SDDS_STRING, &fileFilter->freqName, 1, FILT_FREQNAME_GIVEN, "real", SDDS_STRING, &fileFilter->realName, 1, FILT_REALNAME_GIVEN, "imaginary", SDDS_STRING, &fileFilter->imagName, 1, FILT_IMAGNAME_GIVEN, "magnitude", SDDS_STRING, &fileFilter->magName, 1, FILT_MAGNAME_GIVEN, NULL)) SDDS_Bomb("invalid -fileFilter syntax"); if (!(fileFilter->flags&FILT_FILENAME_GIVEN)) SDDS_Bomb("supply filename=<string> with -fileFilter"); if (!(fileFilter->flags&FILT_FREQNAME_GIVEN)) SDDS_Bomb("supply frequency=<columnName> with -fileFilter"); if (!(fileFilter->flags&FILT_MAGNAME_GIVEN) && !(fileFilter->flags&FILT_REALNAME_GIVEN && fileFilter->flags&FILT_IMAGNAME_GIVEN)) SDDS_Bomb("supply either magnitude=<columnName>, or real=<columnName> and imag=<columnName>, with -fileFilter"); if (fileFilter->flags&FILT_MAGNAME_GIVEN && (fileFilter->flags&FILT_REALNAME_GIVEN || fileFilter->flags&FILT_IMAGNAME_GIVEN)) SDDS_Bomb("magnitude=<columnName> is incompatible with real=<columnName> and imag=<columnName> for -fileFilter"); fileFilter->freqData = NULL; /* indicates no data present */ break; case SET_CLIPFREQ: if (!(clipFilter = filter[filters].filter = calloc(1, sizeof(CLIP_FILTER)))) SDDS_Bomb("allocation failure allocating clip filter"); if (!scanItemList(&clipFilter->flags, item, &items, 0, "high", SDDS_LONG, &clipFilter->high, 1, FILT_HIGH_GIVEN, "low", SDDS_LONG, &clipFilter->low, 1, FILT_LOW_GIVEN, NULL)) SDDS_Bomb("invalid -clipFrequencies syntax"); if (!(clipFilter->flags&FILT_HIGH_GIVEN) && !(clipFilter->flags&FILT_LOW_GIVEN)) SDDS_Bomb("supply at least one of high=<number> or low=<number> with -clipFrequencies"); break; default: SDDS_Bomb("invalid filter code---this shouldn't happen"); break; } }
int main(int argc, char **argv) { FILE *fileID; COLUMN_DATA_STRUCTURES *columnValues; PARAMETER_DATA_STRUCTURES *parameterValues; SDDS_DATASET SDDS_dataset; SCANNED_ARG *s_arg; long i, j, k, n, i_arg; int32_t rows; int32_t maxRows=10000, initRows=10000, row; long par, col, page, size, readline=1, fillin=0; int32_t ptrSize=0; char *input, *output, s[1024], *ptr, *ptr2, data[10240],temp[10240]; unsigned long pipeFlags=0,majorOrderFlag; long noWarnings=0, tmpfile_used=0, columnOrder=0, whitespace=1; short shortValue; int32_t longValue; float floatValue; double doubleValue; char stringValue[SDDS_MAXLINE]; char characterValue; char buffer[124], buffer2[124]; long *parameterIndex, *columnIndex; long binary=0, noRowCount=0, inputBinary=0, count=0; char separator; char commentCharacters[20]; short checkComment=0; short commentFound; long parameters=0, columns=0; long skiplines=0; short abort=0, recover=1, columnMajorOrder=0; input = output = NULL; separator = ' '; columnValues = NULL; parameterValues = NULL; parameterIndex = columnIndex = NULL; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&s_arg, argc, argv); if (argc<3) bomb(NULL, USAGE); for (i_arg=1; i_arg<argc; i_arg++) { if (s_arg[i_arg].arg_type==OPTION) { switch (match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) { case SET_MAJOR_ORDER: majorOrderFlag=0; s_arg[i_arg].n_items -=1; if (s_arg[i_arg].n_items>0 && (!scanItemList(&majorOrderFlag, s_arg[i_arg].list+1, &s_arg[i_arg].n_items, 0, "row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER, "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER, NULL))) SDDS_Bomb("invalid -majorOrder syntax/values"); if (majorOrderFlag&SDDS_COLUMN_MAJOR_ORDER) columnMajorOrder=1; else if (majorOrderFlag&SDDS_ROW_MAJOR_ORDER) columnMajorOrder=0; break; case SET_OUTPUTMODE: if (s_arg[i_arg].n_items!=2) SDDS_Bomb("invalid -outputMode syntax"); switch (match_string(s_arg[i_arg].list[1], mode_name, MODES, 0)) { case ASCII_MODE: binary = 0; break; case BINARY_MODE: binary = 1; break; default: SDDS_Bomb("invalid -outputMode syntax"); break; } break; case SET_INPUTMODE: if (s_arg[i_arg].n_items!=2) SDDS_Bomb("invalid -inputMode syntax"); switch (match_string(s_arg[i_arg].list[1], mode_name, MODES, 0)) { case ASCII_MODE: inputBinary = 0; break; case BINARY_MODE: inputBinary = 1; break; default: SDDS_Bomb("invalid -inputMode syntax"); break; } break; case SET_SEPARATOR: if (s_arg[i_arg].n_items!=2) SDDS_Bomb("invalid -separator syntax"); separator = s_arg[i_arg].list[1][0]; whitespace = 0; break; case SET_COMMENT: if (s_arg[i_arg].n_items!=2) SDDS_Bomb("invalid -commentCharacters syntax"); sprintf(commentCharacters, "%s", s_arg[i_arg].list[1]); checkComment=1; break; case SET_FILLIN: fillin=1; break; case SET_NOROWCOUNT: if (s_arg[i_arg].n_items!=1) SDDS_Bomb("invalid -noRowCount syntax"); noRowCount = 1; break; case SET_ORDER: if (s_arg[i_arg].n_items!=2) SDDS_Bomb("invalid -order syntax"); switch (match_string(s_arg[i_arg].list[1], order_names, ORDERS, 0)) { case ROW_ORDER: columnOrder = 0; break; case COLUMN_ORDER: columnOrder = 1; break; default: SDDS_Bomb("invalid -order syntax"); break; } break; case SET_PARAMETER: if (s_arg[i_arg].n_items<3) SDDS_Bomb("invalid -parameter syntax"); count=1; parameters++; parameterValues = trealloc(parameterValues, sizeof(*parameterValues)*(parameters)); SDDS_CopyString(¶meterValues[parameters-1].name, s_arg[i_arg].list[1]); parameterValues[parameters-1].units = NULL; parameterValues[parameters-1].description = NULL; parameterValues[parameters-1].symbol = NULL; switch (match_string(s_arg[i_arg].list[2], type_name, DATATYPES, 0)) { case TYPE_SHORT: parameterValues[parameters-1].type = SDDS_SHORT; break; case TYPE_LONG: parameterValues[parameters-1].type = SDDS_LONG; break; case TYPE_FLOAT: parameterValues[parameters-1].type = SDDS_FLOAT; break; case TYPE_DOUBLE: parameterValues[parameters-1].type = SDDS_DOUBLE; break; case TYPE_STRING: parameterValues[parameters-1].type = SDDS_STRING; break; case TYPE_CHARACTER: parameterValues[parameters-1].type = SDDS_CHARACTER; break; default: SDDS_Bomb("invalid -parameter type"); break; } for (i=3;i<s_arg[i_arg].n_items;i++) { if (!(ptr=strchr(s_arg[i_arg].list[i], '='))) SDDS_Bomb("invalid -parameter syntax"); *ptr++ = 0; switch (match_string(s_arg[i_arg].list[i], header_elements, HEADERELEMENTS, 0)) { case HEADER_UNITS: SDDS_CopyString(¶meterValues[parameters-1].units, ptr); break; case HEADER_DESCRIPTION: SDDS_CopyString(¶meterValues[parameters-1].description, ptr); break; case HEADER_SYMBOL: SDDS_CopyString(¶meterValues[parameters-1].symbol, ptr); break; case HEADER_COUNT: if (sscanf(ptr, "%ld", &count)!=1 || count<=0) SDDS_Bomb("invalid parameter count value"); sprintf(buffer, "%s", parameterValues[parameters-1].name); sprintf(buffer2, "%s1", buffer); free(parameterValues[parameters-1].name); SDDS_CopyString(¶meterValues[parameters-1].name, buffer2); break; default: SDDS_Bomb("invalid -parameter syntax"); break; } } for (i=2;i<=count;i++) { parameters++; parameterValues = trealloc(parameterValues, sizeof(*parameterValues)*(parameters)); sprintf(buffer2, "%s%ld", buffer, i); SDDS_CopyString(¶meterValues[parameters-1].name, buffer2); parameterValues[parameters-1].units = NULL; parameterValues[parameters-1].description = NULL; parameterValues[parameters-1].symbol = NULL; parameterValues[parameters-1].type = parameterValues[parameters-2].type; if (parameterValues[parameters-2].units) SDDS_CopyString(¶meterValues[parameters-1].units, parameterValues[parameters-2].units); if (parameterValues[parameters-2].description) SDDS_CopyString(¶meterValues[parameters-1].description, parameterValues[parameters-2].description); if (parameterValues[parameters-2].symbol) SDDS_CopyString(¶meterValues[parameters-1].symbol, parameterValues[parameters-2].symbol); } break; case SET_COLUMN: if (s_arg[i_arg].n_items<3) SDDS_Bomb("invalid -column syntax"); count=1; columns++; columnValues = trealloc(columnValues, sizeof(*columnValues)*(columns)); SDDS_CopyString(&columnValues[columns-1].name, s_arg[i_arg].list[1]); columnValues[columns-1].elements = 0; columnValues[columns-1].values = NULL; columnValues[columns-1].stringValues = NULL; columnValues[columns-1].units = NULL; columnValues[columns-1].description = NULL; columnValues[columns-1].symbol = NULL; columnValues[columns-1].skip = 0; switch (match_string(s_arg[i_arg].list[2], type_name, DATATYPES, 0)) { case TYPE_SHORT: columnValues[columns-1].type = SDDS_SHORT; break; case TYPE_LONG: columnValues[columns-1].type = SDDS_LONG; break; case TYPE_FLOAT: columnValues[columns-1].type = SDDS_FLOAT; break; case TYPE_DOUBLE: columnValues[columns-1].type = SDDS_DOUBLE; break; case TYPE_STRING: columnValues[columns-1].type = SDDS_STRING; break; case TYPE_CHARACTER: columnValues[columns-1].type = SDDS_CHARACTER; break; default: SDDS_Bomb("invalid -column type"); break; } for (i=3;i<s_arg[i_arg].n_items;i++) { if (!(ptr=strchr(s_arg[i_arg].list[i], '='))) SDDS_Bomb("invalid -column syntax"); *ptr++ = 0; switch (match_string(s_arg[i_arg].list[i], header_elements, HEADERELEMENTS, 0)) { case HEADER_UNITS: SDDS_CopyString(&columnValues[columns-1].units, ptr); break; case HEADER_DESCRIPTION: SDDS_CopyString(&columnValues[columns-1].description, ptr); break; case HEADER_SYMBOL: SDDS_CopyString(&columnValues[columns-1].symbol, ptr); break; case HEADER_COUNT: if (sscanf(ptr, "%ld", &count)!=1 || count<=0) SDDS_Bomb("invalid column count value"); sprintf(buffer, "%s", columnValues[columns-1].name); sprintf(buffer2, "%s1", buffer); free(columnValues[columns-1].name); SDDS_CopyString(&columnValues[columns-1].name, buffer2); break; default: SDDS_Bomb("invalid -column syntax"); break; } } for (i=2;i<=count;i++) { columns++; columnValues = trealloc(columnValues, sizeof(*columnValues)*(columns)); sprintf(buffer2, "%s%ld", buffer, i); SDDS_CopyString(&columnValues[columns-1].name, buffer2); columnValues[columns-1].elements = 0; columnValues[columns-1].values = NULL; columnValues[columns-1].stringValues = NULL; columnValues[columns-1].units = NULL; columnValues[columns-1].description = NULL; columnValues[columns-1].symbol = NULL; columnValues[columns-1].type = columnValues[columns-2].type; if (columnValues[columns-2].units) SDDS_CopyString(&columnValues[columns-1].units, columnValues[columns-2].units); if (columnValues[columns-2].description) SDDS_CopyString(&columnValues[columns-1].description, columnValues[columns-2].description); if (columnValues[columns-2].symbol) SDDS_CopyString(&columnValues[columns-1].symbol, columnValues[columns-2].symbol); } break; case SET_SKIPCOLUMN: if (s_arg[i_arg].n_items!=2) SDDS_Bomb("invalid -skipcolumn syntax"); count=1; columns++; columnValues = trealloc(columnValues, sizeof(*columnValues)*(columns)); columnValues[columns-1].name = NULL; columnValues[columns-1].elements = 0; columnValues[columns-1].values = NULL; columnValues[columns-1].stringValues = NULL; columnValues[columns-1].units = NULL; columnValues[columns-1].description = NULL; columnValues[columns-1].symbol = NULL; columnValues[columns-1].skip = 1; switch (match_string(s_arg[i_arg].list[1], type_name, DATATYPES, 0)) { case TYPE_SHORT: columnValues[columns-1].type = SDDS_SHORT; break; case TYPE_LONG: columnValues[columns-1].type = SDDS_LONG; break; case TYPE_FLOAT: columnValues[columns-1].type = SDDS_FLOAT; break; case TYPE_DOUBLE: columnValues[columns-1].type = SDDS_DOUBLE; break; case TYPE_STRING: columnValues[columns-1].type = SDDS_STRING; break; case TYPE_CHARACTER: columnValues[columns-1].type = SDDS_CHARACTER; break; default: SDDS_Bomb("invalid -skipcolumn type"); break; } 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; case SET_NOWARNINGS: if (s_arg[i_arg].n_items!=1) SDDS_Bomb("invalid -nowarnings syntax"); noWarnings = 1; break; case SET_SKIPLINES: if (s_arg[i_arg].n_items!=2 || sscanf(s_arg[i_arg].list[1], "%ld", &skiplines)!=1 || skiplines<=0) SDDS_Bomb("invalid -skiplines syntax"); break; default: fprintf(stderr, "error: unknown switch: %s\n", s_arg[i_arg].list[0]); exit(1); break; } } else { if (input == NULL) { input = s_arg[i_arg].list[0]; } else if (output == NULL) { output = s_arg[i_arg].list[0]; } else { fprintf(stderr, "too many filenames"); exit(1); } } } processFilenames("plaindata2sdds", &input, &output, pipeFlags, noWarnings, &tmpfile_used); if (!columns && !parameters) SDDS_Bomb("you must specify one of the -column or the -parameter options"); if (skiplines && inputBinary) SDDS_Bomb("-skiplines does not work with binary input files"); if (!input) { if (inputBinary) { #if defined(_WIN32) if (_setmode(_fileno(stdin), _O_BINARY) == -1) { fprintf(stderr, "error: unable to set stdin to binary mode\n"); exit(1); } #endif } fileID = stdin; } else { if (!fexists(input)) { fprintf(stderr, "input file not found\n"); exit(1); } if (inputBinary) { fileID = fopen(input, "rb"); } else { fileID = fopen(input, "r"); } } if (fileID == NULL) { fprintf(stderr, "unable to open input file for reading\n"); exit(1); } if (!SDDS_InitializeOutput(&SDDS_dataset, binary?SDDS_BINARY:SDDS_ASCII, 1, NULL, NULL, output)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); SDDS_dataset.layout.data_mode.column_major = columnMajorOrder; if (parameters) { parameterIndex = tmalloc(sizeof(*parameterIndex)*parameters); } if (columns) { columnIndex = tmalloc(sizeof(*columnIndex)*columns); } for (i=0; i<parameters; i++) { if ((parameterIndex[i] = SDDS_DefineParameter(&SDDS_dataset, parameterValues[i].name, parameterValues[i].symbol, parameterValues[i].units, parameterValues[i].description, NULL, parameterValues[i].type, 0))<0) { sprintf(s, "Problem defining parameter %s.", parameterValues[i].name); SDDS_SetError(s); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } for (i=0; i<columns; i++) { if (columnValues[i].skip) continue; if ((columnIndex[i] = SDDS_DefineColumn(&SDDS_dataset, columnValues[i].name, columnValues[i].symbol, \ columnValues[i].units, columnValues[i].description, NULL, columnValues[i].type, 0))<0) { sprintf(s, "Problem defining column %s.", columnValues[i].name); SDDS_SetError(s); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } if (!SDDS_WriteLayout(&SDDS_dataset)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!SDDS_StartPage(&SDDS_dataset, initRows)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); row = par = col = page = 0; while (inputBinary) { row = par = col = 0; if (fread(&rows,sizeof(rows),1,fileID) != 1) { if (page == 0) { SDDS_SetError("Unable to read number of rows"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } else { if (!SDDS_Terminate(&SDDS_dataset)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (k=0;k<columns;k++) { if (columnValues[k].type == SDDS_STRING) { SDDS_FreeStringArray(columnValues[k].stringValues,columnValues[k].elements); } else { free(columnValues[k].values); } } return 0; } } page++; for (par=0;par<parameters;par++) { switch (parameterValues[par].type) { case SDDS_SHORT: if (fread(&shortValue,2,1,fileID) != 1) { SDDS_SetError("Unable to read short parameter"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_SetParameters(&SDDS_dataset,SDDS_SET_BY_INDEX|SDDS_PASS_BY_VALUE,par,shortValue,-1)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); break; case SDDS_LONG: if (fread(&longValue,4,1,fileID) != 1) { SDDS_SetError("Unable to read long parameter"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_SetParameters(&SDDS_dataset,SDDS_SET_BY_INDEX|SDDS_PASS_BY_VALUE,par,longValue,-1)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); break; case SDDS_FLOAT: if (fread(&floatValue,4,1,fileID) != 1) { SDDS_SetError("Unable to read float parameter"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_SetParameters(&SDDS_dataset,SDDS_SET_BY_INDEX|SDDS_PASS_BY_VALUE,par,floatValue,-1)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); break; case SDDS_DOUBLE: if (fread(&doubleValue,8,1,fileID) != 1) { SDDS_SetError("Unable to read double parameter"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_SetParameters(&SDDS_dataset,SDDS_SET_BY_INDEX|SDDS_PASS_BY_VALUE,par,doubleValue,-1)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); break; case SDDS_STRING: if (fread(&size,4,1,fileID) != 1) { SDDS_SetError("Unable to read string parameter"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (size>SDDS_MAXLINE-1) SDDS_Bomb("String is too long"); if (size > 0) { if (fread(&stringValue,size,1,fileID) != 1) { SDDS_SetError("Unable to read string parameter"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } stringValue[size] = 0; } else { strcpy(stringValue,""); } if (!SDDS_SetParameters(&SDDS_dataset,SDDS_SET_BY_INDEX|SDDS_PASS_BY_VALUE,par,stringValue,-1)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); break; case SDDS_CHARACTER: if (fread(&characterValue,1,1,fileID) != 1) { SDDS_SetError("Unable to read character parameter"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_SetParameters(&SDDS_dataset,SDDS_SET_BY_INDEX|SDDS_PASS_BY_VALUE,par,characterValue,-1)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); break; } } for (i=0;i<columns;i++) { if (rows > columnValues[i].elements) { if (columnValues[i].type == SDDS_STRING) { columnValues[i].stringValues = AllocateColumnStringData(columnValues[i].stringValues, rows, columnValues[i].elements); } else { columnValues[i].values = AllocateColumnData(columnValues[i].type, columnValues[i].values, rows); } columnValues[i].elements = rows; } } if (columnOrder) { for (col=0;col<columns;col++) { switch (columnValues[col].type) { case SDDS_SHORT: for (i=0;i<rows;i++) { if (fread((short*)(columnValues[col].values)+i,2,1,fileID) != 1) { SDDS_SetError("Unable to read short column"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } break; case SDDS_LONG: for (i=0;i<rows;i++) { if (fread((int32_t*)(columnValues[col].values)+i,4,1,fileID) != 1) { SDDS_SetError("Unable to read long column"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } break; case SDDS_FLOAT: for (i=0;i<rows;i++) { if (fread((float*)(columnValues[col].values)+i,4,1,fileID) != 1) { SDDS_SetError("Unable to read float column"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } break; case SDDS_DOUBLE: for (i=0;i<rows;i++) { if (fread((double*)(columnValues[col].values)+i,8,1,fileID) != 1) { SDDS_SetError("Unable to read double double"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } break; case SDDS_STRING: for (i=0;i<rows;i++) { if (fread(&size,4,1,fileID) != 1) { SDDS_SetError("Unable to read string column"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (size>SDDS_MAXLINE-1) SDDS_Bomb("String is too long"); columnValues[col].stringValues[i] = malloc(size + 1); if (size > 0) { if (fread(columnValues[col].stringValues[i],size,1,fileID) != 1) { SDDS_SetError("Unable to read string column"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } columnValues[col].stringValues[i][size] = 0; } else { strcpy(columnValues[col].stringValues[i],""); } } break; case SDDS_CHARACTER: for (i=0;i<rows;i++) { if (fread((char*)(columnValues[col].values)+i,1,1,fileID) != 1) { SDDS_SetError("Unable to read character column"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } break; } } } else { for (i=0;i<rows;i++) { for (col=0;col<columns;col++) { switch (columnValues[col].type) { case SDDS_SHORT: if (fread((short*)(columnValues[col].values)+i,2,1,fileID) != 1) { SDDS_SetError("Unable to read short column"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } break; case SDDS_LONG: if (fread((int32_t*)(columnValues[col].values)+i,4,1,fileID) != 1) { SDDS_SetError("Unable to read long column"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } break; case SDDS_FLOAT: if (fread((float*)(columnValues[col].values)+i,4,1,fileID) != 1) { SDDS_SetError("Unable to read float column"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } break; case SDDS_DOUBLE: if (fread(((double*)(columnValues[col].values)+i),8,1,fileID) != 1) { SDDS_SetError("Unable to read double column"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } break; case SDDS_STRING: if (fread(&size,4,1,fileID) != 1) { SDDS_SetError("Unable to read string column"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (size>SDDS_MAXLINE-1) SDDS_Bomb("String is too long"); columnValues[col].stringValues[i] = malloc(size + 1); if (size > 0) { if (fread(columnValues[col].stringValues[i],size,1,fileID) != 1) { SDDS_SetError("Unable to read string column"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } columnValues[col].stringValues[i][size] = 0; } else { strcpy(columnValues[col].stringValues[i],""); } break; case SDDS_CHARACTER: if (fread(((char*)(columnValues[col].values)+i),1,1,fileID) != 1) { SDDS_SetError("Unable to read character column"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } break; } } } } if (rows > maxRows) { if (!SDDS_LengthenTable(&SDDS_dataset, rows - maxRows)) { SDDS_SetError("Unable to lengthen table"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } maxRows = rows; } j=n; for (i=0;i<columns;i++) { if (columnValues[i].skip) continue; if (columnValues[i].type == SDDS_STRING) { SetColumnData(columnValues[i].type, &SDDS_dataset, columnValues[i].stringValues, rows, n); } else { SetColumnData(columnValues[i].type, &SDDS_dataset, columnValues[i].values, rows, n); } n++; } if (!SDDS_WritePage(&SDDS_dataset)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } maxRows = 10000; if (!SDDS_StartPage(&SDDS_dataset, initRows)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } row = par = col = n = 0; rows = -1; ptr = NULL; ptr = SDDS_Malloc(sizeof(*ptr)*(ptrSize=2048)); /* ptr2 = NULL; ptr2 = SDDS_Malloc(sizeof(*ptr)*(ptrSize=2048));*/ ptr[0] = 0; while (1) { if (readline) { while (skiplines > 0) { fgets(ptr, ptrSize, fileID); skiplines--; } if (!fgetsSkipCommentsResize(&ptr,&ptrSize,fileID, '!')) break; commentFound=0; if (checkComment) { for (i=0; i<strlen(commentCharacters); i++) { if (ptr[0] == commentCharacters[i]) { commentFound=1; } } } if (commentFound == 1) { continue; } if (ptr[strlen(ptr)-1] == '\n') ptr[strlen(ptr)-1] = 0; strcpy(temp,ptr); /*skip empty lines*/ if (getToken(temp,data,10240,separator,whitespace)<0) continue; } else { readline = 1; } if (par < parameters) { switch (parameterValues[par].type) { case SDDS_SHORT: if (sscanf(ptr,"%hd",&shortValue) != 1) { SDDS_SetError("Invalid short parameter"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_SetParameters(&SDDS_dataset,SDDS_SET_BY_INDEX|SDDS_PASS_BY_VALUE,par,shortValue,-1)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); break; case SDDS_LONG: if (sscanf(ptr,"%" SCNd32 ,&longValue) != 1) { SDDS_SetError("Invalid long parameter"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_SetParameters(&SDDS_dataset,SDDS_SET_BY_INDEX|SDDS_PASS_BY_VALUE,par,longValue,-1)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); break; case SDDS_FLOAT: ConvertDNotationToENotation(ptr); if (sscanf(ptr,"%f",&floatValue) != 1) { SDDS_SetError("Invalid float parameter"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_SetParameters(&SDDS_dataset,SDDS_SET_BY_INDEX|SDDS_PASS_BY_VALUE,par,floatValue,-1)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); break; case SDDS_DOUBLE: ConvertDNotationToENotation(ptr); if (sscanf(ptr,"%lf",&doubleValue) != 1) { SDDS_SetError("Invalid double parameter"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_SetParameters(&SDDS_dataset,SDDS_SET_BY_INDEX|SDDS_PASS_BY_VALUE,par,doubleValue,-1)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); break; case SDDS_STRING: SDDS_GetToken(ptr,stringValue,SDDS_MAXLINE); SDDS_InterpretEscapes(stringValue); if (!SDDS_SetParameters(&SDDS_dataset,SDDS_SET_BY_INDEX|SDDS_PASS_BY_VALUE,par,stringValue,-1)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); break; case SDDS_CHARACTER: SDDS_InterpretEscapes(ptr); characterValue = ptr[0]; if (!SDDS_SetParameters(&SDDS_dataset,SDDS_SET_BY_INDEX|SDDS_PASS_BY_VALUE,par,characterValue,-1)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); break; } par++; } else if ((rows == -1) && (!noRowCount)) { if (sscanf(ptr,"%ld",&rows) != 1) { SDDS_SetError("Invalid row count"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } else if ((columns > 0) && ((row < rows) || (noRowCount))) { if (columnOrder) { if (noRowCount) { cp_str(&ptr2,ptr); rows = 0; while (getToken(ptr2,data,10240,separator,whitespace) >= 0) { rows++; } free(ptr2); } if (rows > columnValues[col].elements) { if (columnValues[col].type == SDDS_STRING) { columnValues[col].stringValues = AllocateColumnStringData(columnValues[col].stringValues, rows, columnValues[col].elements); } else { columnValues[col].values = AllocateColumnData(columnValues[col].type, columnValues[col].values, rows); } columnValues[col].elements = rows; } switch (columnValues[col].type) { case SDDS_SHORT: for (row=0;row<rows;row++) { if (getToken(ptr,data,10240,separator,whitespace) < 0) { SDDS_SetError("Invalid short column element"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (sscanf(data,"%hd",((short*)(columnValues[col].values)+row)) != 1) { SDDS_SetError("Invalid short column element"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } break; case SDDS_LONG: for (row=0;row<rows;row++) { if (getToken(ptr,data,10240,separator,whitespace) < 0) { SDDS_SetError("Invalid long column element"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (sscanf(data,"%" SCNd32,((int32_t*)(columnValues[col].values)+row)) != 1) { SDDS_SetError("Invalid long column element"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } break; case SDDS_FLOAT: for (row=0;row<rows;row++) { if (getToken(ptr,data,10240,separator,whitespace) < 0) { SDDS_SetError("Invalid float column element"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } ConvertDNotationToENotation(data); if (sscanf(data,"%f",((float*)(columnValues[col].values)+row)) != 1) { SDDS_SetError("Invalid float column element"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } break; case SDDS_DOUBLE: for (row=0;row<rows;row++) { if (getToken(ptr,data,10240,separator,whitespace) < 0) { SDDS_SetError("Invalid double column element"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } ConvertDNotationToENotation(data); if (sscanf(data,"%lf",((double*)(columnValues[col].values)+row)) != 1) { SDDS_SetError("Invalid double column element"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } break; case SDDS_STRING: for (row=0;row<rows;row++) { if (getToken(ptr,data,10240,separator,whitespace) < 0) { SDDS_SetError("Invalid string column element"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } SDDS_InterpretEscapes(data); columnValues[col].stringValues[row] = malloc(strlen(data) + 1); strcpy(columnValues[col].stringValues[row],data); } break; case SDDS_CHARACTER: for (row=0;row<rows;row++) { if (getToken(ptr,data,10240,separator,whitespace) < 0) { SDDS_SetError("Invalid character column element"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } SDDS_InterpretEscapes(data); *((char*)(columnValues[col].values)+row) = data[0]; } break; } if (rows > maxRows) { if (!SDDS_LengthenTable(&SDDS_dataset, rows - maxRows)) { SDDS_SetError("Unable to lengthen table"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } maxRows = rows; } if (columnValues[col].skip == 0) { if (columnValues[col].type == SDDS_STRING) { SetColumnData(columnValues[col].type, &SDDS_dataset, columnValues[col].stringValues, rows, col); } else { SetColumnData(columnValues[col].type, &SDDS_dataset, columnValues[col].values, rows, col); } n++; } col++; row = 0; } else { if (noRowCount) { if (row == 0) { rows = 3; } else if (row == rows - 1) { rows = rows + 3; for (i=0;i<columns;i++) { if (rows > columnValues[i].elements) { if (columnValues[i].type == SDDS_STRING) { columnValues[i].stringValues = AllocateColumnStringData(columnValues[i].stringValues, rows, columnValues[i].elements); } else { columnValues[i].values = AllocateColumnData(columnValues[i].type, columnValues[i].values, rows); } } columnValues[i].elements = rows; } } } if (row == 0) for (i=0;i<columns;i++) { if (rows > columnValues[i].elements) { if (columnValues[i].type == SDDS_STRING) { columnValues[i].stringValues = AllocateColumnStringData(columnValues[i].stringValues, rows, columnValues[i].elements); } else { columnValues[i].values = AllocateColumnData(columnValues[i].type, columnValues[i].values, rows); } } columnValues[i].elements = rows; } if (noRowCount) { cp_str(&ptr2,ptr); i = 0; while (getToken(ptr2,data,10240,separator,whitespace) >= 0) i++; free(ptr2); if ((i != columns) && (parameters>0 && i==1)) { if (row > 0) { if (row > maxRows) { if (!SDDS_LengthenTable(&SDDS_dataset, row - maxRows)) { SDDS_SetError("Unable to lengthen table"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } maxRows = row; } n=0; for (j=0;j<columns;j++) { if (columnValues[j].skip) continue; if (columnValues[j].type == SDDS_STRING) { SetColumnData(columnValues[j].type, &SDDS_dataset, columnValues[j].stringValues, row, n); } else { SetColumnData(columnValues[j].type, &SDDS_dataset, columnValues[j].values, row, n); } n++; } if (!SDDS_WritePage(&SDDS_dataset)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); maxRows = 10000; if (!SDDS_StartPage(&SDDS_dataset, initRows)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); row = par = col = 0; rows = -1; } readline = 0; continue; } } for (i=0;i<columns;i++) { if (getToken(ptr,data,10240,separator,whitespace) < 0) { if (!fillin) { SDDS_SetError("Invalid column element"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } else { switch (columnValues[i].type) { case SDDS_SHORT: case SDDS_LONG: case SDDS_FLOAT: case SDDS_DOUBLE: data[0]='0'; data[1]='\0'; break; case SDDS_STRING: case SDDS_CHARACTER: data[0]='\0'; break; } } } switch (columnValues[i].type) { case SDDS_SHORT: if (sscanf(data,"%hd",((short*)(columnValues[i].values)+row)) != 1) { if (recover) { abort=1; row--; } else { SDDS_SetError("Invalid short column element"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } break; case SDDS_LONG: if (sscanf(data,"%" SCNd32,((int32_t*)(columnValues[i].values)+row)) != 1) { if (recover) { abort=1; row--; } else { SDDS_SetError("Invalid long column element"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } break; case SDDS_FLOAT: ConvertDNotationToENotation(data); if (sscanf(data,"%f",((float*)(columnValues[i].values)+row)) != 1) { if (recover) { abort=1; row--; } else { SDDS_SetError("Invalid float column element"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } break; case SDDS_DOUBLE: ConvertDNotationToENotation(data); if (sscanf(data,"%lf",((double*)(columnValues[i].values)+row)) != 1) { if (recover) { abort=1; row--; } else { SDDS_SetError("Invalid double column element"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } break; case SDDS_STRING: SDDS_InterpretEscapes(data); columnValues[i].stringValues[row] = malloc(strlen(data) + 1); strcpy(columnValues[i].stringValues[row],data); break; case SDDS_CHARACTER: SDDS_InterpretEscapes(data); *((char*)(columnValues[i].values)+row) = data[0]; break; } if (recover && abort) { break; } } row++; if ((row == rows) && (!noRowCount)) { if (rows > maxRows) { if (!SDDS_LengthenTable(&SDDS_dataset, rows - maxRows)) { SDDS_SetError("Unable to lengthen table"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } maxRows = rows; } n=0; for (i=0;i<columns;i++) { if (columnValues[i].skip) continue; if (columnValues[i].type == SDDS_STRING) { SetColumnData(columnValues[i].type, &SDDS_dataset, columnValues[i].stringValues, rows, n); } else { SetColumnData(columnValues[i].type, &SDDS_dataset, columnValues[i].values, rows, n); } n++; } } } } if ((par == parameters) && (((!noRowCount) && (rows != -1)) || (noRowCount)) && (((columnOrder) && (col == columns)) || ((columns > 0) && (row == rows)) || (columns == 0))) { if (!SDDS_WritePage(&SDDS_dataset)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } maxRows = 10000; if (!SDDS_StartPage(&SDDS_dataset, initRows)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); row = par = col = 0; rows = -1; } ptr[0] = 0; } if (noRowCount) { if (row > 0) { if (row > maxRows) { if (!SDDS_LengthenTable(&SDDS_dataset, row - maxRows)) { SDDS_SetError("Unable to lengthen table"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } maxRows = row; } n=0; for (j=0;j<columns;j++) { if (columnValues[j].skip) continue; if (columnValues[j].type == SDDS_STRING) { SetColumnData(columnValues[j].type, &SDDS_dataset, columnValues[j].stringValues, row, n); } else { SetColumnData(columnValues[j].type, &SDDS_dataset, columnValues[j].values, row, n); } n++; } if (!SDDS_WritePage(&SDDS_dataset)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } maxRows = 10000; } } for (i=0;i<columns;i++) { if (columnValues[i].type == SDDS_STRING) { for (j=0;j<columnValues[i].elements;j++) { free(columnValues[i].stringValues[j]); } free(columnValues[i].stringValues); } else { free(columnValues[i].values); } if (columnValues[i].name) free(columnValues[i].name); if (columnValues[i].units) free(columnValues[i].units); if (columnValues[i].description) free(columnValues[i].description); if (columnValues[i].symbol) free(columnValues[i].symbol); } for (i=0;i<parameters;i++) { free(parameterValues[i].name); if (parameterValues[i].units) free(parameterValues[i].units); if (parameterValues[i].description) free(parameterValues[i].description); if (parameterValues[i].symbol) free(parameterValues[i].symbol); } if (columnValues) free(columnValues); if (parameterValues) free(parameterValues); if (columnIndex) free(columnIndex); if (parameterIndex) free(parameterIndex); if (ptr) free(ptr); if (!SDDS_Terminate(&SDDS_dataset)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); free_scanargs(&s_arg,argc); return 0; }
int main(int argc, char **argv) { SDDS_TABLE SDDS_table; SCANNED_ARG *scanned; long i, i_arg, points; char *input, *output, *name, *indep_var, *var_list_format; char *mpl_title, *mpl_topline, *descrip_text, *descrip_contents; FILE *fpi; char buffer[BUFSIZE], buffer0[BUFSIZE], buffer1[BUFSIZE]; char *ptr, *ptr1, *ptr2; char **data_name, *package_name, *data_format; long data_names, data_sets_seen, data_sets_expected, realIndex, imagIndex; double **real_data, **imag_data, *var_list; argc = scanargs(&scanned, argc, argv); if (argc<3) bomb(NULL, USAGE); input = output = package_name = data_format = indep_var = var_list_format = ptr = NULL; mpl_title = mpl_topline = descrip_text = descrip_contents = NULL; data_names = realIndex = imagIndex = 0; data_name = NULL; real_data = imag_data = NULL; var_list = NULL; data_sets_expected = data_sets_seen = 0; points = 0; for (i_arg=1; i_arg<argc; i_arg++) { if (scanned[i_arg].arg_type==OPTION) { delete_chars(scanned[i_arg].list[0], "_"); /* process options here */ switch (match_string(scanned[i_arg].list[0], option, N_OPTIONS, 0)) { case SET_DESCRIPTION: if (scanned[i_arg].n_items!=3) SDDS_Bomb("invalid -description syntax"); descrip_text = scanned[i_arg].list[1]; descrip_contents = scanned[i_arg].list[2]; break; case SET_MPL_LABELS: if (scanned[i_arg].n_items!=3) SDDS_Bomb("invalid -mpllabels syntax"); mpl_title = scanned[i_arg].list[1]; mpl_topline = scanned[i_arg].list[2]; break; default: SDDS_Bomb("invalid option seen"); break; } } else { if (!input) input = scanned[i_arg].list[0]; else if (!output) output = scanned[i_arg].list[0]; else SDDS_Bomb("too many filenames"); } } if (!input) SDDS_Bomb("input file not seen"); if (!output) SDDS_Bomb("output file not seen"); fpi = fopen_e(input, "r", 0); if (!fgets(buffer, BUFSIZE, fpi) || strncmp(buffer, CITIFILE_TAG, strlen(CITIFILE_TAG))!=0 || !(ptr=strchr(buffer, ' '))) SDDS_Bomb("valid CITIFILE version line not found"); *ptr++ = 0; ptr[strlen(ptr)-1] = 0; if (strncmp(ptr, CITIFILE_VERSION, strlen(CITIFILE_VERSION))!=0) fprintf(stderr, "warning: the CITIFILE version is %s--this program is only designed for version %s\n", ptr, CITIFILE_VERSION); if (!SDDS_InitializeOutput(&SDDS_table, SDDS_BINARY, 0, descrip_text, descrip_contents, output)) SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); if ((mpl_title && SDDS_DefineParameter(&SDDS_table, "mplTitle", NULL, NULL, NULL, NULL, SDDS_STRING, mpl_title)<0) || (mpl_topline && SDDS_DefineParameter(&SDDS_table, "mplTopline", NULL, NULL, NULL, NULL, SDDS_STRING, mpl_topline)<0)) SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); while (fgets(buffer, BUFSIZE, fpi)) { #if DEBUG fputs(buffer, stderr); #endif buffer[strlen(buffer)-1] = 0; strcpy(buffer0, buffer); ptr1 = NULL; if ((ptr1=strchr(buffer, ' '))) *ptr1++ = 0; switch (match_string(buffer, citi_keyword, N_CITI_KEYWORDS, EXACT_MATCH)) { case CITI_NA_KEYWORD: name = buffer+1; if (!*ptr1 || !(ptr2=strchr(ptr1, ' '))) { fprintf(stderr, "The following line contains an apparently invalid #NA keyword:\n%s\n", buffer0); exit(1); } *(ptr1-1) = '_'; *ptr2++ = 0; if (SDDS_DefineParameter(&SDDS_table, name, NULL, NULL, NULL, NULL, SDDS_STRING, ptr2)<0) { SDDS_SetError(buffer0); SDDS_SetError("Problem creating parameter for #NA keyword in the following line:"); SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); } break; case CITI_NAME_KEYWORD: if (!ptr1 || SDDS_StringIsBlank(ptr1)) { fprintf(stderr, "The following line contains erroneous input:\n%s\n", buffer0); exit(1); } #if DEBUG fprintf(stderr, "NAME: %s\n", ptr1); #endif cp_str(&package_name, ptr1); if (SDDS_DefineParameter(&SDDS_table, "CITIPackageName", NULL, NULL, NULL, NULL, SDDS_STRING, package_name)<0) { SDDS_SetError(buffer0); SDDS_SetError("Problem creating parameter for NAME keyword in the following line:"); SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); } break; case CITI_VAR_KEYWORD: if (!ptr1 || SDDS_StringIsBlank(ptr1)) { fprintf(stderr, "The following line contains erroneous input:\n%s\n", buffer0); exit(1); } #if DEBUG fprintf(stderr, "VAR: %s\n", ptr1); #endif if (!(indep_var=get_token(ptr1)) || !(var_list_format=get_token(ptr1)) || !get_long(&points, ptr1) || points<=0) { fprintf(stderr, "The following line contains erroneous input:\n%s\n", buffer0); exit(1); } if (SDDS_DefineColumn(&SDDS_table, indep_var, NULL, NULL, NULL, NULL, SDDS_DOUBLE, 0)<0) { SDDS_SetError(buffer0); SDDS_SetError("Problem creating column for data element in the following line:"); SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); } break; case CITI_CONSTANT_KEYWORD: if (!ptr1 || !(ptr2=strchr(ptr1, ' '))) { fprintf(stderr, "The following line contains erroneous input:\n%s\n", buffer0); exit(1); } #if DEBUG fprintf(stderr, "CONSTANT: %s\n", ptr1); #endif *ptr2++ = 0; if (SDDS_DefineParameter(&SDDS_table, ptr1, NULL, NULL, NULL, NULL, SDDS_STRING, ptr2)<0) { SDDS_SetError(buffer0); SDDS_SetError("Problem creating parameter for CONSTANT keyword in the following line:"); SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); } break; case CITI_COMMENT_KEYWORD: if (!ptr1 || SDDS_StringIsBlank(ptr1)) { fprintf(stderr, "The following line contains erroneous input:\n%s\n", buffer0); exit(1); } #if DEBUG fprintf(stderr, "COMMENT: %s\n", ptr1); #endif break; case CITI_DATA_KEYWORD: if (!ptr1 || !(ptr2 = strchr(ptr1, ' '))) { fprintf(stderr, "The following line contains erroneous input:\n%s\n", buffer0); exit(1); } #if DEBUG fprintf(stderr, "DATA: %s\n", ptr1); #endif *ptr2++ = 0; cp_str(&data_format, ptr2); data_name = trealloc(data_name, sizeof(*data_name)*(data_names+1)); cp_str(data_name+data_names, ptr1); alter_data_name(data_name[data_names]); real_data = trealloc(real_data, sizeof(*real_data)*(data_names+1)); imag_data = trealloc(imag_data, sizeof(*imag_data)*(data_names+1)); sprintf(buffer, "%sReal", data_name[data_names]); sprintf(buffer1, "%sImag", data_name[data_names]); if ((realIndex=SDDS_DefineColumn(&SDDS_table, buffer, NULL, NULL, NULL, NULL, SDDS_DOUBLE, 0))<0 || (imagIndex=SDDS_DefineColumn(&SDDS_table, buffer1, NULL, NULL, NULL, NULL, SDDS_DOUBLE, 0))<0 ) { SDDS_SetError(buffer0); SDDS_SetError("Problem creating column for data element in the following line:"); SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); } data_names++; break; case CITI_VAR_LIST_KEYWORD: if (ptr1 && !SDDS_StringIsBlank(ptr1)) fprintf(stderr, "The following line contains erroneous input:\n%s\n", buffer0); #if DEBUG fprintf(stderr, "VAR_LIST_BEGIN seen\n"); #endif if (points==0) SDDS_Bomb("VAR_LIST_BEGIN statement seen without prior VAR statement"); var_list = tmalloc(sizeof(*var_list)*points); if (!read_CITI_var_list(fpi, var_list, points)) SDDS_Bomb("unable to read VAR_LIST"); break; case CITI_BEGIN_KEYWORD: if (ptr1 && !SDDS_StringIsBlank(ptr1)) fprintf(stderr, "The following line contains erroneous input:\n%s\n", buffer0); #if DEBUG fprintf(stderr, "BEGIN seen\n"); #endif if (points==0) SDDS_Bomb("BEGIN statement seen without prior VAR statement"); real_data[data_sets_seen] = tmalloc(sizeof(**real_data)*points); imag_data[data_sets_seen] = tmalloc(sizeof(**imag_data)*points); if (!read_CITI_data(fpi, real_data[data_sets_seen], imag_data[data_sets_seen], points)) SDDS_Bomb("problem reading data section"); data_sets_seen++; break; case CITI_SEG_LIST_BEGIN: if (ptr1 && !SDDS_StringIsBlank(ptr1)) fprintf(stderr, "The following line contains erroneous input:\n%s\n", buffer0); #if DEBUG fprintf(stderr, "SEG_LIST_BEGIN seen\n"); #endif if (points==0) SDDS_Bomb("SEG_LIST_BEGIN statement seen without prior SEG statement"); var_list = tmalloc(sizeof(*var_list)*points); if (!read_CITI_seg_list(fpi, var_list, points)) SDDS_Bomb("unable to read SEG_LIST"); break; default: fprintf(stderr, "unidentifiable line in file--not CITI format:\n\"%s\"\n", buffer0); exit(1); break; } } if (!points) SDDS_Bomb("no data in file"); if (data_sets_seen!=(data_sets_expected=data_names)) SDDS_Bomb("fewer data sets than expected were actually present"); if (!var_list) { fprintf(stderr, "warning: no independent variable data---supplying index\n"); var_list = tmalloc(sizeof(*var_list)*points); for (i=0; i<points; i++) var_list[i] = i; } if (!SDDS_WriteLayout(&SDDS_table) || !SDDS_StartTable(&SDDS_table, points)) SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); if (!SDDS_SetColumn(&SDDS_table, SDDS_SET_BY_NAME, (void*)var_list, points, indep_var)) SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); for (i=0; i<data_sets_expected; i++) { if (!SDDS_SetColumn(&SDDS_table, SDDS_SET_BY_INDEX, (void*)real_data[i], points, realIndex) || !SDDS_SetColumn(&SDDS_table, SDDS_SET_BY_INDEX, (void*)imag_data[i], points, imagIndex)) { fprintf(stderr, "problem setting data for column(s) %s\n", data_name[i]); SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); } } if (!SDDS_WriteTable(&SDDS_table) || !SDDS_Terminate(&SDDS_table)) SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); return(0); }
char *makeNameUnitsLabel(PLOT_SPEC *plspec, long panel, long plane, long group) { long set, datasets; PLOT_DATA **dataset; char **symbolUsed, buffer[LABEL_BUFLEN], *ptr; long symbolsUsed, first, maxLabel, unitsIndex, request, unitsDataSet, addUnits; static long warned[2] = { 0,0 } ; long labelLimit = LABEL_BUFLEN-10; dataset = plspec->panel[panel].dataset; datasets = plspec->panel[panel].datasets; symbolsUsed = maxLabel = 0; symbolUsed = NULL; first = addUnits = 1; buffer[0] = 0; unitsDataSet = -1; for (set=0; set<datasets; set++) { if (dataset[set]->scalesGroupIndex[plane]!=group) continue; if (unitsDataSet<0) unitsDataSet = set; request = dataset[set]->request_index; if (plspec->plot_request[request].overlay.flags) continue; if (!first && ((!dataset[set]->info[plane].units && dataset[set-1]->info[plane].units) || (dataset[set]->info[plane].units && !dataset[set-1]->info[plane].units) || (dataset[set]->info[plane].units && dataset[set-1]->info[plane].units && strcmp(dataset[set]->info[plane].units, dataset[set-1]->info[plane].units)!=0))) { if (!warned[plane]) { fprintf(stderr, "Warning: not all %lc quantities have the same units\n", 'x'+plane); warned[plane] = 1; } addUnits = 0; } if (!dataset[set]->info[plane].symbol) { fprintf(stderr, "internal error: info[%ld] symbol is NULL for dataset %ld (panel %ld)\n", plane, set, panel); exit(1); } unitsIndex = set; if (!maxLabel && (first || !symbolsUsed || match_string(dataset[set]->info[plane].symbol, symbolUsed, symbolsUsed, EXACT_MATCH)<0)) { if (!(symbolUsed= SDDS_Realloc(symbolUsed, sizeof(*symbolUsed)*(symbolsUsed+1)))) SDDS_Bomb("Memory allocation failure (makeNameUnitsLabel)"); symbolUsed[symbolsUsed] = dataset[set]->info[plane].symbol; if ((long)(strlen(buffer)+strlen(symbolUsed[symbolsUsed]))<labelLimit) { if (!first) strcat(buffer, ", "); strcat(buffer, symbolUsed[symbolsUsed]); } else { if (first) strncpy(buffer, symbolUsed[symbolsUsed], labelLimit); buffer[labelLimit] = 0; maxLabel = 1; } symbolsUsed++; } first = 0; } if (symbolUsed) free(symbolUsed); if (maxLabel) strcat(buffer, "..."); if (addUnits && unitsDataSet>=0 && dataset[unitsDataSet]->info[plane].units && strlen(dataset[unitsDataSet]->info[plane].units)) { strcat(buffer, " ("); strcat(buffer, dataset[unitsDataSet]->info[plane].units); strcat(buffer, ")"); } if (strlen(buffer)) { if (!SDDS_CopyString(&ptr, buffer)) SDDS_Bomb("String copying problem (makeNameUnitsLabel)"); return ptr; } return NULL; }
int main(int argc, char **argv) { SCANNED_ARG *scanned; SDDS_TABLE inputPage, *copiedPage, outputPage; long copiedPages; char *inputfile, *outputfile; char **column, **excludeColumn=NULL; int32_t columns; long excludeColumns; char *indParameterName; char **copyColumn; int32_t copyColumns; long verbose; long slopeErrors; long iArg,i; double *indVar; char *indVarUnits; char **intColumn,**slopeColumn,**slopeSigmaColumn; char *Units,*slopeUnits; double *depVar; long order; double *coef,*coefsigma,*weight,*diff,chi; long iCol,iRow,iPage; long rows; double *slope, slope2, slopeAve, slopeSigma; unsigned long pipeFlags, majorOrderFlag; long tmpfile_used, noWarnings; long generateIndex; short columnMajorOrder=-1; copiedPage = NULL; slopeSigmaColumn = NULL; slopeUnits = Units = indVarUnits = NULL; rows = 0; slope = NULL; slope2 = 0; coef = coefsigma = weight = diff = slope = NULL; argc = scanargs(&scanned, argc, argv); if (argc == 1) bomb(NULL, USAGE); inputfile = outputfile = NULL; columns = excludeColumns = 0; column = excludeColumn = NULL; indParameterName = NULL; verbose = 0; slopeErrors = 0; pipeFlags = 0; tmpfile_used=0; noWarnings=0; for (iArg = 1; iArg<argc; iArg++) { if (scanned[iArg].arg_type == OPTION) { delete_chars(scanned[iArg].list[0], "_"); switch (match_string(scanned[iArg].list[0], commandline_option, COMMANDLINE_OPTIONS, UNIQUE_MATCH)) { case CLO_MAJOR_ORDER: majorOrderFlag=0; scanned[iArg].n_items--; if (scanned[iArg].n_items>0 && (!scanItemList(&majorOrderFlag, scanned[iArg].list+1, &scanned[iArg].n_items, 0, "row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER, "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER, NULL))) SDDS_Bomb("invalid -majorOrder syntax/values"); if (majorOrderFlag&SDDS_COLUMN_MAJOR_ORDER) columnMajorOrder=1; else if (majorOrderFlag&SDDS_ROW_MAJOR_ORDER) columnMajorOrder=0; break; case CLO_INDEPENDENT_PARAMETER: if (!(indParameterName = scanned[iArg].list[1])) SDDS_Bomb("no string given for option -independentVariable"); break; case CLO_COLUMNS: if (columns) SDDS_Bomb("only one -columns option may be given"); if (scanned[iArg].n_items<2) SDDS_Bomb("invalid -columns syntax"); column = tmalloc(sizeof(*column)*(columns = scanned[iArg].n_items-1)); for (i = 0; i<columns; i++) column[i] = scanned[iArg].list[i+1]; break; case CLO_EXCLUDE: if (excludeColumns) SDDS_Bomb("only one -excludecolumns option may be given"); if (scanned[iArg].n_items<2) SDDS_Bomb("invalid -excludecolumns syntax"); excludeColumn = tmalloc(sizeof(*excludeColumn)*(excludeColumns = scanned[iArg].n_items-1)); for (i = 0; i<excludeColumns; i++) excludeColumn[i] = scanned[iArg].list[i+1]; break; case CLO_VERBOSE: verbose = 1; break; case CLO_PIPE: if (!processPipeOption(scanned[iArg].list+1, scanned[iArg].n_items-1, &pipeFlags)) SDDS_Bomb("invalid -pipe syntax"); break; case CLO_SLOPE_ERRORS: slopeErrors = 1; break; default: SDDS_Bomb("unrecognized option given"); break; } } else { if (!inputfile) inputfile = scanned[iArg].list[0]; else if (!outputfile) outputfile = scanned[iArg].list[0]; else SDDS_Bomb("too many filenames given"); } } processFilenames("sddsvslopes", &inputfile, &outputfile, pipeFlags, noWarnings, &tmpfile_used); if (!indParameterName) SDDS_Bomb("independentVariable not given"); if (!excludeColumns) { excludeColumn = defaultExcludedColumn; excludeColumns = DEFAULT_EXCLUDED_COLUMNS; } if (verbose) fprintf(stderr,"Reading file %s.\n",inputfile); SDDS_InitializeInput(&inputPage, inputfile); copiedPages = 0; while (SDDS_ReadTable(&inputPage)>0) { if (!copiedPages) { copiedPage = (SDDS_TABLE*)malloc(sizeof(SDDS_TABLE)); rows = SDDS_CountRowsOfInterest(&inputPage); } else { copiedPage = (SDDS_TABLE*)realloc(copiedPage, (copiedPages+1)*sizeof(SDDS_TABLE)); } if (!SDDS_InitializeCopy(&copiedPage[copiedPages], &inputPage, NULL, "m")) SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); if (!SDDS_CopyTable(&copiedPage[copiedPages], &inputPage)) SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); copiedPages++; } if (copiedPages<2) { fprintf(stderr,"Insufficient data (i.e. number of data pages) to fit a straight line.\n"); exit(1); } switch(SDDS_CheckColumn(&inputPage, "Rootname", NULL, SDDS_STRING, NULL)) { case SDDS_CHECK_WRONGUNITS: case SDDS_CHECK_OKAY: break; default: fprintf(stderr,"Something wrong with column %s.\n", "Rootname"); exit(1); } switch(SDDS_CheckColumn(&inputPage, "Index", NULL, SDDS_LONG, NULL)) { case SDDS_CHECK_WRONGUNITS: case SDDS_CHECK_OKAY: generateIndex = 0; break; case SDDS_CHECK_NONEXISTENT: generateIndex = 1; break; default: fprintf(stderr,"Something wrong with column %s.\n", "Rootname"); exit(1); } /****************\ * make array of independent variable \**************/ indVar = (double*)malloc(sizeof(*indVar)*copiedPages); switch(SDDS_CheckParameter(&inputPage, indParameterName, NULL, SDDS_DOUBLE, NULL)) { case SDDS_CHECK_WRONGUNITS: case SDDS_CHECK_OKAY: break; default: fprintf(stderr,"Something wrong with parameter %s.\n", indParameterName); exit(1); } for (iPage = 0; iPage<copiedPages; iPage++) { if (!SDDS_GetParameter(&copiedPage[iPage],indParameterName,&indVar[iPage])) SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); } if (!SDDS_GetParameterInformation(&inputPage, "units", &indVarUnits, SDDS_GET_BY_NAME, indParameterName)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!indVarUnits) { indVarUnits = (char *) malloc(sizeof(*indVarUnits)); indVarUnits[0] = 0; } /************************************\ * get columns of interest. use set_multicolumn_flags to simply * return new values for array column. \*************************************/ if (!set_multicolumn_flags(&inputPage, &column, &columns, excludeColumn, excludeColumns)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } /************************************\ * make column names for the output \*************************************/ intColumn = (char**)malloc((sizeof(char*)*columns)); slopeColumn = (char**)malloc((sizeof(char*)*columns)); if (slopeErrors) slopeSigmaColumn = (char**)malloc((sizeof(char*)*columns)); for (i=0; i<columns; i++) { intColumn[i] = (char*)malloc((sizeof(char)*(strlen(column[i])+strlen("Intercept")+1))); strcat(strcpy(intColumn[i], column[i]), "Intercept"); slopeColumn[i] = (char*)malloc((sizeof(char)*(strlen(column[i])+strlen("Slope")+1))); strcat(strcpy(slopeColumn[i], column[i]), "Slope"); if (slopeErrors) { slopeSigmaColumn[i] = (char*)malloc((sizeof(char)*(strlen(column[i])+strlen("SlopeSigma")+1))); strcat(strcpy(slopeSigmaColumn[i], column[i]), "SlopeSigma"); } } /************************************\ * Write layout for output file \*************************************/ if (verbose) fprintf(stderr,"Opening file %s.\n",outputfile); if(!SDDS_InitializeOutput(&outputPage,SDDS_BINARY,1, "lsf of sddsvexperiment",NULL,outputfile) || 0>SDDS_DefineParameter(&outputPage, "InputFile", "InputFile", NULL, "InputFile", NULL, SDDS_STRING, 0) || 0>SDDS_DefineParameter(&outputPage, "IndependentVariable", "IndependentVariable", NULL, "IndependentVariable", NULL, SDDS_STRING, 0) || (0>SDDS_DefineColumn(&outputPage,"Index",NULL,NULL,"Rootname index",NULL,SDDS_LONG,0))|| (0>SDDS_DefineColumn(&outputPage,"Rootname",NULL,NULL,NULL,NULL,SDDS_STRING,0))) SDDS_PrintErrors(stderr,SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (columnMajorOrder!=-1) outputPage.layout.data_mode.column_major = columnMajorOrder; else outputPage.layout.data_mode.column_major = inputPage.layout.data_mode.column_major; for (iCol=0; iCol<columns; iCol++) { if (!SDDS_GetColumnInformation(&inputPage, "units", &Units, SDDS_GET_BY_NAME,column[iCol])) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!Units) { Units = (char*) malloc(sizeof(*Units)); Units[0] = 0; } if (0>SDDS_DefineColumn(&outputPage, intColumn[iCol], NULL, Units, NULL, NULL, SDDS_DOUBLE,0)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); /* units for slopes columns */ if (strlen(indVarUnits) && strlen(Units) ) { slopeUnits = (char*)malloc(sizeof(*slopeUnits)*(strlen(Units)+strlen(indVarUnits)+2)); strcat( strcat( strcpy(slopeUnits, Units), "/"), indVarUnits); } if (strlen(indVarUnits) && !strlen(Units) ) { slopeUnits = (char*)malloc(sizeof(*slopeUnits)*(strlen(indVarUnits)+2)); strcat( strcpy( slopeUnits, "1/"), indVarUnits); } if (!strlen(indVarUnits) && strlen(Units) ) { slopeUnits = (char*)malloc(sizeof(*slopeUnits)*(strlen(Units)+2)); strcpy( slopeUnits, indVarUnits); } if (!strlen(indVarUnits) && !strlen(Units) ) { slopeUnits = (char*)malloc(sizeof(*slopeUnits)); strcpy( slopeUnits, ""); } if (0>SDDS_DefineColumn(&outputPage, slopeColumn[iCol], NULL, slopeUnits, NULL, NULL, SDDS_DOUBLE,0)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (slopeErrors) { if (0>SDDS_DefineColumn(&outputPage, slopeSigmaColumn[iCol], NULL, slopeUnits, NULL, NULL, SDDS_DOUBLE,0)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } free(slopeUnits); } if(!SDDS_WriteLayout(&outputPage) || !SDDS_StartTable(&outputPage,rows) || !SDDS_SetParameters(&outputPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "InputFile",inputfile?inputfile:"pipe",NULL) || !SDDS_SetParameters(&outputPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, 0, "IndependentVariable", indParameterName, NULL) ) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); /************************************\ * Copy columns to output file (usually columns Index and Rootname) \*************************************/ copyColumns = DEFAULT_COPY_COLUMNS; copyColumn = defaultCopyColumn; if (!set_multicolumn_flags(&inputPage, ©Column, ©Columns, NULL, 0)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); exit(1); } if(!SDDS_CopyColumns(&outputPage, &inputPage)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); depVar = (double*)malloc(sizeof(*depVar)*copiedPages); weight = (double*)malloc(sizeof(*weight)*copiedPages); diff = (double*)malloc(sizeof(*weight)*copiedPages); order=1; coef = (double*)malloc(sizeof(*coef)*(order+1)); coefsigma = (double*)malloc(sizeof(*coefsigma)*(order+1)); if(slopeErrors) slope = (double*)malloc(sizeof(*slope)*copiedPages); for (iCol=0; iCol<columns; iCol++) { for (iPage=0; iPage<copiedPages; iPage++) weight[iPage]=1; if (verbose) fprintf(stderr,"Doing column %s.\n", column[iCol]); for (iRow=0; iRow<rows; iRow++) { for (iPage=0; iPage<copiedPages; iPage++) { if (!SDDS_GetValue(&copiedPage[iPage], column[iCol], iRow, &depVar[iPage])) SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); } if (!(lsfn(indVar, depVar, weight, copiedPages, order, coef, coefsigma, &chi, diff))){ fprintf(stderr,"Problem with call to lsfn\n."); exit(1); } if (generateIndex) { if (!SDDS_SetRowValues(&outputPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, iRow, "Index",iRow, intColumn[iCol], coef[0], slopeColumn[iCol], coef[1], NULL)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } else { if (!SDDS_SetRowValues(&outputPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, iRow, intColumn[iCol], coef[0], slopeColumn[iCol], coef[1], NULL)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if(slopeErrors){ /* recalculate the slope with a subset of points */ slopeAve= slope2 = 0; for (iPage=0; iPage<copiedPages; iPage++) { weight[iPage] = 1e10; if(iPage) weight[iPage-1] = 1; if (!(lsfn(indVar, depVar, weight, copiedPages, order, coef, coefsigma, &chi, diff))){ fprintf(stderr,"Problem with call to lsfn\n."); exit(1); } slope[iPage] = coef[1]; slopeAve += slope[iPage]; slope2 += sqr(slope[iPage]); } slopeSigma = sqrt(slope2/copiedPages - sqr(slopeAve/copiedPages)); if (!SDDS_SetRowValues(&outputPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, iRow, slopeSigmaColumn[iCol], slopeSigma, NULL)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } } if( !SDDS_WriteTable(&outputPage)|| SDDS_Terminate(&inputPage) ) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (iPage=0; iPage<copiedPages; iPage++) { if( !SDDS_Terminate(&copiedPage[iPage]) ) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if( SDDS_Terminate(&outputPage) ) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (tmpfile_used && !replaceFileAndBackUp(inputfile, outputfile)) exit(1); SDDS_FreeStringArray(column, columns); free(column); SDDS_FreeStringArray(intColumn, columns); SDDS_FreeStringArray(slopeColumn, columns); free(intColumn); free(slopeColumn); if (slopeErrors) { SDDS_FreeStringArray(slopeSigmaColumn, columns); free(slopeSigmaColumn); } free(copiedPage); free(indVar); free(depVar); if (weight) free(weight); if (diff) free(diff); if (slope) free(slope); if (coef) free(coef); if (coefsigma) free(coefsigma); if (Units) free(Units); if (indVarUnits) free(indVarUnits); free_scanargs(&scanned, argc); return(0); }
int main(int argc, char **argv) { SDDS_DATASET inSet, outSet; SCANNED_ARG *s_arg; char *input, *output, *zeroName, **columnName; long i_arg, i, pageReturned, rows, row, zrow; int32_t columnNames; double **indepData, *depenData, **slopeData, slope, offset; unsigned long pipeFlags, flags, majorOrderFlag; char s[SDDS_MAXLINE]; short columnMajorOrder=-1; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&s_arg, argc, argv); if (argc<2 || argc>(2+N_OPTIONS)) bomb(NULL, USAGE); flags = pipeFlags = 0; input = output = NULL; zeroName = NULL; columnName = NULL; columnNames = 0; offset = 0; for (i_arg=1; i_arg<argc; i_arg++) { if (s_arg[i_arg].arg_type==OPTION) { switch (match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) { case CLO_MAJOR_ORDER: majorOrderFlag=0; s_arg[i_arg].n_items--; if (s_arg[i_arg].n_items>0 && (!scanItemList(&majorOrderFlag, s_arg[i_arg].list+1, &s_arg[i_arg].n_items, 0, "row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER, "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER, NULL))) SDDS_Bomb("invalid -majorOrder syntax/values"); if (majorOrderFlag&SDDS_COLUMN_MAJOR_ORDER) columnMajorOrder=1; else if (majorOrderFlag&SDDS_ROW_MAJOR_ORDER) columnMajorOrder=0; break; case CLO_PIPE: if (!processPipeOption(s_arg[i_arg].list+1, s_arg[i_arg].n_items-1, &pipeFlags)) SDDS_Bomb("invalid -pipe syntax"); break; case CLO_ZEROESOF: if (s_arg[i_arg].n_items!=2) SDDS_Bomb("invalid -zeroesOf syntax"); zeroName = s_arg[i_arg].list[1]; break; case CLO_COLUMNS: if (s_arg[i_arg].n_items<2) SDDS_Bomb("invalid -columns syntax"); columnName = tmalloc(sizeof(*columnName)*(columnNames = s_arg[i_arg].n_items-1)); for (i=0; i<columnNames; i++) columnName[i] = s_arg[i_arg].list[i+1]; break; case CLO_SLOPEOUTPUT: flags |= FL_SLOPEOUTPUT; break; case CLO_OFFSET: if (s_arg[i_arg].n_items!=2 || sscanf(s_arg[i_arg].list[1], "%le", &offset)!=1) SDDS_Bomb("invalid -offset syntax"); break; default: fprintf(stderr, "Error (%s): unknown/ambiguous option: %s\n", argv[0], s_arg[i_arg].list[0]); exit(1); break; } } else { if (input==NULL) input = s_arg[i_arg].list[0]; else if (output==NULL) output = s_arg[i_arg].list[0]; else SDDS_Bomb("too many filenames"); } } processFilenames("sddszerofind", &input, &output, pipeFlags, 0, NULL); if (!zeroName) SDDS_Bomb("-zeroesOf option must be given"); if (!SDDS_InitializeInput(&inSet, input)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!resolveColumnNames(&inSet, zeroName, &columnName, &columnNames) || !SDDS_InitializeOutput(&outSet, SDDS_BINARY, 0, NULL, "sddszerofind output", output) || !SDDS_TransferColumnDefinition(&outSet, &inSet, zeroName, NULL)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (columnMajorOrder!=-1) outSet.layout.data_mode.column_major = columnMajorOrder; else outSet.layout.data_mode.column_major = inSet.layout.data_mode.column_major; for (i=0; i<columnNames; i++) { sprintf(s, "%sSlope", columnName[i]); if (!SDDS_TransferColumnDefinition(&outSet, &inSet, columnName[i], NULL) || (flags&FL_SLOPEOUTPUT && !SDDS_TransferColumnDefinition(&outSet, &inSet, columnName[i], s))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_WriteLayout(&outSet)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); indepData = tmalloc(sizeof(*indepData)*columnNames); slopeData = tmalloc(sizeof(*slopeData)*columnNames); while ((pageReturned=SDDS_ReadPage(&inSet))>0) { if (!SDDS_StartPage(&outSet, 0)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if ((rows=SDDS_CountRowsOfInterest(&inSet))>1) { if (!(depenData = SDDS_GetColumnInDoubles(&inSet, zeroName))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (i=0; i<columnNames; i++) { if (!(indepData[i] = SDDS_GetColumnInDoubles(&inSet, columnName[i]))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (flags&FL_SLOPEOUTPUT) slopeData[i] = tmalloc(sizeof(**slopeData)*rows); } if (offset) for (row=0; row<rows; row++) depenData[row] += offset; for (zrow=row=0; row<rows-1; row++) { if ((depenData[row]<=0 && depenData[row+1]>=0) || (depenData[row]>=0 && depenData[row+1]<=0)) { for (i=0; i<columnNames; i++) { if (indepData[i][row]==indepData[i][row+1]) { if (flags&FL_SLOPEOUTPUT) slopeData[i][zrow] = DBL_MAX; indepData[i][zrow] = indepData[i][row]; } else { slope = (depenData[row+1]-depenData[row])/(indepData[i][row+1]-indepData[i][row]); if (flags&FL_SLOPEOUTPUT) slopeData[i][zrow] = slope; if (slope) indepData[i][zrow] = indepData[i][row] - depenData[row]/slope; else indepData[i][zrow] = (indepData[i][row] + indepData[i][row+1])/2; } } depenData[zrow] = -offset; zrow++; } } if (zrow) { if (!SDDS_LengthenTable(&outSet, zrow) || !SDDS_SetColumnFromDoubles(&outSet, SDDS_SET_BY_NAME, depenData, zrow, zeroName)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (i=0; i<columnNames; i++) { sprintf(s, "%sSlope", columnName[i]); if (!SDDS_SetColumnFromDoubles(&outSet, SDDS_SET_BY_NAME, indepData[i], zrow, columnName[i]) || (flags&FL_SLOPEOUTPUT && !SDDS_SetColumnFromDoubles(&outSet, SDDS_SET_BY_NAME, slopeData[i], zrow, s))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } free(depenData); for (i=0; i<columnNames; i++) free(indepData[i]); if (flags&FL_SLOPEOUTPUT) for (i=0; i<columnNames; i++) free(slopeData[i]); } if (!SDDS_WritePage(&outSet)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_Terminate(&inSet) || !SDDS_Terminate(&outSet)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } 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; }