TPCCLIB
Loading...
Searching...
No Matches
avgfract.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 <unistd.h>
13#include <string.h>
14#include <math.h>
15/*****************************************************************************/
16#include "libtpcmisc.h"
17#include "libtpcmodel.h"
18#include "libtpccurveio.h"
19/*****************************************************************************/
20
21/*****************************************************************************/
22static char *info[] = {
23 "Calculation of the mean of fraction curves. Data is not interpolated or",
24 "extrapolated. Resulting average datafile will contain a weight column;",
25 "weights represent the number of fractions from which each mean",
26 "value was calculated.",
27 " ",
28 "Usage: @P [Options] meanfile fractionfiles",
29 " ",
30 "Options:",
31 " -stdoptions", // List standard options like --help, -v, etc
32 " ",
33 "Example:",
34 " @P mean.rat ue*ap.rat",
35 " ",
36 "See also: metabcor, fit_ppf, avgbolus, simframe, taccalc, tacadd, tacblend",
37 " ",
38 "Keywords: TAC, input, simulation",
39 0};
40/******************************************************************************/
41
42/*****************************************************************************/
43/* Turn on the globbing of the command line, since it is disabled by default in
44 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
45 In Unix&Linux wildcard command line processing is enabled by default. */
46/*
47#undef _CRT_glob
48#define _CRT_glob -1
49*/
50int _dowildcard = -1;
51/******************************************************************************/
52
53/******************************************************************************/
54/* Local functions */
55int samplet_eval(const void *, const void *);
56/*****************************************************************************/
57
58/*****************************************************************************/
62int main(int argc, char **argv)
63{
64 int ai, help=0, version=0, verbose=1;
65 int fi, ti, ri, ret, n;
66 int fileNr=0, file1=0, timeNr=0, timeSpace=0, min_tacs;
67 char *cptr, ofile[FILENAME_MAX], dfile[FILENAME_MAX];
68 DFT dft, mean;
69 double *samplet, *dptr;
70
71
72 /*
73 * Get arguments
74 */
75 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
76 ofile[0]=dfile[0]=(char)0;
77 dftInit(&dft); dftInit(&mean);
78 /* Options */
79 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
80 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(cptr==NULL) continue;
81 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
82 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
83 return(1);
84 } else break;
85
86 /* Print help or version? */
87 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
88 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
89 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
90
91 /* Process other arguments, starting from the first non-option */
92 for(; ai<argc; ai++) {
93 if(!ofile[0]) {
94 strcpy(ofile, argv[ai]); continue;
95 } else {
96 if(fileNr==0) file1=ai;
97 fileNr++;
98 }
99 }
100
101 /* In verbose mode print arguments and options */
102 if(verbose>1) {
103 printf("fileNr := %d\n", fileNr);
104 printf("ofile := %s\n", ofile);
105 }
106
107 /* Is something missing? */
108 if(fileNr==0) {
109 fprintf(stderr, "Error: missing command-line argument; try %s --help\n",
110 argv[0]);
111 return(1);
112 }
113 if(fileNr==1) {
114 fprintf(stderr, "Error: only one input file specified.\n");
115 return(1);
116 }
117
118 /*
119 * Collect all sample times from all input files
120 * and get the min nr of TACs
121 */
122 if(verbose>0) printf("collecting sample times\n");
123 timeNr=timeSpace=0; samplet=(double*)NULL; min_tacs=10000;
124 for(ai=file1; ai<argc; ai++) if(*argv[ai]!='-') {
125 /* Read next input file */
126 strcpy(dfile, argv[ai]);
127 if(verbose>1) printf("dfile := %s\n", dfile);
128 if(dftRead(dfile, &dft)) {
129 fprintf(stderr, "Error in reading '%s': %s\n", dfile, dfterrmsg);
130 free(samplet); dftEmpty(&dft); return(2);
131 }
132 /* Get the nr of TACs */
133 if(dft.voiNr<min_tacs) min_tacs=dft.voiNr;
134 /* Allocate more memory for sample time list, if necessary */
135 if(timeSpace-timeNr<dft.frameNr) {
136 timeSpace+=dft.frameNr;
137 samplet=(double*)realloc(samplet, timeSpace*sizeof(double));
138 if(samplet==NULL) {
139 fprintf(stderr, "Error in allocation of memory.\n");
140 dftEmpty(&dft); return(3);
141 }
142 }
143 /* Copy new sample times to the list */
144 for(fi=0; fi<dft.frameNr; fi++) {
145 /* if all fractions are NA, then the time is not needed */
146 for(ri=n=0; ri<dft.voiNr; ri++) if(!isnan(dft.voi[ri].y[fi])) n++;
147 if(n==0) continue;
148 /* if sample time is already listed, do nothing */
149 for(ti=ret=0; ti<timeNr; ti++) if(samplet[ti]==dft.x[fi]) {ret=1; break;}
150 /* otherwise add it */
151 if(ret==0) {
152 if(verbose>8) printf(" adding time %g\n", dft.x[fi]);
153 samplet[timeNr++]=dft.x[fi];
154 }
155 }
156 dftEmpty(&dft);
157 }
158 if(timeNr==0) {
159 fprintf(stderr, "Error: could not read sample times.\n");
160 free(samplet); return(2);
161 }
162 if(verbose>4) printf("sorting %d sample times\n", timeNr);
163 dptr=samplet;
164 qsort(dptr, timeNr, sizeof(double), samplet_eval);
165 if(verbose>3) {
166 printf("Collected sample times:\n %g", samplet[0]);
167 for(ti=1; ti<timeNr; ti++) printf(", %g", samplet[ti]);
168 printf("\n");
169 }
170 if(verbose>1) printf("min TAC number: %d\n", min_tacs);
171
172 /*
173 * Set space for mean fraction data
174 */
175 ret=dftSetmem(&mean, timeNr, min_tacs);
176 if(ret) {
177 fprintf(stderr, "Error in allocation of memory.\n");
178 free(samplet); dftEmpty(&mean); return(5);
179 }
180 mean.frameNr=timeNr; mean.voiNr=min_tacs;
181 for(ti=0; ti<timeNr; ti++) mean.x[ti]=samplet[ti];
182 free(samplet);
183 mean.isweight=1;
185
186
187 /*
188 * Add the fractions from fraction datafiles
189 */
190 if(verbose>0) printf("collecting the fractions\n");
191 for(ai=file1; ai<argc; ai++) if(*argv[ai]!='-') {
192 /* Read next input file */
193 strcpy(dfile, argv[ai]);
194 if(dftRead(dfile, &dft)) {
195 fprintf(stderr, "Error in reading '%s': %s\n", dfile, dfterrmsg);
196 return(2);
197 }
198 for(fi=0; fi<dft.frameNr; fi++) {
199 /* find the correct sample time */
200 for(ti=0; ti<mean.frameNr; ti++) if(mean.x[ti]==dft.x[fi]) break;
201 if(ti==mean.frameNr) continue; /* not found; should not happen */
202 /* add the fractions */
203 for(ri=n=0; ri<mean.voiNr; ri++) if(!isnan(dft.voi[ri].y[fi])) {
204 mean.voi[ri].y[ti]+=dft.voi[ri].y[fi]; n++;}
205 /* increase weight (if at least one was available) */
206 if(n>0) mean.w[ti]++;
207 }
208 /* Get also some header information */
209 if(ai==file1) {
210 strcpy(mean.unit, dft.unit);
211 mean.timeunit=dft.timeunit;
212 for(ri=0; ri<mean.voiNr; ri++) {
213 strcpy(mean.voi[ri].name, dft.voi[ri].name);
214 strcpy(mean.voi[ri].voiname, dft.voi[ri].voiname);
215 strcpy(mean.voi[ri].hemisphere, dft.voi[ri].hemisphere);
216 strcpy(mean.voi[ri].place, dft.voi[ri].place);
217 }
218 }
219 dftEmpty(&dft);
220 }
221 /* Calculate the means by dividing by weights */
222 for(ti=0; ti<mean.frameNr; ti++) {
223 if(mean.w[ti]<=0.0)
224 for(ri=0; ri<mean.voiNr; ri++) mean.voi[ri].y[ti]=nan("");
225 else
226 for(ri=0; ri<mean.voiNr; ri++) mean.voi[ri].y[ti]/=mean.w[ti];
227 }
228
229
230 /*
231 * Save the mean fraction curves
232 */
233 if(verbose>1) printf("writing %s\n", ofile);
234 if(dftWrite(&mean, ofile)) {
235 fprintf(stderr, "Error in writing '%s': %s\n", ofile, dfterrmsg);
236 dftEmpty(&mean);
237 return(11);
238 }
239 if(verbose>=0) fprintf(stdout, " %s written.\n", ofile),
240
241 dftEmpty(&mean);
242
243 return(0);
244}
245/******************************************************************************/
246
247/******************************************************************************/
248int samplet_eval(const void *n1, const void *n2)
249{
250 if(*(double*)n1 < *(double*)n2) return -1;
251 else if(*(double*)n1 > *(double*)n2) return +1;
252 else return 0;
253}
254/******************************************************************************/
255
256/******************************************************************************/
void dftInit(DFT *data)
Definition dft.c:38
char dfterrmsg[64]
Definition dft.c:6
int dftSetmem(DFT *data, int frameNr, int voiNr)
Definition dft.c:57
void dftEmpty(DFT *data)
Definition dft.c:20
int dftRead(char *filename, DFT *data)
Definition dftio.c:22
int dftWrite(DFT *data, char *filename)
Definition dftio.c:594
Header file for libtpccurveio.
#define DFT_FORMAT_STANDARD
Header file for libtpcmisc.
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
Definition proginfo.c:40
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.
int mean(double *x, double *y, int nr, double *xmean, double *xsd, double *ymean, double *ysd)
Definition pearson.c:341
Voi * voi
int timeunit
int voiNr
int frameNr
double * x
char unit[MAX_UNITS_LEN+1]
char voiname[MAX_REGIONSUBNAME_LEN+1]
double * y
char name[MAX_REGIONNAME_LEN+1]
char hemisphere[MAX_REGIONSUBNAME_LEN+1]
char place[MAX_REGIONSUBNAME_LEN+1]