TPCCLIB
Loading...
Searching...
No Matches
abssexam.c
Go to the documentation of this file.
1
7/*****************************************************************************/
8#include "tpcclibConfig.h"
9/*****************************************************************************/
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <math.h>
14/*****************************************************************************/
15#include "tpcextensions.h"
16#include "tpctac.h"
17#include "tpcstatist.h"
18#include "tpcabss.h"
19/*****************************************************************************/
20
21/*****************************************************************************/
22static char *info[] = {
23 "Scanditronics and GEMS on-line blood sampler have sometimes failed to",
24 "correctly record the count data from either of the two channels, seen as",
25 "substantially lower or zero counts.",
26 "Use this program to examine the channel coincidence count ratio from",
27 "multiple files. Program examines individual ABSS data files whether",
28 "the channel ratio exceeds a predermined limit, and if it does, then prints",
29 "the following information:",
30 " column #1: measurement date,",
31 " column #2: Lower-to-higher channel ratio (the closer to 1 the better),",
32 " column #3: channel 1 mean cps from ten highest overall measurements,",
33 " column #4: channel 2 mean cps from ten highest overall measurements,",
34 " column #5: file name.",
35 " ",
36 "Usage: @P [options] abssfile(s)",
37 " ",
38 "Options:",
39 " -limit=<limit for channel ratio>",
40 " Limit for the lower-to-higher channel ratio; by default, 0.7.",
41 " -stdoptions", // List standard options like --help, -v, etc
42 " ",
43 "Example:",
44 " @P *.bld",
45 " ",
46 "See also: abssfch, absscal, abssbkg, absszero, absstime",
47 " ",
48 "Keywords: input, blood, calibration, ABSS",
49 0};
50/*****************************************************************************/
51
52/*****************************************************************************/
53/* Turn on the globbing of the command line, since it is disabled by default in
54 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
55 In Unix&Linux wildcard command line processing is enabled by default. */
56/*
57#undef _CRT_glob
58#define _CRT_glob -1
59*/
60int _dowildcard = -1;
61/*****************************************************************************/
62
63/*****************************************************************************/
67int main(int argc, char **argv)
68{
69 int ai, help=0, version=0, verbose=1;
70 int ret, fileNr=0, printNr, firstfile=0;
71 char *cptr;
72 double ratioLimit=0.70;
73
74
75 /*
76 * Get arguments
77 */
78 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
79 /* Options */
80 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
81 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
82 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
83 if(strncasecmp(cptr, "LIMIT=", 6)==0 && strlen(cptr)>6) {
84 ratioLimit=atofVerified(cptr+6); if(!isnan(ratioLimit)) continue;
85 }
86 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
87 return(1);
88 } else break;
89
90 TPCSTATUS status; statusInit(&status);
91 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
92 status.verbose=verbose-3;
93
94 /* Print help or version? */
95 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
96 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
97 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
98
99 /* Process other arguments, starting from the first non-option */
100 for(; ai<argc; ai++) {
101 if(firstfile==0) firstfile=ai;
102 fileNr++;
103 }
104 /* Did we get all the information that we need? */
105 if(fileNr==0) {tpcPrintUsage(argv[0], info, stderr); return(1);}
106
107
108 /* In verbose mode print arguments and options */
109 if(verbose>1) {
110 printf("fileNr := %d\n", fileNr);
111 printf("ratioLimit := %g\n", ratioLimit);
112 fflush(stdout);
113 }
114
115
116 /*
117 * Process each file
118 */
119 char abssfile[FILENAME_MAX];
120 TAC abss; tacInit(&abss);
121 fileNr=printNr=0;
122 double r=1.0;
123 for(ai=firstfile; ai<argc; ai++) {
124
125 /*
126 * Read file
127 */
128 strlcpy(abssfile, argv[ai], FILENAME_MAX);
129 if(verbose>5) {fprintf(stdout, "%s\n", abssfile); fflush(stdout);}
130 ret=tacRead(&abss, abssfile, &status);
131 if(ret!=TPCERROR_OK) {
132 if(verbose>0) {
133 fprintf(stdout, "%s\n", abssfile); fflush(stdout);
134 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
135 fflush(stderr);
136 }
137 tacFree(&abss); continue;
138 }
139 /* This program is useful only for Scanditronics/GEMS sampler */
140 if(abss.format!=TAC_FORMAT_ABSS_GEMS &&
142 {
143 if(verbose>0) {
144 fprintf(stderr, "bypassed %s : %s format.\n",
145 abssfile, tacFormattxt(abss.format));
146 fflush(stderr);
147 }
148 tacFree(&abss); continue;
149 }
150
151 /*
152 * Examine the data
153 */
154 /* Divide coincidents by frame duration */
155 ret=abssCalculateCps(&abss);
156 if(ret!=0) {
157 if(verbose>0) {
158 fprintf(stderr, "bypassed %s : invalid contents.\n", abssfile);
159 fflush(stderr);
160 }
161 tacFree(&abss); continue;
162 }
163 /* Allocate extra column for the channel mean */
164 if(tacAllocateMore(&abss, 1)!=TPCERROR_OK) {
165 fprintf(stderr, "bypassed %s : invalid contents.\n", abssfile);
166 tacFree(&abss); continue;
167 }
168 abss.tacNr++;
169 strcpy(abss.c[abss.tacNr-1].name, "mean");
170 /* Calculate the channel mean */
171 ret=abssChannelMean(&abss, abss.c[abss.tacNr-1].y);
172 /* Sort data by the mean column */
173 if(ret==0) ret=tacSortByConc(&abss, abss.tacNr-1, &status);
174
175 /* Calculate the mean from the first 10 frames from the two channels */
176 //printf("x1=%g\n", abss.x1[0]);
177 unsigned int n, m1=0, m2=0;
178 int c1=0, c2=3;
179 double avg1, avg2;
180 n=10; if(n>(unsigned int)abss.sampleNr) n=(unsigned int)abss.sampleNr;
181 ret=statMeanSD(abss.c[c1].y, n, &avg1, NULL, &m1);
182 if(ret==0) ret=statMeanSD(abss.c[c2].y, n, &avg2, NULL, &m2);
183 if(ret!=0 || m1!=m2) {
184 if(verbose>0) {
185 fprintf(stderr, "bypassed %s : invalid contents.\n", abssfile);
186 fflush(stderr);
187 }
188 tacFree(&abss); continue;
189 }
190 /* and then the smaller-to-larger -ratio */
191 r=0.0;
192 if(avg1>avg2) {
193 if(fabs(avg1)>1.0E-10) r=avg2/avg1;
194 } else if(avg1<avg2) {
195 if(fabs(avg2)>1.0E-10) r=avg1/avg2;
196 } else if(avg1==avg2) {
197 r=1.0;
198 }
199 //printf("r=%g\n", r);
200
201 /* Get the date for printing */
202 char abssdate[128];
203 if(tacGetHeaderScanstarttime(&abss.h, abssdate, NULL)==TPCERROR_OK) {
204 abssdate[10]=(char)0;
205 } else {
206 strcpy(abssdate, "1900-01-01");
207 }
208
209 /* If ratio was not lower than the limit, then continue with next file
210 or print it in verbose mode
211 */
212 if(r>=ratioLimit) {
213 if(verbose>1) {
214 if(printNr==0)
215 fprintf(stdout, "%-10s\t%-s\t%-s\t%-s\t%-s\n",
216 "date", "ratio", "ch1", "ch2", "file");
217 fprintf(stdout, "%s\t%5.3f\t\%6.1f\t%6.1f\t%s\tOK\n",
218 abssdate, r, avg1, avg2, abssfile);
219 printNr++;
220 }
221 fflush(stdout);
222 tacFree(&abss);
223 fileNr++;
224 continue;
225 }
226
227 /* If ratio was lower than its limit, then report it */
228 if(printNr==0 && verbose>0)
229 fprintf(stdout, "%-10s\t%-s\t%-s\t%-s\t%-s\n",
230 "date", "ratio", "ch1", "ch2", "file");
231 fprintf(stdout, "%s\t%5.3f\t\%6.1f\t%6.1f\t%s",
232 abssdate, r, avg1, avg2, abssfile);
233 if(verbose>1) printf("\tFAILED\n"); else printf("\n");
234 fflush(stdout);
235 tacFree(&abss);
236 fileNr++; printNr++;
237 } /* next ABSS file */
238
239 /* Check that any files could be successfully processed */
240 if(fileNr<1) {
241 fprintf(stderr,
242 "Error: no on-line sampler datafile was successfully examined.\n");
243 return(1);
244 } else {
245 if(verbose>0)
246 fprintf(stdout, "Ready: %d datafiles were examined.\n", fileNr);
247 }
248
249 return(0);
250}
251/*****************************************************************************/
252
253/*****************************************************************************/
int abssChannelMean(TAC *abss, double *mean)
Definition abss.c:292
int abssCalculateCps(TAC *abss)
Definition abss.c:244
double atofVerified(const char *s)
Definition decpoint.c:75
int statMeanSD(double *data, unsigned int n, double *mean, double *sd, unsigned int *vn)
Definition mean.c:25
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
Definition proginfo.c:47
int tpcHtmlUsage(const char *program, char *text[], const char *path)
Definition proginfo.c:169
void tpcPrintBuild(const char *program, FILE *fp)
Definition proginfo.c:339
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
Definition proginfo.c:114
void statusInit(TPCSTATUS *s)
Definition statusmsg.c:104
char * errorMsg(tpcerror e)
Definition statusmsg.c:68
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition stringext.c:632
char name[MAX_TACNAME_LEN+1]
Definition tpctac.h:81
double * y
Definition tpctac.h:75
Definition tpctac.h:87
tacformat format
Definition tpctac.h:93
int sampleNr
Definition tpctac.h:89
IFT h
Optional (but often useful) header information.
Definition tpctac.h:141
TACC * c
Definition tpctac.h:117
int tacNr
Definition tpctac.h:91
int verbose
Verbose level, used by statusPrint() etc.
tpcerror error
Error code.
void tacFree(TAC *tac)
Definition tac.c:106
void tacInit(TAC *tac)
Definition tac.c:24
int tacAllocateMore(TAC *tac, int tacNr)
Definition tac.c:178
int tacGetHeaderScanstarttime(IFT *h, char *s, TPCSTATUS *status)
Definition tacift.c:372
int tacRead(TAC *d, const char *fname, TPCSTATUS *status)
Definition tacio.c:413
char * tacFormattxt(tacformat c)
Definition tacio.c:98
int tacSortByConc(TAC *d, const int i, TPCSTATUS *status)
Definition tacorder.c:122
Header file for libtpcabss.
Header file for library libtpcextensions.
@ TPCERROR_OK
No error.
Header file for libtpcstatist.
Header file for library libtpctac.
@ TAC_FORMAT_ABSS_GEMS
GEMS ABSS data; reading supported.
Definition tpctac.h:54
@ TAC_FORMAT_ABSS_SCANDITRONICS
Scanditronics ABSS data; reading supported.
Definition tpctac.h:53