5#include "tpcclibConfig.h"
50 int verbose=0;
if(status!=NULL) verbose=status->
verbose;
51 if(verbose>0) printf(
"%s()\n", __func__);
52 if(first!=NULL) *first=0;
53 if(last!=NULL) *last=0;
54 if(d==NULL || startTime==NULL || endTime==NULL) {
60 *startTime=*endTime=0.0;
64 if(*endTime<0.0) *endTime=1.0E+100;
69 if(!isnan(cf)) {*startTime*=cf; *endTime*=cf;}
71 printf(
"startTime := %g\n", *startTime);
72 printf(
"endTime := %g\n", *endTime);
79 if(
tacXRange(d, &s, &e) || e<*startTime || s>*endTime) {
80 *startTime=*endTime=0.0;
81 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INVALID_X_RANGE);
93 if(d->
isframe) f=0.5*(d->
x1[i]+d->
x2[i]);
else f=d->
x[i];
94 if(verbose>3) printf(
" f := %g\n", f);
95 if(s<0 && f>=*startTime) s=i;
96 if(f<=*endTime) e=i;
else break;
99 *startTime=*endTime=0.0;
103 if(first!=NULL) *first=s;
104 if(last!=NULL) *last=e;
111 if(!isnan(cf)) {*startTime/=cf; *endTime/=cf;}
132 const char *tissuefile,
134 const char *inputfile1,
137 const char *inputfile2,
139 const char *inputfile3,
158 int verbose=0;
if(status!=NULL) verbose=status->
verbose;
159 if(verbose>0) printf(
"%s()\n", __func__);
160 if(tis==NULL || inp==NULL) {
164 if(tissuefile==NULL || inputfile1==NULL ||
strnlen(inputfile1, 1)<1) {
171 if(inputfile2!=NULL &&
strnlen(inputfile2, 1)>0) input_nr++;
172 if(inputfile3!=NULL &&
strnlen(inputfile3, 1)>0) {
179 if(verbose>2) printf(
"input_nr := %d\n", input_nr);
183 if(fitSampleNr!=NULL) *fitSampleNr=0;
189 if(verbose>1) printf(
"reading tissue data in %s\n", tissuefile);
190 ret=
tacRead(tis, tissuefile, status);
206 statusSet(status, __func__, __FILE__, __LINE__, ret);
218 if(verbose>1) printf(
"reading input data 1 in %s\n", inputfile1);
219 ret=
tacRead(inp, inputfile1, status);
225 fprintf(stderr,
"Warning: input sample time units not known.\n");}
232 fprintf(stderr,
"Warning: using only first TAC in %s\n", inputfile1);
253 for(
int ii=2; ii<=input_nr; ii++) {
254 if(ii==2) fname=(
char*)inputfile2;
else fname=(
char*)inputfile3;
255 if(verbose>1) printf(
"reading input data %d in %s\n", ii, fname);
256 ret=
tacRead(&tmptac, fname, status);
262 fprintf(stderr,
"Warning: using only first TAC in %s\n", fname);
299 if(ret && verbose>0) {
300 fprintf(stderr,
"Warning: check that regional data times are in minutes.\n");
303 if(ret && verbose>0) {
304 fprintf(stderr,
"Warning: check that input data times are in minutes.\n");
310 if(ret || iend<=0.0 || tend<=0.0) {
315 if(tend>10.0*iend || tend<0.10*iend) {
316 if(verbose>0) fprintf(stderr,
"Warning: check the sample time units.\n");
320 if(ret && verbose>0) {
321 fprintf(stderr,
"Warning: check the calibration units.\n");
327 if(verbose>1) printf(
"checking and setting fit time length\n");
329 double starttime=0, endtime=*fitdur;
330 int fnr=
tacFittime(tis, &starttime, &endtime, NULL, NULL, status);
332 fprintf(stdout,
"tis.sampleNr := %d\n", tis->
sampleNr);
333 fprintf(stdout,
"starttime := %g\n", starttime);
334 fprintf(stdout,
"endtime := %g\n", endtime);
337 fprintf(stdout,
"fitSampleNr := %d\n", fnr);
340 if(fitSampleNr!=NULL) *fitSampleNr=fnr;
343 if(*fitdur>1.2*iend) {
350 if(cutInput && iend>*fitdur) {
351 if(verbose>1) printf(
"cut off too many input samples\n");
354 if(inp->
isframe) f=0.5*(inp->
x1[i]+inp->
x2[i]);
else f=inp->
x[i];
355 if(f>(*fitdur))
break;
357 if(i<inp->sampleNr) i++;
364 if(verbose>2) fprintf(stdout,
"inp.sampleNr := %d\n", inp->
sampleNr);
398 const char *reference,
412 int verbose=0;
if(status!=NULL) verbose=status->
verbose;
413 if(verbose>0) printf(
"%s()\n", __func__);
414 if(tis==NULL || reference==NULL) {
425 if(verbose>1) printf(
"trying to read file %s\n", reference);
432 int n, ret=0, refi=0;
436 if(verbose>0 && ret>0)
437 fprintf(stderr,
"Warning: check the units of reference and tissue data.\n");
449 temp.
c[0].
sw=2; refi=0;
451 for(
int i=0; i<temp.
tacNr; i++) temp.
c[i].
sw=1;
455 if(verbose>1) printf(
"selected ref region := %s\n", temp.
c[refi].
name);
463 if(verbose>2) printf(
"adding %d ref tacs to ref struct\n", temp.
tacNr);
466 statusSet(status, __func__, __FILE__, __LINE__, ret);
469 if(refIndex!=NULL) *refIndex=refi;
471 if(verbose>2) printf(
"adding %d ref tacs to tis struct\n", temp.
tacNr);
472 for(
int i=0; i<tis->
tacNr; i++) tis->
c[i].
sw=0;
477 statusSet(status, __func__, __FILE__, __LINE__, ret);
480 if(refIndex!=NULL) *refIndex=tis->
tacNr+refi;
485 if(verbose>1) printf(
"%d ref region(s) found in file.\n", n);
488 }
else if(verbose>1) {
489 printf(
" '%s' was not a file\n", reference); fflush(stdout);
494 if(verbose>1) printf(
"trying to find reference in TAC struct\n");
497 if(verbose>1) {printf(
"%d region(s) matched.\n", n); fflush(stdout);}
503 if(verbose>2) {printf(
"... which means all region(s) matched.\n"); fflush(stdout);}
512 tis->
c[refi].
sw=2;
if(refIndex!=NULL) *refIndex=refi;
516 if(verbose>1) {printf(
"reference TAC tagged.\n"); fflush(stdout);}
522 if(verbose>1) printf(
"moving reference TAC(s) to output TAC struct\n");
526 statusSet(status, __func__, __FILE__, __LINE__, ret);
529 int i=ref->
tacNr-1; ret=0;
530 while(i>=0 && !ret) {
535 while(i>=0 && !ret) {
545 for(i=0; i<ref->
tacNr; i++)
if(ref->
c[i].
sw==2) {*refIndex=i;
break;}
562 const char *inputfile1,
565 const char *inputfile2,
567 const char *inputfile3,
576 int verbose=0;
if(status!=NULL) verbose=status->
verbose;
577 if(verbose>0) {printf(
"%s()\n", __func__); fflush(stdout);}
582 if(inputfile1==NULL ||
strnlen(inputfile1, 1)<1) {
589 if(inputfile2!=NULL &&
strnlen(inputfile2, 1)>0) input_nr++;
590 if(inputfile3!=NULL &&
strnlen(inputfile3, 1)>0) {
597 if(verbose>2) {printf(
"input_nr := %d\n", input_nr); fflush(stdout);}
607 printf(
"reading input data 1 in %s\n", inputfile1); fflush(stdout);
609 ret=
tacRead(inp, inputfile1, status);
625 fprintf(stderr,
"Warning: using only first TAC in %s\n", inputfile1);
639 for(
int ii=2; ii<=input_nr; ii++) {
640 if(ii==2) fname=(
char*)inputfile2;
else fname=(
char*)inputfile3;
642 printf(
"reading input data %d in %s\n", ii, fname);
645 ret=
tacRead(&tmptac, fname, status);
651 fprintf(stderr,
"Warning: using only first TAC in %s\n", fname);
int tacInterpolateInto(TAC *inp, TAC *tac, TAC *itac, TAC *iitac, TPCSTATUS *status)
Add TACs from one TAC structure into another TAC structure, interpolating the input TACs and allocati...
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
size_t strnlen(const char *s, size_t n)
char name[MAX_TACNAME_LEN+1]
int verbose
Verbose level, used by statusPrint() etc.
int tacDuplicate(TAC *tac1, TAC *tac2)
Make a duplicate of TAC structure.
int tacAllocateMore(TAC *tac, int tacNr)
int tacCopyTacc(TACC *d1, TACC *d2, int sampleNr)
int tacGetIsotope(TAC *tac)
void tacSetIsotope(TAC *tac, int isotope)
int tacRead(TAC *d, const char *fname, TPCSTATUS *status)
int tacSortByTime(TAC *d, TPCSTATUS *status)
int tacDeleteTACC(TAC *d, int i)
int tacSelectTACs(TAC *d, const char *region_name, int reset, TPCSTATUS *status)
int tacSelectBestReference(TAC *d)
int tacYUnitConvert(TAC *tac, const int u, TPCSTATUS *status)
int tacXUnitConvert(TAC *tac, const int u, TPCSTATUS *status)
int tacXMatch(TAC *d1, TAC *d2, const int verbose)
Check whether sample (frame) times are the same (or very close to) in two TAC structures.
int tacCorrectFrameOverlap(TAC *d, TPCSTATUS *status)
Correct PET frame start and end times if frames are slightly overlapping or have small gaps in betwee...
int tacXCopy(TAC *tac1, TAC *tac2, int i1, int i2)
int tacIsX(TAC *d)
Verify if TAC structure contains reasonable x values (times).
int tacXRange(TAC *d, double *xmin, double *xmax)
Get the range of x values (times) in TAC structure.
Header file for library libtpcextensions.
@ UNIT_UNKNOWN
Unknown unit.
@ TPCERROR_NO_REFERENCE
Reference not found.
@ TPCERROR_INVALID_XRANGE
Invalid sample time range.
@ TPCERROR_INVALID_VALUE
Invalid value.
@ TPCERROR_FAIL
General error.
@ TPCERROR_INVALID_X
Invalid sample time.
@ TPCERROR_NO_DATA
File contains no data.
@ TPCERROR_TOO_FEW
File contains too few samples.
@ TPCERROR_MISSING_DATA
File contains missing values.
double unitConversionFactor(const int u1, const int u2)
@ ISOTOPE_UNKNOWN
Unknown.
Header file for libtpcli.
Header file for library libtpctac.
Header file for libtpctacmod.