SDDS ToolKit Programs and Libraries for C and Python
All Classes Files Functions Variables Macros Pages
sddsinteg.c
Go to the documentation of this file.
1/**
2 * @file sddsinteg.c
3 * @brief Performs numerical integration on specified columns of an SDDS dataset.
4 *
5 * @details
6 * This program integrates specified columns of an SDDS dataset using customizable methods.
7 * It supports error propagation, template customization, and column-based exclusions.
8 *
9 * @section Usage
10 * ```
11 * sddsinteg [<inputfile>] [<outputfile>]
12 * [-pipe[=input][,output]]
13 * -integrate=<column-name>[,<sigma-name>] ...
14 * [-exclude=<column-name>[,...]]
15 * -versus=<column-name>[,<sigma-name>]
16 * [-mainTemplates=<item>=<string>[,...]]
17 * [-errorTemplates=<item>=<string>[,...]]
18 * [-copycolumns=<list_of_column_names>]
19 * [-method={trapazoid|GillMiller}]
20 * [-printFinal[=bare][,stdout][,format=<string>]]
21 * ```
22 *
23 * @section Options
24 * | Required | Description |
25 * |---------------------------------------|-------------------------------------------------------------------------------------------------|
26 * | `-integrate` | Column(s) to integrate. Optional sigma names for RMS error may be provided. |
27 * | `-versus` | Column to integrate against. Optionally specify sigma for RMS error. |
28 *
29 * | Optional | Description |
30 * |---------------------------------------|-------------------------------------------------------------------------------------------------|
31 * | `-exclude` | Columns to exclude from integration. |
32 * | `-mainTemplates` | Customizes main templates for name, symbol, or description of integration columns. |
33 * | `-errorTemplates` | Customizes error templates for name, symbol, or description of error columns. |
34 * | `-copycolumns` | List of columns to copy to the output. |
35 * | `-method` | Integration method. `trapazoid` supports error propagation; `GillMiller` does not. |
36 * | `-printFinal` | Outputs the final integral value with formatting options. |
37 *
38 * @subsection Incompatibilities
39 * - `-method=GillMiller` disables error propagation for all integrated columns.
40 *
41 * @copyright
42 * - (c) 2002 The University of Chicago, as Operator of Argonne National Laboratory.
43 * - (c) 2002 The Regents of the University of California, as Operator of Los Alamos National Laboratory.
44 *
45 * @license
46 * This file is distributed under the terms of the Software License Agreement
47 * found in the file LICENSE included with this distribution.
48 *
49 * @author
50 * M. Borland, C. Saunders, R. Soliday, H. Shang
51 */
52
53#include "mdb.h"
54#include "SDDS.h"
55#include "SDDSutils.h"
56#include "scan.h"
57#include <ctype.h>
58
59static char *USAGE =
60 "Usage: sddsinteg [<input>] [<output>]\n"
61 " [-pipe=[input][,output]]\n"
62 " -integrate=<column-name>[,<sigma-name>] ...\n"
63 " [-exclude=<column-name>[,...]]\n"
64 " -versus=<column-name>[,<sigma-name>]\n"
65 " [-mainTemplates=<item>=<string>[,...]]\n"
66 " [-errorTemplates=<item>=<string>[,...]]\n"
67 " [-copycolumns=<list of column names>]\n"
68 " [-method={trapazoid|GillMiller}]\n"
69 " [-printFinal[=bare][,stdout][,format=<string>]]\n\n"
70 "Options:\n"
71 " -pipe Standard SDDS pipe option.\n"
72 " -integrate Name of column to integrate, plus optional RMS error.\n"
73 " Column name may include wildcards, with error name using %%s.\n"
74 " -exclude List of column names to exclude from integration.\n"
75 " -versus Name of column to integrate against, plus optional RMS error.\n"
76 " -mainTemplates Customize main templates for name, symbol, or description.\n"
77 " -errorTemplates Customize error templates for name, symbol, or description.\n"
78 " -copycolumns Comma-separated list of columns to copy to the output.\n"
79 " -method Integration method: trapazoid (default) or GillMiller.\n"
80 " -printFinal Print the final integral value. Options:\n"
81 " bare - Print only the integral value.\n"
82 " stdout - Print to standard output.\n"
83 " format=<s> - Specify printf format string.\n\n"
84 "Program by Michael Borland. (" __DATE__ " " __TIME__ ", SVN revision: " SVN_VERSION ")";
85
86/* Enumeration for option types */
87enum option_type {
88 CLO_INTEGRATE,
89 CLO_VERSUS,
90 CLO_METHOD,
91 CLO_PRINTFINAL,
92 CLO_MAINTEMPLATE,
93 CLO_ERRORTEMPLATE,
94 CLO_PIPE,
95 CLO_EXCLUDE,
96 CLO_MAJOR_ORDER,
97 CLO_COPY,
98 N_OPTIONS
99};
100
101static char *option[N_OPTIONS] = {
102 "integrate",
103 "versus",
104 "method",
105 "printfinal",
106 "maintemplate",
107 "errortemplate",
108 "pipe",
109 "exclude",
110 "majorOrder",
111 "copycolumns",
112};
113
114void trapizoid(double *x, double *y, double *sx, double *sy, int64_t n, double *integ, double *error);
115long checkErrorNames(char **yErrorName, long nIntegrals);
116void makeSubstitutions(char *buffer1, char *buffer2, char *template, char *nameRoot, char *symbolRoot, char *xName, char *xSymbol);
117char *changeInformation(SDDS_DATASET *SDDSout, char *name, char *nameRoot, char *symbolRoot,
118 char *xName, char *xSymbol, char **template, char *newUnits);
119long setupOutputFile(SDDS_DATASET *SDDSout, SDDS_DATASET *SDDSin, char *output,
120 char ***yOutputName, char ***yOutputErrorName, char ***yOutputUnits,
121 char *xName, char *xErrorName, char **yName, char **yErrorName,
122 long yNames, char **mainTemplate, char **errorTemplate, long methodCode,
123 short columnMajorOrder, char **colMatch, int32_t colMatches);
124
125#define NORMAL_PRINTOUT 1
126#define BARE_PRINTOUT 2
127#define STDOUT_PRINTOUT 4
128
129#define TRAPAZOID_METHOD 0
130#define GILLMILLER_METHOD 1
131#define N_METHODS 2
132
133static char *methodOption[N_METHODS] = {
134 "trapazoid",
135 "gillmiller"
136};
137
138int main(int argc, char **argv) {
139 double *xData = NULL, *yData = NULL, *xError = NULL, *yError = NULL, *integral = NULL, *integralError = NULL;
140 char *input = NULL, *output = NULL, *xName = NULL, *xErrorName = NULL;
141 char **yName = NULL, **yErrorName = NULL, **yOutputName = NULL, **yOutputErrorName = NULL, *ptr = NULL, **colMatch = NULL;
142 char **yOutputUnits = NULL, **yExcludeName = NULL;
143 char *mainTemplate[3] = {"%yNameInteg", "Integral w.r.t. %xSymbol of %ySymbol", "$sI$e %ySymbol d%xSymbol"};
144 char *errorTemplate[3] = {"%yNameIntegSigma", "Sigma of integral w.r.t. %xSymbol of %ySymbol",
145 "Sigma[$sI$e %ySymbol d%xSymbol]"};
146 char *GMErrorTemplate[3] = {"%yNameIntegError", "Error estimate for integral w.r.t. %xSymbol of %ySymbol",
147 "Error[$sI$e %ySymbol d%xSymbol]"};
148 long i, iArg, yNames = 0, yExcludeNames = 0;
149 int32_t colMatches = 0;
150 int64_t rows;
151 SDDS_DATASET SDDSin, SDDSout;
152 SCANNED_ARG *scanned = NULL;
153 unsigned long flags = 0, pipeFlags = 0, printFlags = 0, majorOrderFlag = 0;
154 FILE *fpPrint = stderr;
155 char *printFormat = "%21.15e";
156 int methodCode = TRAPAZOID_METHOD;
157 short columnMajorOrder = -1;
158
160
161 argc = scanargs(&scanned, argc, argv);
162 if (argc == 1) {
163 fprintf(stderr, "%s\n", USAGE);
164 return EXIT_FAILURE;
165 }
166
167 colMatch = NULL;
168 colMatches = 0;
169 input = output = xName = xErrorName = NULL;
170 yName = yErrorName = yExcludeName = NULL;
171 integral = integralError = yError = yData = xData = xError = NULL;
172 yNames = yExcludeNames = 0;
173 pipeFlags = printFlags = 0;
174 fpPrint = stderr;
175 printFormat = "%21.15e";
176 methodCode = TRAPAZOID_METHOD;
177
178 for (iArg = 1; iArg < argc; iArg++) {
179 if (scanned[iArg].arg_type == OPTION) {
180 /* process options here */
181 switch (match_string(scanned[iArg].list[0], option, N_OPTIONS, 0)) {
182 case CLO_MAJOR_ORDER:
183 majorOrderFlag = 0;
184 scanned[iArg].n_items--;
185 if (scanned[iArg].n_items > 0 &&
186 !scanItemList(&majorOrderFlag, scanned[iArg].list + 1, &scanned[iArg].n_items, 0,
187 "row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER,
188 "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER, NULL)) {
189 SDDS_Bomb("invalid -majorOrder syntax/values");
190 }
191 if (majorOrderFlag & SDDS_COLUMN_MAJOR_ORDER)
192 columnMajorOrder = 1;
193 else if (majorOrderFlag & SDDS_ROW_MAJOR_ORDER)
194 columnMajorOrder = 0;
195 break;
196 case CLO_INTEGRATE:
197 if (scanned[iArg].n_items != 2 && scanned[iArg].n_items != 3)
198 SDDS_Bomb("invalid -integrate syntax");
199 yName = SDDS_Realloc(yName, sizeof(*yName) * (yNames + 1));
200 yErrorName = SDDS_Realloc(yErrorName, sizeof(*yErrorName) * (yNames + 1));
201 yName[yNames] = scanned[iArg].list[1];
202 if (scanned[iArg].n_items == 3)
203 yErrorName[yNames] = scanned[iArg].list[2];
204 else
205 yErrorName[yNames] = NULL;
206 yNames++;
207 break;
208 case CLO_EXCLUDE:
209 if (scanned[iArg].n_items < 2)
210 SDDS_Bomb("invalid -exclude syntax");
211 moveToStringArray(&yExcludeName, &yExcludeNames, scanned[iArg].list + 1, scanned[iArg].n_items - 1);
212 break;
213 case CLO_VERSUS:
214 if (xName)
215 SDDS_Bomb("give -versus only once");
216 if (scanned[iArg].n_items != 2 && scanned[iArg].n_items != 3)
217 SDDS_Bomb("invalid -versus syntax");
218 xName = scanned[iArg].list[1];
219 if (scanned[iArg].n_items == 3)
220 xErrorName = scanned[iArg].list[2];
221 else
222 xErrorName = NULL;
223 break;
224 case CLO_METHOD:
225 if (scanned[iArg].n_items != 2 ||
226 (methodCode = match_string(scanned[iArg].list[1], methodOption, N_METHODS, 0)) < 0)
227 SDDS_Bomb("invalid -method syntax");
228 break;
229 case CLO_PRINTFINAL:
230 if ((scanned[iArg].n_items -= 1) >= 1) {
231 if (!scanItemList(&printFlags, scanned[iArg].list + 1, &scanned[iArg].n_items, 0,
232 "bare", -1, NULL, 0, BARE_PRINTOUT,
233 "stdout", -1, NULL, 0, STDOUT_PRINTOUT,
234 "format", SDDS_STRING, &printFormat, 1, 0, NULL)) {
235 SDDS_Bomb("invalid -printFinal syntax");
236 }
237 }
238 if (!(printFlags & BARE_PRINTOUT))
239 printFlags |= NORMAL_PRINTOUT;
240 break;
241 case CLO_MAINTEMPLATE:
242 if (scanned[iArg].n_items < 2)
243 SDDS_Bomb("invalid -mainTemplate syntax");
244 scanned[iArg].n_items--;
245 if (!scanItemList(&flags, scanned[iArg].list + 1, &scanned[iArg].n_items, 0,
246 "name", SDDS_STRING, mainTemplate + 0, 1, 0,
247 "description", SDDS_STRING, mainTemplate + 1, 1, 0,
248 "symbol", SDDS_STRING, mainTemplate + 2, 1, 0, NULL)) {
249 SDDS_Bomb("invalid -mainTemplate syntax");
250 }
251 break;
252 case CLO_ERRORTEMPLATE:
253 if (scanned[iArg].n_items < 2)
254 SDDS_Bomb("invalid -errorTemplate syntax");
255 scanned[iArg].n_items--;
256 if (!scanItemList(&flags, scanned[iArg].list + 1, &scanned[iArg].n_items, 0,
257 "name", SDDS_STRING, errorTemplate + 0, 1, 0,
258 "description", SDDS_STRING, errorTemplate + 1, 1, 0,
259 "symbol", SDDS_STRING, errorTemplate + 2, 1, 0, NULL)) {
260 SDDS_Bomb("invalid -errorTemplate syntax");
261 }
262 break;
263 case CLO_PIPE:
264 if (!processPipeOption(scanned[iArg].list + 1, scanned[iArg].n_items - 1, &pipeFlags))
265 SDDS_Bomb("invalid -pipe syntax");
266 break;
267 case CLO_COPY:
268 if (scanned[iArg].n_items < 2)
269 SDDS_Bomb("Invalid copycolumns syntax provided.");
270 colMatch = tmalloc(sizeof(*colMatch) * (colMatches = scanned[iArg].n_items - 1));
271 for (i = 0; i < colMatches; i++)
272 colMatch[i] = scanned[iArg].list[i + 1];
273 break;
274 default:
275 fprintf(stderr, "invalid option seen: %s\n", scanned[iArg].list[0]);
276 return EXIT_FAILURE;
277 }
278 } else {
279 if (!input)
280 input = scanned[iArg].list[0];
281 else if (!output)
282 output = scanned[iArg].list[0];
283 else
284 SDDS_Bomb("too many filenames");
285 }
286 }
287
288 processFilenames("sddsinteg", &input, &output, pipeFlags, 0, NULL);
289
290 if (methodCode != TRAPAZOID_METHOD) {
291 xErrorName = NULL;
292 for (i = 0; i < yNames; i++)
293 yErrorName[i] = NULL;
294 }
295
296 if (printFlags & STDOUT_PRINTOUT)
297 fpPrint = stdout;
298
299 if (yNames == 0)
300 SDDS_Bomb("-integrate option must be given at least once");
301 if (!checkErrorNames(yErrorName, yNames))
302 SDDS_Bomb("either all -integrate quantities must have errors, or none");
303
304 if (!SDDS_InitializeInput(&SDDSin, input))
305 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
306
307 if (!(ptr = SDDS_FindColumn(&SDDSin, FIND_NUMERIC_TYPE, xName, NULL))) {
308 fprintf(stderr, "error: column %s doesn't exist\n", xName);
309 return EXIT_FAILURE;
310 }
311 free(xName);
312 xName = ptr;
313
314 if (xErrorName) {
315 if (!(ptr = SDDS_FindColumn(&SDDSin, FIND_NUMERIC_TYPE, xErrorName, NULL))) {
316 fprintf(stderr, "error: column %s doesn't exist\n", xErrorName);
317 return EXIT_FAILURE;
318 } else {
319 free(xErrorName);
320 xErrorName = ptr;
321 }
322 }
323
324 if (!(yNames = expandColumnPairNames(&SDDSin, &yName, &yErrorName, yNames, yExcludeName, yExcludeNames, FIND_NUMERIC_TYPE, 0))) {
325 fprintf(stderr, "error: no quantities to integrate found in file\n");
326 return EXIT_FAILURE;
327 }
328
329 if (!setupOutputFile(&SDDSout, &SDDSin, output, &yOutputName, &yOutputErrorName, &yOutputUnits,
330 xName, xErrorName, yName, yErrorName, yNames,
331 mainTemplate, (methodCode == GILLMILLER_METHOD) ? (char **)GMErrorTemplate : (char **)errorTemplate,
332 methodCode, columnMajorOrder, colMatch, colMatches)) {
333 SDDS_Bomb("Failed to set up output file.");
334 }
335
336 while (SDDS_ReadPage(&SDDSin) > 0) {
337 rows = SDDS_CountRowsOfInterest(&SDDSin);
338 integral = SDDS_Realloc(integral, sizeof(*integral) * rows);
339 integralError = SDDS_Realloc(integralError, sizeof(*integralError) * rows);
340 if (!SDDS_StartPage(&SDDSout, rows) ||
341 !SDDS_CopyPage(&SDDSout, &SDDSin))
342 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
343
344 xError = NULL;
345 if (!(xData = SDDS_GetColumnInDoubles(&SDDSin, xName)) ||
346 (xErrorName && !(xError = SDDS_GetColumnInDoubles(&SDDSin, xErrorName))))
347 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
348
349 if (!SDDS_SetColumnFromDoubles(&SDDSout, SDDS_BY_NAME, xData, rows, xName) ||
350 (xErrorName && !SDDS_SetColumnFromDoubles(&SDDSout, SDDS_BY_NAME, xError, rows, xErrorName)))
351 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
352
353 for (i = 0; i < yNames; i++) {
354 yError = NULL;
355 if (!(yData = SDDS_GetColumnInDoubles(&SDDSin, yName[i])) ||
356 (yErrorName && yErrorName[i] &&
357 !(yError = SDDS_GetColumnInDoubles(&SDDSin, yErrorName[i]))))
358 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
359
360 switch (methodCode) {
361 case TRAPAZOID_METHOD:
362 trapizoid(xData, yData, xError, yError, rows, integral, integralError);
363 break;
364 case GILLMILLER_METHOD:
365 if (GillMillerIntegration(integral, integralError, yData, xData, rows) != 0)
366 SDDS_Bomb("Problem with integration: check for monotonically changing independent variable values");
367 break;
368 }
369
370 if (!SDDS_SetColumnFromDoubles(&SDDSout, SDDS_BY_NAME, integral, rows, yOutputName[i]) ||
371 (yOutputErrorName && yOutputErrorName[i] &&
372 !SDDS_SetColumnFromDoubles(&SDDSout, SDDS_BY_NAME, integralError, rows, yOutputErrorName[i])))
373 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
374
375 if (printFlags & BARE_PRINTOUT) {
376 fprintf(fpPrint, printFormat, integral[rows - 1]);
377 if (xError || yError || (yOutputErrorName && yOutputErrorName[i])) {
378 fputc(' ', fpPrint);
379 fprintf(fpPrint, printFormat, integralError[rows - 1]);
380 }
381 fputc('\n', fpPrint);
382 } else if (printFlags & NORMAL_PRINTOUT) {
383 fprintf(fpPrint, "%s: ", yName[i]);
384 fprintf(fpPrint, printFormat, integral[rows - 1]);
385 if (xError || yError || (yOutputErrorName && yOutputErrorName[i])) {
386 fputs(" +/- ", fpPrint);
387 fprintf(fpPrint, printFormat, integralError[rows - 1]);
388 fputs(yOutputUnits[i], fpPrint);
389 }
390 fputc('\n', fpPrint);
391 }
392
393 if (yData)
394 free(yData);
395 if (yError)
396 free(yError);
397 yData = yError = NULL;
398 }
399
400 if (!SDDS_WritePage(&SDDSout))
401 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
402
403 if (xData)
404 free(xData);
405 if (xError)
406 free(xError);
407 xData = xError = NULL;
408 }
409
410 if (!SDDS_Terminate(&SDDSin) || !SDDS_Terminate(&SDDSout)) {
411 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
412 return EXIT_FAILURE;
413 }
414
415 if (integral)
416 free(integral);
417 if (integralError)
418 free(integralError);
419 if (colMatch)
420 free(colMatch);
421
422 return EXIT_SUCCESS;
423}
424
425long setupOutputFile(SDDS_DATASET *SDDSout, SDDS_DATASET *SDDSin, char *output,
426 char ***yOutputName, char ***yOutputErrorName, char ***yOutputUnits,
427 char *xName, char *xErrorName, char **yName, char **yErrorName,
428 long yNames, char **mainTemplate, char **errorTemplate, long methodCode,
429 short columnMajorOrder, char **colMatch, int32_t colMatches) {
430 long i;
431 char *xSymbol = NULL, *ySymbol = NULL, **col = NULL;
432 int32_t cols = 0;
433
434 *yOutputName = tmalloc(sizeof(**yOutputName) * yNames);
435 *yOutputErrorName = tmalloc(sizeof(**yOutputErrorName) * yNames);
436 *yOutputUnits = tmalloc(sizeof(**yOutputUnits) * yNames);
437
438 if (!SDDS_InitializeOutput(SDDSout, SDDS_BINARY, 0, NULL, "sddsinteg output", output))
439 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
440
441 if (!SDDS_TransferColumnDefinition(SDDSout, SDDSin, xName, NULL) ||
442 (xErrorName && !SDDS_TransferColumnDefinition(SDDSout, SDDSin, xErrorName, NULL)))
443 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
444
445 if (SDDS_GetColumnInformation(SDDSout, "symbol", &xSymbol, SDDS_GET_BY_NAME, xName) != SDDS_STRING) {
446 fprintf(stderr, "error: problem getting symbol for column %s\n", xName);
447 return EXIT_FAILURE;
448 }
449
450 if (columnMajorOrder != -1)
451 SDDSout->layout.data_mode.column_major = columnMajorOrder;
452 else
453 SDDSout->layout.data_mode.column_major = SDDSin->layout.data_mode.column_major;
454
455 if (!xSymbol)
456 SDDS_CopyString(&xSymbol, xName);
457
458 for (i = 0; i < yNames; i++) {
459 if (!SDDS_TransferColumnDefinition(SDDSout, SDDSin, yName[i], NULL)) {
460 fprintf(stderr, "error: problem transferring definition for column %s\n", yName[i]);
461 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
462 }
463
464 if (SDDS_GetColumnInformation(SDDSout, "symbol", &ySymbol, SDDS_GET_BY_NAME, yName[i]) != SDDS_STRING) {
465 fprintf(stderr, "error: problem getting symbol for column %s\n", yName[i]);
466 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
467 }
468
469 if (!ySymbol || SDDS_StringIsBlank(ySymbol))
470 SDDS_CopyString(&ySymbol, yName[i]);
471
472 (*yOutputUnits)[i] = multiplyColumnUnits(SDDSout, yName[i], xName);
473 (*yOutputName)[i] = changeInformation(SDDSout, yName[i], yName[i], ySymbol, xName, xSymbol, mainTemplate, (*yOutputUnits)[i]);
474
475 if (yErrorName || xErrorName || methodCode == GILLMILLER_METHOD) {
476 if (yErrorName && yErrorName[i]) {
477 if (!SDDS_TransferColumnDefinition(SDDSout, SDDSin, yErrorName[i], NULL)) {
478 fprintf(stderr, "error: problem transferring definition for column %s\n", yErrorName[i]);
479 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
480 }
481 (*yOutputErrorName)[i] = changeInformation(SDDSout, yErrorName[i], yName[i], ySymbol, xName, xSymbol, errorTemplate, (*yOutputUnits)[i]);
482 } else {
483 if (!SDDS_TransferColumnDefinition(SDDSout, SDDSin, yName[i], NULL)) {
484 fprintf(stderr, "error: problem transferring error definition for column %s\n", yName[i]);
485 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
486 }
487 (*yOutputErrorName)[i] = changeInformation(SDDSout, yName[i], yName[i], ySymbol, xName, xSymbol, errorTemplate, (*yOutputUnits)[i]);
488 }
489 } else {
490 (*yOutputErrorName)[i] = NULL;
491 }
492 }
493
494 if (colMatches) {
495 col = getMatchingSDDSNames(SDDSin, colMatch, colMatches, &cols, SDDS_MATCH_COLUMN);
496 for (i = 0; i < cols; i++) {
497 if (SDDS_GetColumnIndex(SDDSout, col[i]) < 0 && !SDDS_TransferColumnDefinition(SDDSout, SDDSin, col[i], NULL))
498 SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
499 }
500 SDDS_FreeStringArray(col, cols);
501 free(col);
502 }
503
504 if (!SDDS_TransferAllParameterDefinitions(SDDSout, SDDSin, SDDS_TRANSFER_KEEPOLD) ||
505 !SDDS_WriteLayout(SDDSout))
506 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
507
508 return 1;
509}
510
511char *changeInformation(SDDS_DATASET *SDDSout, char *name, char *nameRoot, char *symbolRoot,
512 char *xName, char *xSymbol, char **template, char *newUnits) {
513 char buffer1[SDDS_MAXLINE], buffer2[SDDS_MAXLINE], *ptr = NULL;
514
515 if (!SDDS_ChangeColumnInformation(SDDSout, "units", newUnits, SDDS_PASS_BY_VALUE | SDDS_SET_BY_NAME, name))
516 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
517
518 makeSubstitutions(buffer1, buffer2, template[2], nameRoot, symbolRoot, xName, xSymbol);
519 if (!SDDS_ChangeColumnInformation(SDDSout, "symbol", buffer2, SDDS_PASS_BY_VALUE | SDDS_SET_BY_NAME, name))
520 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
521
522 makeSubstitutions(buffer1, buffer2, template[1], nameRoot, symbolRoot, xName, xSymbol);
523 if (!SDDS_ChangeColumnInformation(SDDSout, "description", buffer2, SDDS_PASS_BY_VALUE | SDDS_SET_BY_NAME, name))
524 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
525
526 makeSubstitutions(buffer1, buffer2, template[0], nameRoot, symbolRoot, xName, xSymbol);
527 if (!SDDS_ChangeColumnInformation(SDDSout, "name", buffer2, SDDS_PASS_BY_VALUE | SDDS_SET_BY_NAME, name))
528 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
529
530 SDDS_CopyString(&ptr, buffer2);
531 return ptr;
532}
533
534void makeSubstitutions(char *buffer1, char *buffer2, char *template, char *nameRoot, char *symbolRoot,
535 char *xName, char *xSymbol) {
536 strcpy(buffer2, template);
537 replace_string(buffer1, buffer2, "%ySymbol", symbolRoot);
538 replace_string(buffer2, buffer1, "%xSymbol", xSymbol);
539 replace_string(buffer1, buffer2, "%yName", nameRoot);
540 replace_string(buffer2, buffer1, "%xName", xName);
541 strcpy(buffer1, buffer2);
542}
543
544void trapizoid(double *x, double *y, double *sx, double *sy, int64_t n, double *integ, double *error) {
545 double dx, dy;
546 int64_t i;
547
548 integ[0] = error[0] = 0;
549 for (i = 1; i < n; i++) {
550 dy = y[i] + y[i - 1];
551 dx = x[i] - x[i - 1];
552 integ[i] = integ[i - 1] + dy * dx;
553 if (sx && sy)
554 error[i] = error[i - 1] + sqr(dy) * (sqr(sx[i - 1]) + sqr(sx[i])) + sqr(dx) * (sqr(sy[i - 1]) + sqr(sy[i]));
555 else if (sx)
556 error[i] = error[i - 1] + sqr(dy) * (sqr(sx[i - 1]) + sqr(sx[i]));
557 else if (sy)
558 error[i] = error[i - 1] + sqr(dx) * (sqr(sy[i - 1]) + sqr(sy[i]));
559 else
560 error[i] = 0;
561 }
562 for (i = 0; i < n; i++) {
563 error[i] = sqrt(error[i]) / 2;
564 integ[i] /= 2;
565 }
566}
567
568long checkErrorNames(char **yErrorName, long yNames) {
569 long i;
570 if (yErrorName[0]) {
571 for (i = 1; i < yNames; i++)
572 if (!yErrorName[i])
573 return 0;
574 } else {
575 for (i = 1; i < yNames; i++)
576 if (yErrorName[i])
577 return 0;
578 }
579 return 1;
580}
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)
Definition SDDS_copy.c:578
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.
int64_t SDDS_CountRowsOfInterest(SDDS_DATASET *SDDS_dataset)
Counts the number of rows marked as "of interest" in the current data table.
double * SDDS_GetColumnInDoubles(SDDS_DATASET *SDDS_dataset, char *column_name)
Retrieves the data of a specified numerical column as an array of doubles, considering only rows mark...
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.
Definition SDDS_info.c:364
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.
Definition SDDS_info.c:41
int32_t SDDS_InitializeInput(SDDS_DATASET *SDDS_dataset, char *filename)
Definition SDDS_input.c:49
int32_t SDDS_Terminate(SDDS_DATASET *SDDS_dataset)
int32_t SDDS_ReadPage(SDDS_DATASET *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.
Definition SDDS_utils.c:432
void SDDS_RegisterProgramName(const char *name)
Registers the executable program name for use in error messages.
Definition SDDS_utils.c:288
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.
Definition SDDS_utils.c:342
int32_t SDDS_CopyString(char **target, const char *source)
Copies a source string to a target string with memory allocation.
Definition SDDS_utils.c:856
void * SDDS_Realloc(void *old_ptr, size_t new_size)
Reallocates memory to a new size.
Definition SDDS_utils.c:677
#define SDDS_STRING
Identifier for the string data type.
Definition SDDStypes.h:85
Utility functions for SDDS dataset manipulation and string array operations.
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
Definition array.c:59
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)
Definition scanargs.c:36
long processPipeOption(char **item, long items, unsigned long *flags)
Definition scanargs.c:356
void processFilenames(char *programName, char **input, char **output, unsigned long pipeFlags, long noWarnings, long *tmpOutputUsed)
Definition scanargs.c:390
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.