90char *option[N_OPTIONS] = {
105 "wstandarddeviations",
120char *optionSuffix[N_OPTIONS] = {
155 char *weightColumnName;
156 long optionCode, sumPower;
158 char *percentileString;
167 char *sourceColumn, *weightColumn, *resultColumn, *functionOf;
168 long optionCode, resultIndex, sumPower;
170 char *percentileString;
173 double *value1, *value2, *value3, *value4;
178long addStatRequests(
STAT_REQUEST **statRequest,
long requests,
char **item,
long items,
long code,
double percentile,
long power,
char *functionOf,
long weighted,
char *percentileString);
183int compute_mean_exclude_min_max(
double *value,
double *data,
long n);
185static char *USAGE =
"sddsenvelope [<input>] [<output>] [options]\n"
187 " -copy=<column-names> Copy specified columns.\n"
188 " -pipe=[input][,output] Use pipe for input/output.\n"
189 " -nowarnings Suppress warnings.\n"
190 " -maximum=<column-names> Compute maximum values.\n"
191 " -minimum=<column-names> Compute minimum values.\n"
192 " -cmaximum=<indep-column>,<column-names> Conditional maximum based on an independent column.\n"
193 " -cminimum=<indep-column>,<column-names> Conditional minimum based on an independent column.\n"
194 " -pmaximum=<indep-parameter>,<column-names> Parameter-based maximum.\n"
195 " -pminimum=<indep-parameter>,<column-names> Parameter-based minimum.\n"
196 " -largest=<column-names> Compute the largest absolute values.\n"
197 " -signedLargest=<column-names> Compute the largest signed values.\n"
198 " -mean=<column-names> Compute mean values.\n"
199 " -sum=<power>,<column-names> Compute sum with power.\n"
200 " -median=<column-names> Compute median values.\n"
201 " -decilerange=<column-names> Compute decile range.\n"
202 " -percentile=<percentage>,<column-names> Compute specified percentile.\n"
203 " -standarddeviation=<column-names> Compute standard deviations.\n"
204 " -rms=<column-names> Compute RMS values.\n"
205 " -sigma=<column-names> Compute sigma values.\n"
206 " -slope=<indep-parameter>,<column-names> Compute slope for linear fit.\n"
207 " -intercept=<indep-parameter>,<column-names> Compute intercept for linear fit.\n"
208 " -wmean=<weightColumn>,<columnNames> Compute weighted mean.\n"
209 " -wstandarddeviation=<weightColumn>,<columnNames> Compute weighted standard deviation.\n"
210 " -wrms=<weightColumn>,<columnNames> Compute weighted RMS.\n"
211 " -wsigma=<weightColumn>,<columnNames> Compute weighted sigma.\n"
212 " -majorOrder=row|column Set major order.\n\n"
213 "Processes pages from <input> to produce <output> with\n"
214 "one page containing the specified quantities across pages\n"
215 "for each row of the specified columns.\n"
216 "Program by Michael Borland. (" __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
")";
218int main(
int argc,
char **argv) {
223 SCANNED_ARG *scanned;
225 long i_arg, code, power, iStat, pages, nowarnings = 0;
226 int64_t i, rows, firstRows;
227 char *input, *output;
228 double *inputData, *otherData, indepData, *weight;
229 unsigned long pipeFlags, majorOrderFlag;
230 double decilePoint[2] = {10.0, 90.0}, decileResult[2];
231 double percentilePoint, percentileResult;
233 short columnMajorOrder = -1;
236 argc =
scanargs(&scanned, argc, argv);
238 bomb(
"too few arguments", USAGE);
242 input = output = NULL;
245 stats = requests = pipeFlags = 0;
246 rows = firstRows = i = 0;
248 for (i_arg = 1; i_arg < argc; i_arg++) {
249 if (scanned[i_arg].arg_type == OPTION) {
251 switch (code =
match_string(scanned[i_arg].list[0], option, N_OPTIONS, 0)) {
256 case SET_SIGNEDLARGEST:
264 if (scanned[i_arg].n_items < 2) {
265 fprintf(stderr,
"error: invalid -%s syntax\n", option[code]);
268 requests = addStatRequests(&request, requests, scanned[i_arg].list + 1, scanned[i_arg].n_items - 1, code, 0, 0, NULL, 0, NULL);
274 if (scanned[i_arg].n_items < 3) {
275 fprintf(stderr,
"error: invalid -%s syntax\n", option[code]);
279 requests = addStatRequests(&request, requests, scanned[i_arg].list + 1, scanned[i_arg].n_items - 1, code, 0, 0, NULL, 1, NULL);
282 if (scanned[i_arg].n_items < 3) {
283 fprintf(stderr,
"error: invalid -%s syntax\n", option[code]);
286 if (sscanf(scanned[i_arg].list[1],
"%ld", &power) != 1 || power < 1) {
287 fprintf(stderr,
"error: invalid -%s syntax--bad power in field %s\n", option[code], scanned[i_arg].list[2]);
290 requests = addStatRequests(&request, requests, scanned[i_arg].list + 2, scanned[i_arg].n_items - 2, code, 0, power, NULL, 0, NULL);
293 if (scanned[i_arg].n_items < 3) {
294 fprintf(stderr,
"error: invalid -%s syntax\n", option[code]);
297 if (sscanf(scanned[i_arg].list[1],
"%lf", &percentile) != 1 || percentile < 0 || percentile > 100) {
298 fprintf(stderr,
"error: invalid -%s syntax--bad percentage in field %s\n", option[code], scanned[i_arg].list[1]);
301 requests = addStatRequests(&request, requests, scanned[i_arg].list + 2, scanned[i_arg].n_items - 2, code, percentile, 0, NULL, 0, scanned[i_arg].list[1]);
309 if (scanned[i_arg].n_items < 3) {
310 fprintf(stderr,
"error: invalid -%s syntax\n", option[code]);
313 requests = addStatRequests(&request, requests, scanned[i_arg].list + 2, scanned[i_arg].n_items - 2, code, 0, 0, scanned[i_arg].list[1], 0, NULL);
316 if (!
processPipeOption(scanned[i_arg].list + 1, scanned[i_arg].n_items - 1, &pipeFlags))
322 case SET_MAJOR_ORDER:
324 scanned[i_arg].n_items--;
325 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)))
326 SDDS_Bomb(
"invalid -majorOrder syntax/values");
327 if (majorOrderFlag & SDDS_COLUMN_MAJOR_ORDER)
328 columnMajorOrder = 1;
329 else if (majorOrderFlag & SDDS_ROW_MAJOR_ORDER)
330 columnMajorOrder = 0;
333 fprintf(stderr,
"error: unknown option '%s' given\n", scanned[i_arg].list[0]);
340 input = scanned[i_arg].list[0];
342 output = scanned[i_arg].list[0];
360 SDDS_Bomb(
"empty data page in input file");
363 if (!(stat = compileStatDefinitions(&inTable, &stats, request, requests))) {
367 if (!setupOutputFile(&outTable, output, &inTable, stat, stats, rows, columnMajorOrder)) {
371 fprintf(stderr,
"Error setting up output file.\n");
374 }
else if (firstRows != rows)
375 SDDS_Bomb(
"inconsistent number of rows in input file");
376 for (iStat = 0; iStat < stats; iStat++) {
377 if (stat[iStat].optionCode == SET_COPY) {
378 if (code == 1 && !(stat[iStat].copy =
SDDS_GetColumn(&inTable, stat[iStat].sourceColumn)))
382 stat[iStat].copy = NULL;
385 switch (stat[iStat].optionCode) {
388 for (i = 0; i < rows; i++)
389 stat[iStat].value1[i] = inputData[i];
391 for (i = 0; i < rows; i++)
392 if (stat[iStat].value1[i] > inputData[i])
393 stat[iStat].value1[i] = inputData[i];
397 for (i = 0; i < rows; i++)
398 stat[iStat].value1[i] = inputData[i];
400 for (i = 0; i < rows; i++)
401 if (stat[iStat].value1[i] < inputData[i])
402 stat[iStat].value1[i] = inputData[i];
409 for (i = 0; i < rows; i++) {
410 stat[iStat].value2[i] = inputData[i];
411 stat[iStat].value1[i] = otherData[i];
414 for (i = 0; i < rows; i++)
415 if (stat[iStat].value2[i] > inputData[i]) {
416 stat[iStat].value2[i] = inputData[i];
417 stat[iStat].value1[i] = otherData[i];
426 for (i = 0; i < rows; i++) {
427 stat[iStat].value2[i] = inputData[i];
428 stat[iStat].value1[i] = otherData[i];
431 for (i = 0; i < rows; i++)
432 if (stat[iStat].value2[i] < inputData[i]) {
433 stat[iStat].value2[i] = inputData[i];
434 stat[iStat].value1[i] = otherData[i];
440 for (i = 0; i < rows; i++)
441 stat[iStat].value1[i] = fabs(inputData[i]);
443 for (i = 0; i < rows; i++)
444 if (stat[iStat].value1[i] < fabs(inputData[i]))
445 stat[iStat].value1[i] = fabs(inputData[i]);
447 case SET_SIGNEDLARGEST:
449 for (i = 0; i < rows; i++)
450 stat[iStat].value1[i] = inputData[i];
452 for (i = 0; i < rows; i++)
453 if (fabs(stat[iStat].value1[i]) < fabs(inputData[i]))
454 stat[iStat].value1[i] = inputData[i];
458 for (i = 0; i < rows; i++)
459 stat[iStat].value1[i] = inputData[i];
461 for (i = 0; i < rows; i++)
462 stat[iStat].value1[i] += inputData[i];
467 for (i = 0; i < rows; i++) {
468 stat[iStat].sumWeight[i] += weight[i];
469 stat[iStat].value1[i] += inputData[i] * weight[i];
476 for (i = 0; i < rows; i++) {
477 stat[iStat].value1[i] = inputData[i];
478 stat[iStat].value2[i] = inputData[i] * inputData[i];
481 for (i = 0; i < rows; i++) {
482 stat[iStat].value1[i] += inputData[i];
483 stat[iStat].value2[i] += inputData[i] * inputData[i];
490 for (i = 0; i < rows; i++) {
491 stat[iStat].sumWeight[i] += weight[i];
492 stat[iStat].value1[i] += inputData[i] * weight[i];
493 stat[iStat].value2[i] += inputData[i] * inputData[i] * weight[i];
499 for (i = 0; i < rows; i++)
500 stat[iStat].value1[i] = inputData[i] * inputData[i];
502 for (i = 0; i < rows; i++)
503 stat[iStat].value1[i] += inputData[i] * inputData[i];
508 for (i = 0; i < rows; i++) {
509 stat[iStat].sumWeight[i] += weight[i];
510 stat[iStat].value1[i] += inputData[i] * inputData[i] * weight[i];
516 for (i = 0; i < rows; i++)
517 stat[iStat].value1[i] =
ipow(inputData[i], stat[iStat].sumPower);
519 for (i = 0; i < rows; i++)
520 stat[iStat].value1[i] +=
ipow(inputData[i], stat[iStat].sumPower);
524 fprintf(stderr,
"error: unable to get value of parameter %s\n", stat[iStat].functionOf);
528 for (i = 0; i < rows; i++) {
529 stat[iStat].value2[i] = inputData[i];
530 stat[iStat].value1[i] = indepData;
533 for (i = 0; i < rows; i++) {
534 if (stat[iStat].value2[i] > inputData[i]) {
535 stat[iStat].value2[i] = inputData[i];
536 stat[iStat].value1[i] = indepData;
542 fprintf(stderr,
"error: unable to get value of parameter %s\n", stat[iStat].functionOf);
546 for (i = 0; i < rows; i++) {
547 stat[iStat].value2[i] = inputData[i];
548 stat[iStat].value1[i] = indepData;
551 for (i = 0; i < rows; i++) {
552 if (stat[iStat].value2[i] < inputData[i]) {
553 stat[iStat].value2[i] = inputData[i];
554 stat[iStat].value1[i] = indepData;
561 fprintf(stderr,
"error: unable to get value of parameter %s\n", stat[iStat].functionOf);
571 for (i = 0; i < rows; i++) {
572 stat[iStat].value1[i] = indepData;
573 stat[iStat].value2[i] = indepData * indepData;
574 stat[iStat].value3[i] = inputData[i];
575 stat[iStat].value4[i] = indepData * inputData[i];
578 for (i = 0; i < rows; i++) {
579 stat[iStat].value1[i] += indepData;
580 stat[iStat].value2[i] += indepData * indepData;
581 stat[iStat].value3[i] += inputData[i];
582 stat[iStat].value4[i] += indepData * inputData[i];
590 for (i = 0; i < rows; i++) {
591 stat[iStat].array[i] =
tmalloc(
sizeof(*stat[iStat].array[i]));
592 stat[iStat].array[i][pages - 1] = inputData[i];
595 for (i = 0; i < rows; i++) {
596 stat[iStat].array[i] =
SDDS_Realloc(stat[iStat].array[i],
sizeof(*stat[iStat].array[i]) * pages);
597 stat[iStat].array[i][pages - 1] = inputData[i];
602 SDDS_Bomb(
"invalid statistic code (accumulation loop)");
610 for (iStat = 0; iStat < stats; iStat++) {
611 switch (stat[iStat].optionCode) {
620 case SET_SIGNEDLARGEST:
624 for (i = 0; i < rows; i++)
625 stat[iStat].value1[i] /= pages;
628 for (i = 0; i < rows; i++)
629 if (stat[iStat].sumWeight[i])
630 stat[iStat].value1[i] /= stat[iStat].sumWeight[i];
633 fprintf(stderr,
"warning: the total weight for the %" PRId64
"th row of %s is zero.\n", i + 1, stat[iStat].sourceColumn);
634 stat[iStat].value1[i] = DBL_MAX;
639 stat[iStat].value1[i] = DBL_MAX;
641 for (i = 0; i < rows; i++) {
643 if ((tmp1 = stat[iStat].value2[i] / pages - sqr(stat[iStat].value1[i] / pages)) <= 0)
644 stat[iStat].value1[i] = 0;
646 stat[iStat].value1[i] = sqrt(tmp1 * pages / (pages - 1.0));
651 stat[iStat].value1[i] = DBL_MAX;
653 for (i = 0; i < rows; i++) {
655 if (stat[iStat].sumWeight[i]) {
656 if ((tmp1 = stat[iStat].value2[i] / stat[iStat].sumWeight[i] - sqr(stat[iStat].value1[i] / stat[iStat].sumWeight[i])) <= 0)
657 stat[iStat].value1[i] = 0;
659 stat[iStat].value1[i] = sqrt(tmp1 * pages / (pages - 1.0));
662 fprintf(stderr,
"Warning, the total weight for the %" PRId64
"th row of %s is zero.\n", i + 1, stat[iStat].sourceColumn);
663 stat[iStat].value1[i] = DBL_MAX;
669 stat[iStat].value1[i] = DBL_MAX;
671 for (i = 0; i < rows; i++) {
673 if ((tmp1 = stat[iStat].value2[i] / pages - sqr(stat[iStat].value1[i] / pages)) <= 0)
674 stat[iStat].value1[i] = 0;
676 stat[iStat].value1[i] = sqrt(tmp1 / (pages - 1.0));
681 stat[iStat].value1[i] = DBL_MAX;
683 for (i = 0; i < rows; i++) {
685 if (stat[iStat].sumWeight[i]) {
686 if ((tmp1 = stat[iStat].value2[i] / stat[iStat].sumWeight[i] - sqr(stat[iStat].value1[i] / stat[iStat].sumWeight[i])) <= 0)
687 stat[iStat].value1[i] = 0;
689 stat[iStat].value1[i] = sqrt(tmp1 / (pages - 1.0));
692 fprintf(stderr,
"Warning, the total weight for the %" PRId64
"th row of %s is zero.\n", i + 1, stat[iStat].sourceColumn);
693 stat[iStat].value1[i] = DBL_MAX;
698 for (i = 0; i < rows; i++)
699 stat[iStat].value1[i] = sqrt(stat[iStat].value1[i] / pages);
702 for (i = 0; i < rows; i++) {
703 if (stat[iStat].sumWeight[i])
704 stat[iStat].value1[i] = sqrt(stat[iStat].value1[i] / stat[iStat].sumWeight[i]);
707 fprintf(stderr,
"Warning, the total weight for the %" PRId64
"th row of %s is zero.\n", i + 1, stat[iStat].sourceColumn);
708 stat[iStat].value1[i] = DBL_MAX;
713 for (i = 0; i < rows; i++) {
715 D = pages * stat[iStat].value2[i] - stat[iStat].value1[i] * stat[iStat].value1[i];
716 stat[iStat].value1[i] = (pages * stat[iStat].value4[i] - stat[iStat].value1[i] * stat[iStat].value3[i]) / D;
720 for (i = 0; i < rows; i++) {
722 D = pages * stat[iStat].value2[i] - stat[iStat].value1[i] * stat[iStat].value1[i];
723 stat[iStat].value1[i] = (stat[iStat].value2[i] * stat[iStat].value3[i] - stat[iStat].value1[i] * stat[iStat].value4[i]) / D;
727 for (i = 0; i < rows; i++) {
728 compute_median(&stat[iStat].value1[i], stat[iStat].array[i], pages);
732 for (i = 0; i < rows; i++) {
734 stat[iStat].value1[i] = 0;
736 stat[iStat].value1[i] = decileResult[1] - decileResult[0];
740 percentilePoint = stat[iStat].percentile;
741 for (i = 0; i < rows; i++) {
742 if (!
compute_percentiles(&percentileResult, &percentilePoint, 1, stat[iStat].array[i], pages))
743 stat[iStat].value1[i] = 0;
745 stat[iStat].value1[i] = percentileResult;
749 for (i = 0; i < rows; i++)
750 if (!compute_mean_exclude_min_max(&(stat[iStat].value1[i]), stat[iStat].array[i], pages))
751 stat[iStat].value1[i] = 0;
754 SDDS_Bomb(
"invalid statistic code (final loop)");
757 if (stat[iStat].optionCode == SET_COPY) {
758 if (!
SDDS_SetColumn(&outTable, SDDS_SET_BY_NAME, stat[iStat].copy, rows, stat[iStat].resultColumn)) {
759 fprintf(stderr,
"error setting column values for column %s\n", stat[iStat].resultColumn);
762 }
else if (!
SDDS_SetColumnFromDoubles(&outTable, SDDS_SET_BY_NAME, stat[iStat].value1, rows, stat[iStat].resultColumn)) {
763 fprintf(stderr,
"error setting column values for column %s\n", stat[iStat].resultColumn);
766 if (stat[iStat].value1)
767 free(stat[iStat].value1);
768 if (stat[iStat].value2)
769 free(stat[iStat].value2);
770 if (stat[iStat].value3)
771 free(stat[iStat].value3);
772 if (stat[iStat].value4)
773 free(stat[iStat].value4);
774 if (stat[iStat].copy)
775 free(stat[iStat].copy);
776 if (stat[iStat].array) {
777 for (i = 0; i < rows; i++) {
778 free(stat[iStat].array[i]);
780 free(stat[iStat].array);
782 if (stat[iStat].sumWeight)
783 free(stat[iStat].sumWeight);
784 free(stat[iStat].sourceColumn);
785 free(stat[iStat].resultColumn);
786 stat[iStat].value1 = stat[iStat].value2 = stat[iStat].value3 = stat[iStat].value4 = NULL;
787 stat[iStat].copy = NULL;
788 stat[iStat].array = NULL;
796long addStatRequests(
STAT_REQUEST **statRequest,
long requests,
char **item,
long items,
long code,
double percentile,
long power,
char *functionOf,
long weighted,
char *percentileString) {
799 if (weighted != 0 && weighted != 1)
800 SDDS_Bomb(
"addStatRequests: weighted parameter should be either 0 or 1");
801 if (code == SET_PERCENTILE && (!percentileString || !strlen(percentileString))) {
802 fprintf(stderr,
"Percentile specification is incorrect: percentile=%e, percentileString=%s\n", percentile, percentileString ? percentileString :
"NULL");
805 *statRequest =
SDDS_Realloc(*statRequest,
sizeof(**statRequest) * (requests + items - weighted));
806 for (i = 0; i < items - weighted; i++) {
808 (*statRequest)[requests + i].weightColumnName = item[0];
810 (*statRequest)[requests + i].weightColumnName = NULL;
811 (*statRequest)[i + requests].columnName = item[i + weighted];
812 (*statRequest)[i + requests].optionCode = code;
813 (*statRequest)[i + requests].sumPower = power;
814 (*statRequest)[i + requests].percentile = percentile;
815 (*statRequest)[i + requests].percentileString = percentileString;
816 (*statRequest)[i + requests].functionOf = functionOf;
819 return items + requests - weighted;
824 long iReq, iStat, iName;
826 char s[SDDS_MAXLINE];
830 stat =
tmalloc(
sizeof(*stat) * requests);
831 for (iReq = 0; iReq < requests; iReq++) {
833 stat =
SDDS_Realloc(stat,
sizeof(*stat) * (*stats += 10));
836 sprintf(s,
"error: column %s not found input file", request[iReq].columnName);
840 stat[iStat].weightColumn = request[iReq].weightColumnName;
841 stat[iStat].sourceColumn = request[iReq].columnName;
842 stat[iStat].optionCode = request[iReq].optionCode;
843 stat[iStat].percentile = request[iReq].percentile;
844 stat[iStat].percentileString = request[iReq].percentileString;
845 stat[iStat].sumPower = request[iReq].sumPower;
846 stat[iStat].value1 = stat[iStat].value2 = stat[iStat].value3 = stat[iStat].value4 = NULL;
847 stat[iStat].array = NULL;
848 stat[iStat].copy = NULL;
849 stat[iStat].sumWeight = NULL;
850 if ((stat[iStat].functionOf = request[iReq].functionOf)) {
851 if (stat[iStat].optionCode != SET_CMAXIMA && stat[iStat].optionCode != SET_CMINIMA) {
853 sprintf(s,
"error: parameter %s not found input file (1)", request[iReq].functionOf);
859 sprintf(s,
"error: column %s not found input file (1)", request[iReq].functionOf);
871 sprintf(s,
"no columns selected for wildcard sequence %s", request[iReq].columnName);
875 if (iStat + columnNames > *stats)
876 stat =
SDDS_Realloc(stat,
sizeof(*stat) * (*stats = iStat + columnNames + 10));
877 for (iName = 0; iName < columnNames; iName++) {
878 stat[iStat + iName].weightColumn = request[iReq].weightColumnName;
879 stat[iStat + iName].sourceColumn = columnName[iName];
880 stat[iStat + iName].optionCode = request[iReq].optionCode;
881 stat[iStat + iName].sumPower = request[iReq].sumPower;
882 stat[iStat + iName].percentile = request[iReq].percentile;
883 stat[iStat + iName].percentileString = request[iReq].percentileString;
884 stat[iStat + iName].value1 = stat[iStat + iName].value2 = stat[iStat + iName].value3 = stat[iStat + iName].value4 = NULL;
885 stat[iStat + iName].array = NULL;
886 stat[iStat + iName].copy = NULL;
887 stat[iStat + iName].sumWeight = NULL;
888 if ((stat[iStat + iName].functionOf = request[iReq].functionOf) && iName == 0) {
889 if (stat[iStat + iName].optionCode != SET_CMAXIMA && stat[iStat + iName].optionCode != SET_CMINIMA) {
891 sprintf(s,
"error: parameter %s not found input file (2)", request[iReq].functionOf);
897 sprintf(s,
"error: column %s not found input file (2)", request[iReq].functionOf);
904 iStat += columnNames;
910 for (iStat = 0; iStat < *stats; iStat++) {
911 switch (stat[iStat].optionCode) {
913 strcpy(s, stat[iStat].sourceColumn);
916 if (stat[iStat].sumPower == 1)
917 sprintf(s,
"%s%s", stat[iStat].sourceColumn, optionSuffix[stat[iStat].optionCode]);
919 sprintf(s,
"%s%ld%s", stat[iStat].sourceColumn, stat[iStat].sumPower, optionSuffix[stat[iStat].optionCode]);
922 sprintf(s,
"%s%s%s", stat[iStat].sourceColumn, stat[iStat].percentileString, optionSuffix[stat[iStat].optionCode]);
926 sprintf(s,
"%s%s%s", stat[iStat].functionOf, optionSuffix[stat[iStat].optionCode], stat[iStat].sourceColumn);
930 sprintf(s,
"%s%s%s", stat[iStat].functionOf, optionSuffix[stat[iStat].optionCode], stat[iStat].sourceColumn);
933 sprintf(s,
"%s%s", stat[iStat].sourceColumn, optionSuffix[stat[iStat].optionCode]);
944 char s[SDDS_MAXLINE], *symbol, *symbol1, *units1;
948 if (columnMajorOrder != -1)
949 outTable->layout.data_mode.column_major = columnMajorOrder;
951 outTable->layout.data_mode.column_major = inTable->layout.data_mode.column_major;
952 for (column = 0; column < stats; column++) {
953 stat[column].value1 = calloc(
sizeof(*stat[column].value1), rows);
954 stat[column].value2 = stat[column].value3 = stat[column].value4 = NULL;
955 if (stat[column].optionCode == SET_SDS || stat[column].optionCode == SET_SIGMAS ||
956 stat[column].optionCode == SET_WSDS || stat[column].optionCode == SET_WSIGMAS ||
957 stat[column].optionCode == SET_PMINIMA || stat[column].optionCode == SET_PMAXIMA ||
958 stat[column].optionCode == SET_CMINIMA || stat[column].optionCode == SET_CMAXIMA)
959 stat[column].value2 = calloc(
sizeof(*stat[column].value2), rows);
960 if (stat[column].optionCode == SET_INTERCEPT || stat[column].optionCode == SET_SLOPE) {
961 stat[column].value2 = malloc(
sizeof(*stat[column].value2) * rows);
962 stat[column].value3 = malloc(
sizeof(*stat[column].value3) * rows);
963 stat[column].value4 = malloc(
sizeof(*stat[column].value4) * rows);
965 if (stat[column].optionCode == SET_WSDS || stat[column].optionCode == SET_WSIGMAS ||
966 stat[column].optionCode == SET_WRMSS || stat[column].optionCode == SET_WMEANS)
967 stat[column].sumWeight = calloc(
sizeof(*stat[column].sumWeight), rows);
968 if (stat[column].optionCode == SET_MEDIAN || stat[column].optionCode == SET_DRANGE ||
969 stat[column].optionCode == SET_PERCENTILE || stat[column].optionCode == SET_EXMM_MEAN) {
970 stat[column].array =
tmalloc(
sizeof(*stat[column].array) * rows);
973 sprintf(s,
"Problem transferring definition of column %s to %s\n", stat[column].sourceColumn, stat[column].resultColumn);
979 fprintf(stderr,
"Error: problem setting description for column %s\n", stat[column].resultColumn);
982 if (stat[column].optionCode > 0) {
984 fprintf(stderr,
"Error: problem setting type for column %s\n", stat[column].resultColumn);
990 switch (stat[column].optionCode) {
995 if (stat[column].sumPower == 1)
996 sprintf(s,
"%s[%s]", optionSuffix[stat[column].optionCode], symbol);
998 sprintf(s,
"%s[%s$a%ld$n]", optionSuffix[stat[column].optionCode], symbol, stat[column].sumPower);
1000 case SET_PERCENTILE:
1001 sprintf(s,
"%s[%s,%g]", optionSuffix[stat[column].optionCode], symbol, stat[column].percentile);
1008 symbol1 = stat[column].functionOf;
1009 sprintf(s,
"%s[%s:%s]", optionSuffix[stat[column].optionCode], symbol, symbol1);
1014 fprintf(stderr,
"Error: problem setting units for column %s (1)\n", stat[column].resultColumn);
1018 fprintf(stderr,
"Error: problem setting units for column %s (2)\n", stat[column].resultColumn);
1027 symbol1 = stat[column].functionOf;
1028 sprintf(s,
"%s[%s:%s]", optionSuffix[stat[column].optionCode], symbol, symbol1);
1033 fprintf(stderr,
"Error: problem setting units for column %s\n", stat[column].resultColumn);
1037 fprintf(stderr,
"Error: problem setting units for column %s\n", stat[column].resultColumn);
1046 symbol1 = stat[column].functionOf;
1047 sprintf(s,
"%s[%s:%s]", optionSuffix[stat[column].optionCode], symbol, symbol1);
1050 sprintf(s,
"%s[%s]", optionSuffix[stat[column].optionCode], symbol);
1055 fprintf(stderr,
"Error: problem setting symbol for column %s\n", stat[column].resultColumn);
1064int compute_mean_exclude_min_max(
double *value,
double *data,
long n) {
1068 max = -(min = DBL_MAX);
1069 if (n <= 0 || !data || !value)
1071 for (i = 0; i < n; i++) {
1077 for (i = 0; i < n; i++) {
1078 if (data[i] == min || data[i] == max)
1086 *value = sum / count;
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
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_SetColumn(SDDS_DATASET *SDDS_dataset, int32_t mode, void *data, int64_t rows,...)
Sets the values for one data column in the current data table of an SDDS dataset.
int32_t SDDS_GetParameterInformation(SDDS_DATASET *SDDS_dataset, char *field_name, void *memory, int32_t mode,...)
Retrieves information about a specified parameter in the SDDS dataset.
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.
void SDDS_SetError(char *error_text)
Records an error message in the SDDS error stack.
int32_t SDDS_GetParameterIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named parameter in the SDDS dataset.
int32_t SDDS_GetColumnIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named column in the SDDS dataset.
char ** SDDS_GetColumnNames(SDDS_DATASET *SDDS_dataset, int32_t *number)
Retrieves the names of all columns 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_NumberOfErrors()
Retrieves the number of errors recorded by SDDS library routines.
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.
#define SDDS_LONG
Identifier for the signed 32-bit integer data type.
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
double ipow(const double x, const int64_t p)
Compute x raised to the power p (x^p).
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)
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.
int has_wildcards(char *template)
Check if a template string contains any wildcard characters.