long get_material_property(char *label, char *property, char **str_value, long *int_value, double *double_value) { long rows=0, index, col_index=-1; SDDS_DATASET table; int32_t *intValue = NULL; char **formula, **name; double *doubleValue = NULL; formula = name = NULL; if ((col_index=match_string(property, table_column, TABLE_COLS, 0))<0) { if (verbose) fprintf(stderr, "Property - %s does not exist in the property table.\n", property); return -1; } if (!SDDS_InitializeInput(&table, matTable)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!SDDS_ReadPage(&table) || !(rows=SDDS_CountRowsOfInterest(&table)) || !(formula=(char**)SDDS_GetColumn(&table, "Formula")) || !(name=(char**)SDDS_GetColumn(&table, "Name"))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); index = match_string(label, formula, rows, EXACT_MATCH); if (index<0) index = match_string(label, name, rows, 0); if (index<0) { fprintf(stdout, "%s not found.\n", label); return -1; } switch (col_index) { case FORMULA_COL: SDDS_CopyString(str_value, formula[index]); break; case NAME_COL: SDDS_CopyString(str_value, name[index]); break; case Z_COL: case GROUP_COL: case PERIOD_COL: intValue = (int32_t*)SDDS_GetColumn(&table, table_column[col_index]); *int_value = intValue[index]; free(intValue); break; default: doubleValue = (double*)SDDS_GetColumn(&table, table_column[col_index]); *double_value = doubleValue[index]; free(doubleValue); break; } if (!SDDS_Terminate(&table)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); SDDS_FreeStringArray(formula, rows); SDDS_FreeStringArray(name, rows); free(formula); free(name); return index; }
void addFileFilterOutput(double *outputRI, double *inputRI, long frequencies, double dfrequency, FILE_FILTER *filter) { long i, code; double factor, ifactor, rfactor, rdata, idata, f; if (!filter->freqData) { SDDS_DATASET SDDSin; long readCode = 0; if (!SDDS_InitializeInput(&SDDSin, filter->file) || (readCode=SDDS_ReadPage(&SDDSin))==0) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (readCode<0) { fprintf(stderr, "error: unable to read filter file %s (sddsfdfilter)\n", filter->file); exit(1); } if ((filter->points = SDDS_CountRowsOfInterest(&SDDSin))<=0) { fprintf(stderr, "error: file %s has no data on first page (sddsfdfilter)\n", filter->file); exit(1); } if (!(filter->freqData=SDDS_GetColumnInDoubles(&SDDSin, filter->freqName)) || (filter->magName && !(filter->magData=SDDS_GetColumnInDoubles(&SDDSin, filter->magName))) || (filter->imagName && !(filter->imagData=SDDS_GetColumnInDoubles(&SDDSin, filter->imagName))) || (filter->realName && !(filter->realData=SDDS_GetColumnInDoubles(&SDDSin, filter->realName))) || !SDDS_Terminate(&SDDSin)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (i=1; i<filter->points; i++) if (filter->freqData[i-1]>=filter->freqData[i]) { fprintf(stderr, "error: frequency data not monotonically increasing for %s (sddsfdfilter)\n", filter->file); exit(1); } } for (i=0; i<frequencies; i++) { f = i*dfrequency; if (filter->freqData[0]>f || filter->freqData[filter->points-1]<f) continue; if (filter->magName) { factor = interp(filter->magData, filter->freqData, filter->points, f, 0, 1, &code); outputRI[2*i ] += factor*inputRI[2*i ]; outputRI[2*i+1] += factor*inputRI[2*i+1]; } else { rfactor = interp(filter->realData, filter->freqData, filter->points, f, 0, 1, &code); ifactor = interp(filter->imagData, filter->freqData, filter->points, f, 0, 1, &code); rdata = inputRI[2*i ]; idata = inputRI[2*i+1]; outputRI[2*i ] += rdata*rfactor - idata*ifactor; outputRI[2*i+1] += rdata*ifactor + idata*rfactor; } } }
int main(int argc, char **argv) { SDDS_DATASET SDDS_1, SDDS_2, SDDS_output; long i, j, i_arg, rows1, rows2, reuse, reusePage, i1, i2; SCANNED_ARG *s_arg; char s[200], *ptr; char **match_column, **equate_column; long match_columns, equate_columns; char *input1, *input2, *output; long tmpfile_used, retval1, retval2; long warnings, invert; unsigned long pipeFlags; KEYED_EQUIVALENT **keyGroup=NULL; long keyGroups=0; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&s_arg, argc, argv); if (argc<3) bomb(NULL, USAGE); input1 = input2 = output = NULL; match_column = equate_column = NULL; match_columns = equate_columns = reuse = reusePage = 0; tmpfile_used = invert = 0; warnings = 1; pipeFlags = 0; for (i_arg=1; i_arg<argc; i_arg++) { if (s_arg[i_arg].arg_type==OPTION) { delete_chars(s_arg[i_arg].list[0], "_"); switch (match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) { case SET_MATCH_COLUMN: if (s_arg[i_arg].n_items!=2) SDDS_Bomb("invalid -match syntax"); if (match_columns!=0) SDDS_Bomb("only one -match option may be given"); match_column = tmalloc(sizeof(*match_column)*2); if ((ptr=strchr(s_arg[i_arg].list[1], '='))) *ptr++ = 0; else ptr = s_arg[i_arg].list[1]; match_column[0] = s_arg[i_arg].list[1]; match_column[1] = ptr; match_columns = 1; break; case SET_EQUATE_COLUMN: if (s_arg[i_arg].n_items!=2) SDDS_Bomb("invalid -equate syntax"); if (equate_columns!=0) SDDS_Bomb("only one -equate option may be given"); equate_column = tmalloc(sizeof(*equate_column)*2); if ((ptr=strchr(s_arg[i_arg].list[1], '='))) *ptr++ = 0; else ptr = s_arg[i_arg].list[1]; equate_column[0] = s_arg[i_arg].list[1]; equate_column[1] = ptr; equate_columns = 1; break; case SET_REUSE: if (s_arg[i_arg].n_items==1) reuse = 1; else { char *reuseOptions[2] = {"rows", "page"}; for (i=1; i<s_arg[i_arg].n_items; i++) { switch (match_string(s_arg[i_arg].list[i], reuseOptions, 2, 0)) { case 0: reuse = 1; break; case 1: reusePage = 1; break; default: SDDS_Bomb("unknown reuse keyword"); break; } } } break; case SET_INVERT: invert = 1; break; case SET_NOWARNINGS: warnings = 0; break; case SET_PIPE: if (!processPipeOption(s_arg[i_arg].list+1, s_arg[i_arg].n_items-1, &pipeFlags)) SDDS_Bomb("invalid -pipe syntax"); break; default: fprintf(stderr, "error: unknown switch: %s\n", s_arg[i_arg].list[0]); SDDS_Bomb(NULL); break; } } else { if (input1==NULL) input1 = s_arg[i_arg].list[0]; else if (input2==NULL) input2 = s_arg[i_arg].list[0]; else if (output==NULL) output = s_arg[i_arg].list[0]; else SDDS_Bomb("too many filenames"); } } if (pipeFlags&USE_STDIN && input1) { if (output) SDDS_Bomb("too many filenames (sddsxref)"); output = input2; input2 = input1; input1 = NULL; } processFilenames("sddsselect", &input1, &output, pipeFlags, !warnings, &tmpfile_used); if (!input2) SDDS_Bomb("second input file not specified (sddsxref)"); if (equate_columns && match_columns) SDDS_Bomb("only one of -equate or -match may be given"); if (!equate_columns && !match_columns) SDDS_Bomb("one of -equate or -match must be given"); if (!SDDS_InitializeInput(&SDDS_1, input1)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (!SDDS_InitializeInput(&SDDS_2, input2)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (match_columns) { if ((j=SDDS_GetColumnIndex(&SDDS_1, match_column[0]))<0 || SDDS_GetColumnType(&SDDS_1, j)!=SDDS_STRING) { sprintf(s, "error: column %s not found or not string type in file %s", match_column[0], input1?input1:"stdin"); SDDS_SetError(s); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if ((j=SDDS_GetColumnIndex(&SDDS_2, match_column[1]))<0 || SDDS_GetColumnType(&SDDS_2, j)!=SDDS_STRING) { sprintf(s, "error: column %s not found or not string type in file %s", match_column[1], input2); SDDS_SetError(s); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } if (equate_columns) { if ((j=SDDS_GetColumnIndex(&SDDS_1, equate_column[0]))<0 || !SDDS_NUMERIC_TYPE(SDDS_GetColumnType(&SDDS_1, j))) { sprintf(s, "error: column %s not found or not numeric type in file %s", equate_column[0], input1?input1:"stdin"); SDDS_SetError(s); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if ((j=SDDS_GetColumnIndex(&SDDS_2, equate_column[1]))<0 || !SDDS_NUMERIC_TYPE(SDDS_GetColumnType(&SDDS_2, j))) { sprintf(s, "error: column %s not found or not numeric type in file %s", equate_column[1], input2); SDDS_SetError(s); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } if (output && pipeFlags&USE_STDOUT) SDDS_Bomb("too many filenames with -pipe option"); if (!output && !(pipeFlags&USE_STDOUT)) { if (warnings) fprintf(stderr, "warning: existing file %s will be replaced (sddsselect)\n", input1?input1:"stdin"); tmpfile_used = 1; cp_str(&output, tmpname(NULL)); } if (!SDDS_InitializeCopy(&SDDS_output, &SDDS_1, output, "w")) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (!SDDS_WriteLayout(&SDDS_output)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); while ((retval1=SDDS_ReadPage(&SDDS_1))>0) { if (!reusePage) { if ((retval2=SDDS_ReadPage(&SDDS_2))<=0) { if (warnings) fprintf(stderr, "warning: <input2> ends before <input1>\n"); if (invert) { /* nothing to match, so everything would normally be thrown out */ if (!SDDS_CopyPage(&SDDS_output, &SDDS_1) || !SDDS_WritePage(&SDDS_output)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); continue; } else /* nothing to match, so everything thrown out */ break; } } else { if (retval1==1 && (retval2=SDDS_ReadPage(&SDDS_2))<=0) SDDS_Bomb("<input2> has no data"); SDDS_SetRowFlags(&SDDS_2, 1); } rows1 = SDDS_CountRowsOfInterest(&SDDS_1); rows2 = SDDS_CountRowsOfInterest(&SDDS_2); if (!SDDS_StartPage(&SDDS_output, rows1)) { SDDS_SetError("Problem starting output page"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_CopyParameters(&SDDS_output, &SDDS_2) || !SDDS_CopyArrays(&SDDS_output, &SDDS_2)) { SDDS_SetError("Problem copying parameter or array data from second input file"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_CopyParameters(&SDDS_output, &SDDS_1) || !SDDS_CopyArrays(&SDDS_output, &SDDS_1)) { SDDS_SetError("Problem copying parameter or array data from first input file"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (match_columns) { char **string1, **string2; long matched; string2 = NULL; if (!(string1 = SDDS_GetColumn(&SDDS_1, match_column[0]))) { fprintf(stderr, "Error: problem getting column %s from file %s\n", match_column[0], input1?input1:"stdin"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (rows2 && !(string2 = SDDS_GetColumn(&SDDS_2, match_column[1]))) { fprintf(stderr, "Error: problem getting column %s from file %s\n", match_column[1], input2); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (rows2) keyGroup = MakeSortedKeyGroups(&keyGroups, SDDS_STRING, string2, rows2); for (i1=0; i1<rows1; i1++) { if (!SDDS_CopyRowDirect(&SDDS_output, i1, &SDDS_1, i1)) { sprintf(s, "Problem copying row %ld of first data set", i1); SDDS_SetError(s); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } matched = 0; if (rows2 &&(i2 = FindMatchingKeyGroup(keyGroup, keyGroups, SDDS_STRING, string1+i1, reuse))>=0) { matched = 1; } if ((!matched && !invert) || (matched && invert)) { if (!SDDS_AssertRowFlags(&SDDS_output, SDDS_INDEX_LIMITS, i1, i1, 0)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } if (string1) { for (i=0;i<rows1;i++) free(string1[i]); free(string1); string1 = NULL; } if (string2) { for (i=0;i<rows2;i++) free(string2[i]); free(string2); string2 = NULL; } for (i=0;i<keyGroups;i++) { if (keyGroup[i]) { if (keyGroup[i]->equivalent) free(keyGroup[i]->equivalent); free(keyGroup[i]); keyGroup[i] = NULL; } } if (keyGroups) { free(keyGroup); keyGroup = NULL; keyGroups = 0; } } else if (equate_columns) { double *value1, *value2; long equated; value2 = NULL; if (!(value1 = SDDS_GetColumnInDoubles(&SDDS_1, equate_column[0]))) { fprintf(stderr, "Error: problem getting column %s from file %s\n", equate_column[0], input1?input1:"stdin"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (rows2 && !(value2 = SDDS_GetColumnInDoubles(&SDDS_2, equate_column[1]))) { fprintf(stderr, "Error: problem getting column %s from file %s\n", equate_column[1], input2); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (rows2) keyGroup = MakeSortedKeyGroups(&keyGroups, SDDS_DOUBLE, value2, rows2); for (i1=0; i1<rows1; i1++) { if (!SDDS_CopyRowDirect(&SDDS_output, i1, &SDDS_1, i1)) { sprintf(s, "Problem copying row %ld of first data set", i1); SDDS_SetError(s); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } equated = 0; if (rows2 && (i2 = FindMatchingKeyGroup(keyGroup, keyGroups, SDDS_DOUBLE, value1+i1, reuse))>=0) { equated = 1; } if ((!equated && !invert) || (equated && invert)) { if (!SDDS_AssertRowFlags(&SDDS_output, SDDS_INDEX_LIMITS, i1, i1, 0)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } if (value1) free(value1); value1 = NULL; if (rows2 && value2) free(value2); value2 = NULL; for (i=0;i<keyGroups;i++) { if (keyGroup[i]) { if (keyGroup[i]->equivalent) free(keyGroup[i]->equivalent); free(keyGroup[i]); keyGroup[i] = NULL; } } if (keyGroups) { free(keyGroup); keyGroup = NULL; keyGroups = 0; } } if (!SDDS_WritePage(&SDDS_output)) { SDDS_SetError("Problem writing data to output file"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } if (!SDDS_Terminate(&SDDS_1) || !SDDS_Terminate(&SDDS_2) || !SDDS_Terminate(&SDDS_output)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (tmpfile_used && !replaceFileAndBackUp(input1, output)) exit(1); free_scanargs(&s_arg,argc); if (match_columns) free(match_column); return(0); }
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) { SDDS_DATASET SDDSnew, SDDSold; long i, j, iArg; SCANNED_ARG *scArg; char *input, *output, *columnName; long mode, matchCode, rows, rowsMinus1, tmpfile_used; double gapAmount, *columnData, gapFactor; char *matchPattern; long matchPatternAfter = 0; double changeAmount, changeBase; long retval, newStart, rowLimit, breakNext; int32_t dataType, overlap=0; unsigned long flags, pipeFlags, changeFlags; char **stringData; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&scArg, argc, argv); if (argc<2) { fprintf(stderr, "%s", USAGE); return(1); } columnData = NULL; stringData = NULL; input = output = columnName = NULL; mode = -1; pipeFlags = flags = 0; gapAmount = changeAmount = rowLimit = gapFactor = 0; matchPattern = NULL; for (iArg=1; iArg<argc; iArg++) { if (scArg[iArg].arg_type==OPTION) { switch (matchCode=match_string(scArg[iArg].list[0], option, N_OPTIONS, 0)) { case SET_GAPIN: if ((scArg[iArg].n_items-=2)<0 || !scanItemList(&flags, scArg[iArg].list+2, &scArg[iArg].n_items, 0, "amount", SDDS_DOUBLE, &gapAmount, 1, GAPIN_AMOUNT, "factor", SDDS_DOUBLE, &gapFactor, 1, GAPIN_FACTOR, NULL) || (flags&GAPIN_AMOUNT && gapAmount<=0) || (flags&GAPIN_FACTOR && gapFactor<=0)) { fprintf(stderr, "Error: invalid -gapin syntax/values\n"); return(1); } columnName = scArg[iArg].list[1]; mode = matchCode; break; case SET_INCREASEOF: case SET_DECREASEOF: if (scArg[iArg].n_items!=2) { fprintf(stderr, "Error: invalid option syntax---specify column-name with -increaseof and -decreaseof\n"); return(1); } columnName = scArg[iArg].list[1]; mode = matchCode; break; case SET_CHANGEOF: if ((scArg[iArg].n_items-=2)<0 || !scanItemList(&changeFlags, scArg[iArg].list+2, &scArg[iArg].n_items, 0, "amount", SDDS_DOUBLE, &changeAmount, 1, CHANGEOF_AMOUNT, "base", SDDS_DOUBLE, &changeBase, 1, CHANGEOF_BASE, NULL) || (changeFlags&CHANGEOF_AMOUNT && changeAmount<=0)) { fprintf(stderr, "Error: invalid -changeof syntax/values\n"); return(1); } columnName = scArg[iArg].list[1]; mode = matchCode; break; case SET_ROWLIMIT: if (scArg[iArg].n_items<2) { fprintf(stderr, "Error: invalid -rowlimit syntax\n"); return(1); } if (sscanf(scArg[iArg].list[1], "%ld", &rowLimit)!=1 || rowLimit<=0) { fprintf(stderr, "Error: invalid -rowlimit syntax\n"); return(1); } if (scArg[iArg].n_items>2) { scArg[iArg].n_items-=2; if (!scanItemList(&flags, scArg[iArg].list+2, &scArg[iArg].n_items, 0, "overlap", SDDS_LONG, &overlap, NULL) || overlap<0) { fprintf(stderr, "Error: invalid overlap given in -rowlimit syntax\n"); return(1); } } mode = matchCode; break; case SET_PIPE: if (!processPipeOption(scArg[iArg].list+1, scArg[iArg].n_items-1, &pipeFlags)) { fprintf(stderr, "Error: invalid -pipe syntax\n"); return(1); } break; case SET_MATCHTO: if ((scArg[iArg].n_items!=3 && scArg[iArg].n_items!=4) || strlen(columnName=scArg[iArg].list[1])==0 || strlen(matchPattern=scArg[iArg].list[2])==0) { fprintf(stderr, "Error: invalid -matchTo syntax\n"); return(1); } if (scArg[iArg].n_items==4) { if (strncmp(scArg[iArg].list[3], "after", strlen(scArg[iArg].list[3]))==0) matchPatternAfter = 1; else { fprintf(stderr, "Error: invalid -matchTo syntax\n"); return(1); } } mode = matchCode; break; default: fprintf(stderr, "Error: unknown switch: %s\n", scArg[iArg].list[0]); fprintf(stderr, "%s", USAGE); return(1); } } else { if (input==NULL) input = scArg[iArg].list[0]; else if (output==NULL) output = scArg[iArg].list[0]; else { fprintf(stderr, "Error: too many filenames given\n"); return(1); } } } processFilenames("sddsbreak", &input, &output, pipeFlags, 0, &tmpfile_used); if (mode==-1) { fprintf(stderr, "Error: no break mode specified\n"); return(1); } if (!SDDS_InitializeInput(&SDDSold, input) || !SDDS_InitializeCopy(&SDDSnew, &SDDSold, output, "w")) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } SDDSnew.layout.data_mode.no_row_counts = 0; if (!SDDS_WriteLayout(&SDDSnew)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } if (mode!=SET_ROWLIMIT) { if (SDDS_GetColumnInformation(&SDDSold, "type", &dataType, SDDS_BY_NAME, columnName)!=SDDS_LONG) { SDDS_SetError("problem getting type information on given column"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } if (mode==SET_MATCHTO) { if (!(dataType==SDDS_STRING)) { fprintf(stderr, "Error: given column does not contain string data\n"); return(1); } } else if (!SDDS_NUMERIC_TYPE(dataType)) { if (!(mode==SET_CHANGEOF && !(changeFlags&CHANGEOF_AMOUNT) && !(changeFlags&CHANGEOF_BASE))) { fprintf(stderr, "Error: given column does not contain numeric data\n"); return(1); } } } while ((retval=SDDS_ReadPage(&SDDSold))>0) { if ((rows = SDDS_CountRowsOfInterest(&SDDSold))<0) { SDDS_SetError("Problem getting number of rows of tabular data"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } rowsMinus1 = rows-1; if (!SDDS_StartPage(&SDDSnew, rows) || !SDDS_CopyParameters(&SDDSnew, &SDDSold) || !SDDS_CopyArrays(&SDDSnew, &SDDSold)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } if (rows==0) { if (!SDDS_WritePage(&SDDSnew)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } continue; } switch (mode) { case SET_GAPIN: if (!(columnData=SDDS_GetColumnInDoubles(&SDDSold, columnName))) { SDDS_SetError("unable to read specified column"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } if (!gapAmount && rows>1) { double *gap; gap = tmalloc(sizeof(*gap)*rows); for (i=1; i<rows; i++) gap[i-1] = fabs(columnData[i]-columnData[i-1]); if (!compute_average(&gapAmount, gap, rows-1)) { fprintf(stderr, "Error: unable to determine default gap amount--couldn't find median gap\n"); return(1); } gapAmount *= (gapFactor?gapFactor:2); free(gap); } newStart = 0; for (i=1; i<=rows; i++) { if (i!=rows && fabs(columnData[i]-columnData[i-1])<gapAmount) continue; if (!SDDS_SetRowFlags(&SDDSold, 0) || !SDDS_AssertRowFlags(&SDDSold, SDDS_INDEX_LIMITS, newStart, i-1, 1) || !SDDS_CopyRowsOfInterest(&SDDSnew, &SDDSold) || !SDDS_WritePage(&SDDSnew)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } newStart = i; } free(columnData); break; case SET_INCREASEOF: if (!(columnData=SDDS_GetColumnInDoubles(&SDDSold, columnName))) { SDDS_SetError("unable to read specified column"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } newStart = 0; for (i=1; i<=rows; i++) { if (i!=rows && columnData[i]<=columnData[i-1]) continue; if (!SDDS_SetRowFlags(&SDDSold, 0) || !SDDS_AssertRowFlags(&SDDSold, SDDS_INDEX_LIMITS, newStart, i-1, 1) || !SDDS_CopyRowsOfInterest(&SDDSnew, &SDDSold) || !SDDS_WritePage(&SDDSnew)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } newStart = i; } free(columnData); break; case SET_DECREASEOF: if (!(columnData=SDDS_GetColumnInDoubles(&SDDSold, columnName))) { SDDS_SetError("unable to read specified column"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } newStart = 0; for (i=1; i<=rows; i++) { if (i!=rows && columnData[i]>=columnData[i-1]) continue; if (!SDDS_SetRowFlags(&SDDSold, 0) || !SDDS_AssertRowFlags(&SDDSold, SDDS_INDEX_LIMITS, newStart, i-1, 1) || !SDDS_CopyRowsOfInterest(&SDDSnew, &SDDSold) || !SDDS_WritePage(&SDDSnew)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } newStart = i; } free(columnData); break; case SET_CHANGEOF: if (dataType!=SDDS_STRING) { if (!(columnData=SDDS_GetColumnInDoubles(&SDDSold, columnName))) { SDDS_SetError("unable to read specified column"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } } else { if (!(stringData=SDDS_GetColumn(&SDDSold, columnName))) { SDDS_SetError("unable to read specified column"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } } newStart = 0; if (dataType==SDDS_STRING || !changeAmount) { for (i=1; i<=rows; i++) { if (i!=rows && ((dataType==SDDS_STRING && strcmp(stringData[i], stringData[i-1])==0) || (dataType!=SDDS_STRING && columnData[i]==columnData[i-1]))) continue; if (!SDDS_SetRowFlags(&SDDSold, 0) || !SDDS_AssertRowFlags(&SDDSold, SDDS_INDEX_LIMITS, newStart, i-1, 1) || !SDDS_CopyRowsOfInterest(&SDDSnew, &SDDSold) || !SDDS_WritePage(&SDDSnew)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } newStart = i; } } else { long region, lastRegion; region = lastRegion = 0; if (!(changeFlags&CHANGEOF_BASE) && rows>=1) changeBase = columnData[0]; if (rows>1) lastRegion = (columnData[0]-changeBase)/changeAmount; #ifdef DEBUG fprintf(stderr, "change base=%e, lastRegion=%ld\n", changeBase, lastRegion); fprintf(stderr, "start value = %e\n", columnData[0]); #endif newStart = 0; for (i=1; i<=rows; i++) { if (i!=rows) region = (columnData[i]-changeBase)/changeAmount; if (i!=rows && region==lastRegion) continue; #ifdef DEBUG fprintf(stderr, "split after %e, before %e, region = %d\n", columnData[i-1], columnData[i], region); #endif if (!SDDS_SetRowFlags(&SDDSold, 0) || !SDDS_AssertRowFlags(&SDDSold, SDDS_INDEX_LIMITS, newStart, i-1, 1) || !SDDS_CopyRowsOfInterest(&SDDSnew, &SDDSold) || !SDDS_WritePage(&SDDSnew)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } newStart = i; lastRegion = region; #ifdef DEBUG fprintf(stderr, "start value = %e\n", columnData[i]); #endif } } if (dataType!=SDDS_STRING) free(columnData); else SDDS_FreeStringArray(stringData, rows); break; case SET_MATCHTO: if (!(stringData=SDDS_GetColumn(&SDDSold, columnName))) { SDDS_SetError("unable to read specified column"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } newStart = 0; breakNext = 0; for (i=1; i<=rows; i++) { if (i!=rows && !breakNext) { if (wild_match(stringData[i], matchPattern)) { if (matchPatternAfter) { breakNext = 1; continue; } } else continue; } if (!SDDS_SetRowFlags(&SDDSold, 0) || !SDDS_AssertRowFlags(&SDDSold, SDDS_INDEX_LIMITS, newStart, i-1, 1) || !SDDS_CopyRowsOfInterest(&SDDSnew, &SDDSold) || !SDDS_WritePage(&SDDSnew)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } breakNext = 0; newStart = i; } SDDS_FreeStringArray(stringData, rows); break; case SET_ROWLIMIT: for (i=0; i<rows; i+=rowLimit-overlap) { if ((j=i+rowLimit-1)>=rows) j = rows-1; if (!SDDS_SetRowFlags(&SDDSold, 0) || !SDDS_AssertRowFlags(&SDDSold, SDDS_INDEX_LIMITS, i, j, 1) || !SDDS_CopyRowsOfInterest(&SDDSnew, &SDDSold) || !SDDS_WritePage(&SDDSnew)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } if (j==rows-1) break; } break; default: fprintf(stderr, "Error: unknown break mode code seen---this can't happen\n"); return(1); } } if (retval==0) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } if (!SDDS_Terminate(&SDDSold)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); return(1); } if (tmpfile_used && !replaceFileAndBackUp(input, output)) { return(1); } return(0); }
int main(int argc, char **argv) { 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, *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) { 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) { 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); }
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"); }
/**************************** main ****************************************/ 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 s[256]; char *input, *output; long i, j, i_arg, append_units, nrows, ntable, len; SCANNED_ARG *s_arg; char *text, *contents, *ss, *ptr, *iformat=FORMAT, *format, *rformat; long verbose=0, comments=0, addquotes=1; short nexp; double dd, ddred; float ff, ffred; void *data; unsigned long pipeFlags; SDDS_RegisterProgramName(argv[0]); list_request = -1; input = output = NULL; append_units = 0; 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_COMMENTS: comments = 1; break; case SET_FORMAT: if (s_arg[i_arg].n_items<2) SDDS_Bomb("Invalid -format syntax"); iformat = s_arg[i_arg].list[1]; /* Input format */ 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("sdds2math", &input, &output, pipeFlags, 0, NULL); if (output) { outfile=fopen(output,"w"); if(!outfile) SDDS_Bomb(strcat("Can't open output file ", output)); } else { outfile = stdout; } /* Calculate formats for converting to Mathematica convention */ len=strlen(iformat); /* Inputformat */ format=(char *)calloc(256,sizeof(char)); /* Whole format */ rformat=(char *)calloc(256,sizeof(char)); /* Part before e */ strcpy(format,iformat); if((ptr=strchr(format,'E'))) *ptr='e'; /* Aren't going to use E or G anyway */ if((ptr=strchr(format,'G'))) *ptr='g'; strcpy(rformat,format); if((ptr=strpbrk(rformat,"eg"))) *ptr='f'; /* fprintf(outfile,"Created from SDDS file: %s\n",filename[file]); */ if (!SDDS_InitializeInput(&SDDS_table, input)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } layout = &SDDS_table.layout; /* Start top level */ fprintf(outfile,"{"); /* Description */ fprintf(outfile,"{"); if(verbose) printf("\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) printf("description: %s\n", text); if (text) fprintf(outfile,"\"%s\",",text?text:"No description\t"); if (contents) if(verbose) printf("contents: %s\n", contents); fprintf(outfile,"\"%s\"",contents?contents:"No description\t"); if (layout->data_mode.mode==SDDS_ASCII) { if(verbose) printf("\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); if(verbose) printf("row counts: %s \n", layout->data_mode.no_row_counts?"no":"yes"); } else if(verbose) printf("\ndata is binary\n"); fprintf(outfile,"},\n"); /* Columns */ fprintf(outfile," {"); if (layout->n_columns) { if(verbose) printf("\n%" PRId32 " columns of data:\n", layout->n_columns); if(verbose) printf("NAME UNITS SYMBOL FORMAT TYPE FIELD DESCRIPTION\n"); if(verbose) printf(" LENGTH\n"); for (i=0; i<layout->n_columns; i++) { if(i > 0) fprintf(outfile,",\n "); coldef = layout->column_definition+i; if(verbose) printf("%-15s %-15s %-15s %-15s %-7s %-7" PRId32 " %s\n", coldef->name, coldef->units, coldef->symbol, coldef->format_string, SDDS_type_name[coldef->type-1], coldef->field_length, coldef->description); fprintf(outfile,"{\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",%" PRId32 ",\"%s\"}", coldef->name?coldef->name:"No 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:"No description"); } } fprintf(outfile,"},\n"); /* Parameters */ fprintf(outfile," {"); if (layout->n_parameters) { if(verbose) printf("\n%" PRId32 " parameters:\n", layout->n_parameters); if(verbose) printf("NAME UNITS SYMBOL TYPE DESCRIPTION\n"); for (i=0; i < layout->n_parameters; i++) { if(i > 0) fprintf(outfile,",\n "); pardef = layout->parameter_definition+i; if(verbose) printf("%-19s %-19s %-19s %-19s %s\n", pardef->name, pardef->units, pardef->symbol, SDDS_type_name[pardef->type-1], pardef->description); fprintf(outfile,"{\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"}", pardef->name?pardef->name:"No name", pardef->fixed_value?pardef->units:"", pardef->units?pardef->units:"", pardef->symbol?pardef->symbol:"", SDDS_type_name[pardef->type-1], pardef->description?pardef->description:"No description"); } } fprintf(outfile,"},\n"); /* Arrays */ fprintf(outfile," {"); if (layout->n_arrays) { if(verbose) printf("\n%" PRId32 " arrays of data:\n", layout->n_arrays); if(verbose) printf("NAME UNITS SYMBOL" " FORMAT TYPE FIELD GROUP DESCRIPTION\n"); if(verbose) printf(" LENGTH NAME\n"); for (i=0; i<layout->n_arrays; i++) { if(i > 0) fprintf(outfile,",\n "); arraydef = layout->array_definition+i; if(verbose) printf("%-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); fprintf(outfile,"{\"%s\",\"%s\",\"%s\",\"%s\",\"%s*^%" PRId32 "\",%-7" PRId32 ",\"%s\",\"%s\"}", arraydef->name?arraydef->name:"No name", arraydef->units?arraydef->units:"", arraydef->symbol?arraydef->symbol:"", arraydef->format_string?arraydef->format_string:"", SDDS_type_name[arraydef->type-1], arraydef->dimensions, arraydef->field_length, arraydef->group_name, arraydef->description); } } fprintf(outfile,"},\n"); /* Associates */ fprintf(outfile," {"); if (layout->n_associates) { if(verbose) printf("\n%" PRId32 " associates:\n", layout->n_associates); if(verbose) printf("SDDS FILENAME PATH CONTENTS DESCRIPTION\n"); for (i=0; i<layout->n_associates; i++) if(i > 0) fprintf(outfile,",\n "); if(verbose) printf("%-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\"}", layout->associate_definition[i].sdds?"True":"False", layout->associate_definition[i].filename, layout->associate_definition[i].path, layout->associate_definition[i].contents, layout->associate_definition[i].description); } fprintf(outfile,"},\n"); /* Process tables */ fprintf(outfile," {"); /* Start of array of tables */ while ((ntable=SDDS_ReadTable(&SDDS_table))>0) { if(ntable > 1) fprintf(outfile,",\n "); if(comments) fprintf(outfile,"(*Table %ld*)",ntable); fprintf(outfile,"{\n"); /* Start of this table */ /* Variable parameters */ fprintf(outfile," {"); /* Start of parameters */ for (i=0; i<layout->n_parameters; i++) { if(i > 0) fprintf(outfile,",\n "); pardef = layout->parameter_definition+i; if (!(data=SDDS_GetParameter(&SDDS_table, pardef->name, NULL))) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } /* This parameter */ if(comments) fprintf(outfile,"(*%s*)",pardef->name); addquotes=1; switch(pardef->type) { case SDDS_DOUBLE: dd=*(double *)data; sprintf(s,format,dd); if((ptr=strchr(s,'e'))) { *ptr=' '; sscanf(s,"%lf %hd",&ddred,&nexp); fprintf(outfile,rformat,ddred); fprintf(outfile,"*10^%d",nexp); } else { fprintf(outfile,s); } break; case SDDS_FLOAT: ff=*(float *)data; sprintf(s,format,ff); if((ptr=strchr(s,'e'))) { *ptr=' '; sscanf(s,"%f %hd",&ffred,&nexp); fprintf(outfile,rformat,ffred); fprintf(outfile,"*10^%d",nexp); } else { fprintf(outfile,s); } break; case SDDS_STRING: ss=*(char **)data; if(*ss == '"') addquotes=0; else if(SDDS_StringIsBlank(ss) || SDDS_HasWhitespace(ss)) addquotes=0; case SDDS_CHARACTER: if(addquotes) fprintf(outfile,"\""); SDDS_PrintTypedValue(data, 0, pardef->type, NULL, outfile, 0); if(addquotes) fprintf(outfile,"\""); break; default: SDDS_PrintTypedValue(data, 0, pardef->type, NULL, outfile, 0); break; } } fprintf(outfile,"},\n"); /* End of parameters */ /* Columns */ fprintf(outfile," {"); /* Start of data array */ 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; */ /* fprintf(outfile,"%s%s",coldef->name,delimiter); */ /* } */ /* fprintf(outfile,"\n"); */ if (nrows) { for (j=0; j<nrows; j++) { if(j > 0) fprintf(outfile,",\n "); fprintf(outfile,"{"); /* Start of row */ for (i=0; i<layout->n_columns; i++) { if( i > 0) fprintf(outfile,","); coldef = layout->column_definition+i; if (!(data=SDDS_GetValue(&SDDS_table, coldef->name,j,NULL))) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } addquotes=1; switch(coldef->type) { case SDDS_DOUBLE: dd=*(double *)data; sprintf(s,format,dd); if((ptr=strchr(s,'e'))) { *ptr=' '; sscanf(s,"%lf %hd",&ddred,&nexp); fprintf(outfile,rformat,ddred); fprintf(outfile,"*10^%d",nexp); } else { fprintf(outfile,s); } break; case SDDS_FLOAT: ff=*(float *)data; sprintf(s,format,ff); if((ptr=strchr(s,'e'))) { *ptr=' '; sscanf(s,"%f %hd",&ffred,&nexp); fprintf(outfile,rformat,ffred); fprintf(outfile,"*10^%d",nexp); } else { fprintf(outfile,s); } break; case SDDS_STRING: ss=*(char **)data; if(*ss == '"') addquotes=0; else if(SDDS_StringIsBlank(ss) || SDDS_HasWhitespace(ss)) addquotes=0; case SDDS_CHARACTER: if(addquotes) fprintf(outfile,"\""); SDDS_PrintTypedValue(data, 0, coldef->type, NULL, outfile, 0); if(addquotes) fprintf(outfile,"\""); break; default: SDDS_PrintTypedValue(data, 0, coldef->type, NULL, outfile, 0); break; } } fprintf(outfile,"}"); /* End of row */ } } } fprintf(outfile,"}"); /* End of data array */ fprintf(outfile,"}"); /* End of this table */ } fprintf(outfile,"\n }\n"); /* End of array of tables, last major component */ /* End top level */ fprintf(outfile,"}\n"); /* End of top level */ /* QUIT: */ fflush(stdout); if (!SDDS_Terminate(&SDDS_table)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } return(0); }
int main(int argc, char **argv) { SDDS_DATASET SDDSin, SDDSout; long i_arg, tmpfileUsed; SCANNED_ARG *s_arg; char *input, *output, *description_text, *description_contents; unsigned long pipeFlags; long pageNumber, nonNative = 0; char *outputEndianess = NULL; char buffer[40]; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&s_arg, argc, argv); if (argc<2) bomb(NULL, USAGE); input = output = description_text = description_contents = NULL; pipeFlags = 0; for (i_arg=1; i_arg<argc; i_arg++) { if (s_arg[i_arg].arg_type==OPTION) { delete_chars(s_arg[i_arg].list[0], "_"); switch (match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) { case SET_PIPE: if (!processPipeOption(s_arg[i_arg].list+1, s_arg[i_arg].n_items-1, &pipeFlags)) SDDS_Bomb("invalid -pipe syntax"); break; case NONNATIVE: nonNative = 1; break; default: fprintf(stderr, "Error (%s): unknown switch: %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("sddsendian", &input, &output, pipeFlags, 0, &tmpfileUsed); outputEndianess = getenv("SDDS_OUTPUT_ENDIANESS"); if (outputEndianess) { putenv("SDDS_OUTPUT_ENDIANESS="); } if (!SDDS_InitializeInput(&SDDSin, input)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (!description_text) SDDS_GetDescription(&SDDSin, &description_text, &description_contents); if (!SDDS_InitializeCopy(&SDDSout, &SDDSin, output, "w") || !SDDS_SetDataMode(&SDDSout, nonNative?SDDS_BINARY:-SDDS_BINARY) || !SDDS_WriteLayout(&SDDSout)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (nonNative) { while ((pageNumber=SDDS_ReadNonNativePage(&SDDSin))>=0) { if (!SDDS_CopyPage(&SDDSout, &SDDSin) || !SDDS_WritePage(&SDDSout)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } } } else { while ((pageNumber=SDDS_ReadPage(&SDDSin))>=0) { if (!SDDS_CopyPage(&SDDSout, &SDDSin) || !SDDS_WriteNonNativeBinaryPage(&SDDSout)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } } } if (!SDDS_Terminate(&SDDSin) || !SDDS_Terminate(&SDDSout)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (tmpfileUsed && !replaceFileAndBackUp(input, output)) exit(1); if (outputEndianess) { sprintf(buffer, "SDDS_OUTPUT_ENDIANESS=%s", outputEndianess); putenv(buffer); } return(0); }
int main(int argc, char **argv) { int iArg; char **column, **excludeColumn, *withOnly; long columns, excludeColumns; char *input, *output; SCANNED_ARG *scanned; SDDS_DATASET SDDSin, SDDSout; long i, j, row, rows, count, readCode, rankOrder, iName1, iName2; int32_t outlierStDevPasses; double **data, correlation, significance, outlierStDevLimit; double **rank; short **accept; char s[SDDS_MAXLINE]; unsigned long pipeFlags, dummyFlags, majorOrderFlag; short columnMajorOrder=-1; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&scanned, argc, argv); if (argc<2) bomb(NULL, USAGE); output = input = withOnly = NULL; columns = excludeColumns = 0; column = excludeColumn = NULL; pipeFlags = 0; rankOrder = 0; outlierStDevPasses = 0; outlierStDevLimit = 1.; rank = NULL; accept = NULL; for (iArg=1; iArg<argc; iArg++) { if (scanned[iArg].arg_type==OPTION) { /* process options here */ switch (match_string(scanned[iArg].list[0], option, N_OPTIONS, 0)) { case SET_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 SET_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 SET_EXCLUDE: if (scanned[iArg].n_items<2) SDDS_Bomb("invalid -excludeColumns syntax"); moveToStringArray(&excludeColumn, &excludeColumns, scanned[iArg].list+1, scanned[iArg].n_items-1); break; case SET_WITHONLY: if (withOnly) SDDS_Bomb("only one -withOnly option may be given"); if (scanned[iArg].n_items<2) SDDS_Bomb("invalid -withOnly syntax"); withOnly = scanned[iArg].list[1]; break; case SET_PIPE: if (!processPipeOption(scanned[iArg].list+1, scanned[iArg].n_items-1, &pipeFlags)) SDDS_Bomb("invalid -pipe syntax"); break; case SET_RANKORDER: rankOrder = 1; break; case SET_STDEVOUTLIER: scanned[iArg].n_items--; outlierStDevPasses = 1; outlierStDevLimit = 1; if (!scanItemList(&dummyFlags, scanned[iArg].list+1, &scanned[iArg].n_items, 0, "limit", SDDS_DOUBLE, &outlierStDevLimit, 1, 0, "passes", SDDS_LONG, &outlierStDevPasses, 1, 0, NULL) || outlierStDevPasses<=0 || outlierStDevLimit<=0) SDDS_Bomb("invalid -stdevOutlier syntax/values"); 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("sddscorrelate", &input, &output, pipeFlags, 0, NULL); if (!SDDS_InitializeInput(&SDDSin, input)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!columns) columns = appendToStringArray(&column, columns, "*"); if (withOnly) columns = appendToStringArray(&column, columns, withOnly); if ((columns=expandColumnPairNames(&SDDSin, &column, NULL, columns, excludeColumn, excludeColumns, FIND_NUMERIC_TYPE, 0))<=0) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); SDDS_Bomb("no columns selected for correlation analysis"); } if (!SDDS_InitializeOutput(&SDDSout, SDDS_BINARY, 0, NULL, "sddscorrelate output", output) || SDDS_DefineColumn(&SDDSout, "Correlate1Name", NULL, NULL, "Name of correlated quantity 1", NULL, SDDS_STRING, 0)<0 || SDDS_DefineColumn(&SDDSout, "Correlate2Name", NULL, NULL, "Name of correlated quantity 2", NULL, SDDS_STRING, 0)<0 || SDDS_DefineColumn(&SDDSout, "CorrelatePair", NULL, NULL, "Names of correlated quantities", NULL, SDDS_STRING, 0)<0 || SDDS_DefineColumn(&SDDSout, "CorrelationCoefficient", "r", NULL, "Linear correlation coefficient", NULL, SDDS_DOUBLE, 0)<0 || SDDS_DefineColumn(&SDDSout, "CorrelationSignificance", "P$br$n", NULL, "Linear correlation coefficient significance", NULL, SDDS_DOUBLE, 0)<0 || SDDS_DefineColumn(&SDDSout, "CorrelationPoints", NULL, NULL, "Number of points used for correlation", NULL, SDDS_LONG, 0)<0 || SDDS_DefineParameter(&SDDSout, "CorrelatedRows", NULL, NULL, "Number of data rows in correlation analysis", NULL, SDDS_LONG, NULL)<0 || SDDS_DefineParameter(&SDDSout, "sddscorrelateInputFile", NULL, NULL, "Data file processed by sddscorrelate", NULL, SDDS_STRING, input?input:"stdin")<0 || SDDS_DefineParameter(&SDDSout, "sddscorrelateMode", NULL, NULL, NULL, NULL, SDDS_STRING, rankOrder?"Rank-Order (Spearman)":"Linear (Pearson)")<0 || SDDS_DefineParameter1(&SDDSout, "sddscorrelateStDevOutlierPasses", NULL, NULL, "Number of passes of standard-deviation outlier elimination applied", NULL, SDDS_LONG, &outlierStDevPasses)<0 || SDDS_DefineParameter1(&SDDSout, "sddscorrelateStDevOutlierLimit", NULL, NULL, "Standard-deviation outlier limit applied", NULL, SDDS_DOUBLE, &outlierStDevLimit)<0) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (columnMajorOrder!=-1) SDDSout.layout.data_mode.column_major = columnMajorOrder; else SDDSout.layout.data_mode.column_major = SDDSin.layout.data_mode.column_major; if (!SDDS_WriteLayout(&SDDSout)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!(data = (double**)malloc(sizeof(*data)*columns)) || (rankOrder && !(rank = (double**)malloc(sizeof(*rank)*columns))) || !(accept = (short**)malloc(sizeof(*accept)*columns))) SDDS_Bomb("allocation failure"); while ((readCode=SDDS_ReadPage(&SDDSin))>0) { if ((rows = SDDS_CountRowsOfInterest(&SDDSin))<3) continue; if (!SDDS_StartPage(&SDDSout, columns*(columns-1)/2) || !SDDS_SetParameters(&SDDSout, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "CorrelatedRows", rows, NULL)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (i=0; i<columns; i++) { if (!(data[i] = SDDS_GetColumnInDoubles(&SDDSin, column[i]))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (rankOrder) rank[i] = findRank(data[i], rows); if (outlierStDevPasses) { if (!(accept[i] = (short*)malloc(sizeof(**accept)*rows))) SDDS_Bomb("allocation failure"); markStDevOutliers(data[i], outlierStDevLimit, outlierStDevPasses, accept[i], rows); } else accept[i] = NULL; } for (i=row=0; i<columns; i++) { for (j=i+1; j<columns; j++) { iName1 = i; iName2 = j; if (withOnly) { if (strcmp(withOnly, column[i])==0) { iName1 = j; iName2 = i; } else if (strcmp(withOnly, column[j])==0) { iName1 = i; iName2 = j; } else continue; } correlation = linearCorrelationCoefficient(rankOrder?rank[i]:data[i], rankOrder?rank[j]:data[j], accept[i], accept[j], rows, &count); significance = linearCorrelationSignificance(correlation, count); sprintf(s, "%s.%s", column[iName1], column[iName2]); if (!SDDS_SetRowValues(&SDDSout, SDDS_SET_BY_INDEX|SDDS_PASS_BY_VALUE, row++, 0, column[iName1], 1, column[iName2], 2, s, 3, correlation, 4, significance, 5, count, -1)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } for (i=0; i<columns; i++) { free(data[i]); if (rankOrder) free(rank[i]); if (accept[i]) free(accept[i]); } if (!SDDS_WritePage(&SDDSout)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_Terminate(&SDDSin)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (!SDDS_Terminate(&SDDSout)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } return 0; }
int main(int argc, char **argv) { double tolerance, result; long nEval; int32_t nEvalMax = 5000, nPassMax = 25; double a[4], da[4]; double alo[4], ahi[4]; long n_dimen = 4, zeroes; SDDS_DATASET InputTable, OutputTable; SCANNED_ARG *s_arg; long i_arg, i, clue, fullOutput; char *input, *output, *xName, *yName; int32_t xIndex, yIndex, fitIndex, residualIndex; long retval; double *fitData, *residualData, rmsResidual; unsigned long guessFlags, dummyFlags, pipeFlags; double constantGuess, factorGuess, freqGuess, phaseGuess; double firstZero, lastZero; unsigned long simplexFlags = 0; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&s_arg, argc, argv); if (argc<2 || argc>(2+N_OPTIONS)) bomb(NULL, USAGE); input = output = NULL; tolerance = 1e-6; verbosity = fullOutput = 0; clue = -1; xName = yName = NULL; guessFlags = 0; pipeFlags = 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_TOLERANCE: if (s_arg[i_arg].n_items!=2 || sscanf(s_arg[i_arg].list[1], "%lf", &tolerance)!=1) SDDS_Bomb("incorrect -tolerance syntax"); break; case SET_VERBOSITY: if (s_arg[i_arg].n_items!=2 || sscanf(s_arg[i_arg].list[1], "%ld", &verbosity)!=1) SDDS_Bomb("incorrect -verbosity syntax"); break; case SET_GUESS: if (s_arg[i_arg].n_items<2) SDDS_Bomb("incorrect -guess syntax"); s_arg[i_arg].n_items -= 1; if (!scanItemList(&guessFlags, s_arg[i_arg].list+1, &s_arg[i_arg].n_items, 0, "constant", SDDS_DOUBLE, &constantGuess, 1, GUESS_CONSTANT_GIVEN, "factor", SDDS_DOUBLE, &factorGuess, 1, GUESS_FACTOR_GIVEN, "frequency", SDDS_DOUBLE, &freqGuess, 1, GUESS_FREQ_GIVEN, "phase", SDDS_DOUBLE, &phaseGuess, 1, GUESS_PHASE_GIVEN, NULL)) SDDS_Bomb("invalid -guess syntax"); break; case SET_COLUMNS: if (s_arg[i_arg].n_items!=3) SDDS_Bomb("invalid -columns syntax"); xName = s_arg[i_arg].list[1]; yName = s_arg[i_arg].list[2]; break; case SET_FULLOUTPUT: fullOutput = 1; break; case SET_LIMITS: if (s_arg[i_arg].n_items<2) SDDS_Bomb("incorrect -limits syntax"); s_arg[i_arg].n_items -= 1; if (!scanItemList(&dummyFlags, s_arg[i_arg].list+1, &s_arg[i_arg].n_items, 0, "evaluations", SDDS_LONG, &nEvalMax, 1, 0, "passes", SDDS_LONG, &nPassMax, 1, 0, NULL) || nEvalMax<=0 || nPassMax<=0) SDDS_Bomb("invalid -limits syntax"); break; case SET_PIPE: if (!processPipeOption(s_arg[i_arg].list+1, s_arg[i_arg].n_items-1, &pipeFlags)) SDDS_Bomb("invalid -pipe syntax"); break; default: fprintf(stderr, "error: unknown/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("sddssinefit", &input, &output, pipeFlags, 0, NULL); if (!xName || !yName) SDDS_Bomb("-columns option must be given"); if (!SDDS_InitializeInput(&InputTable, input) || SDDS_GetColumnIndex(&InputTable, xName)<0 || SDDS_GetColumnIndex(&InputTable, yName)<0) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); setupOutputFile(&OutputTable, &xIndex, &yIndex, &fitIndex, &residualIndex, output, fullOutput, &InputTable, xName, yName); fitData = residualData = NULL; alo[0] = - (ahi[0] = DBL_MAX); alo[1] = alo[2] = 0; ahi[1] = ahi[2] = DBL_MAX; alo[3] = - (ahi[3] = PIx2); firstZero = lastZero = 0; while ((retval=SDDS_ReadPage(&InputTable))>0) { if (!(xData = SDDS_GetColumnInDoubles(&InputTable, xName)) || !(yData = SDDS_GetColumnInDoubles(&InputTable, yName))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if ((nData = SDDS_CountRowsOfInterest(&InputTable))<4) continue; find_min_max(&yMin, &yMax, yData, nData); find_min_max(&xMin, &xMax, xData, nData); zeroes = 0; for (i=1; i<nData; i++) if (yData[i]*yData[i-1]<=0) { i++; if (!zeroes) firstZero = (xData[i]+xData[i-1])/2; else lastZero = (xData[i]+xData[i-1])/2; zeroes ++; } a[0] = (yMin+yMax)/2; a[1] = (yMax-yMin)/2; if (!zeroes) a[2] = 2/fabs(xMax-xMin); else a[2] = zeroes/(2*fabs(lastZero-firstZero)); a[3] = 0; if (guessFlags&GUESS_CONSTANT_GIVEN) a[0] = constantGuess; if (guessFlags&GUESS_FACTOR_GIVEN) a[1] = factorGuess; if (guessFlags&GUESS_FREQ_GIVEN) a[2] = freqGuess; if (guessFlags&GUESS_PHASE_GIVEN) a[3] = phaseGuess; alo[1] = a[1]/2; if (!(da[0] = a[0]*0.1)) da[0] = 0.01; if (!(da[1] = a[1]*0.1)) da[1] = 0.01; da[2] = a[2]*0.25; da[3] = 0.01; nEval = simplexMin(&result, a, da, alo, ahi, NULL, n_dimen, -DBL_MAX, tolerance, fitFunction, (verbosity>0?report:NULL), nEvalMax, nPassMax, 12, 3, 1.0, simplexFlags); fitData = trealloc(fitData, sizeof(*fitData)*nData); residualData = trealloc(residualData, sizeof(*residualData)*nData); for (i=result=0; i<nData; i++) result += sqr(residualData[i] = yData[i]-(fitData[i]=a[0]+a[1]*sin(PIx2*a[2]*xData[i]+a[3]))); rmsResidual = sqrt(result/nData); if (verbosity>1) { fprintf(stderr, "RMS deviation: %.15e\n", rmsResidual); fprintf(stderr, "(RMS deviation)/(largest value): %.15e\n", rmsResidual/MAX(fabs(yMin), fabs(yMax))); } if (verbosity>0) { fprintf(stderr, "coefficients of fit to the form y = a0 + a1*sin(2*PI*a2*x+a3), a = \n"); for (i=0; i<4; i++) fprintf(stderr, "%.8e ", a[i]); fprintf(stderr, "\n"); } if (!SDDS_StartPage(&OutputTable, nData) || !SDDS_SetColumn(&OutputTable, SDDS_SET_BY_INDEX, xData, nData, xIndex) || !SDDS_SetColumn(&OutputTable, SDDS_SET_BY_INDEX, fitData, nData, fitIndex) || !SDDS_SetParameters(&OutputTable, SDDS_PASS_BY_VALUE|SDDS_SET_BY_NAME, "sinefitConstant", a[0], "sinefitFactor", a[1], "sinefitFrequency", a[2], "sinefitPhase", a[3], "sinefitRmsResidual", rmsResidual, NULL) || (fullOutput && (!SDDS_SetColumn(&OutputTable, SDDS_SET_BY_INDEX, yData, nData, yIndex) || !SDDS_SetColumn(&OutputTable, SDDS_SET_BY_INDEX, residualData, nData, residualIndex) )) || !SDDS_WritePage(&OutputTable)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } return 0; }
int main(int argc, char **argv) { char *indepQuantity, **depenQuantity, **exclude, **depenQuantityPair; long depenQuantities, excludes; char *input, *output; long iArg, i, j, rows, readCode, noWarnings, items; long rowsToUse; unsigned long flags, pairFlags, tmpFlags, pipeFlags; SCANNED_ARG *scArg; SDDS_DATASET SDDSin, SDDSout; double *tdata, *data, t0, dt; double fracRMSChangeLimit, fracFreqAccuracyLimit; int32_t frequenciesDesired, maxFrequencies, freqCycleLimit; short truncate; double *frequency, *amplitude=NULL, *phase=NULL, *significance=NULL, *phase1=NULL, *amplitude1=NULL, *significance1=NULL; long frequenciesFound; long *frequencyIndex, *amplitudeIndex, *phaseIndex, *significanceIndex, pairs; long *amplitudeIndex1, *phaseIndex1, *significanceIndex1; SDDS_RegisterProgramName(argv[0]); #ifdef DEBUG if (1) { long code; double x, y; x = 1.1; code = OneDFunctionOptimize(&y, &x, 0.07, -4, 4, trialFn, 50, 1e-6, 0, 1); fprintf(stderr, "code: %ld x=%e, y=%e\n", code, x, y); x = .9; code = OneDFunctionOptimize(&y, &x, 0.15, -4, 4, trialFn, 50, 1e-6, 0, 1); fprintf(stderr, "code: %ld x=%e, y=%e\n", code, x, y); x = .999; code = OneDFunctionOptimize(&y, &x, 0.11, -4, 4, trialFn, 50, 1e-6, 0, 1); fprintf(stderr, "code: %ld x=%e, y=%e\n", code, x, y); exit(0); } #endif argc = scanargs(&scArg, argc, argv); if (argc<3) { fprintf(stderr, "%s%s", USAGE1, USAGE2); exit(1); } output = input = NULL; flags = pipeFlags = excludes = truncate = pairFlags= 0; indepQuantity = NULL; depenQuantity = exclude = depenQuantityPair=NULL; depenQuantities = 0; noWarnings = 0; fracRMSChangeLimit = 0.0; fracFreqAccuracyLimit = 0.00001; frequenciesDesired = 1; maxFrequencies = 4; freqCycleLimit = 100; pairs=0; for (iArg=1; iArg<argc; iArg++) { if (scArg[iArg].arg_type==OPTION) { /* process options here */ switch (match_string(scArg[iArg].list[0], option, N_OPTIONS, 0)) { case SET_TRUNCATE: truncate = 1; break; case SET_PAIR: if (depenQuantities) SDDS_Bomb("Invalid -pair option, the depent-quantity is provided by -column option already."); if (scArg[iArg].n_items!=3) SDDS_Bomb("invalid -pair syntax"); depenQuantity = SDDS_Realloc(depenQuantity, sizeof(*depenQuantity)*(pairs+1)); depenQuantityPair = SDDS_Realloc(depenQuantityPair, sizeof(*depenQuantityPair)*(pairs+1)); depenQuantity[pairs] = scArg[iArg].list[1]; depenQuantityPair[pairs] = scArg[iArg].list[2]; pairs++; break; case SET_COLUMN: if (indepQuantity) SDDS_Bomb("only one -column option may be given"); if (scArg[iArg].n_items<2) SDDS_Bomb("invalid -column syntax"); indepQuantity = scArg[iArg].list[1]; if (scArg[iArg].n_items>=2) { if (pairs) SDDS_Bomb("Invalid -column syntax, the depent-quantity is provided by -pair option already."); depenQuantity = tmalloc(sizeof(*depenQuantity)*(depenQuantities=scArg[iArg].n_items-2)); for (i=0; i<depenQuantities; i++) depenQuantity[i] = scArg[iArg].list[i+2]; } break; case SET_PIPE: if (!processPipeOption(scArg[iArg].list+1, scArg[iArg].n_items-1, &pipeFlags)) SDDS_Bomb("invalid -pipe syntax"); break; case SET_EXCLUDE: if (scArg[iArg].n_items<2) SDDS_Bomb("invalid -exclude syntax"); moveToStringArray(&exclude, &excludes, scArg[iArg].list+1, scArg[iArg].n_items-1); break; case SET_NOWARNINGS: noWarnings = 1; break; case SET_TERM_SEARCH: items = scArg[iArg].n_items-1; flags &= ~(NAFF_RMS_CHANGE_LIMIT|NAFF_FREQS_DESIRED|NAFF_MAX_FREQUENCIES); fracRMSChangeLimit = 0; frequenciesDesired = 0; maxFrequencies = 10; if (!scanItemList(&tmpFlags, scArg[iArg].list+1, &items, 0, "changelimit", SDDS_DOUBLE, &fracRMSChangeLimit, 1, NAFF_RMS_CHANGE_LIMIT, "maxfrequencies", SDDS_LONG, &maxFrequencies, 1, NAFF_MAX_FREQUENCIES, "frequencies", SDDS_LONG, &frequenciesDesired, 1, NAFF_FREQS_DESIRED, NULL) || (tmpFlags&NAFF_RMS_CHANGE_LIMIT && tmpFlags&NAFF_FREQS_DESIRED) || maxFrequencies<1) SDDS_Bomb("invalid -terminateSearch syntax"); flags |= tmpFlags; if (frequenciesDesired) maxFrequencies = frequenciesDesired; break; case SET_ITERATE_FREQ: items = scArg[iArg].n_items - 1; flags &= ~(NAFF_FREQ_CYCLE_LIMIT|NAFF_FREQ_ACCURACY_LIMIT); if (!scanItemList(&tmpFlags, scArg[iArg].list+1, &items, 0, "cyclelimit", SDDS_LONG, &freqCycleLimit, 1, NAFF_FREQ_CYCLE_LIMIT, "accuracylimit", SDDS_DOUBLE, &fracFreqAccuracyLimit, 1, NAFF_FREQ_ACCURACY_LIMIT, NULL) || !bitsSet(tmpFlags) || freqCycleLimit<2) SDDS_Bomb("invalid -iterateFrequency syntax"); flags |= tmpFlags; break; default: fprintf(stderr, "error: unknown/ambiguous option: %s\n", scArg[iArg].list[0]); exit(1); break; } } else { if (!input) input = scArg[iArg].list[0]; else if (!output) output = scArg[iArg].list[0]; else SDDS_Bomb("too many filenames seen"); } } processFilenames("sddsnaff", &input, &output, pipeFlags, 0, NULL); if (!indepQuantity) SDDS_Bomb("supply the independent quantity name with the -columns option"); if (!SDDS_InitializeInput(&SDDSin, input)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (SDDS_CheckColumn(&SDDSin, indepQuantity, NULL, SDDS_ANY_NUMERIC_TYPE, stderr) !=SDDS_CHECK_OKAY) exit(1); excludes = appendToStringArray(&exclude, excludes, indepQuantity); if (pairs) { pairFlags = flags | NAFF_FREQ_FOUND; depenQuantities = pairs; } if (!depenQuantities) depenQuantities = appendToStringArray(&depenQuantity, depenQuantities, "*"); if (!pairs) { if ((depenQuantities=expandColumnPairNames(&SDDSin, &depenQuantity, NULL, depenQuantities, exclude, excludes, FIND_NUMERIC_TYPE, 0))<=0) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); SDDS_Bomb("No quantities selected to fft"); } } if (!SetupNAFFOutput(&SDDSout, output, &SDDSin, indepQuantity, depenQuantities, depenQuantity, &frequencyIndex, &litudeIndex, &phaseIndex, &significanceIndex, depenQuantityPair,&litudeIndex1, &phaseIndex1,&significanceIndex1)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!(frequency = SDDS_Malloc(sizeof(*frequency)*maxFrequencies)) || !(amplitude = SDDS_Malloc(sizeof(*amplitude)*maxFrequencies)) || !(phase = SDDS_Malloc(sizeof(*phase )*maxFrequencies)) || !(significance= SDDS_Malloc(sizeof(*significance)*maxFrequencies))) SDDS_Bomb("memory allocation failure"); if (pairs) { if (!(amplitude1 = SDDS_Malloc(sizeof(*amplitude1)*maxFrequencies)) || !(phase1 = SDDS_Malloc(sizeof(*phase1 )*maxFrequencies)) || !(significance1 = SDDS_Malloc(sizeof(*significance1)*maxFrequencies))) SDDS_Bomb("memory allocation failure"); } while ((readCode=SDDS_ReadPage(&SDDSin))>0) { if ((rows = SDDS_CountRowsOfInterest(&SDDSin))<0) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (rows) { long primeRows; rowsToUse = rows; primeRows = greatestProductOfSmallPrimes(rows); if (rows!=primeRows) { if (truncate) rowsToUse = greatestProductOfSmallPrimes(rows); else if (largest_prime_factor(rows)>100 && !noWarnings) fputs("Warning: number of points has large prime factors.\nThis could take a very long time.\nConsider using the -truncate option.\n", stderr); } if (!SDDS_StartPage(&SDDSout, maxFrequencies) || !SDDS_CopyParameters(&SDDSout, &SDDSin)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!(tdata = SDDS_GetColumnInDoubles(&SDDSin, indepQuantity))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (i=1; i<rowsToUse; i++) if (tdata[i]<=tdata[i-1]) SDDS_Bomb("independent data is not monotonically increasing"); dt = (tdata[rowsToUse-1]-tdata[0])/(rowsToUse-1.0), t0 = tdata[0]; free(tdata); for (i=0; i<depenQuantities; i++) { if (!(data = SDDS_GetColumnInDoubles(&SDDSin, depenQuantity[i]))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (j=0; j<maxFrequencies; j++) frequency[j] = amplitude[j] = phase[j] = significance[j] = -1; frequenciesFound = PerformNAFF(frequency, amplitude, phase, significance, t0, dt, data, rowsToUse, flags, fracRMSChangeLimit, maxFrequencies, freqCycleLimit, fracFreqAccuracyLimit, 0, 0); #ifdef DEBUG fprintf(stderr, "Column %s: ", depenQuantity[i]); fprintf(stderr, "f=%10.3e a=%10.3e p=%10.3e s=%10.3e\n", frequency[0], amplitude[0], phase[0], significance[0]); #endif free(data); data = NULL; if (pairs) { if (!(data = SDDS_GetColumnInDoubles(&SDDSin, depenQuantityPair[i]))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); frequenciesFound = PerformNAFF(frequency, amplitude1, phase1, significance1, t0, dt, data, rowsToUse, pairFlags, fracRMSChangeLimit, maxFrequencies, freqCycleLimit, fracFreqAccuracyLimit, 0, 0); for (j=0; j<maxFrequencies; j++) if (frequency[j]!=-1) frequency[j] = adjustFrequencyHalfPlane(frequency[j], phase[j], phase1[j], dt); free(data); data = NULL; } if (!SDDS_SetColumn(&SDDSout, SDDS_SET_BY_INDEX, frequency, maxFrequencies, frequencyIndex[i]) || !SDDS_SetColumn(&SDDSout, SDDS_SET_BY_INDEX, amplitude, maxFrequencies, amplitudeIndex[i]) || !SDDS_SetColumn(&SDDSout, SDDS_SET_BY_INDEX, phase, maxFrequencies, phaseIndex[i]) || !SDDS_SetColumn(&SDDSout, SDDS_SET_BY_INDEX, significance, maxFrequencies, significanceIndex[i])) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (pairs) { if (!SDDS_SetColumn(&SDDSout, SDDS_SET_BY_INDEX, amplitude1, maxFrequencies, amplitudeIndex1[i]) || !SDDS_SetColumn(&SDDSout, SDDS_SET_BY_INDEX, phase1, maxFrequencies, phaseIndex1[i]) || !SDDS_SetColumn(&SDDSout, SDDS_SET_BY_INDEX, significance1, maxFrequencies, significanceIndex1[i])) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } } else { if (!SDDS_StartPage(&SDDSout, 0) || !SDDS_CopyParameters(&SDDSout, &SDDSin)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_WritePage(&SDDSout)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_Terminate(&SDDSin)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (!SDDS_Terminate(&SDDSout)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } free(frequency); free(amplitude); free(phase); free(significance); if (pairs) { free(amplitude1); free(phase1); free(significance1); free(amplitudeIndex1); free(phaseIndex1); free(significanceIndex1); free(depenQuantityPair); } free(depenQuantity); free(frequencyIndex); free(amplitudeIndex); free(phaseIndex); free(significanceIndex); return 0; }
int main(int argc, char **argv) { 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); }
int main(int argc, char **argv) { int iArg; char *input, *output,*meanPar, *sigmaPar, *maxPar, *minPar; long i, j, mainInputOpened, haltonID=0, requireInput=0; unsigned long pipeFlags; SCANNED_ARG *scanned; SDDS_DATASET SDDSin, SDDSout, *SDDSptr; long randomNumberSeed = 0; SEQ_REQUEST *seqRequest; long samples, values, seqRequests, randomizationGroups=0; double *sample, *IVValue, *CDFValue; char msgBuffer[1000]; RANDOMIZED_ORDER *randomizationData = NULL; long verbose, optimalHalton=0; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&scanned, argc, argv); if (argc<2) { fprintf(stderr, "%s%s%s\n", USAGE1, USAGE2, USAGE3); return(1); } seqRequest = NULL; seqRequests = 0; output = input = NULL; pipeFlags = 0; samples = values = 0; sample = IVValue = CDFValue = NULL; verbose = 0; maxPar = minPar = meanPar = sigmaPar = NULL; for (iArg=1; iArg<argc; iArg++) { if (scanned[iArg].arg_type==OPTION) { /* process options here */ switch (match_string(scanned[iArg].list[0], option, CLO_OPTIONS, 0)) { case CLO_COLUMNS: if (scanned[iArg].n_items<3) SDDS_Bomb("invalid -columns syntax"); if (!(seqRequest = SDDS_Realloc(seqRequest, sizeof(*seqRequest)*(seqRequests+1)))) SDDS_Bomb("memory allocation failure"); scanned[iArg].n_items -= 1; memset(seqRequest+seqRequests, 0, sizeof(*seqRequest)); /*remove following pointer initialization because memset already initialize them */ seqRequest[seqRequests].randomizationGroup = -1; seqRequest[seqRequests].factor = 1; seqRequest[seqRequests].offset = 0; if (!scanItemList(&seqRequest[seqRequests].flags, scanned[iArg].list+1, &scanned[iArg].n_items, 0, "datafile", SDDS_STRING, &seqRequest[seqRequests].dataFileName, 1, SEQ_DATAFILE, "independentvariable", SDDS_STRING, &seqRequest[seqRequests].indepName, 1, SEQ_INDEPNAME, "cdf", SDDS_STRING, &seqRequest[seqRequests].CDFName, 1, SEQ_CDFNAME, "df", SDDS_STRING, &seqRequest[seqRequests].DFName, 1, SEQ_DFNAME, "output", SDDS_STRING, &seqRequest[seqRequests].outputName, 1, SEQ_OUTPUTNAME, "units", SDDS_STRING, &seqRequest[seqRequests].units, 1, SEQ_UNITSGIVEN, "haltonradix", SDDS_LONG, &seqRequest[seqRequests].haltonRadix, 1, SEQ_HALTONRADIX, "haltonoffset", SDDS_LONG, &seqRequest[seqRequests].haltonOffset, 1, SEQ_HALTONOFFSET, "randomize", -1, NULL, 0, SEQ_RANDOMIZE, "group", SDDS_LONG, &seqRequest[seqRequests].randomizationGroup, 1, SEQ_RANDOMGROUP, "factor", SDDS_DOUBLE, &seqRequest[seqRequests].factor, 1, 0, "offset", SDDS_DOUBLE, &seqRequest[seqRequests].offset, 1, 0, NULL) || bitsSet(seqRequest[seqRequests].flags&(SEQ_INDEPNAME+SEQ_CDFNAME+SEQ_DFNAME))!=2) SDDS_Bomb("invalid -columns syntax"); if (seqRequest[seqRequests].flags&SEQ_RANDOMGROUP && seqRequest[seqRequests].randomizationGroup<=0) SDDS_Bomb("use a positive integer for the randomization group ID"); if (seqRequest[seqRequests].flags&SEQ_CDFNAME && seqRequest[seqRequests].flags&SEQ_DFNAME) SDDS_Bomb("give df or cdf for -columns, not both"); if (seqRequest[seqRequests].flags&SEQ_HALTONRADIX && !is_prime(seqRequest[seqRequests].haltonRadix)) SDDS_Bomb("halton radix must be a prime number"); seqRequests ++; scanned[iArg].n_items += 1; break; case CLO_GAUSSIAN: if (scanned[iArg].n_items<2) SDDS_Bomb("invalid -gaussian syntax"); if (!(seqRequest = SDDS_Realloc(seqRequest, sizeof(*seqRequest)*(seqRequests+1)))) SDDS_Bomb("memory allocation failure"); memset(seqRequest+seqRequests, 0, sizeof(*seqRequest)); scanned[iArg].n_items -= 1; seqRequest[seqRequests].randomizationGroup = -1; seqRequest[seqRequests].mean = 0; seqRequest[seqRequests].sigma = 1; if (!scanItemList(&seqRequest[seqRequests].flags, scanned[iArg].list+1, &scanned[iArg].n_items, 0, "columnName", SDDS_STRING, &seqRequest[seqRequests].outputName, 1, SEQ_OUTPUTNAME, "meanValue", SDDS_STRING, &meanPar, 1, 0, "sigmaValue", SDDS_STRING, &sigmaPar, 1, 0, "units", SDDS_STRING, &seqRequest[seqRequests].units, 1, SEQ_UNITSGIVEN, NULL)) SDDS_Bomb("invalid -gaussian syntax"); seqRequest[seqRequests].flags |= SEQ_DIRECT_GAUSSIAN; if (!(seqRequest[seqRequests].flags&SEQ_OUTPUTNAME) || !(seqRequest[seqRequests].outputName)) SDDS_Bomb("columnName is not provided for gaussian distribution/"); if (meanPar) { if (wild_match(meanPar, "@*")) SDDS_CopyString(&seqRequest[seqRequests].meanPar, meanPar+1); else if (!get_double(&seqRequest[seqRequests].mean, meanPar)) SDDS_Bomb("Invalid value given for mean value of -gaussian distribution."); free(meanPar); meanPar = NULL; } if (sigmaPar) { if (wild_match(sigmaPar, "@*")) SDDS_CopyString(&seqRequest[seqRequests].sigmaPar, sigmaPar+1); else if (!get_double(&seqRequest[seqRequests].sigma, sigmaPar)) SDDS_Bomb("Invalid value given for sigma value of -gaussian distribution."); free(sigmaPar); sigmaPar = NULL; } seqRequests ++; scanned[iArg].n_items += 1; break; case CLO_UNIFORM: if (scanned[iArg].n_items<2) SDDS_Bomb("invalid -uniform syntax"); if (!(seqRequest = SDDS_Realloc(seqRequest, sizeof(*seqRequest)*(seqRequests+1)))) SDDS_Bomb("memory allocation failure"); memset(seqRequest+seqRequests, 0, sizeof(*seqRequest)); scanned[iArg].n_items -= 1; memset(seqRequest+seqRequests, 0, sizeof(*seqRequest)); seqRequest[seqRequests].randomizationGroup = -1; seqRequest[seqRequests].min = 0; seqRequest[seqRequests].max = 1; if (!scanItemList(&seqRequest[seqRequests].flags, scanned[iArg].list+1, &scanned[iArg].n_items, 0, "columnName", SDDS_STRING, &seqRequest[seqRequests].outputName, 1, SEQ_OUTPUTNAME, "minimumValue", SDDS_STRING, &minPar, 1, 0, "maximumValue", SDDS_STRING, &maxPar, 1, 0, "units", SDDS_STRING, &seqRequest[seqRequests].units, 1, SEQ_UNITSGIVEN, NULL)) SDDS_Bomb("invalid -uniform syntax"); seqRequest[seqRequests].flags |= SEQ_DIRECT_UNIFORM; if (!(seqRequest[seqRequests].flags&SEQ_OUTPUTNAME) || !(seqRequest[seqRequests].outputName)) SDDS_Bomb("columnName is not provided for uniform distribution/"); if (minPar) { if (wild_match(minPar, "@*")) SDDS_CopyString(&seqRequest[seqRequests].minPar, minPar+1); else if (!get_double(&seqRequest[seqRequests].min, minPar)) SDDS_Bomb("Invalid value given for minimum value of -uniform distribution."); free(minPar); minPar = NULL; } if (maxPar) { if (wild_match(maxPar, "@*")) SDDS_CopyString(&seqRequest[seqRequests].maxPar, maxPar+1); else if (!get_double(&seqRequest[seqRequests].max, maxPar)) SDDS_Bomb("Invalid value given for maximum value of -uniform distribution."); free(maxPar); maxPar = NULL; } seqRequests ++; scanned[iArg].n_items += 1; break; case CLO_POISSON: if (scanned[iArg].n_items<2) SDDS_Bomb("invalid -poisson syntax"); if (!(seqRequest = SDDS_Realloc(seqRequest, sizeof(*seqRequest)*(seqRequests+1)))) SDDS_Bomb("memory allocation failure"); memset(seqRequest+seqRequests, 0, sizeof(*seqRequest)); scanned[iArg].n_items -= 1; memset(seqRequest+seqRequests, 0, sizeof(*seqRequest)); seqRequest[seqRequests].randomizationGroup = -1; seqRequest[seqRequests].mean = 1; if (!scanItemList(&seqRequest[seqRequests].flags, scanned[iArg].list+1, &scanned[iArg].n_items, 0, "columnName", SDDS_STRING, &seqRequest[seqRequests].outputName, 1, SEQ_OUTPUTNAME, "meanValue", SDDS_STRING, &meanPar, 1, 0, "units", SDDS_STRING, &seqRequest[seqRequests].units, 1, SEQ_UNITSGIVEN, NULL)) SDDS_Bomb("invalid -poisson syntax"); seqRequest[seqRequests].flags |= SEQ_DIRECT_POISSON; if (!(seqRequest[seqRequests].flags&SEQ_OUTPUTNAME) || !(seqRequest[seqRequests].outputName)) SDDS_Bomb("columnName is not provided for uniform distribution/"); if (meanPar) { if (wild_match(meanPar, "@*")) SDDS_CopyString(&seqRequest[seqRequests].meanPar, meanPar+1); else if (!get_double(&seqRequest[seqRequests].mean, meanPar)) SDDS_Bomb("Invalid value given for mean value of -poisson distribution."); free(meanPar); meanPar = NULL; } seqRequests ++; scanned[iArg].n_items += 1; break; case CLO_SAMPLES: if (scanned[iArg].n_items!=2 || sscanf(scanned[iArg].list[1], "%ld", &samples)!=1 || samples<=0) SDDS_Bomb("invalid -samples syntax"); break; case CLO_SEED: if (scanned[iArg].n_items!=2 || sscanf(scanned[iArg].list[1], "%ld", &randomNumberSeed)!=1 || randomNumberSeed<=0) SDDS_Bomb("invalid -seed syntax"); break; case CLO_PIPE: if (!processPipeOption(scanned[iArg].list+1, scanned[iArg].n_items-1, &pipeFlags)) SDDS_Bomb("invalid -pipe syntax"); break; case CLO_VERBOSE: verbose = 1; break; case CLO_OPTIMAL_HALTON: optimalHalton = 1; break; default: fprintf(stderr, "error: unknown/ambiguous option: %s\n", scanned[iArg].list[0]); exit(1); break; } } else { if (!input) input = scanned[iArg].list[0]; else if (!output) output = scanned[iArg].list[0]; else SDDS_Bomb("too many filenames seen"); } } if (!seqRequests) SDDS_Bomb("give one or more -columns options"); if (samples<1) SDDS_Bomb("-samples option not given"); for (i=0; i<seqRequests; i++) { if (!(seqRequest[i].flags& (SEQ_DATAFILE|SEQ_DIRECT_GAUSSIAN|SEQ_DIRECT_UNIFORM|SEQ_DIRECT_POISSON))) break; } if (i==seqRequests) { /* all columns options have either their own input files or else use * one of the "direct" distributions. Hence, we don't expect an input * file. */ if (!input) pipeFlags |= USE_STDIN; /* not really, but fakes out processFilenames */ if (input && !output) { output = input; input = NULL; pipeFlags |= USE_STDIN; if (fexists(output)) { sprintf(msgBuffer, "%s exists already (sddssampledist)", output); SDDS_Bomb(msgBuffer); } } } processFilenames("sddssampledist", &input, &output, pipeFlags, 0, NULL); if (!SDDS_InitializeOutput(&SDDSout, SDDS_BINARY, 0, NULL, NULL, output)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (verbose) fprintf(stderr, "Initialized output file %s\n", output); /* open and check input files */ for (i=mainInputOpened=0; i<seqRequests; i++) { if (seqRequest[i].flags&SEQ_DIRECT_GAUSSIAN) { if (seqRequest[i].meanPar || seqRequest[i].sigmaPar) { if (!mainInputOpened) { if (!SDDS_InitializeInput(&SDDSin, input) || !SDDS_TransferAllParameterDefinitions(&SDDSout, &SDDSin, 0)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); mainInputOpened = 1; } requireInput = 1; SDDSptr = &SDDSin; if ((seqRequest[i].meanPar && SDDS_CheckParameter(SDDSptr, seqRequest[i].meanPar, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK) || (seqRequest[i].sigmaPar && SDDS_CheckParameter(SDDSptr, seqRequest[i].sigmaPar, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } } if (!SDDS_DefineSimpleColumn(&SDDSout, seqRequest[i].outputName, NULL, SDDS_DOUBLE)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } else if (seqRequest[i].flags&SEQ_DIRECT_UNIFORM) { if (seqRequest[i].minPar || seqRequest[i].maxPar) { if (!mainInputOpened) { if (!SDDS_InitializeInput(&SDDSin, input) || !SDDS_TransferAllParameterDefinitions(&SDDSout, &SDDSin, 0)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); mainInputOpened = 1; } requireInput = 1; SDDSptr = &SDDSin; if ((seqRequest[i].minPar && SDDS_CheckParameter(SDDSptr, seqRequest[i].minPar, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK) || (seqRequest[i].maxPar && SDDS_CheckParameter(SDDSptr, seqRequest[i].maxPar, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } } if (!SDDS_DefineSimpleColumn(&SDDSout, seqRequest[i].outputName, NULL, SDDS_DOUBLE)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } else if (seqRequest[i].flags&SEQ_DIRECT_POISSON) { if (seqRequest[i].meanPar) { if (!mainInputOpened) { if (!SDDS_InitializeInput(&SDDSin, input) || !SDDS_TransferAllParameterDefinitions(&SDDSout, &SDDSin, 0)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); mainInputOpened = 1; } requireInput = 1; SDDSptr = &SDDSin; if ( SDDS_CheckParameter(SDDSptr, seqRequest[i].meanPar, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } } if (!SDDS_DefineSimpleColumn(&SDDSout, seqRequest[i].outputName, NULL, SDDS_LONG)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } else { if (seqRequest[i].flags&SEQ_RANDOMIZE) { long newGroupID=0; /* define randomization groups */ if (seqRequest[i].flags&SEQ_RANDOMGROUP) { newGroupID = seqRequest[i].randomizationGroup; for (j=0; j<randomizationGroups; j++) if (randomizationData[j].group==newGroupID) { newGroupID = 0; break; } } else { seqRequest[i].randomizationGroup = newGroupID = -(i+1); } if (newGroupID!=0) { if (!(randomizationData = SDDS_Realloc(randomizationData, sizeof(*randomizationData)*(randomizationGroups+1)))) SDDS_Bomb("memory allocation failure"); randomizationData[randomizationGroups].group = newGroupID; randomizationData[randomizationGroups].order = NULL; randomizationGroups ++; } } if (seqRequest[i].flags&SEQ_DATAFILE) { if (!SDDS_InitializeInput(&seqRequest[i].SDDSin, seqRequest[i].dataFileName)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); SDDSptr = &seqRequest[i].SDDSin; } else { if (!mainInputOpened) { if (!SDDS_InitializeInput(&SDDSin, input) || !SDDS_TransferAllParameterDefinitions(&SDDSout, &SDDSin, 0)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); mainInputOpened = 1; } requireInput = 1; SDDSptr = &SDDSin; } if (SDDS_CheckColumn(SDDSptr, seqRequest[i].indepName, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK || ((seqRequest[i].flags&SEQ_CDFNAME) && SDDS_CheckColumn(SDDSptr, seqRequest[i].CDFName, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK) || ((seqRequest[i].flags&SEQ_DFNAME) && SDDS_CheckColumn(SDDSptr, seqRequest[i].DFName, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK) || !SDDS_TransferColumnDefinition(&SDDSout, SDDSptr, seqRequest[i].indepName, seqRequest[i].outputName)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } } if (seqRequest[i].flags&SEQ_UNITSGIVEN && !SDDS_ChangeColumnInformation(&SDDSout, "units", seqRequest[i].units, SDDS_SET_BY_NAME, seqRequest[i].outputName)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (verbose) fprintf(stderr, "Initialized input files\n"); if (!SDDS_WriteLayout(&SDDSout)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (randomNumberSeed==0) { randomNumberSeed = (long)time((time_t *) NULL); randomNumberSeed = 2*(randomNumberSeed/2) + 1; #if defined(_WIN32) random_1(-labs(randomNumberSeed)); #else random_1(-FABS(randomNumberSeed)); #endif } else random_1(-randomNumberSeed); if (!((sample = calloc(sizeof(*sample), samples)))) SDDS_Bomb("memory allocation failure"); while (1) { if (verbose) fprintf(stderr, "Beginning page loop\n"); if (input && SDDS_ReadPage(&SDDSin)<=0) break; for (i=0; i<seqRequests; i++) { if (seqRequest[i].flags&SEQ_DATAFILE && SDDS_ReadPage(&seqRequest[i].SDDSin)<=0) break; } if (i!=seqRequests) break; if (!SDDS_StartPage(&SDDSout, samples) || (input && !SDDS_CopyParameters(&SDDSout, &SDDSin))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (verbose) fprintf(stderr, "Defining randomization tables\n"); /* define randomization tables */ for (i=0; i<randomizationGroups; i++) { if (!(randomizationData[i].order= SDDS_Malloc(sizeof(*randomizationData[i].order)*samples))) SDDS_Bomb("memory allocation failure"); for (j=0; j<samples; j++) randomizationData[i].order[j] = j; randomizeOrder((char*)randomizationData[i].order, sizeof(*randomizationData[i].order), samples, 0, random_1); } if (verbose) fprintf(stderr, "Beginning loop over sequence requests\n"); for (i=0; i<seqRequests; i++) { if (verbose) fprintf(stderr, "Processing sequence request %ld\n", i); if (seqRequest[i].flags&SEQ_DIRECT_GAUSSIAN) { if ((seqRequest[i].meanPar && !SDDS_GetParameterAsDouble(&SDDSin, seqRequest[i].meanPar, &seqRequest[i].mean)) || (seqRequest[i].sigmaPar && !SDDS_GetParameterAsDouble(&SDDSin, seqRequest[i].sigmaPar, &seqRequest[i].sigma))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (j=0; j<samples; j++) sample[j] = gauss_rn_lim(seqRequest[i].mean, seqRequest[i].sigma, -1, random_1); } else if (seqRequest[i].flags&SEQ_DIRECT_UNIFORM) { if ((seqRequest[i].minPar && !SDDS_GetParameterAsDouble(&SDDSin, seqRequest[i].minPar, &seqRequest[i].min)) || (seqRequest[i].maxPar && !SDDS_GetParameterAsDouble(&SDDSin, seqRequest[i].maxPar, &seqRequest[i].max))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (j=0; j<samples; j++) sample[j] = seqRequest[i].min + (seqRequest[i].max - seqRequest[i].min) * random_1(1); } else if (seqRequest[i].flags&SEQ_DIRECT_POISSON) { double *pos_x, *pos_cdf, CDF; long pos_points, code; pos_x = pos_cdf = NULL; if ((seqRequest[i].meanPar && !SDDS_GetParameterAsDouble(&SDDSin, seqRequest[i].meanPar, &seqRequest[i].mean))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); pos_points = CreatePoissonDistributionTable(&pos_x, &pos_cdf, seqRequest[i].mean); for (j=0; j<samples; j++) { CDF = random_1(1); sample[j]= (int)(interp(pos_x, pos_cdf, pos_points, CDF, 0, 1, &code)); /* fprintf(stderr, "%d, cdf=%f, sample=%f\n", j, CDF, sample[j]);*/ } free(pos_x); free(pos_cdf); } else { if (input && !(seqRequest[i].flags&SEQ_DATAFILE)) SDDSptr = &SDDSin; else SDDSptr = &seqRequest[i].SDDSin; if ((values = SDDS_CountRowsOfInterest(SDDSptr))) { if (!(IVValue = SDDS_GetColumnInDoubles(SDDSptr, seqRequest[i].indepName)) || (seqRequest[i].flags&SEQ_CDFNAME && !(CDFValue = SDDS_GetColumnInDoubles(SDDSptr, seqRequest[i].CDFName))) || (seqRequest[i].flags&SEQ_DFNAME && !(CDFValue = SDDS_GetColumnInDoubles(SDDSptr, seqRequest[i].DFName)))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } else { sprintf(msgBuffer, "empty page for file %s\n", seqRequest[i].flags&SEQ_DATAFILE? seqRequest[i].dataFileName:input); SDDS_Bomb(msgBuffer); } if (verbose) fprintf(stderr, "Checking and converting CDF/DF values\n"); /* check/convert CDF/DF values */ for (j=1; j<values; j++) { if (IVValue[j-1]>IVValue[j]) { sprintf(msgBuffer, "random variate values not monotonically increasing for %s", seqRequest[i].flags&SEQ_DATAFILE? seqRequest[i].dataFileName:input); SDDS_Bomb(msgBuffer); } if (seqRequest[i].flags&SEQ_DFNAME) /* convert DF to CDF */ CDFValue[j] += CDFValue[j-1]; if (CDFValue[j] < CDFValue[j-1]) { sprintf(msgBuffer, "CDF values decreasing for %s", seqRequest[i].flags&SEQ_DATAFILE? seqRequest[i].dataFileName:input); SDDS_Bomb(msgBuffer); } } if (verbose) fprintf(stderr, "Normalizing CDF\n"); /* normalize the CDF */ if (CDFValue[values-1]<=0) { sprintf(msgBuffer, "CDF not valid for %s\n", seqRequest[i].dataFileName); SDDS_Bomb(msgBuffer); } for (j=0; j<values; j++) CDFValue[j] /= CDFValue[values-1]; if (seqRequest[i].flags&SEQ_HALTONRADIX) { if (verbose) fprintf(stderr, "Starting halton sequence, offset=%" PRId32 "\n", seqRequest[i].haltonOffset); if (!optimalHalton) haltonID = startHaltonSequence(&seqRequest[i].haltonRadix, 0.5); else haltonID = startModHaltonSequence(&seqRequest[i].haltonRadix, 0); while (seqRequest[i].haltonOffset-- >0) { if (!optimalHalton) nextHaltonSequencePoint(haltonID); else nextModHaltonSequencePoint(haltonID); } } if (verbose) fprintf(stderr, "Generating samples\n"); for (j=0; j<samples; j++) { double CDF; long code; while (1) { if (seqRequest[i].flags&SEQ_HALTONRADIX) { if (!optimalHalton) CDF = nextHaltonSequencePoint(haltonID); else CDF = nextModHaltonSequencePoint(haltonID); } else CDF = random_1(1); if (CDF<=CDFValue[values-1] && CDF>=CDFValue[0]) break; } sample[j] = seqRequest[i].factor*interp(IVValue, CDFValue, values, CDF, 0, 1, &code) + seqRequest[i].offset; } if (seqRequest[i].flags&SEQ_RANDOMIZE) { long k, l; double *sample1; if (verbose) fprintf(stderr, "Randomizing order of values\n"); if (!(sample1 = malloc(sizeof(*sample1)*samples))) SDDS_Bomb("memory allocation failure"); for (l=0; l<randomizationGroups; l++) if (randomizationData[l].group==seqRequest[i].randomizationGroup) break; if (l==randomizationGroups) SDDS_Bomb("problem with construction of randomization groups!"); for (k=0; k<samples; k++) sample1[k] = sample[randomizationData[l].order[k]]; free(sample); sample = sample1; } free(IVValue); free(CDFValue); } if (verbose) fprintf(stderr, "Setting SDDS column values\n"); if (!SDDS_SetColumnFromDoubles(&SDDSout, SDDS_SET_BY_NAME, sample, samples, seqRequest[i].outputName? seqRequest[i].outputName: seqRequest[i].indepName)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (verbose) fprintf(stderr, "Writing data page\n"); if (!SDDS_WritePage(&SDDSout)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!requireInput) break; } if (verbose) fprintf(stderr, "Exited read loop\n"); free(sample); if ((input && !SDDS_Terminate(&SDDSin)) || !SDDS_Terminate(&SDDSout)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (i=0; i<seqRequests; i++) { if (seqRequest[i].dataFileName) free(seqRequest[i].dataFileName); if (seqRequest[i].indepName) free(seqRequest[i].indepName); if (seqRequest[i].outputName) free(seqRequest[i].outputName); if (seqRequest[i].DFName) free(seqRequest[i].DFName); if (seqRequest[i].CDFName) free(seqRequest[i].CDFName); if (seqRequest[i].units) free(seqRequest[i].units); if (seqRequest[i].meanPar) free(seqRequest[i].meanPar); if (seqRequest[i].sigmaPar) free(seqRequest[i].sigmaPar); if (seqRequest[i].minPar) free(seqRequest[i].minPar); if (seqRequest[i].maxPar) free(seqRequest[i].maxPar); if (seqRequest[i].flags&SEQ_DATAFILE && !SDDS_Terminate(&(seqRequest[i].SDDSin))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } free(seqRequest); for (i=0; i<randomizationGroups; i++) free(randomizationData[i].order); if (randomizationData) free(randomizationData); free_scanargs(&scanned, argc); return(0); }
int main(int argc, char **argv) { 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) { 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) { 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) { SDDS_DATASET SDDS_orig, SDDS_out1, SDDS_out2; int32_t i, j, k, row, str_len; SCANNED_ARG *s_arg; char *inputFile, *outputRoot, output1[1024], output2[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 ****data; 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; inputFile = s_arg[1].list[0]; if (argc==3) outputRoot = s_arg[2].list[0]; else outputRoot = inputFile; sprintf(output1, "%s.yz", outputRoot); sprintf(output2, "%s.xz", outputRoot); data = NULL; if (!SDDS_InitializeInput(&SDDS_orig, inputFile)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } columnName = (char**)SDDS_GetColumnNames(&SDDS_orig, &columnNames); for (i=0; i<columnNames; i++) { if (wild_match(columnName[i], "*_1")) { outputColumn = SDDS_Realloc(outputColumn, (outputColumns+1)*sizeof(*outputColumn)); outputColumn[outputColumns] = malloc(sizeof(**outputColumn)*strlen(columnName[i])); str_len = strlen(columnName[i]); strncpy(outputColumn[outputColumns], columnName[i], str_len-2); outputColumn[outputColumns][str_len-2]=0; outputColumns ++; } } data = malloc(sizeof(*data)*outputColumns); 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_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); } SetupOutputFile(&SDDS_out1, output1, 1, outputColumns, outputColumn); SetupOutputFile(&SDDS_out2, output2, 0, outputColumns, outputColumn); for (i=0; i<outputColumns; i++) { data[i] = malloc(sizeof(**data)*zdim); for (j=0; j<zdim; j++) data[i][j] = malloc(sizeof(***data)*ydim); } } for (i=0; i<outputColumns; i++) { for (j=1; j<=ydim; j++) { sprintf(tmpcol, "%s_%d", outputColumn[i], j); data[i][page][j-1] = NULL; if (!(data[i][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(data, outputColumns, zdim, ydim); fprintf(stderr, "Error, the page number does not equal to zdim size.\n"); exit(1); } /* write to yz output, each page has the same x */ for (page=0; page<xdim; page++) { if (!SDDS_StartPage(&SDDS_out1, ydim*zdim) || !SDDS_SetParameters(&SDDS_out1,SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "origin1", xmin, "origin2", ymin, "origin3", zmin, "max_ext1", xmax, "max_ext2", ymax, "max_ext3", zmax, "delta1", xinterval, "delta2", yinterval, "delta3", zinterval, "numPhysCells1", xdim, "numPhysCells2", ydim, "numPhysCells3", zdim, "Variable1Name", "Y", "Variable2Name", "Z", "ZMinimum", zmin, "ZMaximum", zmax, "ZInterval", zinterval, "ZDimension", zdim, "YMinimum", ymin, "YMaximum", ymax, "YInterval", yinterval, "YDimension", ydim, NULL)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (i=0; i<outputColumns; i++) { row = 0; for (j=0; j<ydim; j++) { for (k=0; k<zdim; k++) { if (!SDDS_SetRowValues(&SDDS_out1, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, row, outputColumn[i], data[i][k][j][page], "z", k*zinterval + zmin, "y", j*yinterval + ymin, NULL)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); row ++; } } } if (!SDDS_WritePage(&SDDS_out1)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_Terminate(&SDDS_out1)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); /* write to xz output, each page has the same y */ for (page=0; page<ydim; page++) { if (!SDDS_StartPage(&SDDS_out2, xdim*zdim) || !SDDS_SetParameters(&SDDS_out2, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "origin1", xmin, "origin2", ymin, "origin3", zmin, "max_ext1", xmax, "max_ext2", ymax, "max_ext3", zmax, "delta1", xinterval, "delta2", yinterval, "delta3", zinterval, "numPhysCells1", xdim, "numPhysCells2", ydim, "numPhysCells3", zdim, "Variable1Name", "X", "Variable2Name", "Z", "ZMinimum", zmin, "ZMaximum", zmax, "ZInterval", zinterval, "ZDimension", zdim, "XMinimum", xmin, "XMaximum", xmax, "XInterval", xinterval, "XDimension", xdim, NULL)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (i=0; i<outputColumns; i++) { row = 0; for (j=0; j<xdim; j++) { for (k=0; k<zdim; k++) { if (!SDDS_SetRowValues(&SDDS_out2, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, row, outputColumn[i], data[i][k][page][j], "z", k*zinterval + zmin, "x", j*xinterval+xmin, NULL)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); row ++; } } } if (!SDDS_WritePage(&SDDS_out2)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } free_data_memory(data, outputColumns, zdim, ydim); if (!SDDS_Terminate(&SDDS_out2)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); return 0; }
int main( int argc, char **argv) { SCANNED_ARG *scanned; char *inputfile, *outputfile; SDDS_DATASET twissPage, resultsPage; double particles, charge, length; long verbosity, noWarning, i, elements, superperiods, growthRatesOnly, force; double pCentral0, I1, I2, I3, I4, I5, taux, tauy, taudelta; double EMeV; double emitx0, emitx, emitxInput, emityInput, emity, coupling, sigmaz0, sigmaz; double sigmaDelta0, sigmaDelta, sigmaDeltaInput, xGrowthRate, yGrowthRate, zGrowthRate; double xGrowthRateInitial, yGrowthRateInitial, zGrowthRateInitial; double emitxOld, sigmaDeltaOld; long method, converged; /* used in simplex minimization */ double yReturn, *xGuess, *dxGuess, *xLowerLimit, *xUpperLimit; short *disable; long dimensions = 15, maxEvaluations = 500, maxPasses = 2; double target = 1e-6; int32_t integrationTurns, integrationStepSize; long integrationPoints = 0; double *exInteg=NULL, *eyInteg=NULL, *elInteg=NULL, *xRateInteg=NULL, *yRateInteg=NULL, *zRateInteg=NULL; double *SdeltaInteg=NULL, *SzInteg=NULL; int32_t *passInteg=NULL; unsigned long dummyFlags; double rfVoltage, rfHarmonic; double alphac, U0, circumference, energy; double *xRateVsS, *yRateVsS, *zRateVsS; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&scanned, argc, argv); if (argc == 1) bomb(NULL, USAGE); xRateVsS = yRateVsS = zRateVsS = NULL; inputfile = NULL; outputfile = NULL; energy = 0; verbosity = 0; isRing = 1; particles = 0; charge = 0; coupling = emityInput = 0; force = 1; length = 0; superperiods=1; method = 0; emitxInput = 0; sigmaDeltaInput = 0; growthRatesOnly = 0; integrationTurns = 0; rfVoltage = rfHarmonic = 0; noWarning = 0; for (i = 1; i<argc; i++) { if (scanned[i].arg_type == OPTION) { delete_chars(scanned[i].list[0], "_"); switch(match_string(scanned[i].list[0], option, N_OPTIONS, UNIQUE_MATCH)) { case VERBOSE: if(scanned[i].n_items > 1 ) { get_long(&verbosity, scanned[i].list[1]); } else { verbosity=1; } break; case ISRING: if(scanned[i].n_items > 1 ) { get_long(&isRing, scanned[i].list[1]); } else { isRing=1; } break; case CHARGE: get_double(&charge, scanned[i].list[1]); break; case EMITXINPUT: /* This is really the emitx+emity, not emitx */ get_double(&emitxInput, scanned[i].list[1]); break; case EMITINPUT: get_double(&emitxInput, scanned[i].list[1]); break; case DELTAINPUT: get_double(&sigmaDeltaInput, scanned[i].list[1]); break; case LENGTH: get_double(&length, scanned[i].list[1]); length /= 1000; /* convert input length from mm to m */ break; case COUPLING: get_double(&coupling, scanned[i].list[1]); break; case EMITYINPUT: get_double(&emityInput, scanned[i].list[1]); break; case FORCECOUPLING: get_long(&force, scanned[i].list[1]); break; case PARTICLES: get_double(&particles, scanned[i].list[1]); break; case SUPERPERIOD: get_long(&superperiods, scanned[i].list[1]); break; case METHOD: get_long(&method, scanned[i].list[1]); break; case GROWTHRATESONLY: growthRatesOnly = 1; break; case SET_TARGET: if (scanned[i].n_items!=2 || !get_double(&target, scanned[i].list[1]) || target<0) bomb("invalid -target syntax", NULL); break; case RF: if (scanned[i].n_items<2) bomb("invalid -rf syntax", NULL); scanned[i].n_items--; rfVoltage = rfHarmonic = 0; if (!scanItemList(&dummyFlags, scanned[i].list+1, &scanned[i].n_items, 0, "voltage", SDDS_DOUBLE, &rfVoltage, 1, 0, "harmonic", SDDS_DOUBLE, &rfHarmonic, 1, 0, NULL) || rfVoltage<=0 || rfHarmonic<=0) bomb("invalid -rf syntax/values", "-rf=voltage=MV,harmonic=<value>"); break; case SET_ENERGY: if (scanned[i].n_items!=2) bomb("invalid -energy syntax", NULL); if (!sscanf(scanned[i].list[1], "%lf", &energy) || energy<=0) bomb("invalid -energy syntax/values", "-energy=<MeV>"); break; case SET_INTEGRATE: if (scanned[i].n_items<2) bomb("invalid -integrate syntax", NULL); integrationTurns = 0; integrationStepSize = 1; scanned[i].n_items--; if (!scanItemList(&dummyFlags, scanned[i].list+1, &scanned[i].n_items, 0, "turns", SDDS_LONG, &integrationTurns, 1, 0, "stepsize", SDDS_LONG, &integrationStepSize, 1, 0, NULL) || integrationTurns<=0 || integrationStepSize<1) bomb("invalid -integrate syntax", NULL); break; case NO_WARNING: noWarning = 1; break; default: fprintf(stderr, "Unknown option %s given", scanned[i].list[0]); exit(1); break; } } else { if (!inputfile) inputfile = scanned[i].list[0]; else if (!outputfile) outputfile = scanned[i].list[0]; else bomb("too many filenames given", NULL); } } if (charge && particles) { bomb("Options charge and particles cannot be both specified.",NULL); } if (!charge) charge = particles * e_mks; if (!particles) { /* command line input value is in units of nC */ charge /= 1e9; particles = charge/ e_mks; } if ((!coupling && !emityInput) || (coupling && emityInput)) bomb("Give -coupling or -emityInput (but not both)", NULL); if (!length && !rfVoltage) bomb("Specify either the bunch length or the rf voltage.", NULL); if (growthRatesOnly && integrationTurns) { growthRatesOnly = 0; if (!noWarning) fprintf( stdout, "*Warning* -growthRatesOnly option is incompatiable with -integrate option. The -growthRatesOnly will be disabled.\n"); } if (!growthRatesOnly && !integrationTurns && !noWarning) fprintf( stdout, "*Warning* The growth rate contribution columns in the results file will be those calculated from the equilibrium (or final) condition.\n"); if (integrationTurns && !isRing) { integrationTurns = 0; fprintf( stdout, "*Warning* -isRing=0 is incompatiable with -integrate option. The -integrate will be disabled.\n"); } if (energy && !isRing) { energy = 0; fprintf( stdout, "*Warning* you can not scale energy for linac beam. Scaling will be disabled.\n"); } /***************************************************\ * get parameter information from first input file * \***************************************************/ if (verbosity) fprintf( stdout, "Opening \"%s\" for checking presence of parameters.\n", inputfile); if (!SDDS_InitializeInput(&twissPage, inputfile)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); /* read first page of input file to get parameters I1 I2 I3 I4 I5. Check presence of first radiation integral. */ SDDS_ReadPage(&twissPage); /* parameter Type */ switch(SDDS_CheckParameter(&twissPage, "I1", NULL, SDDS_DOUBLE, verbosity?stdout:NULL)) { case SDDS_CHECK_NONEXISTENT: if (verbosity) fprintf( stdout, "\tParameter I1 not found in input file.\n"); break; case SDDS_CHECK_WRONGTYPE: SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); exitElegant(1); break; case SDDS_CHECK_OKAY: break; default: fprintf( stdout, "Unexpected result from SDDS_CheckParameter routine while checking parameter Type.\n"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); exitElegant(1); break; } if (verbosity) fprintf( stdout, "Opening \"%s\" for writing...\n", outputfile); if (!SDDS_InitializeOutput(&resultsPage, SDDS_BINARY, 1, "Intra-beam scattering rates", "Intra-beam scattering rates", outputfile)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!SDDS_TransferParameterDefinition(&resultsPage, &twissPage, "I1", NULL) || !SDDS_TransferParameterDefinition(&resultsPage, &twissPage, "I2", NULL) || !SDDS_TransferParameterDefinition(&resultsPage, &twissPage, "I3", NULL) || !SDDS_TransferParameterDefinition(&resultsPage, &twissPage, "I4", NULL) || !SDDS_TransferParameterDefinition(&resultsPage, &twissPage, "I5", NULL) || !SDDS_TransferParameterDefinition(&resultsPage, &twissPage, "pCentral", NULL) || !SDDS_TransferParameterDefinition(&resultsPage, &twissPage, "taux", NULL) || !SDDS_TransferParameterDefinition(&resultsPage, &twissPage, "tauy", NULL) || !SDDS_TransferParameterDefinition(&resultsPage, &twissPage, "taudelta", NULL) ) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (0>SDDS_DefineParameter(&resultsPage, "Superperiods", NULL, NULL, "Superperiods", NULL, SDDS_LONG, NULL) || 0>SDDS_DefineParameter(&resultsPage, "Energy", "E", "MeV", "Total Energy", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "Particles", NULL, NULL, "Particles", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "Charge", NULL, "nC", "Charge", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "PeakCurrent", "I$bp$n", "A", "Peak Current", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "RfVoltage", NULL, "MV", "Rf Voltage", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "xGrowthRateInitial", "g$bIBS,x$n", "1/s", "Initial IBS emittance growth rate in the horizontal plane", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "yGrowthRateInitial", "g$bIBS,y$n", "1/s", "Initial IBS emittance growth rate in the vertical plane", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "zGrowthRateInitial", "g$bIBS,z$n", "1/s", "Initial IBS emittance growth rate in the longitudinal plane", NULL, SDDS_DOUBLE, NULL)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (0>SDDS_DefineParameter(&resultsPage, "Convergence", NULL, NULL, "Convergence state of emittance calculations", NULL, SDDS_STRING, NULL) || 0>SDDS_DefineParameter(&resultsPage, "emitx0", "$ge$r$bx,0$n", "$gp$rm", "Equilibrium horizontal emittance with no coupling and no IBS", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "emitxInput", "$ge$r$bx,Input$n", "$gp$rm", "Initial horizontal emittance with coupling", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "emityInput", "$ge$r$by,Input$n", "$gp$rm", "Initial vertical emittance with coupling", NULL, SDDS_DOUBLE, NULL)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); /* requested equilibrium emittances or integrate emittances turn-by-turn*/ if (!growthRatesOnly) { if (0>SDDS_DefineParameter(&resultsPage, "xGrowthRate", "g$bIBS,x$n", "1/s", "IBS emittance growth rate in the horizontal plane", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "yGrowthRate", "g$bIBS,y$n", "1/s", "IBS emittance growth rate in the vertical plane", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "zGrowthRate", "g$bIBS,z$n", "1/s", "IBS emittance growth rate in the longitudinal plane", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "emitx", "$ge$r$bx$n", "$gp$rm", "Horizontal emittance with coupling and with IBS", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "emity", "$ge$r$by$n", "$gp$rm", "Vertical emittance with coupling and with IBS", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "sigmaDelta", "$gs$r$bd$n", NULL, "Relative momentum spread with IBS", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "sigmaz", "$gs$r$bz$n", "m", "Bunch length with IBS", NULL, SDDS_DOUBLE, NULL)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (0>SDDS_DefineParameter(&resultsPage, "sigmaDelta0", "$gs$r$bd,0$n", NULL, "Equilibrium relative momentum spread without IBS", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "sigmaDeltaInput", "$gs$r$bd,0$n", NULL, "Initial relative momentum spread", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "sigmaz0", "$gs$r$bz,0$n", "m", "Bunch length without IBS", NULL, SDDS_DOUBLE, NULL)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (verbosity) fprintf( stdout, "Opening for reading \"%s\"\n", inputfile); if (!SDDS_InitializeInput(&twissPage, inputfile)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (integrationTurns) { if (SDDS_DefineColumn(&resultsPage, "ex", "$ge$r$bx$n", "$gp$rm", "Horizontal Emittance", NULL, SDDS_DOUBLE, 0)<0 || SDDS_DefineColumn(&resultsPage, "ey", "$ge$r$by$n", "$gp$rm", "Vertical Emittance", NULL, SDDS_DOUBLE, 0)<0 || SDDS_DefineColumn(&resultsPage, "el", "$ge$r$bl$n", "s", "Longitudinal Emittance", NULL, SDDS_DOUBLE, 0)<0 || SDDS_DefineColumn(&resultsPage, "Sdelta", "$gs$bd$n$r", "", "Fractional RMS Energy Spread", NULL, SDDS_DOUBLE, 0)<0 || SDDS_DefineColumn(&resultsPage, "Sz", "$gs$r$bz$n", "m", "RMS Bunch Length", NULL, SDDS_DOUBLE, 0)<0 || SDDS_DefineColumn(&resultsPage, "IBSRatex", NULL, "1/s", "Horizontal IBS Emittance Growth Rate", NULL, SDDS_DOUBLE, 0)<0 || SDDS_DefineColumn(&resultsPage, "IBSRatey", NULL, "1/s", "Vertical IBS Emittance Growth Rate", NULL, SDDS_DOUBLE, 0)<0 || SDDS_DefineColumn(&resultsPage, "IBSRatel", NULL, "1/s", "Longitudinal IBS Emittance Growth Rate", NULL, SDDS_DOUBLE, 0)<0 || SDDS_DefineColumn(&resultsPage, "Pass", NULL, NULL, NULL, NULL, SDDS_LONG, 0)<0) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); integrationPoints = integrationTurns/integrationStepSize+1; if (!(exInteg = SDDS_Malloc(sizeof(*exInteg)*integrationPoints)) || !(eyInteg = SDDS_Malloc(sizeof(*eyInteg)*integrationPoints)) || !(elInteg = SDDS_Malloc(sizeof(*elInteg)*integrationPoints)) || !(SdeltaInteg = SDDS_Malloc(sizeof(*SdeltaInteg)*integrationPoints)) || !(SzInteg = SDDS_Malloc(sizeof(*SzInteg)*integrationPoints)) || !(xRateInteg = SDDS_Malloc(sizeof(*xRateInteg)*integrationPoints)) || !(yRateInteg = SDDS_Malloc(sizeof(*yRateInteg)*integrationPoints)) || !(zRateInteg = SDDS_Malloc(sizeof(*zRateInteg)*integrationPoints)) || !(passInteg = SDDS_Malloc(sizeof(*passInteg)*integrationPoints))) bomb("memory allocation failure (integration arrays)", NULL); } else { if (SDDS_DefineColumn(&resultsPage, "s", NULL, "m", "Position", NULL, SDDS_DOUBLE, 0)<0 || SDDS_DefineColumn(&resultsPage, "dIBSRatex", NULL, "1/s", "Local Horizontal IBS Emittance Growth Rate", NULL, SDDS_DOUBLE, 0)<0 || SDDS_DefineColumn(&resultsPage, "dIBSRatey", NULL, "1/s", "Local Vertical IBS Emittance Growth Rate", NULL, SDDS_DOUBLE, 0)<0 || SDDS_DefineColumn(&resultsPage, "dIBSRatel", NULL, "1/s", "Local Longitudinal IBS Emittance Growth Rate", NULL, SDDS_DOUBLE, 0)<0) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_WriteLayout(&resultsPage) ) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); while(SDDS_ReadPage(&twissPage)>0) { if (!SDDS_GetParameters(&twissPage, "pCentral", &pCentral0, "I1", &I1, "I2", &I2, "I3", &I3, "I4", &I4, "I5", &I5, "taux", &taux, "tauy", &tauy, "taudelta", &taudelta, "alphac", &alphac, "U0", &U0, NULL) ) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); EMeV = sqrt(sqr(pCentral0) + 1) * me_mev; elements = SDDS_CountRowsOfInterest(&twissPage); s = SDDS_GetColumnInDoubles(&twissPage, "s"); pCentral = SDDS_GetColumnInDoubles(&twissPage, "pCentral0"); circumference = s[elements-1]*superperiods; U0 *= superperiods; if (energy!=0) { /* scale to new energy */ pCentral0 = sqrt(sqr(energy/me_mev)-1); taux /= ipow(energy/EMeV, 3); tauy /= ipow(energy/EMeV, 3); taudelta /= ipow(energy/EMeV, 3); U0 *= ipow(energy/EMeV, 4); for (i=0; i<elements; i++) pCentral[i] = pCentral0; EMeV = energy; } if (!length && U0>rfVoltage) bomb("energy loss per turn is greater than rf voltage", NULL); betax = SDDS_GetColumnInDoubles(&twissPage, "betax"); betay = SDDS_GetColumnInDoubles(&twissPage, "betay"); alphax = SDDS_GetColumnInDoubles(&twissPage, "alphax"); alphay = SDDS_GetColumnInDoubles(&twissPage, "alphay"); etax = SDDS_GetColumnInDoubles(&twissPage, "etax"); etaxp = SDDS_GetColumnInDoubles(&twissPage, "etaxp"); etay = SDDS_GetColumnInDoubles(&twissPage, "etay"); etayp = SDDS_GetColumnInDoubles(&twissPage, "etayp"); /* emitx0 and sigmaDelta0 are unperturbed quantities (i.e. no coupling and no IBS) that zibs requires to internally calculate the quantum excitation. (zibs doesn't use the radiation integrals but should!) */ emitx0 = 55.0/ (32.*sqrt(3.)) * hbar_mks * sqr(pCentral0)/ (me_mks * c_mks) * I5 / (I2 - I4); sigmaDelta0 = sqrt(55.0/ (32.*sqrt(3.)) * hbar_mks * sqr(pCentral0)/ (me_mks * c_mks) * I3 / (2 * I2 + I4)); /* use unperturbed quantities in no input supplied. */ if (!sigmaDeltaInput) sigmaDeltaInput = sigmaDelta0; if (!emitxInput) emitxInput = emitx0/ ( 1 + coupling); else /* The emitxInput value is really emit=emitx+emity */ emitxInput = emitxInput/ ( 1 + coupling); if (!emityInput) emityInput = emitxInput * coupling; else coupling = emityInput/emityInput; sigmaDelta = sigmaDeltaInput; if (length) sigmaz0 = length; else { /* compute length in m from rf voltage, energy spread, etc */ sigmaz0 = circumference*sigmaDelta* sqrt(alphac*EMeV/(PIx2*rfHarmonic*sqrt(sqr(rfVoltage)-sqr(U0)))); } sigmaz = sigmaz0; emity = emityInput; emitx = emitxInput; if (integrationPoints) { IBSIntegrate(exInteg, eyInteg, elInteg, passInteg, SdeltaInteg, SzInteg, xRateInteg, yRateInteg, zRateInteg, integrationTurns, integrationStepSize, pCentral0, emitx, emity, sigmaDelta, sigmaz, particles, emitx0, sigmaDelta0, 2./taux, 2./tauy, 2./taudelta, coupling, s, pCentral, betax, alphax, betay, alphay, etax, etaxp, etay, etayp, elements, superperiods, verbosity, isRing, force); } else { if (!(xRateVsS = SDDS_Realloc(xRateVsS, sizeof(*xRateVsS)*elements)) || !(yRateVsS = SDDS_Realloc(yRateVsS, sizeof(*yRateVsS)*elements)) || !(zRateVsS = SDDS_Realloc(zRateVsS, sizeof(*zRateVsS)*elements)) ) bomb("memory allocation failure", NULL); } /* This call is to get the initial growth rates for writing to results file. This applies for any running option selected in the commandline */ IBSRate(particles, elements, superperiods, verbosity, isRing, emitx, emity, sigmaDelta, sigmaz, s, pCentral, betax, alphax, betay, alphay, etax, etaxp, etay, etayp, NULL, NULL, NULL, &xGrowthRateInitial, &yGrowthRateInitial, &zGrowthRateInitial, 0); /* iterating for equilibrium emittances and final growth rates */ if (!integrationTurns && !growthRatesOnly && isRing) { if (verbosity > 1) { fprintf (stdout, "Starting values:\nemitx: %10.5g sigmaDelta %10.5g.\n", emitx, sigmaDelta); } emitxOld = emitx; sigmaDeltaOld = sigmaDelta; xGuess = (double*) malloc(sizeof(double)*dimensions); dxGuess = (double*) malloc(sizeof(double)*dimensions); xLowerLimit = (double*) malloc(sizeof(double)*dimensions); xUpperLimit = (double*) malloc(sizeof(double)*dimensions); disable = (short*) malloc(sizeof(short)*dimensions); xGuess[0] = MAX(emitx, emitx0/ (1 + coupling)); xGuess[1] = MAX(sigmaDelta, sigmaDelta0); dxGuess[0] = emitx * 0.1; dxGuess[1] = sigmaDelta * 0.1; xLowerLimit[0] = emitx0/ (1 + coupling); xLowerLimit[1] = sigmaDelta0; xUpperLimit[0] = emitx0/ (1 + coupling) * 200; xUpperLimit[1] = MIN(sigmaDelta0 * 100, 1.0); /* assign other variables to array which are not supoosed to be varied by simplex minimization */ xGuess[2] = pCentral0; xGuess[3] = emity; xGuess[4] = sigmaz0; xGuess[5] = particles; xGuess[6] = emitx0; xGuess[7] = sigmaDelta0; xGuess[8] = taux; xGuess[9] = tauy; xGuess[10] = taudelta; xGuess[11] = coupling; xGuess[12] = elements; xGuess[13] = superperiods; xGuess[14] = verbosity; xLowerLimit[2] = pCentral0; xLowerLimit[3] = emity; xLowerLimit[4] = sigmaz0; xLowerLimit[5] = particles; xLowerLimit[6] = emitx0; xLowerLimit[7] = sigmaDelta0; xLowerLimit[8] = taux; xLowerLimit[9] = tauy; xLowerLimit[10] = taudelta; xLowerLimit[11] = coupling; xLowerLimit[12] = elements; xLowerLimit[13] = superperiods; xLowerLimit[14] = verbosity; xUpperLimit[2] = pCentral0; xUpperLimit[3] = emity; xUpperLimit[4] = sigmaz0; xUpperLimit[5] = particles; xUpperLimit[6] = emitx0; xUpperLimit[7] = sigmaDelta0; xUpperLimit[8] = taux; xUpperLimit[9] = tauy; xUpperLimit[10] = taudelta; xUpperLimit[11] = coupling; xUpperLimit[12] = elements; xUpperLimit[13] = superperiods; xUpperLimit[14] = verbosity; disable[0] = 0; disable[1] = 0; for (i=2 ; i<dimensions ; i++) { dxGuess[i] = 0.0; disable[i] = 1; } if (verbosity) { fprintf( stdout, "Doing simplex minimization...\n"); } simplexMin( &yReturn, xGuess, dxGuess, xLowerLimit, xUpperLimit, disable, dimensions, target, target/100.0, IBSequations, verbosity?IBSsimplexReport:NULL, maxEvaluations, maxPasses, 12, 3.0, 1.0, 0); /* final answers */ emitx = xGuess[0]; sigmaDelta = xGuess[1]; emity = emitx * coupling; sigmaz = sigmaz0 * (sigmaDelta/ sigmaDelta0); } /* calculate growth rates contributions at equilibrium or just one time (-growthRateOnly option) */ if (!integrationPoints) { IBSRate(particles, elements, superperiods, verbosity, isRing, emitx, emity, sigmaDelta, sigmaz, s, pCentral, betax, alphax, betay, alphay, etax, etaxp, etay, etayp, xRateVsS, yRateVsS, zRateVsS, &xGrowthRate, &yGrowthRate, &zGrowthRate, 0); } else { /* final growth rates and emittances after integration */ xGrowthRate = xRateInteg[integrationPoints - 1] ; yGrowthRate = yRateInteg[integrationPoints - 1] ; zGrowthRate = zRateInteg[integrationPoints - 1] ; emitx = exInteg[integrationPoints - 1] ; emity = eyInteg[integrationPoints - 1] ; sigmaDelta = SdeltaInteg[integrationPoints - 1] ; sigmaz = SzInteg[integrationPoints - 1] ; } converged = 1; if (0>SDDS_StartPage(&resultsPage, integrationPoints?integrationPoints:elements) || !SDDS_SetParameters(&resultsPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "Convergence", converged?"Emittance converged":"Emittance did not converge", "pCentral", pCentral0, "RfVoltage", rfVoltage, "I1", I1, "I2", I2, "I3", I3, "I4", I4, "I5", I5, "taux", taux, "tauy", tauy, "taudelta", taudelta, "Energy", EMeV, "Particles", particles, "Charge", (1e9 * charge), "PeakCurrent", (charge*c_mks/(sqrt(2*PI)*sigmaz)), "Superperiods", superperiods, "emitx0", emitx0, "emitxInput", emitxInput, "emityInput", emityInput, "xGrowthRateInitial", xGrowthRateInitial, "yGrowthRateInitial", yGrowthRateInitial, "zGrowthRateInitial", zGrowthRateInitial, "sigmaDeltaInput", sigmaDeltaInput, "sigmaDelta0", sigmaDelta0, "sigmaz0", sigmaz0, NULL) || (!growthRatesOnly && !SDDS_SetParameters(&resultsPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "xGrowthRate", xGrowthRate, "yGrowthRate", yGrowthRate, "zGrowthRate", zGrowthRate, "emitx", emitx, "emity", emity, "sigmaDelta", sigmaDelta, "sigmaz", sigmaz, NULL)) || (integrationPoints && (!SDDS_SetColumn(&resultsPage, SDDS_SET_BY_NAME, exInteg, integrationPoints, "ex") || !SDDS_SetColumn(&resultsPage, SDDS_SET_BY_NAME, eyInteg, integrationPoints, "ey") || !SDDS_SetColumn(&resultsPage, SDDS_SET_BY_NAME, elInteg, integrationPoints, "el") || !SDDS_SetColumn(&resultsPage, SDDS_SET_BY_NAME, SdeltaInteg, integrationPoints, "Sdelta") || !SDDS_SetColumn(&resultsPage, SDDS_SET_BY_NAME, SzInteg, integrationPoints, "Sz") || !SDDS_SetColumn(&resultsPage, SDDS_SET_BY_NAME, xRateInteg, integrationPoints, "IBSRatex") || !SDDS_SetColumn(&resultsPage, SDDS_SET_BY_NAME, yRateInteg, integrationPoints, "IBSRatey") || !SDDS_SetColumn(&resultsPage, SDDS_SET_BY_NAME, zRateInteg, integrationPoints, "IBSRatel") || !SDDS_SetColumn(&resultsPage, SDDS_SET_BY_NAME, passInteg, integrationPoints, "Pass"))) || (!integrationPoints && (!SDDS_SetColumn(&resultsPage, SDDS_SET_BY_NAME, s, elements, "s") || !SDDS_SetColumn(&resultsPage, SDDS_SET_BY_NAME, xRateVsS, elements, "dIBSRatex") || !SDDS_SetColumn(&resultsPage, SDDS_SET_BY_NAME, yRateVsS, elements, "dIBSRatey") || !SDDS_SetColumn(&resultsPage, SDDS_SET_BY_NAME, zRateVsS, elements, "dIBSRatel"))) || !SDDS_WritePage(&resultsPage)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_Terminate(&twissPage) || !SDDS_Terminate(&resultsPage)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); 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) { char *input, *output; char **copyColumnName, **usersCopyColumnName; GROUPS *group; int32_t copyColumns; long usersCopyColumns, groups; long iArg, i, rows, readCode, items; unsigned long flags, pipeFlags; SCANNED_ARG *scArg; SDDS_DATASET SDDSin, SDDSout; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&scArg, argc, argv); if (argc<2) bomb(USAGE, NULL); output = input = NULL; flags = pipeFlags = 0; group = NULL; copyColumnName = usersCopyColumnName = NULL; usersCopyColumns = copyColumns = groups = 0; for (iArg=1; iArg<argc; iArg++) { if (scArg[iArg].arg_type==OPTION) { /* process options here */ switch (match_string(scArg[iArg].list[0], option, N_OPTIONS, 0)) { case SET_PIPE: if (!processPipeOption(scArg[iArg].list+1, scArg[iArg].n_items-1, &pipeFlags)) SDDS_Bomb("invalid -pipe syntax"); break; case SET_GROUP: if ((items = scArg[iArg].n_items-1)<2) SDDS_Bomb("invalid -group syntax"); if (!(group = SDDS_Realloc(group, sizeof(*group)*(groups+1))) || !SDDS_CopyString(&group[groups].newName, scArg[iArg].list[1]) || !(group[groups].usersOldName = SDDS_Malloc(sizeof(*group[groups].usersOldName)* (group[groups].usersOldNames=items-1))) || !SDDS_CopyStringArray(group[groups].usersOldName, scArg[iArg].list+2, group[groups].usersOldNames)) SDDS_Bomb("memory allocation failure"); group[groups].oldName = NULL; group[groups].oldNames = 0; groups++; break; case SET_COPY: if (usersCopyColumns) SDDS_Bomb("give -copy only once"); if ((usersCopyColumns=scArg[iArg].n_items-1)<1) SDDS_Bomb("invalid -copy syntax"); if (!(usersCopyColumnName = SDDS_Malloc(sizeof(*usersCopyColumnName)*usersCopyColumns)) || !SDDS_CopyStringArray(usersCopyColumnName, scArg[iArg].list+1, usersCopyColumns)) SDDS_Bomb("memory allocation failure"); break; default: fprintf(stderr, "error: unknown/ambiguous option: %s\n", scArg[iArg].list[0]); exit(1); break; } } else { if (!input) input = scArg[iArg].list[0]; else if (!output) output = scArg[iArg].list[0]; else SDDS_Bomb("too many filenames seen"); } } if (groups==0) SDDS_Bomb("no groups defined"); processFilenames("sddsseparate", &input, &output, pipeFlags, 0, NULL); if (!SDDS_InitializeInput(&SDDSin, input)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (usersCopyColumns) { SDDS_SetColumnFlags(&SDDSin, 0); for (i=0; i<usersCopyColumns; i++) SDDS_SetColumnsOfInterest(&SDDSin, SDDS_MATCH_STRING, usersCopyColumnName[i], SDDS_OR); if (!(copyColumnName = SDDS_GetColumnNames(&SDDSin, ©Columns)) || copyColumns==0) SDDS_Bomb("no match for copy columns"); } for (i=0; i<groups; i++) { long j, type=0; SDDS_SetColumnFlags(&SDDSin, 0); for (j=0; j<group[i].usersOldNames; j++) SDDS_SetColumnsOfInterest(&SDDSin, SDDS_MATCH_STRING, group[i].usersOldName[j], SDDS_OR); if (!(group[i].oldName = SDDS_GetColumnNames(&SDDSin, &group[i].oldNames))) { fprintf(stderr, "No match for group %s (sddsseparate)\n", group[i].newName); exit(1); } if (i && group[i-1].oldNames!=group[i].oldNames) { fprintf(stderr, "Group %s comprises %" PRId32 " columns, whereas the last group comprises %" PRId32 " (sddsseparate)\n", group[i].newName, group[i].oldNames, group[i-1].oldNames); exit(1); } type = SDDS_GetColumnType(&SDDSin, SDDS_GetColumnIndex(&SDDSin, group[i].oldName[0])); for (j=1; j<group[i].oldNames; j++) { if (type != SDDS_GetColumnType(&SDDSin, SDDS_GetColumnIndex(&SDDSin, group[i].oldName[j]))) { fprintf(stderr, "Inconsistent data types in group %s (sddsseparate)\n", group[i].newName); fprintf(stderr, "First inconsistent column is %s\n", group[i].oldName[j]); exit(1); } } } if (!SDDS_InitializeOutput(&SDDSout, SDDS_BINARY, 0, NULL, NULL, output) || !SDDS_TransferAllParameterDefinitions(&SDDSout, &SDDSin, 0)) SDDS_Bomb("problem initializing output file"); for (i=0; i<copyColumns; i++) if (!SDDS_TransferColumnDefinition(&SDDSout, &SDDSin, copyColumnName[i], NULL)) SDDS_Bomb("problem transferring copy column definitions to output file"); for (i=0; i<groups; i++) { char *name; if (!SDDS_TransferColumnDefinition(&SDDSout, &SDDSin, group[i].oldName[0], group[i].newName)) { fprintf(stderr, "Problem transferring column %s as %s to output file (sddsseparate)\n", group[i].oldName[0], group[i].newName); exit(1); } if (!(group[i].parameterName = SDDS_Malloc(sizeof(*name)*(strlen(group[i].newName)+100)))) SDDS_Bomb("memory allocation failure"); sprintf(group[i].parameterName, "%sSourceColumn", group[i].newName); if (!SDDS_DefineSimpleParameter(&SDDSout, group[i].parameterName, NULL, SDDS_STRING)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_WriteLayout(&SDDSout)) SDDS_Bomb("problem writing layout to output file"); while ((readCode=SDDS_ReadPage(&SDDSin))>0) { if ((rows = SDDS_CountRowsOfInterest(&SDDSin))<0) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!rows) continue; for (i=0; i<group[0].oldNames; i++) { long ic, ig; if (!SDDS_StartPage(&SDDSout, rows) || !SDDS_CopyParameters(&SDDSout, &SDDSin)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (ic=0; ic<copyColumns; ic++) { void *data; if (!(data = SDDS_GetInternalColumn(&SDDSin, copyColumnName[ic])) || !SDDS_SetColumn(&SDDSout, SDDS_SET_BY_NAME, data, rows, copyColumnName[ic])) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } for (ig=0; ig<groups; ig++) { void *data; if (!SDDS_SetParameters(&SDDSout, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, group[ig].parameterName, group[ig].oldName[i], NULL) || !(data = SDDS_GetInternalColumn(&SDDSin, group[ig].oldName[i])) || !SDDS_SetColumn(&SDDSout, SDDS_SET_BY_NAME, data, rows, group[ig].newName)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_WritePage(&SDDSout)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } if (!SDDS_Terminate(&SDDSin)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (!SDDS_Terminate(&SDDSout)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } return 0; }
int main(int argc, char **argv) { 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; }
void ReadInputFile(char *inputFile, char *xCol, char *yCol, char *zCol, char *stdCol, long *pages, long **rows, double ***x, double ***y, double ***z, double ***std, SDDS_DATASET *SDDS_in) { int32_t rows1=0; long pages0=0, std_exist=0; *rows = NULL; if (!SDDS_InitializeInput(SDDS_in, inputFile)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (SDDS_CheckColumn(SDDS_in, xCol, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK) { fprintf(stderr,"x column - %s does not exist!\n", xCol); exit(1); } if (SDDS_CheckColumn(SDDS_in, yCol, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK) { fprintf(stderr,"y column - %s does not exist!\n", yCol); exit(1); } if (SDDS_CheckColumn(SDDS_in, zCol, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK) { fprintf(stderr,"z column - %s does not exist!\n", zCol); exit(1); } if (SDDS_CheckColumn(SDDS_in, stdCol, NULL, SDDS_ANY_NUMERIC_TYPE, NULL)==SDDS_CHECK_OK) std_exist = 1; while (SDDS_ReadPage(SDDS_in)>0) { rows1 = 0; rows1=SDDS_CountRowsOfInterest(SDDS_in); if (!rows1) continue; *rows = SDDS_Realloc(*rows, sizeof(**rows)*(pages0+1)); (*rows)[pages0] = rows1; if (!(*x = SDDS_Realloc(*x, sizeof(**x)*(pages0+1))) || !(*y = SDDS_Realloc(*y, sizeof(**y)*(pages0+1))) || !(*z = SDDS_Realloc(*z, sizeof(**z)*(pages0+1)))) { fprintf(stderr, "Memory allocation error.\n"); exit(1); } if (std_exist) { if (!(*std = SDDS_Realloc(*std, sizeof(**std)*(pages0+1)))) { fprintf(stderr, "Memory allocation error.\n"); exit(1); } (*std)[pages0] = NULL; } (*x)[pages0] = (*y)[pages0] = (*z)[pages0] = NULL; if (!((*x)[pages0]=(double*)SDDS_GetColumnInDoubles(SDDS_in, xCol)) || !((*y)[pages0]=(double*)SDDS_GetColumnInDoubles(SDDS_in, yCol)) || !((*z)[pages0]=(double*)SDDS_GetColumnInDoubles(SDDS_in, zCol))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (std_exist && !((*std)[pages0]=(double*)SDDS_GetColumnInDoubles(SDDS_in, stdCol))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); pages0 ++; } if (!pages0) { fprintf(stderr, "No data found in the input file.\n"); exit(1); } *pages = pages0; return; }
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 ReadPointFile(char *inputFile, char *xCol, char *yCol, long *pages, OUT_POINT **out_point) { SDDS_DATASET SDDS_in; int32_t rows1=0; long pages0=0, std_exist=0; double *x, *y, dx, dy, xmin, xmax, firstx, firsty, ymin, ymax; int nx, ny, i; if (!SDDS_InitializeInput(&SDDS_in, inputFile)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (SDDS_CheckColumn(&SDDS_in, xCol, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK) { fprintf(stderr,"x column - %s does not exist!\n", xCol); exit(1); } if (SDDS_CheckColumn(&SDDS_in, yCol, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK) { fprintf(stderr,"y column - %s does not exist!\n", yCol); exit(1); } while (SDDS_ReadPage(&SDDS_in)>0) { rows1 = 0; rows1=SDDS_CountRowsOfInterest(&SDDS_in); if (!rows1) continue; *out_point = SDDS_Realloc(*out_point, sizeof(**out_point)*(pages0+1)); x = y = NULL; (*out_point)[pages0].pout = malloc(sizeof(point)*rows1); (*out_point)[pages0].nout = rows1; if (!(x=(double*)SDDS_GetColumnInDoubles(&SDDS_in, xCol)) || !(y=(double*)SDDS_GetColumnInDoubles(&SDDS_in, yCol))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); xmin = xmax = firstx = x[0]; ymin = ymax = firsty = y[0]; nx = ny = 0; for (i=0; i<rows1; i++) { (*out_point)[pages0].pout[i].x = x[i]; (*out_point)[pages0].pout[i].y = y[i]; if (x[i]<xmin) xmin = x[i]; else if (x[i]>xmax) xmax = x[i]; if (x[i]==firstx) ny ++; if (y[i]<ymin) ymin = y[i]; else if (y[i]>ymax) ymax = y[i]; if (y[i]==firsty) nx ++; } free(x); free(y); (*out_point)[pages0].xmin = xmin; (*out_point)[pages0].xmax = xmax; (*out_point)[pages0].ymin = ymin; (*out_point)[pages0].ymax = ymax; (*out_point)[pages0].nx = nx; (*out_point)[pages0].ny = ny; if (nx>1) (*out_point)[pages0].dx = (xmax - xmin)/(nx-1); else (*out_point)[pages0].dx = 0; if (ny>1) (*out_point)[pages0].dx = (ymax - ymin)/(ny-1); else (*out_point)[pages0].dy = 0; pages0 ++; } if (!SDDS_Terminate(&SDDS_in)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!pages0) { fprintf(stderr, "No data found in the points file.\n"); exit(1); } *pages = pages0; return; }
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) { STAT_DEFINITION *stat; long stats; STAT_REQUEST *request; long requests, count; SCANNED_ARG *scanned; SDDS_DATASET inData, outData; int32_t power; long i_arg, code, iStat, rows, tmpFileUsed, iColumn, row, posColIndex; long noWarnings, maxSourceColumns; char *input, *output, *positionColumn, **posColumnName; double **inputData, *outputData, value1, value2, topLimit, bottomLimit; unsigned long pipeFlags, scanFlags, majorOrderFlag; char s[100]; double *statWorkArray; double quartilePoint[2] = {25.0, 75.0 }, quartileResult[2]; double decilePoint[2] = {10.0, 90.0 }, decileResult[2]; double percent; short columnMajorOrder=-1; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&scanned, argc, argv); if (argc<2) { bomb("too few arguments", USAGE); } posColumnName = NULL; input = output = positionColumn = NULL; stat = NULL; request = NULL; stats = requests = pipeFlags = 0; noWarnings = 0; outputData = NULL; statWorkArray = NULL; for (i_arg=1; i_arg<argc; i_arg++) { scanFlags = 0; if (scanned[i_arg].arg_type==OPTION) { /* process options here */ switch (code=match_string(scanned[i_arg].list[0], option, N_OPTIONS, 0)) { case SET_MAXIMUM: case SET_MINIMUM: case SET_MEAN: case SET_MEDIAN: case SET_STANDARDDEVIATION: case SET_RMS: case SET_SIGMA: case SET_MAD: case SET_COUNT: case SET_DRANGE: case SET_QRANGE: case SET_SMALLEST: case SET_LARGEST: case SET_SPREAD: if (scanned[i_arg].n_items<3) { fprintf(stderr, "error: invalid -%s syntax\n", option[code]); exit(1); } if (!scanItemList(&scanFlags, scanned[i_arg].list, &scanned[i_arg].n_items, SCANITEMLIST_UNKNOWN_VALUE_OK|SCANITEMLIST_REMOVE_USED_ITEMS| SCANITEMLIST_IGNORE_VALUELESS, "positionColumn", SDDS_STRING, &positionColumn, 1, POSITIONCOLUMN_GIVEN, "toplimit", SDDS_DOUBLE, &topLimit, 1, TOPLIMIT_GIVEN, "bottomlimit", SDDS_DOUBLE, &bottomLimit, 1, BOTTOMLIMIT_GIVEN, NULL)) { sprintf(s, "invalid -%s syntax", scanned[i_arg].list[0]); SDDS_Bomb(s); } requests = addStatRequests(&request, requests, scanned[i_arg].list+1, scanned[i_arg].n_items-1, code, scanFlags); request[requests-1].topLimit = topLimit; request[requests-1].bottomLimit = bottomLimit; if (positionColumn) { if (code==SET_MAXIMUM || code==SET_MINIMUM || code==SET_LARGEST || code==SET_SMALLEST) SDDS_CopyString(&request[requests-1].positionColumn, positionColumn); free(positionColumn); positionColumn = NULL; } break; case SET_PERCENTILE: if (scanned[i_arg].n_items<3) { fprintf(stderr, "error: invalid -%s syntax\n", option[code]); exit(1); } if (!scanItemList(&scanFlags, scanned[i_arg].list, &scanned[i_arg].n_items, SCANITEMLIST_UNKNOWN_VALUE_OK|SCANITEMLIST_REMOVE_USED_ITEMS| SCANITEMLIST_IGNORE_VALUELESS, "value", SDDS_DOUBLE, &percent, 1, PERCENT_GIVEN, "toplimit", SDDS_DOUBLE, &topLimit, 1, TOPLIMIT_GIVEN, "bottomlimit", SDDS_DOUBLE, &bottomLimit, 1, BOTTOMLIMIT_GIVEN, NULL) || !(scanFlags&PERCENT_GIVEN) || percent<=0 || percent>=100) SDDS_Bomb("invalid -percentile syntax"); requests = addStatRequests(&request, requests, scanned[i_arg].list+1, scanned[i_arg].n_items-1, code, scanFlags); request[requests-1].percent = percent; request[requests-1].topLimit = topLimit; request[requests-1].bottomLimit = bottomLimit; break; case SET_SUM: if (scanned[i_arg].n_items<3) { fprintf(stderr, "error: invalid -%s syntax\n", option[code]); exit(1); } power = 1; if (!scanItemList(&scanFlags, scanned[i_arg].list, &scanned[i_arg].n_items, SCANITEMLIST_UNKNOWN_VALUE_OK|SCANITEMLIST_REMOVE_USED_ITEMS| SCANITEMLIST_IGNORE_VALUELESS, "power", SDDS_LONG, &power, 1, 0, "toplimit", SDDS_DOUBLE, &topLimit, 1, TOPLIMIT_GIVEN, "bottomlimit", SDDS_DOUBLE, &bottomLimit, 1, BOTTOMLIMIT_GIVEN, NULL)) SDDS_Bomb("invalid -sum syntax"); requests = addStatRequests(&request, requests, scanned[i_arg].list+1, scanned[i_arg].n_items-1, code, scanFlags); request[requests-1].sumPower = power; request[requests-1].topLimit = topLimit; request[requests-1].bottomLimit = bottomLimit; break; case SET_PIPE: if (!processPipeOption(scanned[i_arg].list+1, scanned[i_arg].n_items-1, &pipeFlags)) SDDS_Bomb("invalid -pipe syntax"); break; case SET_NOWARNINGS: noWarnings = 1; break; case SET_MAJOR_ORDER: majorOrderFlag=0; scanned[i_arg].n_items --; if (scanned[i_arg].n_items>0 && (!scanItemList(&majorOrderFlag, scanned[i_arg].list+1, &scanned[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; default: fprintf(stderr, "error: unknown option '%s' given\n", scanned[i_arg].list[0]); exit(1); break; } } else { /* argument is filename */ if (!input) input = scanned[i_arg].list[0]; else if (!output) output = scanned[i_arg].list[0]; else SDDS_Bomb("too many filenames seen"); } } processFilenames("sddsrowstats", &input, &output, pipeFlags, noWarnings, &tmpFileUsed); if (!requests) SDDS_Bomb("no statistics requested"); if (!SDDS_InitializeInput(&inData, input)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!(stat=compileStatDefinitions(&inData, request, requests, &stats, noWarnings))) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (stats<0) SDDS_Bomb("No valid statistics requests."); for (iStat=maxSourceColumns=0; iStat<stats; iStat++) { if (stat[iStat].sourceColumns>maxSourceColumns) maxSourceColumns = stat[iStat].sourceColumns; } if (!(statWorkArray=malloc(sizeof(*statWorkArray)*maxSourceColumns))) SDDS_Bomb("allocation failure (statWorkArray)"); if (!setupOutputFile(&outData, output, &inData, stat, stats)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } inputData = NULL; while ((code=SDDS_ReadPage(&inData))>0) { if (!SDDS_CopyPage(&outData, &inData)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if ((rows = SDDS_CountRowsOfInterest(&inData))) { if (!(outputData = (double*)malloc(sizeof(*outputData)*rows))) SDDS_Bomb("memory allocation failure"); if (!(posColumnName = (char**)malloc(sizeof(*posColumnName)*rows))) SDDS_Bomb("memory allocation failure"); for (iStat=0; iStat<stats; iStat++) { if (!(inputData = (double**)malloc(sizeof(*inputData)*stat[iStat].sourceColumns))) SDDS_Bomb("memory allocation failure"); for (iColumn=0; iColumn<stat[iStat].sourceColumns; iColumn++) { if (!(inputData[iColumn] = SDDS_GetColumnInDoubles(&inData, stat[iStat].sourceColumn[iColumn]))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } for (row=0; row<rows; row++) outputData[row] = DBL_MAX; switch (stat[iStat].optionCode) { case SET_MINIMUM: for (row=0; row<rows; row++) { value1 = DBL_MAX; posColIndex = 0; posColumnName[row] = NULL; for (iColumn=0; iColumn<stat[iStat].sourceColumns; iColumn++) { if (stat[iStat].flags&TOPLIMIT_GIVEN && inputData[iColumn][row]>stat[iStat].topLimit) continue; if (stat[iStat].flags&BOTTOMLIMIT_GIVEN && inputData[iColumn][row]<stat[iStat].bottomLimit) continue; if (inputData[iColumn][row]<value1) { value1 = inputData[iColumn][row]; posColIndex = iColumn; } } outputData[row] = value1; if (stat[iStat].positionColumn) posColumnName[row] = stat[iStat].sourceColumn[posColIndex]; } break; case SET_MAXIMUM: for (row=0; row<rows; row++) { posColIndex = 0; value1 = -DBL_MAX; posColumnName[row] = NULL; for (iColumn=0; iColumn<stat[iStat].sourceColumns; iColumn++) { if (stat[iStat].flags&TOPLIMIT_GIVEN && inputData[iColumn][row]>stat[iStat].topLimit) continue; if (stat[iStat].flags&BOTTOMLIMIT_GIVEN && inputData[iColumn][row]<stat[iStat].bottomLimit) continue; if (inputData[iColumn][row]>value1) { posColIndex = iColumn; value1 = inputData[iColumn][row]; } } outputData[row] = value1; if (stat[iStat].positionColumn) posColumnName[row] = stat[iStat].sourceColumn[posColIndex]; } break; case SET_MEAN: for (row=0; row<rows; row++) { value1 = 0; count = 0; for (iColumn=0; iColumn<stat[iStat].sourceColumns; iColumn++) { if (stat[iStat].flags&TOPLIMIT_GIVEN && inputData[iColumn][row]>stat[iStat].topLimit) continue; if (stat[iStat].flags&BOTTOMLIMIT_GIVEN && inputData[iColumn][row]<stat[iStat].bottomLimit) continue; value1 += inputData[iColumn][row]; count ++; } if (count) outputData[row] = value1/count; } break; case SET_MEDIAN: for (row=0; row<rows; row++) { for (iColumn=count=0; iColumn<stat[iStat].sourceColumns; iColumn++) { if (stat[iStat].flags&TOPLIMIT_GIVEN && inputData[iColumn][row]>stat[iStat].topLimit) continue; if (stat[iStat].flags&BOTTOMLIMIT_GIVEN && inputData[iColumn][row]<stat[iStat].bottomLimit) continue; statWorkArray[count] = inputData[iColumn][row]; count++; } if (count) compute_median(outputData+row, statWorkArray, count); } break; case SET_STANDARDDEVIATION: for (row=0; row<rows; row++) { value1 = 0; value2 = 0; count = 0; for (iColumn=0; iColumn<stat[iStat].sourceColumns; iColumn++) { if (stat[iStat].flags&TOPLIMIT_GIVEN && inputData[iColumn][row]>stat[iStat].topLimit) continue; if (stat[iStat].flags&BOTTOMLIMIT_GIVEN && inputData[iColumn][row]<stat[iStat].bottomLimit) continue; value1 += inputData[iColumn][row]; value2 += inputData[iColumn][row]*inputData[iColumn][row]; count ++; } if (count>1) { if ((value1 = value2/count - sqr(value1/count))<=0) outputData[row] = 0; else outputData[row] = sqrt(value1*count/(count-1.0)); } } break; case SET_SIGMA: for (row=0; row<rows; row++) { value1 = 0; value2 = 0; count = 0; for (iColumn=0; iColumn<stat[iStat].sourceColumns; iColumn++) { if (stat[iStat].flags&TOPLIMIT_GIVEN && inputData[iColumn][row]>stat[iStat].topLimit) continue; if (stat[iStat].flags&BOTTOMLIMIT_GIVEN && inputData[iColumn][row]<stat[iStat].bottomLimit) continue; value1 += inputData[iColumn][row]; value2 += inputData[iColumn][row]*inputData[iColumn][row]; count ++; } if (count>1) { if ((value1 = value2/count - sqr(value1/count))<=0) outputData[row] = 0; else outputData[row] = sqrt(value1/(count-1.0)); } } break; case SET_RMS: for (row=0; row<rows; row++) { value1 = 0; count = 0; for (iColumn=0; iColumn<stat[iStat].sourceColumns; iColumn++) { if (stat[iStat].flags&TOPLIMIT_GIVEN && inputData[iColumn][row]>stat[iStat].topLimit) continue; if (stat[iStat].flags&BOTTOMLIMIT_GIVEN && inputData[iColumn][row]<stat[iStat].bottomLimit) continue; value1 += sqr(inputData[iColumn][row]); count ++; } if (count) outputData[row] = sqrt(value1/count); } break; case SET_SUM: for (row=0; row<rows; row++) { value1 = 0; count = 0; for (iColumn=0; iColumn<stat[iStat].sourceColumns; iColumn++) { if (stat[iStat].flags&TOPLIMIT_GIVEN && inputData[iColumn][row]>stat[iStat].topLimit) continue; if (stat[iStat].flags&BOTTOMLIMIT_GIVEN && inputData[iColumn][row]<stat[iStat].bottomLimit) continue; value1 += ipow(inputData[iColumn][row], stat[iStat].sumPower); count ++; } if (count) outputData[row] = value1; } break; case SET_COUNT: for (row=0; row<rows; row++) { count = 0; for (iColumn=0; iColumn<stat[iStat].sourceColumns; iColumn++) { if (stat[iStat].flags&TOPLIMIT_GIVEN && inputData[iColumn][row]>stat[iStat].topLimit) continue; if (stat[iStat].flags&BOTTOMLIMIT_GIVEN && inputData[iColumn][row]<stat[iStat].bottomLimit) continue; count++; } outputData[row] = count; } break; case SET_MAD: for (row=0; row<rows; row++) { for (iColumn=count=value1=0; iColumn<stat[iStat].sourceColumns; iColumn++) { if (stat[iStat].flags&TOPLIMIT_GIVEN && inputData[iColumn][row]>stat[iStat].topLimit) continue; if (stat[iStat].flags&BOTTOMLIMIT_GIVEN && inputData[iColumn][row]<stat[iStat].bottomLimit) continue; statWorkArray[count] = inputData[iColumn][row]; count++; } if (count) computeMoments(NULL, NULL, NULL, &outputData[row], statWorkArray, count); } break; case SET_DRANGE: for (row=0; row<rows; row++) { for (iColumn=count=value1=0; iColumn<stat[iStat].sourceColumns; iColumn++) { if (stat[iStat].flags&TOPLIMIT_GIVEN && inputData[iColumn][row]>stat[iStat].topLimit) continue; if (stat[iStat].flags&BOTTOMLIMIT_GIVEN && inputData[iColumn][row]<stat[iStat].bottomLimit) continue; statWorkArray[count] = inputData[iColumn][row]; count++; } if (count && compute_percentiles(decileResult, decilePoint, 2, statWorkArray, count)) outputData[row] = decileResult[1] - decileResult[0]; } break; case SET_QRANGE: for (row=0; row<rows; row++) { for (iColumn=count=value1=0; iColumn<stat[iStat].sourceColumns; iColumn++) { if (stat[iStat].flags&TOPLIMIT_GIVEN && inputData[iColumn][row]>stat[iStat].topLimit) continue; if (stat[iStat].flags&BOTTOMLIMIT_GIVEN && inputData[iColumn][row]<stat[iStat].bottomLimit) continue; statWorkArray[count] = inputData[iColumn][row]; count++; } if (count && compute_percentiles(quartileResult, quartilePoint, 2, statWorkArray, count)) outputData[row] = quartileResult[1] - quartileResult[0]; } break; case SET_SMALLEST: for (row=0; row<rows; row++) { value1 = DBL_MAX; posColIndex = 0; posColumnName[row] = NULL; for (iColumn=0; iColumn<stat[iStat].sourceColumns; iColumn++) { if (stat[iStat].flags&TOPLIMIT_GIVEN && inputData[iColumn][row]>stat[iStat].topLimit) continue; if (stat[iStat].flags&BOTTOMLIMIT_GIVEN && inputData[iColumn][row]<stat[iStat].bottomLimit) continue; if ((value2=fabs(inputData[iColumn][row]))<value1) { posColIndex = iColumn; value1 = value2; } } outputData[row] = value1; if (stat[iStat].positionColumn) posColumnName[row] = stat[iStat].sourceColumn[posColIndex]; } break; case SET_LARGEST: for (row=0; row<rows; row++) { value1 = 0; posColIndex = 0; posColumnName[row] = NULL; for (iColumn=0; iColumn<stat[iStat].sourceColumns; iColumn++) { if (stat[iStat].flags&TOPLIMIT_GIVEN && inputData[iColumn][row]>stat[iStat].topLimit) continue; if (stat[iStat].flags&BOTTOMLIMIT_GIVEN && inputData[iColumn][row]<stat[iStat].bottomLimit) continue; if ((value2=fabs(inputData[iColumn][row]))>value1) { posColIndex = iColumn; value1 = value2; } } outputData[row] = value1; if (stat[iStat].positionColumn) posColumnName[row] = stat[iStat].sourceColumn[posColIndex]; } break; case SET_SPREAD: for (row=0; row<rows; row++) { value1 = DBL_MAX; /* min */ value2 = -DBL_MAX; /* max */ for (iColumn=0; iColumn<stat[iStat].sourceColumns; iColumn++) { if (stat[iStat].flags&TOPLIMIT_GIVEN && inputData[iColumn][row]>stat[iStat].topLimit) continue; if (stat[iStat].flags&BOTTOMLIMIT_GIVEN && inputData[iColumn][row]<stat[iStat].bottomLimit) continue; if (inputData[iColumn][row]<value1) value1 = inputData[iColumn][row]; if (inputData[iColumn][row]>value2) value2 = inputData[iColumn][row]; } outputData[row] = value2-value1; } break; case SET_PERCENTILE: for (row=0; row<rows; row++) { for (iColumn=count=value1=0; iColumn<stat[iStat].sourceColumns; iColumn++) { if (stat[iStat].flags&TOPLIMIT_GIVEN && inputData[iColumn][row]>stat[iStat].topLimit) continue; if (stat[iStat].flags&BOTTOMLIMIT_GIVEN && inputData[iColumn][row]<stat[iStat].bottomLimit) continue; statWorkArray[count] = inputData[iColumn][row]; count++; } outputData[row] = HUGE_VAL; if (count) compute_percentiles(&outputData[row], &stat[iStat].percent, 1, statWorkArray, count); } break; default: SDDS_Bomb("invalid statistic code (accumulation loop)"); break; } if (!SDDS_SetColumn(&outData, SDDS_SET_BY_INDEX, outputData, rows, stat[iStat].resultIndex)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (stat[iStat].positionColumn) { if (!SDDS_SetColumn(&outData, SDDS_SET_BY_INDEX, posColumnName, rows, stat[iStat].positionColumnIndex)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } for (iColumn=0; iColumn<stat[iStat].sourceColumns; iColumn++) free(inputData[iColumn]); free(inputData); inputData = NULL; } free(outputData); outputData = NULL; free(posColumnName); posColumnName = NULL; } if (!SDDS_WritePage(&outData)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } free_scanargs(&scanned, argc); for (iStat=0; iStat<stats; iStat++) { if (stat[iStat].positionColumn) free(stat[iStat].positionColumn); for (iColumn=0; iColumn<stat[iStat].sourceColumns; iColumn++) free(stat[iStat].sourceColumn[iColumn]); free(stat[iStat].sourceColumn); } free(request); free(stat); if (statWorkArray) free(statWorkArray); if (!SDDS_Terminate(&inData) || !SDDS_Terminate(&outData)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (tmpFileUsed && !replaceFileAndBackUp(input, output)) exit(1); return 0; }