TPCCLIB
Loading...
Searching...
No Matches
tacmidy.c
Go to the documentation of this file.
1
8/*****************************************************************************/
9#include "tpcclibConfig.h"
10/*****************************************************************************/
11#include <stdio.h>
12#include <stdlib.h>
13#include <unistd.h>
14#include <string.h>
15#include <math.h>
16/*****************************************************************************/
17#include "tpcextensions.h"
18#include "tpctac.h"
19#include "tpcli.h"
20/*****************************************************************************/
21
22/*****************************************************************************/
23static char *info[] = {
24 "This program replaces the TAC frame start and end times with mid times,",
25 "and the y values with estimates that aim to conserve the AUC.",
26 "The concentrations (y values) in PET TACs with frame start and end times",
27 "represent the average concentration during the frame duration (fdur).",
28 "For each frame AUC=y*fdur. However, AUC is usually needed at frame mid time",
29 "and frame AUC at frame mid time as AUC=y*fdur/2, or calculating area under",
30 "curve formed by joining TAC data points with lines, will lead to biased AUCs.",
31 " ",
32 "Not to be used in actual data analyses!",
33 " ",
34 "Usage: @P [Options] tacfile newfile",
35 " ",
36 "Options:",
37 " -stdoptions", // List standard options like --help, -v, etc
38 " ",
39 "See also: tacframe, simframe, tacsetx, tacadd0, tacformat",
40 " ",
41 "Keywords: TAC, tool, software testing",
42 0};
43/*****************************************************************************/
44
45/*****************************************************************************/
46/* Turn on the globbing of the command line, since it is disabled by default in
47 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
48 In Unix&Linux wildcard command line processing is enabled by default. */
49/*
50#undef _CRT_glob
51#define _CRT_glob -1
52*/
53int _dowildcard = -1;
54/*****************************************************************************/
55
56/*****************************************************************************/
60int main(int argc, char **argv)
61{
62 int ai, help=0, version=0, verbose=1;
63 int ret;
64//char *cptr;
65 char tacfile1[FILENAME_MAX], tacfile2[FILENAME_MAX];
66 TAC tac;
67
68 /*
69 * Get arguments
70 */
71 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
72 tacInit(&tac); tacfile1[0]=tacfile2[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 fprintf(stderr, "Error: invalid option '%s'\n", argv[ai]);
78 return(1);
79 } else break; // tac name argument may start with '-'
80
81 TPCSTATUS status; statusInit(&status);
82 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
83 status.verbose=verbose-1;
84
85 /* Print help or version? */
86 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
87 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
88 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
89
90 /* Arguments */
91 for(; ai<argc; ai++) {
92 if(!tacfile1[0]) {
93 strlcpy(tacfile1, argv[ai], FILENAME_MAX); continue;
94 } else if(!tacfile2[0]) {
95 strlcpy(tacfile2, argv[ai], FILENAME_MAX); continue;
96 }
97 fprintf(stderr, "Error: invalid argument '%s'.\n", argv[ai]);
98 return(1);
99 }
100 /* Is something missing? */
101 if(!tacfile2[0]) {tpcPrintUsage(argv[0], info, stdout); return(1);}
102
103 /* In verbose mode print arguments and options */
104 if(verbose>1) {
105 for(ai=0; ai<argc; ai++)
106 printf("%s ", argv[ai]);
107 printf("\n");
108 printf("tacfile1 := %s\n", tacfile1);
109 printf("tacfile2 := %s\n", tacfile2);
110 }
111
112 /*
113 * Read TAC file
114 */
115 if(verbose>1) printf("reading %s\n", tacfile1);
116 ret=tacRead(&tac, tacfile1, &status);
117 if(ret!=TPCERROR_OK) {
118 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
119 tacFree(&tac); return(2);
120 }
121 if(verbose>2) {
122 printf("fileformat := %s\n", tacFormattxt(tac.format));
123 printf("tacNr := %d\n", tac.tacNr);
124 printf("sampleNr := %d\n", tac.sampleNr);
125 printf("xunit := %s\n", unitName(tac.tunit));
126 printf("yunit := %s\n", unitName(tac.cunit));
127 }
128 /* Check that we have frame start and end times */
129 if(tac.isframe==0) {
130 fprintf(stderr, "Error: file does not contain frame start and end times.\n");
131 tacFree(&tac); return(3);
132 }
133 if(tacXNaNs(&tac)>0) {
134 fprintf(stderr, "Error: missing sample times in TAC file.\n");
135 tacFree(&tac); return(3);
136 }
137 /* Check sample number */
138 if(tac.sampleNr<3) {
139 fprintf(stderr, "Warning: TAC contains only %d sample(s).\n", tac.sampleNr);
140 }
141
142
143 /*
144 * Do the thing
145 */
146 double y2[tac.sampleNr];
147 ret=0;
148 for(int i=0; i<tac.tacNr && !ret; i++) {
149 ret=liFrameMidValue(tac.x1, tac.x2, tac.c[i].y, tac.sampleNr, y2, verbose-2);
150 for(int j=0; j<tac.sampleNr; j++) tac.c[i].y[j]=y2[j];
151 }
152 if(ret) {
153 fprintf(stderr, "Error: cannot calculate y values.\n");
154 if(verbose>0) printf("ret := %d\n", ret);
155 tacFree(&tac); return(8);
156 }
157 tac.isframe=0; // prevent accidentally repeating this
158
159
160 /*
161 * Save TAC data
162 */
163 if(verbose>2) printf("writing %s\n", tacfile2);
164 FILE *fp; fp=fopen(tacfile2, "w");
165 if(fp==NULL) {
166 fprintf(stderr, "Error: cannot open file for writing (%s)\n", tacfile2);
167 tacFree(&tac); return(11);
168 }
169 ret=tacWrite(&tac, fp, TAC_FORMAT_UNKNOWN, 1, &status);
170 fclose(fp); tacFree(&tac);
171 if(ret!=TPCERROR_OK) {
172 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
173 return(12);
174 }
175
176 return(0);
177}
178/*****************************************************************************/
179
180/*****************************************************************************/
182
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
unit cunit
Definition tpctac.h:105
tacformat format
Definition tpctac.h:93
int sampleNr
Definition tpctac.h:89
int isframe
Definition tpctac.h:95
TACC * c
Definition tpctac.h:117
double * x2
Definition tpctac.h:101
unit tunit
Definition tpctac.h:109
double * x1
Definition tpctac.h:99
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
char * tacFormattxt(tacformat c)
Definition tacio.c:98
int tacWrite(TAC *tac, FILE *fp, tacformat format, int extra, TPCSTATUS *status)
Definition tacio.c:332
int tacXNaNs(TAC *tac)
Definition tacnan.c:23
Header file for library libtpcextensions.
@ TPCERROR_OK
No error.
char * unitName(int unit_code)
Definition units.c:143
Header file for libtpcli.
Header file for library libtpctac.
@ TAC_FORMAT_UNKNOWN
Unknown format.
Definition tpctac.h:28