SDDS ToolKit Programs and Libraries for C and Python
All Classes Files Functions Variables Macros Pages
sddsinteg.c File Reference

Detailed Description

Performs numerical integration on specified columns of an SDDS dataset.

This program integrates specified columns of an SDDS dataset using customizable methods. It supports error propagation, template customization, and column-based exclusions.

Usage

sddsinteg [<inputfile>] [<outputfile>]
[-pipe[=input][,output]]
-integrate=<column-name>[,<sigma-name>] ...
[-exclude=<column-name>[,...]]
-versus=<column-name>[,<sigma-name>]
[-mainTemplates=<item>=<string>[,...]]
[-errorTemplates=<item>=<string>[,...]]
[-copycolumns=<list_of_column_names>]
[-method={trapazoid|GillMiller}]
[-printFinal[=bare][,stdout][,format=<string>]]

Options

Required Description
-integrate Column(s) to integrate. Optional sigma names for RMS error may be provided.
-versus Column to integrate against. Optionally specify sigma for RMS error.
Optional Description
-exclude Columns to exclude from integration.
-mainTemplates Customizes main templates for name, symbol, or description of integration columns.
-errorTemplates Customizes error templates for name, symbol, or description of error columns.
-copycolumns List of columns to copy to the output.
-method Integration method. trapazoid supports error propagation; GillMiller does not.
-printFinal Outputs the final integral value with formatting options.

Incompatibilities

  • -method=GillMiller disables error propagation for all integrated columns.
License
This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
Author
M. Borland, C. Saunders, R. Soliday, H. Shang

Definition in file sddsinteg.c.

#include "mdb.h"
#include "SDDS.h"
#include "SDDSutils.h"
#include "scan.h"
#include <ctype.h>

Go to the source code of this file.

Functions

void trapizoid (double *x, double *y, double *sx, double *sy, int64_t n, double *integ, double *error)
 
long checkErrorNames (char **yErrorName, long nIntegrals)
 
void makeSubstitutions (char *buffer1, char *buffer2, char *template, char *nameRoot, char *symbolRoot, char *xName, char *xSymbol)
 
char * changeInformation (SDDS_DATASET *SDDSout, char *name, char *nameRoot, char *symbolRoot, char *xName, char *xSymbol, char **template, char *newUnits)
 
long setupOutputFile (SDDS_DATASET *SDDSout, SDDS_DATASET *SDDSin, char *output, char ***yOutputName, char ***yOutputErrorName, char ***yOutputUnits, char *xName, char *xErrorName, char **yName, char **yErrorName, long yNames, char **mainTemplate, char **errorTemplate, long methodCode, short columnMajorOrder, char **colMatch, int32_t colMatches)
 
int main (int argc, char **argv)
 

Function Documentation

◆ changeInformation()

char * changeInformation ( SDDS_DATASET * SDDSout,
char * name,
char * nameRoot,
char * symbolRoot,
char * xName,
char * xSymbol,
char ** template,
char * newUnits )

Definition at line 511 of file sddsinteg.c.

512 {
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}
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
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
Definition SDDS_utils.c:432
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

◆ checkErrorNames()

long checkErrorNames ( char ** yErrorName,
long nIntegrals )

Definition at line 568 of file sddsinteg.c.

568 {
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}

◆ main()

int main ( int argc,
char ** argv )

Definition at line 138 of file sddsinteg.c.

138 {
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}
int GillMillerIntegration(double *integral, double *error, double *f, double *x, long n)
Performs numerical integration using the Gill-Miller method.
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_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_WritePage(SDDS_DATASET *SDDS_dataset)
Writes the current data table to the output file.
void SDDS_RegisterProgramName(const char *name)
Registers the executable program name for use in error messages.
Definition SDDS_utils.c:288
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
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
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 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.

◆ makeSubstitutions()

void makeSubstitutions ( char * buffer1,
char * buffer2,
char * template,
char * nameRoot,
char * symbolRoot,
char * xName,
char * xSymbol )

Definition at line 534 of file sddsinteg.c.

535 {
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}
int replace_string(char *t, char *s, char *orig, char *repl)
Replace all occurrences of one string with another string.

◆ setupOutputFile()

long setupOutputFile ( SDDS_DATASET * SDDSout,
SDDS_DATASET * SDDSin,
char * output,
char *** yOutputName,
char *** yOutputErrorName,
char *** yOutputUnits,
char * xName,
char * xErrorName,
char ** yName,
char ** yErrorName,
long yNames,
char ** mainTemplate,
char ** errorTemplate,
long methodCode,
short columnMajorOrder,
char ** colMatch,
int32_t colMatches )

Definition at line 425 of file sddsinteg.c.

429 {
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}
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_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_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.
int32_t SDDS_StringIsBlank(char *s)
Checks if a string is blank (contains only whitespace characters).

◆ trapizoid()

void trapizoid ( double * x,
double * y,
double * sx,
double * sy,
int64_t n,
double * integ,
double * error )

Definition at line 544 of file sddsinteg.c.

544 {
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}