TPCCLIB
Loading...
Searching...
No Matches
dftinteg.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 <math.h>
14#include <string.h>
15/*****************************************************************************/
16#include "libtpcmisc.h"
17#include "libtpccurveio.h"
18#include "libtpcmodel.h"
19#include "libtpcmodext.h"
20/*****************************************************************************/
21
22/*****************************************************************************/
23static char *info[] = {
24 "Calculation of an AUC (integral over time) from regional or plasma TACs.",
25 " ",
26 "Usage: @P [Options] tacfile starttime integrationtime aucfile",
27 " ",
28 "Enter the start time and integration time in the same units that are used",
29 "in the TAC file. Note that 3rd argument is not the integration end time.",
30 "Program will automatically set the integration start time and duration",
31 "based on the time range in the TAC file, if both are set to zero.",
32 "AUC/mean is by default written in DFT format, or in RES format if filename",
33 "has extension .res.",
34 " ",
35 "Options:",
36 " -avg",
37 " Average during specified range is calculated instead of AUC.",
38 " -stdoptions", // List standard options like --help, -v, etc
39 " ",
40 "Example 1: calculate AUC(32-302) with command",
41 " @P s5998dy1.dft 32 270 s5998int.dft",
42 "Example 2: integrate the regional TACs from a static or dynamic study",
43 "from the start time of the first frame to the end of the last frame:",
44 " @P a773dy1.dft 0 0 a773int.dft",
45 "Example 3: mean between 0 and 10 is calculated and saved in result format:",
46 " @P -avg ec4568.dft 0 10 ec4568_mean0-10.res",
47 " ",
48 "See also: interpol, dftratio, dftsuv, regfur, tacunit, imginteg, tac2suv",
49 " ",
50 "Keywords: TAC, modelling, AUC, autoradiography",
51 0};
52/*****************************************************************************/
53
54/*****************************************************************************/
55/* Turn on the globbing of the command line, since it is disabled by default in
56 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
57 In Unix&Linux wildcard command line processing is enabled by default. */
58/*
59#undef _CRT_glob
60#define _CRT_glob -1
61*/
62int _dowildcard = -1;
63/*****************************************************************************/
64
65/*****************************************************************************/
69int main(int argc, char **argv)
70{
71 int ai, help=0, version=0, verbose=1;
72 int ret;
73 char *cptr, dftfile[FILENAME_MAX], outfile[FILENAME_MAX], tmp[512];
74 int calc_avg=0; // 0=integral, 1=average
75 DFT dft, idft;
76 double tstart, tstop, tdur;
77
78
79 /*
80 * Get arguments
81 */
82 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
83 dftfile[0]=outfile[0]=(char)0;
84 tstart=tdur=tstop=-1.0;
85 dftInit(&dft); dftInit(&idft);
86 /* Options */
87 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
88 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(cptr==NULL) continue;
89 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
90 if(strcasecmp(cptr, "AVG")==0) {
91 calc_avg=1; continue;
92 } else if(strcasecmp(cptr, "MEAN")==0) {
93 calc_avg=1; continue;
94 }
95 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
96 return(1);
97 } else break;
98
99 /* Print help or version? */
100 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
101 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
102 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
103
104 /* Process other arguments, starting from the first non-option */
105 for(; ai<argc; ai++) {
106 if(!dftfile[0]) {
107 strlcpy(dftfile, argv[ai], FILENAME_MAX); continue;
108 } else if(tstart<0) {
109 if(atof_with_check(argv[ai], &tstart)==0) continue;
110 } else if(tdur<0) {
111 if(atof_with_check(argv[ai], &tdur)==0) {tstop=tstart+tdur; continue;}
112 } else if(!outfile[0]) {
113 strlcpy(outfile, argv[ai], FILENAME_MAX); continue;
114 }
115 fprintf(stderr, "Error: invalid argument '%s'.\n", argv[ai]);
116 return(1);
117 }
118
119 /* Is something missing? */
120 if(!outfile[0]) {
121 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
122 return(1);
123 }
124
125
126 /* In verbose mode print arguments and options */
127 if(verbose>1) {
128 printf("dftfile := %s\n", dftfile);
129 printf("outfile := %s\n", outfile);
130 printf("calc_avg := %d\n", calc_avg);
131 if(tstart>=1.0E-03 || tdur>=1.0E-02) printf("tstart := %g\ntstop := %g\n", tstart, tstop);
132 }
133
134
135 /*
136 * Read the datafile
137 */
138 if(dftRead(dftfile, &dft)) {
139 fprintf(stderr, "Error in reading '%s': %s\n", dftfile, dfterrmsg);
140 return(2);
141 }
142 /* Check and remove NaNs if possible */
143 if(dftNAfill(&dft)!=0) {
144 fprintf(stderr, "Error: missing values in %s.\n", dftfile);
145 dftEmpty(&dft); return(2);
146 }
147 /* Make sure that there is no overlap in image frames */
148 if(dft.timetype==DFT_TIME_STARTEND) {
149 if(verbose>1) fprintf(stdout, "checking frame overlap in %s\n", dftfile);
150 ret=dftDeleteFrameOverlap(&dft);
151 if(ret) {
152 fprintf(stderr, "Error: %s has overlapping frame times.\n", dftfile);
153 dftEmpty(&dft); return(2);
154 }
155 }
156 /* If both integration start time and duration were set to zero, then take
157 these from the TAC data */
158 if(tstart<1.0E-03 && tdur<1.0E-02) {
159 if(dft.timetype==DFT_TIME_STARTEND) {
160 tstart=dft.x1[0]; tstop=dft.x2[dft.frameNr-1];
161 } else {
162 tstart=dft.x[0]; tstop=dft.x[dft.frameNr-1];
163 }
164 tdur=tstop-tstart;
165 if(verbose>1) {
166 printf("tstart := %g\n", tstart);
167 printf("tstop := %g\n", tstop);
168 printf("tdur := %g\n", tdur);
169 }
170 }
171 if(verbose>10) dftPrint(&dft);
172
173 /*
174 * Calculate the AUC
175 */
176 ret=dftTimeIntegral(&dft, tstart, tstop, &idft, calc_avg, tmp, verbose-2);
177 if(ret!=0) {
178 fprintf(stderr, "Error: %s\n", tmp);
179 dftEmpty(&dft); return(5);
180 }
181 dftEmpty(&dft);
182
183 /*
184 * Save the AUC file, either as DFT or as RES
185 */
186 if(verbose>2) {
187 if(calc_avg==0) printf("writing AUC in %s\n", outfile);
188 else printf("writing mean in %s\n", outfile);
189 }
190 cptr=strrchr(outfile, '.'); if(cptr!=NULL) cptr++;
191 if(strcasecmp(cptr, "RES")==0) { // Save in RES format
192 RES res;
193 resInit(&res);
194 ret=dftToResult(&idft, &res, tmp);
195 if(ret!=0) {
196 fprintf(stderr, "Error in making results: %s\n", tmp);
197 dftEmpty(&idft); resEmpty(&res); return(11);
198 }
199 if(calc_avg==0) {strcpy(res.parname[0], "AUC");}
200 else {strcpy(res.parname[0], "Mean"); strcpy(res.parunit[0], idft.unit);}
201 tpcProgramName(argv[0], 1, 1, res.program, 256);
202 strcpy(res.datafile, dftfile);
203 sprintf(res.datarange, "%g - %g", idft.x1[0], idft.x2[0]);
204 res.datanr=0;
205 if(verbose>0) resPrint(&res);
206 if(resWrite(&res, outfile, verbose-3)) {
207 fprintf(stderr, "Error in writing '%s': %s\n", outfile, reserrmsg);
208 dftEmpty(&idft); resEmpty(&res); return(11);
209 }
210 resEmpty(&res);
211 } else { // save in DFT format
212 dftSetComments(&idft);
213 if(dftWrite(&idft, outfile)) {
214 fprintf(stderr, "Error in writing %s: %s\n", outfile, dfterrmsg);
215 dftEmpty(&idft); dftEmpty(&idft); return(11);
216 }
217 }
218 if(verbose>0) printf("%s written.\n", outfile);
219
220 dftEmpty(&idft);
221
222 return(0);
223}
224/*****************************************************************************/
225
226/*****************************************************************************/
int atof_with_check(char *double_as_string, double *result_value)
Definition decpoint.c:107
void dftInit(DFT *data)
Definition dft.c:38
char dfterrmsg[64]
Definition dft.c:6
int dftDeleteFrameOverlap(DFT *dft)
Definition dft.c:1237
void dftSetComments(DFT *dft)
Definition dft.c:1326
void dftEmpty(DFT *data)
Definition dft.c:20
int dftNAfill(DFT *dft)
Definition dft.c:930
int dftTimeIntegral(DFT *dft, double t1, double t2, DFT *idft, int calc_mode, char *status, int verbose)
Definition dftint.c:382
int dftRead(char *filename, DFT *data)
Definition dftio.c:22
void dftPrint(DFT *data)
Definition dftio.c:538
int dftWrite(DFT *data, char *filename)
Definition dftio.c:594
int dftToResult(DFT *dft, RES *res, char *status)
Definition dftres.c:60
Header file for libtpccurveio.
char reserrmsg[64]
Definition result.c:6
void resInit(RES *res)
Definition result.c:52
void resPrint(RES *res)
Definition result.c:186
int resWrite(RES *res, char *filename, int verbose)
Definition result.c:565
#define DFT_TIME_STARTEND
void resEmpty(RES *res)
Definition result.c:22
Header file for libtpcmisc.
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
Definition proginfo.c:40
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition strext.c:245
void tpcProgramName(const char *program, int version, int copyright, char *prname, int n)
Definition proginfo.c:101
int tpcHtmlUsage(const char *program, char *text[], const char *path)
Definition proginfo.c:213
void tpcPrintBuild(const char *program, FILE *fp)
Definition proginfo.c:383
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
Definition proginfo.c:158
Header file for libtpcmodel.
Header file for libtpcmodext.
int timetype
double * x1
double * x2
int frameNr
double * x
char unit[MAX_UNITS_LEN+1]
char parname[MAX_RESPARAMS][MAX_RESPARNAME_LEN+1]
int datanr
char program[1024]
char datarange[128]
char datafile[FILENAME_MAX]
char parunit[MAX_RESPARAMS][MAX_RESPARNAME_LEN+1]