9#include "tpcclibConfig.h"
21static char *info[] = {
22 "Extract specified time interval from PET time-activity curves (TACs).",
24 "Usage: @P [Options] tacfile starttime endtime [outputfile]",
27 " -LS | -UptoPeak | -FromPeak | -UptoPeakN | -FromPeakP",
28 " Extract the last sample, samples up to peak, from peak to the end",
29 " up to peak plus one, or from one sample before peak to the end;",
30 " times are not effective with these options.",
33 "See also: taccat, tactime, tacunit, tacframe, interpol, dftrmovl, imgdelfr",
35 "Keywords: TAC, tool, simulation, time, cropping",
54int main(
int argc,
char **argv)
56 int ai, help=0, version=0, verbose=1;
58 char dfile1[FILENAME_MAX], dfile2[FILENAME_MAX];
66 if(argc==1) {
tpcPrintUsage(argv[0], info, stderr);
return(1);}
68 dfile1[0]=dfile2[0]=(char)0;
71 for(ai=1; ai<argc; ai++)
if(*argv[ai]==
'-') {
73 char *cptr=argv[ai]+1;
if(*cptr==
'-') cptr++;
if(!*cptr)
continue;
74 if(strcasecmp(cptr,
"LS")==0) {calcMode=1;
continue;}
75 if(strcasecmp(cptr,
"UPTOPEAK")==0) {calcMode=2;
continue;}
76 if(strcasecmp(cptr,
"FROMPEAK")==0) {calcMode=3;
continue;}
77 if(strcasecmp(cptr,
"UPTOPEAKN")==0) {calcMode=4;
continue;}
78 if(strcasecmp(cptr,
"FROMPEAKP")==0) {calcMode=5;
continue;}
79 fprintf(stderr,
"Error: invalid option '%s'\n", argv[ai]);
88 if(help==2) {
tpcHtmlUsage(argv[0], info,
"");
return(0);}
93 if(ai<argc) {
strlcpy(dfile1, argv[ai++], FILENAME_MAX);}
97 fprintf(stderr,
"Error: invalid start time '%s'.\n", argv[ai]);
105 if(
atofCheck(argv[ai], &endT)!=0 || endT<=startT) {
106 fprintf(stderr,
"Error: invalid end time '%s'.\n", argv[ai]);
112 if(ai<argc) {
strlcpy(dfile2, argv[ai++], FILENAME_MAX);}
113 if(ai<argc) {fprintf(stderr,
"Error: too many arguments: '%s'.\n", argv[ai]);
return(1);}
116 if(!dfile1[0]) {
tpcPrintUsage(argv[0], info, stderr);
return(1);}
118 if(calcMode==0 && (isnan(startT) || isnan(endT) || endT<=startT)) {
119 fprintf(stderr,
"Error: invalid time range.\n");
return(1);}
122 if(!dfile2[0]) strcpy(dfile2, dfile1);
126 printf(
"calcMode := %d\n", calcMode);
127 printf(
"dfile1 := %s\n", dfile1);
128 printf(
"dfile2 := %s\n", dfile2);
129 if(!isnan(startT)) printf(
"startT := %g\n", startT);
130 if(!isnan(endT)) printf(
"endT := %g\n", endT);
137 if(verbose>1) printf(
"reading %s\n", dfile1);
138 ret=
tacRead(&tac, dfile1, &status);
145 printf(
"tacNr := %d\n", tac.
tacNr);
146 printf(
"sampleNr := %d\n", tac.
sampleNr);
159 if(calcMode>=2 && calcMode<=5) {
160 int maxi;
double maxy;
161 ret=
tacYRange(&tac, -1, NULL, &maxy, NULL, &maxi, NULL, NULL);
163 fprintf(stderr,
"Error: cannot find TAC peak.\n");
166 if(verbose>0) {printf(
"Peak value %g found at %g\n", maxy, tac.
x[maxi]); fflush(stdout);}
169 if(tac.
isframe) endT=tac.
x2[maxi];
else endT=tac.
x[maxi];
170 }
else if(calcMode==3) {
171 if(tac.
isframe) startT=tac.
x1[maxi];
else startT=tac.
x[maxi];
173 }
else if(calcMode==4) {
176 if(tac.
isframe) endT=tac.
x2[maxi];
else endT=tac.
x[maxi];
177 }
else if(calcMode==5) {
179 if(tac.
isframe) startT=tac.
x1[maxi];
else startT=tac.
x[maxi];
196 fprintf(stderr,
"Error: invalid range.\n");
197 if(verbose>1) printf(
"error_msg := %s\n",
errorMsg(ret));
201 printf(
"original_frameNr := %d\n", origNr);
202 printf(
"final_frameNr := %d\n", tac2.
sampleNr);
207 if(origNr==tac2.
sampleNr && strcasecmp(dfile1, dfile2)==0) {
208 if(verbose>0) printf(
"nothing done.\n");
216 if(verbose>1) printf(
"writing %s\n", dfile2);
217 FILE *fp; fp=fopen(dfile2,
"w");
219 fprintf(stderr,
"Error: cannot open file for writing (%s)\n", dfile2);
228 if(verbose>0) printf(
"%d sample(s) were extracted.\n", tac2.
sampleNr);
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)
char * tacFormattxt(tacformat c)
int tacWrite(TAC *tac, FILE *fp, tacformat format, int extra, TPCSTATUS *status)
int tacSortByTime(TAC *d, TPCSTATUS *status)
int tacExtractSamples(TAC *d1, TAC *d2, int si, int ei)
Extract the specified sample range from TAC structure.
int tacExtractRange(TAC *d1, TAC *d2, double startT, double endT)
Extract the specified time (x) range from TAC structure.
int tacYRange(TAC *d, int i, double *ymin, double *ymax, int *smin, int *smax, int *imin, int *imax)
Get the range of y values (concentrations) in TAC struct.
Header file for library libtpcextensions.
Header file for library libtpctac.
@ TAC_FORMAT_UNKNOWN
Unknown format.