TPCCLIB
Loading...
Searching...
No Matches
taccuty.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/*****************************************************************************/
18
19/*****************************************************************************/
20static char *info[] = {
21 "Extract specified y (concentration) interval from PET time-activity",
22 "curves (TACs).",
23 " ",
24 "Usage: @P [Options] tacfile lower upper [outputfile]",
25 " ",
26 "Options:",
27 " -stdoptions", // List standard options like --help, -v, etc
28 " ",
29 "See also: taccut, tacblend, tacinv, tacsety, dftmax, tacdel, tacsplit, taclkup",
30 " ",
31 "Keywords: TAC, tool, simulation, cropping",
32 0};
33/*****************************************************************************/
34
35/*****************************************************************************/
36/* Turn on the globbing of the command line, since it is disabled by default in
37 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
38 In Unix&Linux wildcard command line processing is enabled by default. */
39/*
40#undef _CRT_glob
41#define _CRT_glob -1
42*/
43int _dowildcard = -1;
44/*****************************************************************************/
45
46/*****************************************************************************/
50int main(int argc, char **argv)
51{
52 int ai, help=0, version=0, verbose=1;
53 int origNr, ret;
54 char dfile1[FILENAME_MAX], dfile2[FILENAME_MAX];
55 double lowerY, upperY;
56
57 /*
58 * Get arguments
59 */
60 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
61 dfile1[0]=dfile2[0]=(char)0;
62 lowerY=upperY=nan("");
63 /* Options */
64 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
65 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
66 fprintf(stderr, "Error: invalid option '%s'\n", argv[ai]);
67 return(1);
68 } else break;
69
70 TPCSTATUS status; statusInit(&status);
71 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
72 status.verbose=verbose-1;
73
74 /* Print help or version? */
75 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
76 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
77 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
78
79 /* Process other arguments, starting from the first non-option */
80 if(ai<argc) {strlcpy(dfile1, argv[ai++], FILENAME_MAX);}
81 if(ai<argc) {lowerY=atofVerified(argv[ai++]);}
82 if(ai<argc) {upperY=atofVerified(argv[ai++]);}
83 if(ai<argc) {strlcpy(dfile2, argv[ai++], FILENAME_MAX);}
84 if(ai<argc) {
85 fprintf(stderr, "Error: too many arguments: '%s'.\n", argv[ai]);
86 return(1);
87 }
88
89 /* Check that we got what we need */
90 if(!dfile1[0] || isnan(lowerY) || isnan(upperY) || upperY<lowerY) {
91 tpcPrintUsage(argv[0], info, stderr); return(1);
92 }
93
94 /* If output filename was not given, then edit the input file */
95 if(!dfile2[0]) strcpy(dfile2, dfile1);
96
97 /* In verbose mode print arguments and options */
98 if(verbose>1) {
99 printf("dfile1 := %s\n", dfile1);
100 printf("dfile2 := %s\n", dfile2);
101 printf("lowerY := %g\n", lowerY);
102 printf("upperY := %g\n", upperY);
103 }
104
105
106 /*
107 * Read data
108 */
109 if(verbose>1) printf("reading %s\n", dfile1);
110 TAC tac; tacInit(&tac);
111 ret=tacRead(&tac, dfile1, &status);
112 if(ret!=TPCERROR_OK) {
113 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
114 tacFree(&tac); return(2);
115 }
116 if(verbose>1) {
117 printf("fileformat := %s\n", tacFormattxt(tac.format));
118 printf("tacNr := %d\n", tac.tacNr);
119 printf("sampleNr := %d\n", tac.sampleNr);
120 }
121 origNr=tacNotNaNs(&tac, -1); // includes both x and y values
122
123 /* Get y range */
124 double ymin, ymax;
125 ret=tacYRange(&tac, -1, &ymin, &ymax, NULL, NULL, NULL, NULL);
126 if(ret!=0 || origNr<1) {
127 fprintf(stderr, "Error: invalid value range in file.\n");
128 tacFree(&tac); return(2);
129 }
130 if(verbose>1) {
131 printf("orig_min := %g\n", ymin);
132 printf("orig_max := %g\n", ymax);
133 }
134 /* Check if we already are inside the required range */
135 if(ymin>=lowerY && ymax<=upperY) {
136 if(verbose>1) printf("all values are inside required range.\n");
137 /* Just quit, if user did not even give a new filename */
138 if(strcasecmp(dfile1, dfile2)==0) {
139 if(verbose>0) printf("nothing done.\n");
140 return(0);
141 }
142 /* Otherwise save the data with new name */
143 if(verbose>1) printf("writing %s\n", dfile2);
144 FILE *fp; fp=fopen(dfile2, "w");
145 if(fp==NULL) {
146 fprintf(stderr, "Error: cannot open file for writing (%s)\n", dfile2);
147 tacFree(&tac); return(11);
148 }
149 ret=tacWrite(&tac, fp, TAC_FORMAT_UNKNOWN, 1, &status);
150 fclose(fp);
151 if(ret!=TPCERROR_OK) {
152 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
153 tacFree(&tac); return(12);
154 }
155 if(verbose>0) printf("All sample(s) were extracted.\n");
156 /* ... and quit */
157 tacFree(&tac);
158 return(0);
159 }
160
161
162 /*
163 * Set outlier values to NaN
164 */
165 for(int i=0; i<tac.sampleNr; i++) {
166 int j, n=0;
167 for(j=0; j<tac.tacNr; j++) {
168 if(tac.c[j].y[i]<lowerY || tac.c[j].y[i]>upperY) tac.c[j].y[i]=nan("");
169 if(!isnan(tac.c[j].y[i])) n++;
170 }
171 /* If no y values left in this time frame, then set also x to NaN */
172 if(n==0) tac.x[i]=tac.x1[i]=tac.x2[i]=nan("");
173 }
174
175 /* Remove time frames that have no data left */
176 if(tacXNaNs(&tac)>0) {
177 if(verbose>1) printf("deleting missing samples\n");
178 ret=tacDeleteMissingSamples(&tac);
179 }
180 int finalNr=tacNotNaNs(&tac, -1); // includes both x and y values
181 if(verbose>1) {
182 printf("original_sampleNr := %d\n", origNr);
183 printf("final_sampleNr := %d\n", finalNr);
184 }
185 if(tac.sampleNr==0) {
186 fprintf(stderr, "Error: no data inside the range.\n");
187 tacFree(&tac); return(9);
188 }
189
190 /*
191 * Save the extracted TACs
192 */
193 if(verbose>1) printf("writing %s\n", dfile2);
194 FILE *fp; fp=fopen(dfile2, "w");
195 if(fp==NULL) {
196 fprintf(stderr, "Error: cannot open file for writing (%s)\n", dfile2);
197 tacFree(&tac); return(11);
198 }
199 ret=tacWrite(&tac, fp, TAC_FORMAT_UNKNOWN, 1, &status);
200 fclose(fp);
201 if(ret!=TPCERROR_OK) {
202 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
203 tacFree(&tac); return(12);
204 }
205
206 /* Free memory */
207 tacFree(&tac);
208
209 return(0);
210}
211/*****************************************************************************/
212
213/*****************************************************************************/
double atofVerified(const char *s)
Definition decpoint.c:75
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
double * x2
Definition tpctac.h:101
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 tacNotNaNs(TAC *tac, const int i)
Definition tacnan.c:84
int tacXNaNs(TAC *tac)
Definition tacnan.c:23
int tacDeleteMissingSamples(TAC *d)
Delete those samples (time frames) from TAC structure, which contain only missing y values,...
Definition tacx.c:450
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.
Definition tacy.c:26
Header file for library libtpcextensions.
@ TPCERROR_OK
No error.
Header file for library libtpctac.
@ TAC_FORMAT_UNKNOWN
Unknown format.
Definition tpctac.h:28