68 int ret, fi, n, first, last, ii, input_nr=0;
71 double f, starttime, endtime;
73 char *fname, tmp[512];
76 if(loginfo!=NULL && verbose>0) {
77 fprintf(loginfo,
"imgReadModelingData(\n");
78 fprintf(loginfo,
" %s,\n", petfile);
79 fprintf(loginfo,
" %s,\n", inputfile1);
80 fprintf(loginfo,
" %s,\n", inputfile2);
81 fprintf(loginfo,
" %s,\n", inputfile3);
82 fprintf(loginfo,
" %g,\n", *fitdur);
83 fprintf(loginfo,
" *fitframeNr, *tis, *inp, *iinp *loginfo, %d, *status\n", verbose);
84 fprintf(loginfo,
")\n");
87 if(status!=NULL) sprintf(status,
"program error");
88 if(img==NULL || (inp==NULL && iinp==NULL))
return -1;
89 if(petfile==NULL || strlen(petfile)<1)
return -2;
91 if(inputfile1==NULL || strlen(inputfile1)<1)
return -3;
else input_nr++;
92 if(inputfile2!=NULL && strlen(inputfile2)>0) input_nr++;
93 if(inputfile3!=NULL && strlen(inputfile3)>0) {
94 if(input_nr<2)
return -4;
97 if(fitframeNr==NULL || fitdur==NULL)
return -5;
98 if(status!=NULL) sprintf(status,
"arguments validated");
103 if(loginfo!=NULL && verbose>1) fprintf(loginfo,
"checking access to input files\n");
104 if(access(inputfile1, 0) == -1) {
105 sprintf(tmp,
"cannot read '%s'", inputfile1);
106 if(status!=NULL) strcpy(status, tmp);
107 else if(loginfo!=NULL) fprintf(loginfo,
"Error: %s\n", tmp);
108 else fprintf(wfp,
"Error: %s\n", tmp);
111 for(ii=2; ii<=input_nr; ii++) {
112 if(ii==2) fname=inputfile2;
else fname=inputfile3;
113 if(access(fname, 0) == -1) {
114 sprintf(tmp,
"cannot read '%s'", fname);
115 if(status!=NULL) strcpy(status, tmp);
116 else if(loginfo!=NULL) fprintf(loginfo,
"Error: %s\n", tmp);
117 else fprintf(wfp,
"Error: %s\n", tmp);
129 if(loginfo!=NULL && verbose>0) fprintf(loginfo,
"reading image %s\n", petfile);
131 ret=
imgRead(petfile, img); fflush(stderr); fflush(stdout);
133 sprintf(tmp,
"cannot read '%s': %s", petfile, img->
statmsg);
134 if(status!=NULL) strcpy(status, tmp);
135 else if(loginfo!=NULL) fprintf(loginfo,
"Error: %s\n", tmp);
136 else fprintf(wfp,
"Error: %s\n", tmp);
137 if(loginfo!=NULL) fprintf(loginfo,
"imgRead(%s, img) := %d\n", petfile, ret);
142 sprintf(tmp,
"%s is not an image.\n", petfile);
143 if(status!=NULL) strcpy(status, tmp);
144 else if(loginfo!=NULL) fprintf(loginfo,
"Error: %s\n", tmp);
145 else fprintf(wfp,
"Error: %s\n", tmp);
148 if(loginfo!=NULL && verbose>2)
149 fprintf(loginfo,
"image contains %d frames and %d planes.\n", img->
dimt, img->
dimz);
152 if(siffile!=NULL && siffile[0]) {
153 if(loginfo!=NULL && verbose>0) fprintf(loginfo,
"reading SIF %s\n", siffile);
157 sprintf(tmp,
"cannot read '%s': %s", siffile,
siferrmsg);
158 if(status!=NULL) strcpy(status, tmp);
159 else if(loginfo!=NULL) fprintf(loginfo,
"Error: %s\n", tmp);
160 else fprintf(wfp,
"Error: %s\n", tmp);
169 if(loginfo!=NULL && verbose>0) fprintf(loginfo,
"isotope code read from %s\n", siffile);
174 for(fi=0; fi<img->
dimt; fi++) {
176 img->
mid[fi]=0.5*(sif.
x1[fi]+sif.
x2[fi]);
178 if(loginfo!=NULL && verbose>0) fprintf(loginfo,
"image frame times read from %s\n", siffile);
184 if(loginfo!=NULL && verbose>1) fprintf(loginfo,
"checking image contents\n");
186 strcpy(tmp,
"image frame times not available");
187 if(status!=NULL) strcpy(status, tmp);
188 else if(loginfo!=NULL) fprintf(loginfo,
"Error: %s\n", tmp);
189 else fprintf(wfp,
"Error: %s\n", tmp);
193 if(loginfo!=NULL && verbose>1) fprintf(loginfo,
"checking frame overlap in %s\n", petfile);
196 strcpy(tmp,
"image has overlapping frame times");
197 if(status!=NULL) strcpy(status, tmp);
198 else if(loginfo!=NULL) fprintf(loginfo,
"Error: %s\n", tmp);
199 else fprintf(wfp,
"Error: %s\n", tmp);
203 if(!(*fitdur>0.0)) *fitdur=1.0E+99;
204 else if(*fitdur<1.0E+10) *fitdur*=60.0;
206 if(*fitframeNr<1 || *fitdur<=3.5) {
207 strcpy(tmp,
"image has no data in fit time range");
208 if(status!=NULL) strcpy(status, tmp);
209 else if(loginfo!=NULL) fprintf(loginfo,
"Error: %s\n", tmp);
210 else fprintf(wfp,
"Error: %s\n", tmp);
214 if(loginfo!=NULL && verbose>3) {
215 fprintf(loginfo,
"fittimeFinal := %g min\n", *fitdur);
216 fprintf(loginfo,
"fitframeNr := %d\n", *fitframeNr);
223 if(loginfo!=NULL && verbose>0) fprintf(loginfo,
"reading input data in %s\n", inputfile1);
225 if(
dftRead(inputfile1, &dft)) {
226 sprintf(tmp,
"cannot read '%s': %s", inputfile1,
dfterrmsg);
227 if(status!=NULL) strcpy(status, tmp);
228 else if(loginfo!=NULL) fprintf(loginfo,
"Error: %s\n", tmp);
229 else fprintf(wfp,
"Error: %s\n", tmp);
235 strcpy(tmp,
"only first TAC is used as input");
236 if(loginfo!=NULL) fprintf(loginfo,
"Warning: %s.\n", tmp);
237 else fprintf(wfp,
"Warning: %s.\n", tmp);
243 strcpy(tmp,
"missing values in input file");
244 if(status!=NULL) strcpy(status, tmp);
245 else if(loginfo!=NULL) fprintf(loginfo,
"Error: %s.\n", tmp);
246 else fprintf(wfp,
"Error: %s.\n", tmp);
252 if(loginfo!=NULL && verbose>3) fprintf(loginfo,
"%s\n", tmp);
254 fprintf(wfp,
"Warning: %s\n", tmp);
256 if(status!=NULL) strcpy(status, tmp);
257 else if(loginfo!=NULL) fprintf(loginfo,
"Error: %s.\n", tmp);
258 else fprintf(wfp,
"Error: %s.\n", tmp);
262 printf(
"input time range := %g - %g %s\n",
268 fprintf(wfp,
"Warning: assuming that times are in %s in %s\n",
274 printf(
"input time range := %g - %g %s\n",
280 if(loginfo!=NULL && verbose>1) fprintf(loginfo,
"verifying input peak\n");
283 if(status!=NULL) strcpy(status, tmp);
284 else if(loginfo!=NULL) fprintf(loginfo,
"Error: %s.\n", tmp);
285 else fprintf(wfp,
"Error: %s.\n", tmp);
290 printf(
"input time range := %g - %g %s\n",
298 for(ii=2; ii<=input_nr; ii++) {
299 if(ii==2) fname=inputfile2;
else fname=inputfile3;
304 strcpy(tmp,
"cannot allocate more memory");
305 if(status!=NULL) strcpy(status, tmp);
306 else if(loginfo!=NULL) fprintf(loginfo,
"Error: %s\n", tmp);
307 else fprintf(wfp,
"Error: %s\n", tmp);
311 if(loginfo!=NULL && verbose>0) fprintf(loginfo,
"reading input data in %s\n", fname);
313 sprintf(tmp,
"cannot read '%s': %s", fname,
dfterrmsg);
314 if(status!=NULL) strcpy(status, tmp);
315 else if(loginfo!=NULL) fprintf(loginfo,
"Error: %s\n", tmp);
316 else fprintf(wfp,
"Error: %s\n", tmp);
322 strcpy(tmp,
"only first TAC is used as input");
323 if(loginfo!=NULL) fprintf(loginfo,
"Warning: %s.\n", tmp);
324 else fprintf(wfp,
"Warning: %s.\n", tmp);
330 strcpy(tmp,
"missing values in input file");
331 if(status!=NULL) strcpy(status, tmp);
332 else if(loginfo!=NULL) fprintf(loginfo,
"Error: %s\n", tmp);
333 else fprintf(wfp,
"Error: %s\n", tmp);
337 if(tmpdft.
timeunit==TUNIT_UNKNOWN) {
340 fprintf(wfp,
"Warning: assuming that times are in %s in %s\n",
347 if(loginfo!=NULL && verbose>3) fprintf(loginfo,
"%s\n", tmp);
349 fprintf(wfp,
"Warning: %s\n", tmp);
351 if(status!=NULL) strcpy(status, tmp);
352 else if(loginfo!=NULL) fprintf(loginfo,
"Error: %s\n", tmp);
353 else fprintf(wfp,
"Error: %s\n", tmp);
357 if(loginfo!=NULL && verbose>1)
358 fprintf(loginfo,
"interpolating %d samples into %d samples.\n", tmpdft.
frameNr, dft.
frameNr);
361 if(loginfo!=NULL) fprintf(loginfo,
"dftInterpolateInto() := %d\n", ret);
362 if(status!=NULL) strcpy(status, tmp);
363 else if(loginfo!=NULL) fprintf(loginfo,
"Error: %s\n", tmp);
364 else fprintf(wfp,
"Error: %s\n", tmp);
372 printf(
"input time range := %g - %g %s\n",
379 if(loginfo!=NULL && verbose>0) fprintf(loginfo,
"checking and setting input sample time range\n");
382 starttime=0; endtime=*fitdur;
384 if(loginfo!=NULL && verbose>2) {
385 fprintf(loginfo,
"starttime := %g min\n", starttime);
386 fprintf(loginfo,
"endtime := %g min\n", endtime);
387 fprintf(loginfo,
"sample_nr := %d\n", n);
389 if(*fitdur>1.5*endtime && (*fitdur-endtime)>0.15) {
390 strcpy(tmp,
"input TAC is too short");
391 if(status!=NULL) strcpy(status, tmp);
392 else if(verbose>0) fprintf(wfp,
"Error: %s\n", tmp);
399 strcpy(tmp,
"too few input samples in specified fit duration");
400 if(status!=NULL) strcpy(status, tmp);
401 else if(loginfo!=NULL) fprintf(loginfo,
"Error: %s\n", tmp);
402 else fprintf(wfp,
"Error: %s\n", tmp);
408 printf(
"input time range := %g - %g %s\n",
410 printf(
"fitdur := %g min\n", *fitdur);
414 if(loginfo!=NULL && verbose>0) fprintf(loginfo,
"Input TAC cutoff at %g sec\n", f);
415 for(fi=0; fi<dft.
frameNr; fi++)
if(dft.
x[fi]>f)
break;
419 if(verbose>10) printf(
"input time range := %g - %g %s\n",
424 if(loginfo!=NULL && verbose>0) fprintf(loginfo,
"copying input TAC\n");
427 strcpy(tmp,
"cannot copy TAC contents");
428 if(status!=NULL) strcpy(status, tmp);
429 else if(loginfo!=NULL) fprintf(loginfo,
"Error: %s.\n", tmp);
430 else fprintf(wfp,
"Error: %s\n", tmp);
437 if(loginfo!=NULL && verbose>0) fprintf(loginfo,
"interpolating input TAC\n");
440 if(status!=NULL) strcpy(status, tmp);
441 else if(loginfo!=NULL) fprintf(loginfo,
"Error: %s.\n", tmp);
442 else fprintf(wfp,
"Error: %s\n", tmp);
448 if(status!=NULL) sprintf(status,
"ok");