TPCCLIB
Loading...
Searching...
No Matches
imgdecayc.c File Reference

Physical decay and isotopes in IMG. More...

#include "libtpcimgio.h"

Go to the source code of this file.

Functions

int imgDecayCorrection (IMG *image, int mode)
 
char * imgIsotope (IMG *img)
 
int imgSetDecayCorrFactors (IMG *image, int mode)
 
int imgBranchingCorrection (IMG *image, int mode, int verbose, char *status)
 

Detailed Description

Physical decay and isotopes in IMG.

Author
Vesa Oikonen

Definition in file imgdecayc.c.

Function Documentation

◆ imgBranchingCorrection()

int imgBranchingCorrection ( IMG * image,
int mode,
int verbose,
char * status )

Corrects image data for branching fraction (mode=1) or removes correction (mode=0). Removal is primarily based on branching factor stored in IMG structure, secondarily on isotope; after removal, branching factor is set to 1, and pixel values and calibration factor are multiplied with it. Correction is based on branching fractions in branch.h; pixel values and calibration factor are divided by it, and its value is stored in IMG structure.

Note
This function can not know if branching fraction correction is included in the data (as it usually is) or not.
Returns
Returns 0 if ok.
See also
imgDecayCorrection, imgSetDecayCorrFactors, imgIsotope
Parameters
imagePointer to IMG data
modeBranching fraction correction (1) or removal of correction (0)
verboseVerbose level; if zero, then nothing is printed into stdout or stderr
statusPointer to allocated string where error message will be written; NULL, if not needed.

Definition at line 132 of file imgdecayc.c.

141 {
142 int isotope;
143 float bf;
144 float cf;
145
146 if(verbose>0) printf("imgBranchingCorrection(*img, %d, %d, *status)\n",
147 mode, verbose);
148 /* Check for arguments */
149 if(status!=NULL) strcpy(status, "invalid input");
150 if(image->status!=IMG_STATUS_OCCUPIED) return(1);
151 if(image->isotopeHalflife<=0.0) {
152 if(verbose>0) printf("Error: unknown isotope.\n");
153 if(status!=NULL) strcpy(status, "unknown isotope");
154 return(2);
155 }
156
157 /* If branching factor not found in IMG, then get it based on half-life */
158 bf=image->branchingFraction;
159 if(bf<=0.0 || bf>=1.0) {
160 isotope=hlIsotopeFromHalflife(image->isotopeHalflife/60.0);
161 if(verbose>2) printf("isotope := %d\n", isotope);
162 bf=branchingFraction(isotope);
163 }
164 if(bf<=0.0 || bf>=1.0) {
165 if(verbose>0) printf("Error: branching fraction unknown for the isotope.\n");
166 if(status!=NULL) strcpy(status, "branching fraction unknown for the isotope");
167 return(3);
168 }
169
170 /* Multiply with BF to remove correction, and divide to correct */
171 if(mode==0) cf=bf; else cf=1.0/bf;
172 /* Process pixel values */
173 if(verbose>1) printf("multiplying data by %g\n", cf);
174 for(int fi=0; fi<image->dimt; fi++) {
175 for(int pi=0; pi<image->dimz; pi++)
176 for(int i=0; i<image->dimy; i++)
177 for(int j=0; j<image->dimx; j++)
178 image->m[pi][i][j][fi]*=cf;
179 }
180
181 /* Fix header contents, too */
182 if(image->calibrationFactor>0.0) image->calibrationFactor*=cf;
183 if(mode==0) image->branchingFraction=0.0;
184 else image->branchingFraction=bf;
185
186 if(status!=NULL) strcpy(status, "ok");
187 return(0);
188}
float branchingFraction(int isotope)
Definition branch.c:14
int hlIsotopeFromHalflife(double halflife)
Definition halflife.c:195
#define IMG_STATUS_OCCUPIED
unsigned short int dimx
float branchingFraction
float **** m
char status
unsigned short int dimt
unsigned short int dimz
unsigned short int dimy
float calibrationFactor
float isotopeHalflife

◆ imgDecayCorrection()

int imgDecayCorrection ( IMG * image,
int mode )

Corrects (mode=1) or removes correction (mode=0) for physical decay. Removal is based on existing decay correction factors, when possible.

Returns
0 if ok, 1 image status is not 'occupied', 2 decay already corrected/not corrected, 3 image frame times missing
See also
imgSetDecayCorrFactors, imgBranchingCorrection, imgIsotope
Parameters
imagePointer to IMG data.
mode0=Remove decay correction; 1=Correct for decay.

Definition at line 16 of file imgdecayc.c.

21 {
22 float lambda;
23 float cf, dur;
24
25 /* Check for arguments */
26 if(image->status!=IMG_STATUS_OCCUPIED) return(1);
27 if(image->isotopeHalflife<=0.0) return(1);
28 /* Existing/nonexisting decay correction is an error */
29 if(mode==1 && image->decayCorrection!=IMG_DC_NONCORRECTED) return(2);
30 if(mode==0 && image->decayCorrection!=IMG_DC_CORRECTED) return(2);
31
32 /* All time frames */
33 for(int fi=0; fi<image->dimt; fi++) {
34 dur=image->end[fi]-image->start[fi];
35 if(image->end[fi]>0.0) {
36 if(mode==0 && image->decayCorrFactor[fi]>1.000001) {
37 /* if decay correction is to be removed, and factor is known,
38 then use it */
39 cf=1.0/image->decayCorrFactor[fi];
40 } else {
41 lambda=hl2lambda(image->isotopeHalflife); if(lambda<0.0) return(1);
42 /* remove decay correction by giving negative lambda */
43 if(mode==0) lambda=-lambda;
44 if(fi==image->dimt-1 && image->end[fi]<=0.0) return(3);
45 cf=hlLambda2factor_float(lambda, image->start[fi], dur);
46 }
47 if(IMG_TEST) printf("applied_dc_factor[%d] := %g\n", fi+1, cf);
48 /* Set decay correction factor inside IMG for future */
49 if(mode==0) {
50 image->decayCorrFactor[fi]=1.0;
51 } else {
52 image->decayCorrFactor[fi]=cf;
53 }
54 /* All planes, all matrices */
55 for(int pi=0; pi<image->dimz; pi++)
56 for(int i=0; i<image->dimy; i++)
57 for(int j=0; j<image->dimx; j++)
58 image->m[pi][i][j][fi]*=cf;
59 if(mode==0) image->decayCorrection=IMG_DC_NONCORRECTED;
61 /* in some cases left unchanged! */
62 }
63 } /* next frame */
64 return(0);
65}
double hl2lambda(double halflife)
Definition halflife.c:84
float hlLambda2factor_float(float lambda, float frametime, float framedur)
Definition halflife.c:118
int IMG_TEST
Definition img.c:6
#define IMG_DC_CORRECTED
#define IMG_DC_NONCORRECTED
char decayCorrection
float * start
float * end
float * decayCorrFactor

◆ imgIsotope()

char * imgIsotope ( IMG * img)

Returns pointer to string describing the isotope in image data

Parameters
imgimage structure
Returns
pointer to string
See also
imgDecayCorrection

Definition at line 76 of file imgdecayc.c.

76 {
78}
char * hlIsotopeCode(int isotope)
Definition halflife.c:36

Referenced by ecat63AddImg(), ecat63WriteAllImg(), img2sif(), imgSetEcat63MHeader(), imgSetEcat7MHeader(), and sifAllocateWithIMG().

◆ imgSetDecayCorrFactors()

int imgSetDecayCorrFactors ( IMG * image,
int mode )

Sets (mode=1) or removes (mode=0) decay correction factors in IMG. IMG pixel data is not changed.

Returns
0 if ok, 1 image status is not 'occupied', 2 invalid exponent value, 3 image frame times are missing.
See also
imgDecayCorrection, imgBranchingCorrection, imgIsotope
Parameters
imagePointer to IMG data.
modeFactors are calculated for decay correction (1) or for removing decay correction (0).

Definition at line 87 of file imgdecayc.c.

92 {
93 float lambda, cf, dur;
94
95 /* Check for arguments */
96 if(image->status!=IMG_STATUS_OCCUPIED) return(1);
97 if(image->isotopeHalflife<=0.0) return(1);
98
99 /* Check that image contains frame times */
100 if(mode!=0 && image->end[image->dimt-1]<=0.0) return(3);
101
102 /* All time frames */
103 for(int fi=0; fi<image->dimt; fi++) {
104 if(mode==0) {
105 image->decayCorrFactor[fi]=1.0;
106 } else {
107 dur=image->end[fi]-image->start[fi];
108 if(image->end[fi]>0.0) {
109 lambda=hl2lambda(image->isotopeHalflife); if(lambda<0.0) return(2);
110 cf=hlLambda2factor_float(lambda, image->start[fi], dur);
111 image->decayCorrFactor[fi]=cf;
112 }
113 }
114 } /* next frame */
115 if(mode==0) image->decayCorrection=IMG_DC_NONCORRECTED;
117 return(0);
118}