TPCCLIB
Loading...
Searching...
No Matches
egetstrt.c
Go to the documentation of this file.
1
7/*****************************************************************************/
8#include "tpcclibConfig.h"
9/*****************************************************************************/
10#include <stdio.h>
11#include <stdlib.h>
12#include <math.h>
13#include <string.h>
14#include <unistd.h>
15#include <time.h>
16/*****************************************************************************/
17#include "libtpcmisc.h"
18#include "libtpcimgio.h"
19/*****************************************************************************/
20
21/*****************************************************************************/
22static char *info[] = {
23 "Print the scan_start_time in ECAT 6 or 7 files on screen (stdout).",
24 " ",
25 "Usage: @P [Options] ecatfile(s)",
26 " ",
27 "Options:",
28 " -o=<output filename>",
29 " Output is written in given file; any existing file is overwritten.",
30 " -dif[=<min|sec>]",
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.",
33 " -stdoptions", // List standard options like --help, -v, etc
34 " ",
35 "See also: lmhdr, ecattime, esetstrt, tactime, injdifft, eframe",
36 " ",
37 "Keywords: image, ECAT, input, tool, scan start time",
38 0};
39/*****************************************************************************/
40
41/*****************************************************************************/
42/* Turn on the globbing of the command line, since it is disabled by default in
43 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
44 In Unix&Linux wildcard command line processing is enabled by default. */
45/*
46#undef _CRT_glob
47#define _CRT_glob -1
48*/
49int _dowildcard = -1;
50/*****************************************************************************/
51
52/*****************************************************************************/
56int main(int argc, char **argv)
57{
58 int ai, help=0, version=0, verbose=1;
59 int ret, fileNr=0;
60//int fformat=63;
61 int is_diff=0;
62 int time_unit=TUNIT_MIN;
63 char ecatfile[FILENAME_MAX], outfile[FILENAME_MAX],
64 tmp[32], *cptr;
65 FILE *fp, *ofp;
66 ECAT63_mainheader e63mhdr;
67 ECAT7_mainheader e7mhdr;
68 struct tm *stp, tm, stm;
69 time_t timet1=0, timet2;
70 double time_diff_sec=0;
71 int firstfile=0;
72
73
74 /*
75 * Get arguments
76 */
77 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
78 ecatfile[0]=outfile[0]=(char)0;
79 /* Options */
80 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') { /* options */
81 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(cptr==NULL) continue;
82 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
83 cptr=argv[ai]+1;
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;
96 }
97 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
98 return(1);
99 } else break;
100
101 /* Print help or version? */
102 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
103 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
104 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
105
106 /* Process other arguments, starting from the first non-option */
107 for(; ai<argc; ai++) {
108 if(firstfile<1) firstfile=ai;
109 /* check that file exists */
110 if(access(argv[ai], 0) == -1) {
111 fprintf(stderr, "Error: file '%s' does not exist.\n", argv[ai]);
112 return(1);
113 }
114 fileNr++;
115 }
116
117 /* Is something missing? */
118 if(fileNr<1) {
119 fprintf(stderr, "Error: missing command-line argument; try %s --help\n",
120 argv[0]);
121 return(1);
122 }
123 /* Time difference option is not valid with just one file */
124 if(fileNr==1 && is_diff) is_diff=0;
125
126 /* In verbose mode print arguments and options */
127 if(verbose>1) {
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);
132 }
133
134
135 /*
136 * Open output file if necessary
137 */
138 if(outfile[0]) {
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);
143 return(2);
144 }
145 } else {
146 ofp=stdout;
147 }
148
149 /*
150 * Process one file at a time
151 */
152 for(ai=firstfile, fileNr=0; ai<argc; ai++) {
153 strcpy(ecatfile, argv[ai]);
154 if(verbose>0) fprintf(ofp, "filename := %s\n", ecatfile);
155
156 /* Open file */
157 if((fp=fopen(ecatfile, "rb")) == NULL) {
158 fprintf(stderr, "Error: cannot open file %s\n", ecatfile);
159 if(outfile[0]) fclose(ofp);
160 return(3);
161 }
162
163 /* Try to read ECAT 7.x main header */
164 ret=ecat7ReadMainheader(fp, &e7mhdr);
165 if(ret) {
166 fprintf(stderr, "Error (%d): cannot read main header.\n", ret);
167 if(outfile[0]) fclose(ofp);
168 fclose(fp); return(3);
169 }
170
171 /* If header could be read, check for magic number */
172 if(strncmp(e7mhdr.magic_number, ECAT7V_MAGICNR, 7)==0) {
173 /* This is ECAT 7.x file */
174 //fformat=7;
175 /* All file types are supported */
176 if(verbose>4) printf(" identified as an ECAT 7 file.\n");
177 if(verbose>9) ecat7PrintMainheader(&e7mhdr, stdout);
178 /* Get the scan_start_time */
179 timet2=e7mhdr.scan_start_time; if(fileNr==0) timet1=timet2;
180 if(timet2<86400 || !gmtime_r(&timet2, &tm)) {
181 fprintf(stderr, "Error: %s does not contain scan start time.\n",
182 ecatfile);
183 if(outfile[0]) fclose(ofp);
184 fclose(fp); return(4);
185 }
186 stp=&tm;
187 } else { /* Try to read as ECAT 6.3 file */
188 if((ret=ecat63ReadMainheader(fp, &e63mhdr))) {
189 fprintf(stderr, "Error (%d): cannot read main header.\n", ret);
190 if(outfile[0]) fclose(ofp);
191 fclose(fp); return(2);
192 }
193 if(verbose>9) ecat63PrintMainheader(&e63mhdr, stdout);
194 /* This is ECAT 6.3 file */
195 //fformat=63;
196 if(verbose>4) printf(" identified as an ECAT 6.3 file.\n");
197 if(verbose>1) {
198 char tmp[20]; fprintf(stdout, "Scan start: %s\n",
199 ecat63ScanstarttimeInt(&e63mhdr, tmp));
200 }
201 /* Get the scan_start_time */
202 ecat63ScanstarttimeToTm(&e63mhdr, &stm);
203 timet2=timegm(&stm);
204 if(timet2==-1 || timet2<86400) {
205 fprintf(stderr, "Error: %s does not contain scan start time.\n",
206 ecatfile);
207 if(outfile[0]) fclose(ofp);
208 fclose(fp); return(2);
209 }
210 if(fileNr==0) timet1=timet2;
211 stp=&stm;
212 }
213 /* Close file */
214 fclose(fp);
215
216 /* Print either start time or time difference */
217 if(is_diff==0 || fileNr==0) {
218 /* Print the time */
219 //strftime(tmp, 32, "%Y-%m-%d %T", stp); THIS DOES NOT WORK IN MINGW!!!
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);
226 else
227 fprintf(ofp, "scan_start_time_difference := %g\n", time_diff_sec/60.0);
228 }
229
230 fileNr++;
231 } /* next file */
232
233 if(outfile[0]) fclose(ofp);
234 return(0);
235}
236/*****************************************************************************/
237
238/*****************************************************************************/
time_t timegm(struct tm *tm)
Inverse of gmtime, converting struct tm to time_t.
Definition datetime.c:69
struct tm * gmtime_r(const time_t *t, struct tm *tm)
Convert time_t to GMT struct tm.
Definition datetime.c:22
void ecat63PrintMainheader(ECAT63_mainheader *h, FILE *fp)
Definition ecat63p.c:16
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 ...
Definition ecat63p.c:391
int ecat63ReadMainheader(FILE *fp, ECAT63_mainheader *h)
Definition ecat63r.c:25
struct tm * ecat63ScanstarttimeToTm(const ECAT63_mainheader *h, struct tm *tm)
Convert scan_start_time in ECAT 6.3 main header into a struct tm.
Definition ecat63w.c:900
void ecat7PrintMainheader(ECAT7_mainheader *h, FILE *fp)
Definition ecat7p.c:16
int ecat7ReadMainheader(FILE *fp, ECAT7_mainheader *h)
Definition ecat7r.c:15
Header file for libtpcimgio.
Header file for libtpcmisc.
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
Definition proginfo.c:40
int tpcHtmlUsage(const char *program, char *text[], const char *path)
Definition proginfo.c:213
void tpcPrintBuild(const char *program, FILE *fp)
Definition proginfo.c:383
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
Definition proginfo.c:158
char magic_number[14]