TPCCLIB
Loading...
Searching...
No Matches
absszero.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 "tpcabss.h"
18/*****************************************************************************/
19
20/*****************************************************************************/
21static char *info[] = {
22 "Delete initial \"bumps\" in Scanditronics, GEMS, or Allogg on-line blood",
23 "sampler data by setting the activities during the specified sample time",
24 "range (in seconds) to zero.",
25 "Notice that the original data file is overwritten by default.",
26 " ",
27 "Usage: @P [options] abssfile starttime endtime",
28 " ",
29 "Options:",
30 " -o=<filename>",
31 " Name for corrected file; by default the original file is overwritten.",
32 " -stdoptions", // List standard options like --help, -v, etc
33 " ",
34 "Example:",
35 " @P us1328.bld 0 40",
36 " ",
37 "See also: abssbkg, absscal, absstime, abssfch, abssexam, fitdelay",
38 " ",
39 "Keywords: input, blood, background, ABSS",
40 0};
41/*****************************************************************************/
42
43/*****************************************************************************/
44/* Turn on the globbing of the command line, since it is disabled by default in
45 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
46 In Unix&Linux wildcard command line processing is enabled by default. */
47/*
48#undef _CRT_glob
49#define _CRT_glob -1
50*/
51int _dowildcard = -1;
52/*****************************************************************************/
53
54/*****************************************************************************/
58int main(int argc, char **argv)
59{
60 int ai, help=0, version=0, verbose=1;
61 char abssfile[FILENAME_MAX], outfile[FILENAME_MAX];
62 char *cptr;
63 double startTime=nan("");
64 double endTime=nan("");
65 int ret;
66
67
68 /*
69 * Get arguments
70 */
71 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
72 abssfile[0]=outfile[0]=(char)0;
73 /* Options */
74 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
75 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
76 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
77 if(strncasecmp(cptr, "O=", 2)==0 && strlen(cptr)>2) {
78 strlcpy(outfile, cptr+2, FILENAME_MAX); continue;
79 }
80 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
81 return(1);
82 } else break;
83
84 TPCSTATUS status; statusInit(&status);
85 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
86 status.verbose=verbose-3;
87
88 /* Print help or version? */
89 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
90 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
91 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
92
93 /* Process other arguments, starting from the first non-option */
94 if(ai<argc) strlcpy(abssfile, argv[ai++], FILENAME_MAX);
95 if(ai<argc) {
96 if(atofCheck(argv[ai], &startTime)) {
97 fprintf(stderr, "Error: invalid time range.\n");
98 return(1);
99 }
100 ai++;
101 }
102 if(ai<argc) {
103 if(atofCheck(argv[ai], &endTime) || endTime<=startTime) {
104 fprintf(stderr, "Error: invalid time range.\n");
105 return(1);
106 }
107 ai++;
108 }
109 if(ai<argc) {
110 fprintf(stderr, "Error: invalid argument '%s'.\n", argv[ai]);
111 return(1);
112 }
113 /* Did we get all the information that we need? */
114 if(isnan(endTime)) {
115 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
116 return(1);
117 }
118 if(!outfile[0]) strcpy(outfile, abssfile);
119
120
121 /* In verbose mode print arguments and options */
122 if(verbose>1) {
123 printf("abssfile := %s\n", abssfile);
124 printf("startTime := %g\n", startTime);
125 printf("endTime := %g\n", endTime);
126 printf("outfile := %s\n", outfile);
127 fflush(stdout);
128 }
129
130 /*
131 * Read ABSS file
132 */
133 if(verbose>1) printf("reading %s\n", abssfile);
134 TAC abss; tacInit(&abss);
135 ret=tacRead(&abss, abssfile, &status);
136 if(ret!=TPCERROR_OK) {
137 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
138 tacFree(&abss); return(2);
139 }
140 if(verbose>1) {
141 printf("sampleNr := %d\n", abss.sampleNr);
142 }
143 if(verbose>2) {
144 printf("fileformat := %d\n", abss.format);
145 printf("tacNr := %d\n", abss.tacNr);
146 }
147 if(abss.format!=TAC_FORMAT_ABSS_ALLOGG &&
151 {
152 fprintf(stderr, "Error: not valid ABSS format.\n");
153 tacFree(&abss); return(2);
154 }
155 if(verbose>100) abssWrite(&abss, stdout, NULL);
156 if(abss.tacNr<1 || abss.sampleNr<1) {
157 fprintf(stderr, "Error: not valid ABSS format.\n");
158 tacFree(&abss); return(2);
159 }
160
161
162 /*
163 * Remove the background from the data
164 */
165 if(verbose>1) {printf("removing the background\n"); fflush(stdout);}
166 int i, j, n=0;
167 for(i=0; i<abss.sampleNr; i++) {
168 if(abss.x[i]<startTime || abss.x[i]>endTime) continue;
169 for(j=0; j<abss.tacNr; j++) abss.c[j].y[i]=0.0;
170 n++;
171 }
172 if(verbose>2) {printf("%d sample(s) set to zero.\n", n); fflush(stdout);}
173 if(n==0) {
174 fprintf(stderr, "Warning: no samples inside the time range.\n");
175 if(verbose>0) {printf("Note: file not modified.\n"); fflush(stdout);}
176 tacFree(&abss); return(0);
177 }
178
179 if(verbose>1) {
180 printf("writing corrected data file in %s\n", outfile); fflush(stdout);}
181 FILE *fp;
182 fp=fopen(outfile, "w");
183 if(fp==NULL) {
184 fprintf(stderr, "Error: cannot open file for writing.\n");
185 tacFree(&abss); return(11);
186 }
187 ret=abssWrite(&abss, fp, &status);
188 fclose(fp);
189 if(ret!=TPCERROR_OK) {
190 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
191 tacFree(&abss); return(12);
192 }
193 if(verbose>0) {
194 printf("corrected data written in %s\n", outfile);
195 fflush(stdout);
196 }
197
198 tacFree(&abss);
199 return(0);
200}
201/*****************************************************************************/
202
203/*****************************************************************************/
int abssWrite(TAC *d, FILE *fp, TPCSTATUS *status)
Definition abssio.c:53
int atofCheck(const char *s, double *v)
Definition decpoint.c:94
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
double * y
Definition tpctac.h:75
Definition tpctac.h:87
double * x
Definition tpctac.h:97
tacformat format
Definition tpctac.h:93
int sampleNr
Definition tpctac.h:89
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 tacRead(TAC *d, const char *fname, TPCSTATUS *status)
Definition tacio.c:413
Header file for libtpcabss.
Header file for library libtpcextensions.
@ TPCERROR_OK
No error.
Header file for library libtpctac.
@ TAC_FORMAT_ABSS_ALLOGG
ALLOGG ABSS data; reading supported.
Definition tpctac.h:56
@ TAC_FORMAT_ABSS_GEMS
GEMS ABSS data; reading supported.
Definition tpctac.h:54
@ TAC_FORMAT_ABSS_ALLOGG_OLD
ALLOGG ABSS data (old format); reading supported.
Definition tpctac.h:55
@ TAC_FORMAT_ABSS_SCANDITRONICS
Scanditronics ABSS data; reading supported.
Definition tpctac.h:53