8#include "tpcclibConfig.h"
22static char *info[] = {
23 "Print the scan_start_time in ECAT 6 or 7 files on screen (stdout).",
25 "Usage: @P [Options] ecatfile(s)",
28 " -o=<output filename>",
29 " Output is written in given file; any existing file is overwritten.",
31 " Instead of scan start times, program lists the difference to the first",
32 " one, either in seconds (sec) or in minutes (min); minutes by default.",
35 "See also: lmhdr, ecattime, esetstrt, tactime, injdifft, eframe",
37 "Keywords: image, ECAT, input, tool, scan start time",
56int main(
int argc,
char **argv)
58 int ai, help=0, version=0, verbose=1;
62 int time_unit=TUNIT_MIN;
63 char ecatfile[FILENAME_MAX], outfile[FILENAME_MAX],
68 struct tm *stp, tm, stm;
69 time_t timet1=0, timet2;
70 double time_diff_sec=0;
77 if(argc==1) {
tpcPrintUsage(argv[0], info, stderr);
return(1);}
78 ecatfile[0]=outfile[0]=(char)0;
80 for(ai=1; ai<argc; ai++)
if(*argv[ai]==
'-') {
81 cptr=argv[ai]+1;
if(*cptr==
'-') cptr++;
if(cptr==NULL)
continue;
84 if(strncasecmp(cptr,
"DIF=MIN", 5)==0) {
85 time_unit=TUNIT_MIN; is_diff=1;
continue;
86 }
else if(strncasecmp(cptr,
"DIF=SEC", 5)==0) {
87 time_unit=TUNIT_SEC; is_diff=1;
continue;
88 }
else if(strcasecmp(cptr,
"DIF")==0) {
89 time_unit=TUNIT_MIN; is_diff=1;
continue;
90 }
else if(strncasecmp(cptr,
"D=MIN", 3)==0) {
91 time_unit=TUNIT_MIN; is_diff=1;
continue;
92 }
else if(strncasecmp(cptr,
"D=SEC", 3)==0) {
93 time_unit=TUNIT_SEC; is_diff=1;
continue;
94 }
else if(strncasecmp(cptr,
"O=", 2)==0) {
95 strcpy(outfile, cptr+2);
if(strlen(outfile)>0)
continue;
97 fprintf(stderr,
"Error: invalid option '%s'.\n", argv[ai]);
102 if(help==2) {
tpcHtmlUsage(argv[0], info,
"");
return(0);}
107 for(; ai<argc; ai++) {
108 if(firstfile<1) firstfile=ai;
110 if(access(argv[ai], 0) == -1) {
111 fprintf(stderr,
"Error: file '%s' does not exist.\n", argv[ai]);
119 fprintf(stderr,
"Error: missing command-line argument; try %s --help\n",
124 if(fileNr==1 && is_diff) is_diff=0;
128 printf(
"fileNr := %d\n", fileNr);
129 printf(
"outfile := %s\n", outfile);
130 printf(
"is_diff := %d\n", is_diff);
131 printf(
"time_unit := %d\n", time_unit);
139 if(access(outfile, 0) != -1)
140 fprintf(stderr,
"Warning: %s is overwritten.\n", outfile);
141 ofp=fopen(outfile,
"w");
if(ofp==NULL) {
142 fprintf(stderr,
"Error: cannot open file %s for writing.\n", outfile);
152 for(ai=firstfile, fileNr=0; ai<argc; ai++) {
153 strcpy(ecatfile, argv[ai]);
154 if(verbose>0) fprintf(ofp,
"filename := %s\n", ecatfile);
157 if((fp=fopen(ecatfile,
"rb")) == NULL) {
158 fprintf(stderr,
"Error: cannot open file %s\n", ecatfile);
159 if(outfile[0]) fclose(ofp);
166 fprintf(stderr,
"Error (%d): cannot read main header.\n", ret);
167 if(outfile[0]) fclose(ofp);
168 fclose(fp);
return(3);
172 if(strncmp(e7mhdr.
magic_number, ECAT7V_MAGICNR, 7)==0) {
176 if(verbose>4) printf(
" identified as an ECAT 7 file.\n");
180 if(timet2<86400 || !
gmtime_r(&timet2, &tm)) {
181 fprintf(stderr,
"Error: %s does not contain scan start time.\n",
183 if(outfile[0]) fclose(ofp);
184 fclose(fp);
return(4);
189 fprintf(stderr,
"Error (%d): cannot read main header.\n", ret);
190 if(outfile[0]) fclose(ofp);
191 fclose(fp);
return(2);
196 if(verbose>4) printf(
" identified as an ECAT 6.3 file.\n");
198 char tmp[20]; fprintf(stdout,
"Scan start: %s\n",
204 if(timet2==-1 || timet2<86400) {
205 fprintf(stderr,
"Error: %s does not contain scan start time.\n",
207 if(outfile[0]) fclose(ofp);
208 fclose(fp);
return(2);
210 if(fileNr==0) timet1=timet2;
217 if(is_diff==0 || fileNr==0) {
220 strftime(tmp, 32,
"%Y-%m-%d %H:%M:%S", stp);
221 fprintf(ofp,
"scan_start_time := %s\n", tmp);
222 }
else if(fileNr>0) {
223 time_diff_sec=difftime(timet2, timet1);
224 if(time_unit==TUNIT_SEC)
225 fprintf(ofp,
"scan_start_time_difference := %g\n", time_diff_sec);
227 fprintf(ofp,
"scan_start_time_difference := %g\n", time_diff_sec/60.0);
233 if(outfile[0]) fclose(ofp);
time_t timegm(struct tm *tm)
Inverse of gmtime, converting struct tm to time_t.
struct tm * gmtime_r(const time_t *t, struct tm *tm)
Convert time_t to GMT struct tm.
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 ecat63ReadMainheader(FILE *fp, ECAT63_mainheader *h)
struct tm * ecat63ScanstarttimeToTm(const ECAT63_mainheader *h, struct tm *tm)
Convert scan_start_time in ECAT 6.3 main header into a struct tm.
void ecat7PrintMainheader(ECAT7_mainheader *h, FILE *fp)
int ecat7ReadMainheader(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)