62 "Usage: sddsinteg [<input>] [<output>] [-pipe=[input][,output]]\n"
63 " -integrate=<column-name>[,<sigma-name>] ...\n"
64 " [-exclude=<column-name>[,...]]\n"
65 " -versus=<column-name>[,<sigma-name>]\n"
66 " [-mainTemplates=<item>=<string>[,...]]\n"
67 " [-errorTemplates=<item>=<string>[,...]]\n"
68 " [-copycolumns=<list of column names>]\n"
69 " [-method={trapazoid|GillMiller}]\n"
70 " [-printFinal[=bare][,stdout][,format=<string>]]\n\n"
72 " -pipe Standard SDDS pipe option.\n"
73 " -integrate Name of column to integrate, plus optional RMS error.\n"
74 " Column name may include wildcards, with error name using %%s.\n"
75 " -exclude List of column names to exclude from integration.\n"
76 " -versus Name of column to integrate against, plus optional RMS error.\n"
77 " -mainTemplates Customize main templates for name, symbol, or description.\n"
78 " -errorTemplates Customize error templates for name, symbol, or description.\n"
79 " -copycolumns Comma-separated list of columns to copy to the output.\n"
80 " -method Integration method: trapazoid (default) or GillMiller.\n"
81 " -printFinal Print the final integral value. Options:\n"
82 " bare - Print only the integral value.\n"
83 " stdout - Print to standard output.\n"
84 " format=<s> - Specify printf format string.\n\n"
85 "Program by Michael Borland. (" __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
")";
102static char *option[N_OPTIONS] = {
115void trapizoid(
double *x,
double *y,
double *sx,
double *sy, int64_t n,
double *integ,
double *error);
116long checkErrorNames(
char **yErrorName,
long nIntegrals);
117void makeSubstitutions(
char *buffer1,
char *buffer2,
char *
template,
char *nameRoot,
char *symbolRoot,
char *xName,
char *xSymbol);
118char *changeInformation(
SDDS_DATASET *SDDSout,
char *name,
char *nameRoot,
char *symbolRoot,
119 char *xName,
char *xSymbol,
char **
template,
char *newUnits);
121 char ***yOutputName,
char ***yOutputErrorName,
char ***yOutputUnits,
122 char *xName,
char *xErrorName,
char **yName,
char **yErrorName,
123 long yNames,
char **mainTemplate,
char **errorTemplate,
long methodCode,
124 short columnMajorOrder,
char **colMatch, int32_t colMatches);
126#define NORMAL_PRINTOUT 1
127#define BARE_PRINTOUT 2
128#define STDOUT_PRINTOUT 4
130#define TRAPAZOID_METHOD 0
131#define GILLMILLER_METHOD 1
134static char *methodOption[N_METHODS] = {
139int main(
int argc,
char **argv) {
140 double *xData = NULL, *yData = NULL, *xError = NULL, *yError = NULL, *integral = NULL, *integralError = NULL;
141 char *input = NULL, *output = NULL, *xName = NULL, *xErrorName = NULL;
142 char **yName = NULL, **yErrorName = NULL, **yOutputName = NULL, **yOutputErrorName = NULL, *ptr = NULL, **colMatch = NULL;
143 char **yOutputUnits = NULL, **yExcludeName = NULL;
144 char *mainTemplate[3] = {
"%yNameInteg",
"Integral w.r.t. %xSymbol of %ySymbol",
"$sI$e %ySymbol d%xSymbol"};
145 char *errorTemplate[3] = {
"%yNameIntegSigma",
"Sigma of integral w.r.t. %xSymbol of %ySymbol",
146 "Sigma[$sI$e %ySymbol d%xSymbol]"};
147 char *GMErrorTemplate[3] = {
"%yNameIntegError",
"Error estimate for integral w.r.t. %xSymbol of %ySymbol",
148 "Error[$sI$e %ySymbol d%xSymbol]"};
149 long i, iArg, yNames = 0, yExcludeNames = 0;
150 int32_t colMatches = 0;
153 SCANNED_ARG *scanned = NULL;
154 unsigned long flags = 0, pipeFlags = 0, printFlags = 0, majorOrderFlag = 0;
155 FILE *fpPrint = stderr;
156 char *printFormat =
"%21.15e";
157 int methodCode = TRAPAZOID_METHOD;
158 short columnMajorOrder = -1;
162 argc =
scanargs(&scanned, argc, argv);
164 fprintf(stderr,
"%s\n", USAGE);
170 input = output = xName = xErrorName = NULL;
171 yName = yErrorName = yExcludeName = NULL;
172 integral = integralError = yError = yData = xData = xError = NULL;
173 yNames = yExcludeNames = 0;
174 pipeFlags = printFlags = 0;
176 printFormat =
"%21.15e";
177 methodCode = TRAPAZOID_METHOD;
179 for (iArg = 1; iArg < argc; iArg++) {
180 if (scanned[iArg].arg_type == OPTION) {
182 switch (
match_string(scanned[iArg].list[0], option, N_OPTIONS, 0)) {
183 case CLO_MAJOR_ORDER:
185 scanned[iArg].n_items--;
186 if (scanned[iArg].n_items > 0 &&
187 !
scanItemList(&majorOrderFlag, scanned[iArg].list + 1, &scanned[iArg].n_items, 0,
188 "row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER,
189 "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER, NULL)) {
190 SDDS_Bomb(
"invalid -majorOrder syntax/values");
192 if (majorOrderFlag & SDDS_COLUMN_MAJOR_ORDER)
193 columnMajorOrder = 1;
194 else if (majorOrderFlag & SDDS_ROW_MAJOR_ORDER)
195 columnMajorOrder = 0;
198 if (scanned[iArg].n_items != 2 && scanned[iArg].n_items != 3)
200 yName =
SDDS_Realloc(yName,
sizeof(*yName) * (yNames + 1));
201 yErrorName =
SDDS_Realloc(yErrorName,
sizeof(*yErrorName) * (yNames + 1));
202 yName[yNames] = scanned[iArg].list[1];
203 if (scanned[iArg].n_items == 3)
204 yErrorName[yNames] = scanned[iArg].list[2];
206 yErrorName[yNames] = NULL;
210 if (scanned[iArg].n_items < 2)
212 moveToStringArray(&yExcludeName, &yExcludeNames, scanned[iArg].list + 1, scanned[iArg].n_items - 1);
217 if (scanned[iArg].n_items != 2 && scanned[iArg].n_items != 3)
219 xName = scanned[iArg].list[1];
220 if (scanned[iArg].n_items == 3)
221 xErrorName = scanned[iArg].list[2];
226 if (scanned[iArg].n_items != 2 ||
227 (methodCode =
match_string(scanned[iArg].list[1], methodOption, N_METHODS, 0)) < 0)
231 if ((scanned[iArg].n_items -= 1) >= 1) {
232 if (!
scanItemList(&printFlags, scanned[iArg].list + 1, &scanned[iArg].n_items, 0,
233 "bare", -1, NULL, 0, BARE_PRINTOUT,
234 "stdout", -1, NULL, 0, STDOUT_PRINTOUT,
235 "format",
SDDS_STRING, &printFormat, 1, 0, NULL)) {
239 if (!(printFlags & BARE_PRINTOUT))
240 printFlags |= NORMAL_PRINTOUT;
242 case CLO_MAINTEMPLATE:
243 if (scanned[iArg].n_items < 2)
244 SDDS_Bomb(
"invalid -mainTemplate syntax");
245 scanned[iArg].n_items--;
246 if (!
scanItemList(&flags, scanned[iArg].list + 1, &scanned[iArg].n_items, 0,
248 "description",
SDDS_STRING, mainTemplate + 1, 1, 0,
249 "symbol",
SDDS_STRING, mainTemplate + 2, 1, 0, NULL)) {
250 SDDS_Bomb(
"invalid -mainTemplate syntax");
253 case CLO_ERRORTEMPLATE:
254 if (scanned[iArg].n_items < 2)
255 SDDS_Bomb(
"invalid -errorTemplate syntax");
256 scanned[iArg].n_items--;
257 if (!
scanItemList(&flags, scanned[iArg].list + 1, &scanned[iArg].n_items, 0,
259 "description",
SDDS_STRING, errorTemplate + 1, 1, 0,
260 "symbol",
SDDS_STRING, errorTemplate + 2, 1, 0, NULL)) {
261 SDDS_Bomb(
"invalid -errorTemplate syntax");
265 if (!
processPipeOption(scanned[iArg].list + 1, scanned[iArg].n_items - 1, &pipeFlags))
269 if (scanned[iArg].n_items < 2)
270 SDDS_Bomb(
"Invalid copycolumns syntax provided.");
271 colMatch =
tmalloc(
sizeof(*colMatch) * (colMatches = scanned[iArg].n_items - 1));
272 for (i = 0; i < colMatches; i++)
273 colMatch[i] = scanned[iArg].list[i + 1];
276 fprintf(stderr,
"invalid option seen: %s\n", scanned[iArg].list[0]);
281 input = scanned[iArg].list[0];
283 output = scanned[iArg].list[0];
291 if (methodCode != TRAPAZOID_METHOD) {
293 for (i = 0; i < yNames; i++)
294 yErrorName[i] = NULL;
297 if (printFlags & STDOUT_PRINTOUT)
301 SDDS_Bomb(
"-integrate option must be given at least once");
302 if (!checkErrorNames(yErrorName, yNames))
303 SDDS_Bomb(
"either all -integrate quantities must have errors, or none");
308 if (!(ptr =
SDDS_FindColumn(&SDDSin, FIND_NUMERIC_TYPE, xName, NULL))) {
309 fprintf(stderr,
"error: column %s doesn't exist\n", xName);
316 if (!(ptr =
SDDS_FindColumn(&SDDSin, FIND_NUMERIC_TYPE, xErrorName, NULL))) {
317 fprintf(stderr,
"error: column %s doesn't exist\n", xErrorName);
325 if (!(yNames = expandColumnPairNames(&SDDSin, &yName, &yErrorName, yNames, yExcludeName, yExcludeNames, FIND_NUMERIC_TYPE, 0))) {
326 fprintf(stderr,
"error: no quantities to integrate found in file\n");
330 if (!setupOutputFile(&SDDSout, &SDDSin, output, &yOutputName, &yOutputErrorName, &yOutputUnits,
331 xName, xErrorName, yName, yErrorName, yNames,
332 mainTemplate, (methodCode == GILLMILLER_METHOD) ? (
char **)GMErrorTemplate : (
char **)errorTemplate,
333 methodCode, columnMajorOrder, colMatch, colMatches)) {
334 SDDS_Bomb(
"Failed to set up output file.");
339 integral =
SDDS_Realloc(integral,
sizeof(*integral) * rows);
340 integralError =
SDDS_Realloc(integralError,
sizeof(*integralError) * rows);
354 for (i = 0; i < yNames; i++) {
357 (yErrorName && yErrorName[i] &&
361 switch (methodCode) {
362 case TRAPAZOID_METHOD:
363 trapizoid(xData, yData, xError, yError, rows, integral, integralError);
365 case GILLMILLER_METHOD:
367 SDDS_Bomb(
"Problem with integration: check for monotonically changing independent variable values");
372 (yOutputErrorName && yOutputErrorName[i] &&
376 if (printFlags & BARE_PRINTOUT) {
377 fprintf(fpPrint, printFormat, integral[rows - 1]);
378 if (xError || yError || (yOutputErrorName && yOutputErrorName[i])) {
380 fprintf(fpPrint, printFormat, integralError[rows - 1]);
382 fputc(
'\n', fpPrint);
383 }
else if (printFlags & NORMAL_PRINTOUT) {
384 fprintf(fpPrint,
"%s: ", yName[i]);
385 fprintf(fpPrint, printFormat, integral[rows - 1]);
386 if (xError || yError || (yOutputErrorName && yOutputErrorName[i])) {
387 fputs(
" +/- ", fpPrint);
388 fprintf(fpPrint, printFormat, integralError[rows - 1]);
389 fputs(yOutputUnits[i], fpPrint);
391 fputc(
'\n', fpPrint);
398 yData = yError = NULL;
408 xData = xError = NULL;
427 char ***yOutputName,
char ***yOutputErrorName,
char ***yOutputUnits,
428 char *xName,
char *xErrorName,
char **yName,
char **yErrorName,
429 long yNames,
char **mainTemplate,
char **errorTemplate,
long methodCode,
430 short columnMajorOrder,
char **colMatch, int32_t colMatches) {
432 char *xSymbol = NULL, *ySymbol = NULL, **col = NULL;
435 *yOutputName =
tmalloc(
sizeof(**yOutputName) * yNames);
436 *yOutputErrorName =
tmalloc(
sizeof(**yOutputErrorName) * yNames);
437 *yOutputUnits =
tmalloc(
sizeof(**yOutputUnits) * yNames);
447 fprintf(stderr,
"error: problem getting symbol for column %s\n", xName);
451 if (columnMajorOrder != -1)
452 SDDSout->layout.data_mode.column_major = columnMajorOrder;
454 SDDSout->layout.data_mode.column_major = SDDSin->layout.data_mode.column_major;
459 for (i = 0; i < yNames; i++) {
461 fprintf(stderr,
"error: problem transferring definition for column %s\n", yName[i]);
466 fprintf(stderr,
"error: problem getting symbol for column %s\n", yName[i]);
473 (*yOutputUnits)[i] = multiplyColumnUnits(SDDSout, yName[i], xName);
474 (*yOutputName)[i] = changeInformation(SDDSout, yName[i], yName[i], ySymbol, xName, xSymbol, mainTemplate, (*yOutputUnits)[i]);
476 if (yErrorName || xErrorName || methodCode == GILLMILLER_METHOD) {
477 if (yErrorName && yErrorName[i]) {
479 fprintf(stderr,
"error: problem transferring definition for column %s\n", yErrorName[i]);
482 (*yOutputErrorName)[i] = changeInformation(SDDSout, yErrorName[i], yName[i], ySymbol, xName, xSymbol, errorTemplate, (*yOutputUnits)[i]);
485 fprintf(stderr,
"error: problem transferring error definition for column %s\n", yName[i]);
488 (*yOutputErrorName)[i] = changeInformation(SDDSout, yName[i], yName[i], ySymbol, xName, xSymbol, errorTemplate, (*yOutputUnits)[i]);
491 (*yOutputErrorName)[i] = NULL;
497 for (i = 0; i < cols; i++) {
512char *changeInformation(
SDDS_DATASET *SDDSout,
char *name,
char *nameRoot,
char *symbolRoot,
513 char *xName,
char *xSymbol,
char **
template,
char *newUnits) {
514 char buffer1[SDDS_MAXLINE], buffer2[SDDS_MAXLINE], *ptr = NULL;
519 makeSubstitutions(buffer1, buffer2,
template[2], nameRoot, symbolRoot, xName, xSymbol);
523 makeSubstitutions(buffer1, buffer2,
template[1], nameRoot, symbolRoot, xName, xSymbol);
527 makeSubstitutions(buffer1, buffer2,
template[0], nameRoot, symbolRoot, xName, xSymbol);
535void makeSubstitutions(
char *buffer1,
char *buffer2,
char *
template,
char *nameRoot,
char *symbolRoot,
536 char *xName,
char *xSymbol) {
537 strcpy(buffer2,
template);
542 strcpy(buffer1, buffer2);
545void trapizoid(
double *x,
double *y,
double *sx,
double *sy, int64_t n,
double *integ,
double *error) {
549 integ[0] = error[0] = 0;
550 for (i = 1; i < n; i++) {
551 dy = y[i] + y[i - 1];
552 dx = x[i] - x[i - 1];
553 integ[i] = integ[i - 1] + dy * dx;
555 error[i] = error[i - 1] + sqr(dy) * (sqr(sx[i - 1]) + sqr(sx[i])) + sqr(dx) * (sqr(sy[i - 1]) + sqr(sy[i]));
557 error[i] = error[i - 1] + sqr(dy) * (sqr(sx[i - 1]) + sqr(sx[i]));
559 error[i] = error[i - 1] + sqr(dx) * (sqr(sy[i - 1]) + sqr(sy[i]));
563 for (i = 0; i < n; i++) {
564 error[i] = sqrt(error[i]) / 2;
569long checkErrorNames(
char **yErrorName,
long yNames) {
572 for (i = 1; i < yNames; i++)
576 for (i = 1; i < yNames; i++)
int GillMillerIntegration(double *integral, double *error, double *f, double *x, long n)
Performs numerical integration using the Gill-Miller method.
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
int32_t SDDS_CopyPage(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
int32_t SDDS_StartPage(SDDS_DATASET *SDDS_dataset, int64_t expected_n_rows)
int32_t SDDS_SetColumnFromDoubles(SDDS_DATASET *SDDS_dataset, int32_t mode, double *data, int64_t rows,...)
Sets the values for a single data column using double-precision floating-point numbers.
int32_t SDDS_ChangeColumnInformation(SDDS_DATASET *SDDS_dataset, char *field_name, void *memory, int32_t mode,...)
Modifies a specific field in a column definition within the SDDS dataset.
int32_t SDDS_GetColumnInformation(SDDS_DATASET *SDDS_dataset, char *field_name, void *memory, int32_t mode,...)
Retrieves information about a specified column in the SDDS dataset.
int32_t SDDS_InitializeOutput(SDDS_DATASET *SDDS_dataset, int32_t data_mode, int32_t lines_per_row, const char *description, const char *contents, const char *filename)
Initializes the SDDS output dataset.
int32_t SDDS_WritePage(SDDS_DATASET *SDDS_dataset)
Writes the current data table to the output file.
int32_t SDDS_WriteLayout(SDDS_DATASET *SDDS_dataset)
Writes the SDDS layout header to the output file.
int32_t SDDS_TransferColumnDefinition(SDDS_DATASET *target, SDDS_DATASET *source, char *name, char *newName)
Transfers a column definition from a source dataset to a target dataset.
int32_t SDDS_TransferAllParameterDefinitions(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source, uint32_t mode)
Transfers all parameter definitions from a source dataset to a target dataset.
int32_t SDDS_FreeStringArray(char **string, int64_t strings)
Frees an array of strings by deallocating each individual string.
char ** getMatchingSDDSNames(SDDS_DATASET *dataset, char **matchName, int32_t matches, int32_t *names, short type)
Retrieves an array of matching SDDS entity names based on specified criteria.
int32_t SDDS_GetColumnIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named column in the SDDS dataset.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
void SDDS_RegisterProgramName(const char *name)
Registers the executable program name for use in error messages.
int32_t SDDS_StringIsBlank(char *s)
Checks if a string is blank (contains only whitespace characters).
char * SDDS_FindColumn(SDDS_DATASET *SDDS_dataset, int32_t mode,...)
Finds the first column in the SDDS dataset that matches the specified criteria.
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
int32_t SDDS_CopyString(char **target, const char *source)
Copies a source string to a target string with memory allocation.
void * SDDS_Realloc(void *old_ptr, size_t new_size)
Reallocates memory to a new size.
#define SDDS_STRING
Identifier for the string data type.
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
long match_string(char *string, char **option, long n_options, long mode)
Matches a given string against an array of option strings based on specified modes.
int replace_string(char *t, char *s, char *orig, char *repl)
Replace all occurrences of one string with another string.
int scanargs(SCANNED_ARG **scanned, int argc, char **argv)
long processPipeOption(char **item, long items, unsigned long *flags)
void processFilenames(char *programName, char **input, char **output, unsigned long pipeFlags, long noWarnings, long *tmpOutputUsed)
long scanItemList(unsigned long *flags, char **item, long *items, unsigned long mode,...)
Scans a list of items and assigns values based on provided keywords and types.