8#include "tpcclibConfig.h"
21static char *info[] = {
22 "Combine two or more dynamic ECAT 6.3 or 7.x images or sinograms.",
23 "This program can be used to catenate separate scans/images from one study,",
24 "when imaging session has been interrupted for some reason, or when",
25 "flat data with frames in separate files were converted to ECAT format.",
27 "Program corrects for decay and frame times to the start time of the first",
28 "specified image/sinogram. Use the original images or sinograms, if possible,",
29 "Input images must be decay corrected to the scan start time that is",
30 "specified in the main header and shown by this program.",
32 "Usage: @P [Options] file1 file2 [file3 ...] outputfile",
37 "See also: lmlist, eframe, lmhdr, sifcat, taccat, esplit, imgdelfr, imgcat",
40 " @P a2345dy1.v a2345dy2.v a2345dy.v",
42 "Keywords: image, ECAT, tool, catenate, time",
61int main(
int argc,
char **argv)
63 int ai, help=0, version=0, verbose=1;
64 int i, ii, m, ret, frameNr, planeNr, blkNr;
66 int plane, prev_plane, frame, catframeNr, catplaneNr;
67 int matrixNr, pxlNr=0, blk, nxtblk, dimz;
68 char ecatfile[FILENAME_MAX], catfile[FILENAME_MAX],
69 tmp[FILENAME_MAX], *mdata, *cptr;
83 int matnum, datatype=0, t;
90 if(argc==1) {
tpcPrintUsage(argv[0], info, stderr);
return(1);}
91 ecatfile[0]=catfile[0]=(char)0;
94 for(ai=1; ai<argc; ai++)
if(*argv[ai]==
'-') {
95 cptr=argv[ai]+1;
if(*cptr==
'-') cptr++;
if(cptr==NULL)
continue;
97 fprintf(stderr,
"Error: invalid option '%s'.\n", argv[ai]);
102 if(help==2) {
tpcHtmlUsage(argv[0], info,
"");
return(0);}
107 strcpy(catfile, argv[argc-1]);
110 for(; ai<argc-1; ai++) {
113 if(access(argv[ai], 0) == -1) {
114 fprintf(stderr,
"Error: file '%s' does not exist.\n", argv[ai]);
118 if(strcasecmp(argv[ai], catfile)==0) {
119 fprintf(stderr,
"Error: same name for input and output file.\n");
127 fprintf(stderr,
"Error: missing command-line argument; try %s --help\n",
134 printf(
"catenated_file := %s\n", catfile);
135 printf(
"fileNr := %d\n", fileNr);
143 fprintf(stderr,
"Error: %s\n", tmp);
151 strcpy(ecatfile, argv[ffi]);
152 if(verbose>1) printf(
"reading first main header in %s\n", ecatfile);
154 if((fp1=fopen(ecatfile,
"rb")) == NULL) {
155 fprintf(stderr,
"Error: cannot open file %s\n", ecatfile);
162 fprintf(stderr,
"Error (%d): cannot read main header.\n", ret);
163 fclose(fp1);
return(2);
166 if(strncmp(e7first_mhdr.
magic_number, ECAT7V_MAGICNR, 7)==0) {
180 fprintf(stderr,
"Error: illegal file_type in %s\n", ecatfile);
183 if(verbose>1) printf(
"%s is identified as an ECAT 7 file.\n", ecatfile);
186 fprintf(stderr,
"Error (%d): cannot read main header.\n", ret);
187 fclose(fp1);
return(2);
195 fprintf(stderr,
"Error: illegal file_type in %s\n", ecatfile);
200 if(verbose>1) printf(
"%s is identified as an ECAT 6.3 file.\n", ecatfile);
206 if(verbose>1) printf(
"creating output file %s\n", catfile);
207 if(fformat==63) fp2=
ecat63Create(catfile, &e63first_mhdr);
210 fprintf(stderr,
"Error: cannot write file %s\n", catfile);
217 catframeNr=catplaneNr=0;
218 for(ii=ffi; ii<argc-1; ii++) {
221 strcpy(ecatfile, argv[ii]);
222 if(verbose>0) {fprintf(stdout,
" reading %s\n", ecatfile); fflush(stdout);}
223 if((fp1=fopen(ecatfile,
"rb")) == NULL) {
224 fprintf(stderr,
"Error: cannot open file %s\n", ecatfile);
225 fclose(fp2); remove(catfile);
return(2);
231 fprintf(stderr,
"Error (%d): cannot read main header.\n", ret);
232 fclose(fp1); fclose(fp2); remove(catfile);
return(2);
242 fprintf(stderr,
"Error: different file_type in %s\n", ecatfile);
243 fclose(fp1); fclose(fp2); remove(catfile);
return(3);
246 fprintf(stderr,
"Error: different data_type in %s\n", ecatfile);
247 fclose(fp1); fclose(fp2); remove(catfile);
return(3);
256 fprintf(stderr,
"Warning: invalid scan_start_time in %s\n", ecatfile);
259 fprintf(stdout,
" scan_start_time := %s\n", tmp);
272 fprintf(stderr,
"Error: check the order of files and scan start times.\n");
273 fclose(fp1); fclose(fp2); remove(catfile);
return(3);
276 fprintf(stderr,
" Warning: possible inaccuracies in scan start times.\n");
286 if(verbose>0) printf(
" t=%d sec => decayf=%g\n", t, decayf);
295 fprintf(stderr,
"Error (%d): cannot read matrix list.\n", ret);
296 fclose(fp1); fclose(fp2); remove(catfile);
return(4);
298 if((fformat==63 && e63mlist.
matrixNr<=0) ||
299 (fformat==7 && e7mlist.
matrixNr<=0)) {
300 fprintf(stderr,
"Error: matrix list is empty.\n");
301 fclose(fp1); fclose(fp2); remove(catfile);
return(4);
311 printf(
"and after sorting:\n");
319 prev_plane=plane=-1; frameNr=planeNr=0;
320 if(fformat==63) matrixNr=e63mlist.
matrixNr;
322 for(m=0; m<matrixNr; m++) {
327 plane=e63matval.
plane;
329 else frame=e63matval.
gate;
330 if(verbose>1) printf(
" frame=%d plane=%d\n", frame, plane);
333 plane=e7matval.
plane;
335 else frame=e7matval.
gate;
336 if(verbose>1) printf(
" frame=%d\n", frame);
340 if(plane!=prev_plane) {frameNr=1; planeNr++;}
else {frameNr++;}
353 fprintf(stderr,
"Error: cannot read matrix %u subheader in '%s'.\n",
355 fclose(fp1); fclose(fp2); remove(catfile);
375 fprintf(stderr,
"Error: cannot read matrix %u subheader in '%s'.\n",
377 fclose(fp1); fclose(fp2); remove(catfile);
394 fprintf(stderr,
"Error: out of memory.\n");
395 fclose(fp1); fclose(fp2); remove(catfile);
412 fprintf(stderr,
"Error: cannot read matrix data!\n");
413 fclose(fp1); fclose(fp2); remove(catfile);
458 e63matval.
data, e63matval.
bed);
503 fprintf(stderr,
"Error (%d) in writing data on pl%02d fr%02d.\n",
505 fclose(fp1); fclose(fp2); remove(catfile);
517 fprintf(stdout,
" %d frame(s) and %d plane(s) written in %s\n",
518 frameNr, planeNr, catfile);
520 fprintf(stdout,
" %d frame(s) written in %s\n",
524 catframeNr+=frameNr;
if(planeNr>catplaneNr) catplaneNr=planeNr;
536 if(catplaneNr>1) e7first_mhdr.
num_planes=catplaneNr;
543 fprintf(stderr,
"Error: cannot write the mainheader.\n");
547 if(verbose>0) fprintf(stdout,
" done.\n");
int backupExistingFile(char *filename, char *backup_ext, char *status)
char * ctime_r_int(const time_t *t, char *buf)
Convert calendard time t into a null-terminated string of the form YYYY-MM-DD hh:mm:ss,...
int ecat63ReadMatlist(FILE *fp, MATRIXLIST *ml, int verbose)
void ecat63InitMatlist(MATRIXLIST *mlist)
void ecat63EmptyMatlist(MATRIXLIST *mlist)
int mat_numcod(int frame, int plane, int gate, int data, int bed)
void ecat63SortMatlistByPlane(MATRIXLIST *ml)
void ecat63PrintMatlist(MATRIXLIST *ml)
void mat_numdoc(int matnum, Matval *matval)
void ecat63PrintMainheader(ECAT63_mainheader *h, FILE *fp)
char * ecat63ScanstarttimeInt(const ECAT63_mainheader *h, char *buf)
Convert scan_start_time in ECAT 6.3 main header into a null-terminated string of the form YYYY-MM-DD ...
int ecat63ReadScanheader(FILE *fp, int blk, ECAT63_scanheader *h, int verbose, char *errmsg)
int ecat63ReadMatdata(FILE *fp, int strtblk, int blkNr, char *data, int dtype)
int ecat63ReadImageheader(FILE *fp, int blk, ECAT63_imageheader *h, int verbose, char *errmsg)
int ecat63ReadMainheader(FILE *fp, ECAT63_mainheader *h)
int ecat63WriteScan(FILE *fp, int matnum, ECAT63_scanheader *h, void *data)
int ecat63WriteImage(FILE *fp, int matnum, ECAT63_imageheader *h, void *data)
FILE * ecat63Create(const char *fname, ECAT63_mainheader *h)
int ecat63WriteMainheader(FILE *fp, ECAT63_mainheader *h)
void ecat7InitMatlist(ECAT7_MATRIXLIST *mlist)
void ecat7SortMatlistByPlane(ECAT7_MATRIXLIST *ml)
void ecat7_id_to_val(int matrix_id, ECAT7_Matval *matval)
int ecat7ReadMatlist(FILE *fp, ECAT7_MATRIXLIST *ml, int verbose)
void ecat7EmptyMatlist(ECAT7_MATRIXLIST *mlist)
int ecat7EnterMatrix(FILE *fp, int matrix_id, int block_nr)
int ecat7_val_to_id(int frame, int plane, int gate, int data, int bed)
void ecat7PrintMatlist(ECAT7_MATRIXLIST *ml)
void ecat7PrintMainheader(ECAT7_mainheader *h, FILE *fp)
int ecat7pxlbytes(short int data_type)
int ecat7ReadScanheader(FILE *fp, int blk, ECAT7_scanheader *h)
int ecat7ReadMatrixdata(FILE *fp, int start_block, int block_nr, char *data, int dtype)
int ecat7ReadMainheader(FILE *fp, ECAT7_mainheader *h)
int ecat7Read2DScanheader(FILE *fp, int blk, ECAT7_2Dscanheader *h)
int ecat7ReadImageheader(FILE *fp, int blk, ECAT7_imageheader *h)
int ecat7WriteScanheader(FILE *fp, int blk, ECAT7_scanheader *h)
int ecat7WriteImageheader(FILE *fp, int blk, ECAT7_imageheader *h)
int ecat7WriteMatrixdata(FILE *fp, int start_block, char *data, long long pxl_nr, int pxl_size)
int ecat7Write2DScanheader(FILE *fp, int blk, ECAT7_2Dscanheader *h)
FILE * ecat7Create(const char *fname, ECAT7_mainheader *h)
int ecat7WriteMainheader(FILE *fp, ECAT7_mainheader *h)
Header file for libtpcimgio.
Header file for libtpcmisc.
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
int tpcHtmlUsage(const char *program, char *text[], const char *path)
void tpcPrintBuild(const char *program, FILE *fp)
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
short int scan_start_second
short int scan_start_minute
short int scan_start_hour