9#include "tpcclibConfig.h"
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 calculate the channel1-to-channel2 ratio of coincidence",
27 "counts, or, to correct the data from failed channel using user-provided",
28 "correct channel1-to-channel2 ratio.",
30 "Usage: @P [options] abssfile [ratio]",
34 " Name for corrected file; by default the original file is overwritten.",
36 " Set the channel (1 or 2) to fix; by default the channel with lower",
40 "Example 1: calculate channel1-to-channel2 ratio.",
42 "Example 2: correct the failed data with given channel1-to-channel2 ratio.",
43 " @P -o=us1328_corr.bld us1328.bld 0.982",
45 "See also: abssexam, absscal, abssbkg, absszero, absstime",
47 "Keywords: input, blood, calibration, ABSS",
66int main(
int argc,
char **argv)
68 int ai, help=0, version=0, verbose=1;
69 char abssfile[FILENAME_MAX], outfile[FILENAME_MAX];
71 double chRatio=nan(
"");
79 if(argc==1) {
tpcPrintUsage(argv[0], info, stderr);
return(1);}
80 abssfile[0]=outfile[0]=(char)0;
82 for(ai=1; ai<argc; ai++)
if(*argv[ai]==
'-') {
84 cptr=argv[ai]+1;
if(*cptr==
'-') cptr++;
if(!*cptr)
continue;
85 if(strncasecmp(cptr,
"O=", 2)==0 && strlen(cptr)>2) {
86 strlcpy(outfile, cptr+2, FILENAME_MAX);
continue;
87 }
else if(strncasecmp(cptr,
"FIX=", 4)==0 && strlen(cptr)>4) {
88 chFix=atoi(cptr+4);
if(chFix==1 || chFix==2)
continue;
90 fprintf(stderr,
"Error: invalid option '%s'.\n", argv[ai]);
99 if(help==2) {
tpcHtmlUsage(argv[0], info,
"");
return(0);}
104 if(ai<argc)
strlcpy(abssfile, argv[ai++], FILENAME_MAX);
106 if(
atofCheck(argv[ai], &chRatio) || chRatio<=0.0) {
107 fprintf(stderr,
"Error: invalid channel ratio '%s'.\n", argv[ai]);
113 fprintf(stderr,
"Error: invalid argument '%s'.\n", argv[ai]);
118 fprintf(stderr,
"Error: missing command-line argument; use option --help\n");
121 if(!outfile[0]) strcpy(outfile, abssfile);
122 if(chFix!=0 && isnan(chRatio)) {
123 fprintf(stderr,
"Error: missing channel ratio.\n");
130 printf(
"abssfile := %s\n", abssfile);
131 if(!isnan(chRatio)) printf(
"chRatio := %g\n", chRatio);
132 if(chFix!=0) printf(
"chFix := %d\n", chFix);
133 printf(
"outfile := %s\n", outfile);
140 if(verbose>1) printf(
"reading %s\n", abssfile);
142 ret=
tacRead(&abss, abssfile, &status);
148 printf(
"sampleNr := %d\n", abss.
sampleNr);
151 printf(
"fileformat := %d\n", abss.
format);
152 printf(
"tacNr := %d\n", abss.
tacNr);
159 fprintf(stderr,
"Error: not valid ABSS format.\n");
162 if(verbose>100)
abssWrite(&abss, stdout, NULL);
164 fprintf(stderr,
"Error: not valid ABSS format.\n");
171 fprintf(stderr,
"Error: not applicable to provided ABSS system.\n");
178 if(verbose>1) printf(
"checking for dead channels\n");
183 fprintf(stderr,
"Error: not valid ABSS format.\n");
187 printf(
"positive_samples[1] := %d\n", n1);
188 printf(
"positive_samples[2] := %d\n", n2);
189 printf(
"positive_samples := %d\n", n);
193 fprintf(stderr,
"Error: bad ABSS data.\n"); fflush(stderr);
197 fprintf(stderr,
"Warning: bad ABSS data.\n"); fflush(stderr);
200 if(n1<=n/2) c1ok=0;
else c1ok=1;
201 if(n2<=n/2) c2ok=0;
else c2ok=1;
204 fprintf(stderr,
"Error: bad ABSS data.\n"); fflush(stderr);
207 if(verbose>0 && !c1ok) printf(
"channel1 is dead\n");
208 if(verbose>0 && !c2ok) printf(
"channel2 is dead\n");
210 if(chFix==1 && !c2ok) {
211 fprintf(stderr,
"Error: channel2 cannot be used to fix channel1.\n");
214 if(chFix==2 && !c1ok) {
215 fprintf(stderr,
"Error: channel1 cannot be used to fix channel2.\n");
227 fprintf(stderr,
"Error: not valid ABSS format.\n");
231 printf(
"higher_samples[1] := %d\n", n1);
232 printf(
"higher_samples[2] := %d\n", n2);
241 if(verbose>1) {printf(
"calculating the ratio\n"); fflush(stdout);}
243 if(isnan(chRatio) && (c1ok+c2ok)<2) {
244 fprintf(stderr,
"Error: cannot compute channel ratio.\n"); fflush(stderr);
252 if(verbose>1 || nratio<10) {
253 printf(
"channel ratio calculated from %d samples.\n", nratio);
256 if(isnan(chRatio) || verbose>0) {
257 fprintf(stdout,
"Ch1/Ch2 := %.6f\n", ratio);
262 fprintf(stderr,
"Error: cannot compute channel ratio.\n");
263 fflush(stderr);
tacFree(&abss);
return(5);
271 if(verbose>1) printf(
"Ch1/ch2 not given; no correction done.\n");
280 if(c1ok) chFix=2;
else chFix=1;
281 }
else if(n1>n2 && ratio>1.0) {
283 }
else if(n2>n1 && ratio<1.0) {
286 fprintf(stderr,
"Error: not sure which channel to fix.\n");
287 fflush(stderr);
tacFree(&abss);
return(6);
289 if(verbose>1) printf(
"chFix := %d\n", chFix);
295 if(verbose>1) {printf(
"fixing channel %d\n", chFix); fflush(stdout);}
298 fprintf(stderr,
"Error: cannot fix channel counts.\n");
299 fflush(stderr);
tacFree(&abss);
return(7);
304 printf(
"channel ratio after correction := %g\n", ratio);
311 printf(
"writing corrected data file in %s\n", outfile); fflush(stdout);}
313 fp=fopen(outfile,
"w");
315 fprintf(stderr,
"Error: cannot open file for writing.\n");
325 printf(
"corrected data written in %s\n", outfile);
int abssAboveZero(TAC *abss, int *n1, int *n2, int *n)
int abssHigherCounts(TAC *abss, int *n1, int *n2)
int abssFixChannel(TAC *abss, int channel, double ratio)
int abssCalculateRatio(TAC *abss, double *ratio, int *n)
int abssWrite(TAC *d, FILE *fp, TPCSTATUS *status)
int atofCheck(const char *s, double *v)
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
int tpcHtmlUsage(const char *program, char *text[], const char *path)
void tpcPrintBuild(const char *program, FILE *fp)
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
void statusInit(TPCSTATUS *s)
char * errorMsg(tpcerror e)
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
size_t strlcpy(char *dst, const char *src, size_t dstsize)
int verbose
Verbose level, used by statusPrint() etc.
tpcerror error
Error code.
int tacRead(TAC *d, const char *fname, TPCSTATUS *status)
Header file for libtpcabss.
Header file for library libtpcextensions.
Header file for library libtpctac.
@ TAC_FORMAT_ABSS_ALLOGG
ALLOGG ABSS data; reading supported.
@ TAC_FORMAT_ABSS_GEMS
GEMS ABSS data; reading supported.
@ TAC_FORMAT_ABSS_ALLOGG_OLD
ALLOGG ABSS data (old format); reading supported.
@ TAC_FORMAT_ABSS_SCANDITRONICS
Scanditronics ABSS data; reading supported.