10#include "tpcclibConfig.h"
23#define LKUP_HIST_NR 25
27static char *info[] = {
28 "Replace the y values in TAC file with the values from a given",
30 "The look-up table must contain two columns: program looks from the first",
31 "column a matching value for the TAC y value, and replaces the y",
32 "value with the value from the second column of the table.",
33 "Look-up table must be sorted in ascending order.",
35 "Usage: @P [options] TAC lkupfile outputfile",
37 "Output file is written in TAC file format, unless filename",
38 "extension is *.res or *.par.",
42 " If exact match in look-up table is not found, the closest value",
43 " is selected; by default, value is interpolated from the table.",
45 " Histogram of the results is written in TAC format in specified file.",
48 "See also: arlkup, imglkup, dftinteg, taccalc, tacunit, tacsety",
50 "Keywords: look-up table, autoradiography, ARG, perfusion, blood flow, radiowater",
69int main(
int argc,
char **argv)
71 int ai, help=0, version=0, verbose=1;
72 char tacfile[FILENAME_MAX], lkupfile[FILENAME_MAX], resfile[FILENAME_MAX],
73 hisfile[FILENAME_MAX];
82 if(argc==1) {
tpcPrintUsage(argv[0], info, stderr);
return(1);}
83 tacfile[0]=lkupfile[0]=resfile[0]=hisfile[0]=(char)0;
85 for(ai=1; ai<argc; ai++)
if(*argv[ai]==
'-') {
87 cptr=argv[ai]+1;
if(*cptr==
'-') cptr++;
if(!*cptr)
continue;
88 if(strncasecmp(cptr,
"CLOSEST", 1)==0) {
89 takeClosest=1;
continue;
90 }
else if(strncasecmp(cptr,
"HIS=", 4)==0) {
91 strlcpy(hisfile, cptr+4, FILENAME_MAX);
if(strlen(hisfile)>0)
continue;
93 fprintf(stderr,
"Error: invalid option '%s'.\n", argv[ai]);
102 if(help==2) {
tpcHtmlUsage(argv[0], info,
"");
return(0);}
107 if(ai<argc)
strlcpy(tacfile, argv[ai++], FILENAME_MAX);
108 if(ai<argc)
strlcpy(lkupfile, argv[ai++], FILENAME_MAX);
109 if(ai<argc)
strlcpy(resfile, argv[ai++], FILENAME_MAX);
111 fprintf(stderr,
"Error: invalid argument '%s'.\n", argv[ai]);
116 fprintf(stderr,
"Error: missing command-line argument; use option --help\n");
123 printf(
"tacfile := %s\n", tacfile);
124 printf(
"lkupfile := %s\n", lkupfile);
125 printf(
"resfile := %s\n", resfile);
126 if(hisfile[0]) printf(
"hisfile := %s\n", hisfile);
127 printf(
"takeClosest := %d\n", takeClosest);
135 if(verbose>1) printf(
"reading %s\n", lkupfile);
137 ret=
tacRead(&lkup, lkupfile, &status);
144 printf(
"lkup.tacNr := %d\n", lkup.
tacNr);
145 printf(
"lkup.sampleNr := %d\n", lkup.
sampleNr);
150 fprintf(stderr,
"Error: invalid look-up table %s\n", lkupfile);
155 fprintf(stderr,
"Error: missing look-up table values.\n");
160 double *table1=lkup.
x;
161 double *table2=lkup.
c[0].
y;
163 printf(
"Look-up y: %g - %g\n", table2[0], table2[tableSize-1]);
164 printf(
"Look-up x: %g - %g\n", table1[0], table1[tableSize-1]);
171 if(verbose>1) printf(
"reading %s\n", tacfile);
173 ret=
tacRead(&tac, tacfile, &status);
180 printf(
"tac.tacNr := %d\n", tac.
tacNr);
181 printf(
"tac.sampleNr := %d\n", tac.
sampleNr);
190 if(verbose>1) printf(
"allocating memory for the histogram\n");
194 fprintf(stderr,
"Error: cannot allocate memory for histogram.\n");
200 strcpy(hist.
c[0].
name,
"n");
202 double histogram_bin=(table2[tableSize-1]-table2[0])/(
double)LKUP_HIST_NR;
203 if(verbose>2) printf(
"histogram_bin_size := %g\n", histogram_bin);
204 hist.
x1[0]=table2[0]; hist.
x2[0]=hist.
x1[0]+histogram_bin;
206 hist.
x1[i]=hist.
x2[i-1];
207 hist.
x2[i]=hist.
x1[i]+histogram_bin;
209 for(i=0; i<hist.
sampleNr; i++) hist.
c[0].
y[i]=0.0;
215 if(verbose>1) fprintf(stdout,
"using look-up table\n");
220 if(isnan(v))
continue;
224 }
else if(v>table1[tableSize-1]) {
225 v=table2[tableSize-1];
228 low=mid=0; high=tableSize-1; ret=1;
231 if(v<table1[mid]) high=mid-1;
232 else if(v>table1[mid]) low=mid+1;
233 else {v=table2[mid]; ret=0;
break;}
237 n=high; high=low; low=n;
239 if(v-table1[low]<table1[high]-v) v=table2[low];
243 (v-table1[low])*(table2[high]-table2[low])
244 /(table1[high]-table1[low]);
249 if(v>=hist.
x1[n] && v<=hist.
x2[n]) {
250 hist.
c[0].
y[n]+=1.0;
break;
266 if(verbose>1) printf(
"writing %s\n", resfile);
270 cptr=strrchr(resfile,
'.');
if(cptr!=NULL) cptr++;
275 FILE *fp; fp=fopen(resfile,
"w");
277 fprintf(stderr,
"Error: cannot open file for writing (%s)\n", resfile);
288 ret=
parWrite(&par, fp, format, 1, &status);
296 if(verbose>0) printf(
"Converted data saved in %s.\n", resfile);
302 if(!hisfile[0] && verbose<=1) {
306 if(verbose>1) printf(
"writing %s\n", hisfile);
307 fp=fopen(hisfile,
"w");
309 fprintf(stderr,
"Error: cannot open file for writing (%s)\n", hisfile);
313 fp=stdout; printf(
"\nHistogram:\n"); fflush(stdout);
317 tacFree(&hist);
if(hisfile[0]) fclose(fp);
322 if(verbose>0 && hisfile[0]) printf(
"Histogram saved in %s.\n", hisfile);
int parWrite(PAR *par, FILE *fp, parformat format, int extra, TPCSTATUS *status)
int tacToPAR(TAC *tac, PAR *par, TPCSTATUS *status)
Copy the contents of TAC struct into PAR struct.
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)
char name[MAX_TACNAME_LEN+1]
int verbose
Verbose level, used by statusPrint() etc.
tpcerror error
Error code.
int tacAllocate(TAC *tac, int sampleNr, int tacNr)
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)
Header file for library libtpcextensions.
char * unitName(int unit_code)
Header file for library libtpcift.
Header file for libtpcpar.
@ PAR_FORMAT_HTML
HTML table format (currently not supported).
@ PAR_FORMAT_CSV_UK
UK CSV.
@ PAR_FORMAT_RES
Model result format of Turku PET Centre.
Header file for library libtpctac.
@ TAC_FORMAT_UNKNOWN
Unknown format.
@ TAC_FORMAT_PMOD
PMOD TAC format.
Header file for libtpctacmod.