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

Calibrate reconstructed ECAT 931 image from counts to kBq/mL using specific file containing calibration coefficients. More...

Go to the source code of this file.

Functions

int selectEcat931Calibrationfile (char *foldername, struct tm *stm, char *fname, int verbose)
int readEcat931Calibrationfile (char *fname, double *coef, int verbose)

Detailed Description

Calibrate reconstructed ECAT 931 image from counts to kBq/mL using specific file containing calibration coefficients.

Updated cal4img version 1.3 (1997-09-14) which could only be compiled and run in Sun UNIX workstations.

Author
Vesa Oikonen

Definition in file ecalibr.c.

Function Documentation

◆ readEcat931Calibrationfile()

int readEcat931Calibrationfile ( char * fname,
double * coef,
int verbose )

Read ECAT 931 calibration coefficients from specific file.

Returns
0 if successful.
Parameters
fnameFilename which contains calibration factors for each image plane
coefPointer to double array of size 15, into which calibration coefficients are written.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout

Definition at line 453 of file ecalibr.c.

461 {
462 if(verbose>0) printf("readEcat931Calibrationfile('%s', ...)\n", fname);
463 if(fname==NULL || coef==NULL) return(1);
464
465 /* Read file into IFT struct */
466 IFT ift; iftInit(&ift);
467 if(iftRead(&ift, fname, 0, 0)!=0) return(2);
468 if(ift.keyNr<15) {iftEmpty(&ift); return(3);}
469
470 /* Jump over the first line which should contain the date, and then
471 read plane factors from the next 15 lines */
472 double v;
473 for(int i=1; i<=15; i++) {
474 if(verbose>1) printf("\t%d\t%s\n", i, ift.item[i].value);
475 if(atof_with_check(ift.item[i].value, &v)) {iftEmpty(&ift); return(4);}
476 if(v<=0.0) {iftEmpty(&ift); return(5);}
477 coef[i-1]=v;
478 }
479 iftEmpty(&ift);
480 return(0);
481}
int atof_with_check(char *double_as_string, double *result_value)
Definition decpoint.c:107
void iftEmpty(IFT *ift)
Definition ift.c:60
void iftInit(IFT *ift)
Definition ift.c:45
int iftRead(IFT *ift, char *filename, int is_key_required, int verbose)
Definition iftfile.c:24
int keyNr
Definition libtpcmisc.h:270
IFT_KEY_AND_VALUE * item
Definition libtpcmisc.h:279

◆ selectEcat931Calibrationfile()

int selectEcat931Calibrationfile ( char * foldername,
struct tm * stm,
char * fname,
int verbose )

Select correct calibration file from given folder.

Returns
0 if successful.
Parameters
foldernameFolder where calibration files are located
stmPointer to scan start time; calibration file is selected based on it.
fnamePointer to allocated string where filename will be written
verboseVerbose level; if zero, then nothing is printed to stderr or stdout

Definition at line 378 of file ecalibr.c.

387 {
388 if(verbose>0) printf("selectEcat931Calibrationfile('%s', ...)\n", foldername);
389 if(foldername==NULL || stm==NULL || fname==NULL) return(1);
390 strcpy(fname, "");
391
392 char buf[256];
393
394 /* Check whether foldername is a directory */
395 struct stat fst;
396 stat(foldername, &fst);
397 if(!S_ISDIR(fst.st_mode)) { /* it is not */
398 if(verbose>1) printf(" %s is not a directory\n", foldername);
399 return(1);
400 }
401
402 /* Open the directory */
403 DIR *dp;
404 dp=opendir(foldername); if(dp==NULL) {
405 if(verbose>1) printf("Error: cannot open directory %s\n", foldername);
406 return(2);
407 }
408
409 /* Go throught the directory */
410 struct dirent *de;
411 char year[10], month[10], day[10], cfname[FILENAME_MAX];
412 struct tm ftm;
413 double tdif, tdif_min=-1.0E30;
414 cfname[0]=(char)0;
415 while((de=readdir(dp))!=NULL) {
416 if(verbose>2) printf("d_name='%s'\n", de->d_name);
417 if(de->d_name[0]=='.') continue; /* Ignore hidden and 'system' dirs */
418 if(strlen(de->d_name)<6) continue;
419 /* Identify the date from filename which should be in format YYMMDD */
420 strlcpy(year, de->d_name, 3);
421 strlcpy(month, de->d_name+2, 3);
422 strlcpy(day, de->d_name+4, 3);
423 sprintf(buf, "%s.%s.%s 16:00:00", day, month, year);
424 /* Convert to struct tm */
425 if(get_datetime(buf, &ftm, 0)) continue;
426 if(verbose>3) printf(" %s\n", buf);
427 /* Calculate time difference between calibration and scan time */
428 tdif=tmDifference(&ftm, stm);
429 if(verbose>3) printf(" sec_difference := %.0f\n", tdif);
430 /* If later than scan time, then forget this */
431 if(tdif>0.0) continue;
432 /* If smaller difference than before, then save this */
433 if(tdif>tdif_min) {tdif_min=tdif; strcpy(cfname, de->d_name);}
434 }
435 closedir(dp);
436 if(tdif_min<-1.0E10 || !cfname[0]) return(3);
437 if(verbose>1) printf("selected_file := %s\n", cfname);
438
439 /* Combine path and name */
440 if(foldername[strlen(foldername)-1]!='/')
441 sprintf(fname, "%s/%s", foldername, cfname);
442 else
443 sprintf(fname, "%s%s", foldername, cfname);
444
445 return(0);
446}
int get_datetime(char *str, struct tm *date, int verbose)
Definition datetime.c:322
double tmDifference(struct tm *tm1, struct tm *tm0)
Definition datetime.c:502
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition strext.c:245