TPCCLIB
Loading...
Searching...
No Matches
esetstrt.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 "Set the scan_start_time in ECAT 6 or 7 file.",
24 "New scan start time can ge given directly, or as time difference",
25 "(positive or negative) as minutes.",
26 " ",
27 "Usage: @P [Options] ecatfile YYYY-MM-DD hh:mm:ss",
28 " or",
29 " @P [Options] ecatfile timedifference",
30 " ",
31 "Options:",
32 " -stdoptions", // List standard options like --help, -v, etc
33 " ",
34 "Example 1:",
35 " @P a2345dy1.img 2003-12-25 13:15:03",
36 "Example 2:",
37 " @P a2345dy1.img 59.8",
38 " ",
39 "See also: egetstrt, lmhdr, ecattime, ecatcat, edecay, injdifft",
40 " ",
41 "Keywords: image, ECAT, input, tool, scan start time",
42 0};
43/*****************************************************************************/
44
45/*****************************************************************************/
46/* Turn on the globbing of the command line, since it is disabled by default in
47 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
48 In Unix&Linux wildcard command line processing is enabled by default. */
49/*
50#undef _CRT_glob
51#define _CRT_glob -1
52*/
53int _dowildcard = -1;
54/*****************************************************************************/
55
56/*****************************************************************************/
60int main(int argc, char **argv)
61{
62 int ai, help=0, version=0, verbose=1;
63 int ret;
64 int fformat=63;
65 char ecatfile[FILENAME_MAX], *cptr;
66 char datestr[256], timestr[256];
67 double diftime=nan("");
68 char tmp1[32], tmp2[32];
69 FILE *fp;
70 ECAT63_mainheader e63mhdr;
71 ECAT7_mainheader e7mhdr;
72 struct tm stn;
73
74
75 /*
76 * Get arguments
77 */
78 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
79 ecatfile[0]=datestr[0]=timestr[0]=(char)0;
80 /* Options */
81 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') { /* options */
82 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(cptr==NULL) continue;
83 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
84 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
85 return(1);
86 } else break;
87
88 /* Print help or version? */
89 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
90 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
91 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
92
93 /* Process other arguments, starting from the first non-option */
94 if(ai<argc) { // first argument is the filename
95 strlcpy(ecatfile, argv[ai++], FILENAME_MAX);
96 /* check that file exists */
97 if(access(ecatfile, 0) == -1) {
98 fprintf(stderr, "Error: file '%s' does not exist.\n", ecatfile);
99 return(1);
100 }
101 }
102 if(ai==argc-1) { // if this is the last argument, then it must be time diff
103 ret=atof_with_check(argv[ai], &diftime);
104 if(ret==0) diftime*=60.0;
105 if(ret!=0 || diftime==0) {
106 fprintf(stderr, "Error: invalid time difference.\n");
107 return(1);
108 }
109 ai++;
110 }
111 if((ai+1)<argc) { // if there are two arguments, those must be date and time
112 strlcpy(datestr, argv[ai++], 256);
113 strlcpy(timestr, argv[ai++], 256);
114 /* Check the date and time, put in international format in timestr */
115 strlcat(datestr, " ", 256); strlcat(datestr, timestr, 256);
116 if(isdatetime(datestr, timestr)!=0) {
117 fprintf(stderr, "Error: invalid date and time.\n");
118 return(1);
119 }
120 if(verbose>4) printf("timestr='%s'\n", timestr);
121 /* Convert it to struct tm */
122 if(get_datetime(timestr, &stn, verbose-2)!=0) {
123 fprintf(stderr, "Error: invalid date and time.\n");
124 return(1);
125 }
126 if(verbose>3) printf("new_hour := %d\n", stn.tm_hour);
127 }
128 if(ai<argc) {
129 fprintf(stderr, "Error: invalid argument %s\n", argv[ai]);
130 return(1);
131 }
132
133 /* Is something missing? */
134 if(!timestr[0] && isnan(diftime)) {
135 fprintf(stderr, "Error: missing command-line argument; try %s --help\n",
136 argv[0]);
137 return(1);
138 }
139
140
141 /* In verbose mode print arguments and options */
142 if(verbose>1) {
143 printf("ecatfile := %s\n", ecatfile);
144 if(timestr[0]) printf("date_and_time := %s\n", timestr);
145 if(!isnan(diftime)) printf("diftime[s] := %g\n", diftime);
146 }
147
148
149 /*
150 * Read main header
151 */
152 if(verbose>1) printf("opening file %s\n", ecatfile);
153
154 /* Open file for read and write */
155 if((fp=fopen(ecatfile, "r+b")) == NULL) {
156 fprintf(stderr, "Error: cannot open file %s\n", ecatfile);
157 return(2);
158 }
159
160 /* Try to read ECAT 7.x main header */
161 ret=ecat7ReadMainheader(fp, &e7mhdr);
162 if(ret) {
163 fprintf(stderr, "Error (%d): cannot read main header.\n", ret);
164 fclose(fp); return(3);
165 }
166
167 /* If header could be read, check for magic number */
168 if(strncmp(e7mhdr.magic_number, ECAT7V_MAGICNR, 7)==0) {
169 /* This is ECAT 7.x file */
170 fformat=7;
171 /* All file types are supported */
172 if(verbose>4) printf(" identified as an ECAT 7 file.\n");
173 if(verbose>9) ecat7PrintMainheader(&e7mhdr, stdout);
174 } else { /* Try to read as ECAT 6.3 file */
175 if((ret=ecat63ReadMainheader(fp, &e63mhdr))) {
176 fprintf(stderr, "Error (%d): cannot read main header.\n", ret);
177 fclose(fp); return(3);
178 }
179 if(verbose>9) ecat63PrintMainheader(&e63mhdr, stdout);
180 /* This is ECAT 6.3 file */
181 fformat=63;
182 if(verbose>4) printf(" identified as an ECAT 6.3 file.\n");
183 }
184
185
186 /*
187 * Set scan_start_time and rewrite the main header
188 */
189 if(verbose>1) printf("setting scan start time\n");
190 if(fformat==7) {
191
192 /* ECAT 7 main header contains scan_start_time as time_t */
193
194 if(isnan(diftime)) { // new scan start time was given by user
195
196 /* Get the original scan_start_time */
197 time_t t;
198 t=e7mhdr.scan_start_time;
199 if(!ctime_r_int(&t, tmp1)) strcpy(tmp1, "1900-01-01 00:00:00");
200 if(verbose>3) printf("tmp1 := '%s'\n", tmp1);
201
202 /* Calculate new scan_start_time as time_t */
203 if(strftime(tmp2, 32, "%Y-%m-%d %H:%M:%S", &stn)==0)
204 strcpy(tmp2, "1900-01-01 00:00:00");
205 if(verbose>3) printf("tmp2 := '%s'\n", tmp2);
206
207 /* Put it in the header */
208 e7mhdr.scan_start_time=timegm(&stn);
209
210 } else { // time difference was given by user
211
212 /* Put it in the header */
213 e7mhdr.scan_start_time+=(int)diftime;
214 if(verbose>3) {
215 time_t t;
216 t=e7mhdr.scan_start_time;
217 if(!ctime_r_int(&t, tmp2)) strcpy(tmp2, "1900-01-01 00:00:00");
218 if(verbose>3) printf("tmp2 := '%s'\n", tmp2);
219 }
220
221 }
222
223 /* Write */
224 if(verbose>1) fprintf(stdout, "writing main header\n");
225 ret=ecat7WriteMainheader(fp, &e7mhdr);
226
227 } else {
228
229 /* ECAT 6 main header contains years, months etc as separate numbers */
230 int YYYY, MM, DD, hh, mm, ss;
231
232 /* Get the original scan_start_time as string */
233 ecat63ScanstarttimeInt(&e63mhdr, tmp1);
234 if(verbose>3) printf("tmp1 := '%s'\n", tmp1);
235
236 if(isnan(diftime)) { // new scan start time was given by user
237
238 /* Get the new scan_start_time as years, months etc */
239 strlcpy(tmp2, timestr, 32);
240 ret=sscanf(tmp2, "%d-%d-%d %d:%d:%d", &YYYY, &MM, &DD, &hh, &mm, &ss);
241 if(ret!=6) {
242 fprintf(stderr, "Error: invalid date and time.\n");
243 fclose(fp); return(3);
244 }
245
246 } else { // time difference was given by user
247
248 ret=get_datetime(tmp1, &stn, 0);
249 if(ret==0) tmAdd((int)diftime, &stn);
250 if(ret==0) {
251 if(strftime(tmp2, 32, "%Y-%m-%d %H:%M:%S", &stn)==0) {
252 strcpy(tmp2, "1900-01-01 00:00:00");
253 ret=1;
254 }
255 if(verbose>3) printf("tmp2 := '%s'\n", tmp2);
256 }
257 if(ret==0) {
258 if(sscanf(tmp2, "%d-%d-%d %d:%d:%d", &YYYY, &MM, &DD, &hh, &mm, &ss)!=6)
259 ret=1;
260 }
261 if(ret) {
262 fprintf(stderr, "Error: new scan start time is not valid.\n");
263 fclose(fp); return(5);
264 }
265
266 }
267
268 /* Put it into header */
269 e63mhdr.scan_start_year=YYYY;
270 e63mhdr.scan_start_month=MM;
271 e63mhdr.scan_start_day=DD;
272 e63mhdr.scan_start_hour=hh;
273 e63mhdr.scan_start_minute=mm;
274 e63mhdr.scan_start_second=ss;
275
276 /* Write */
277 if(verbose>1) fprintf(stdout, "writing main header\n");
278 ret=ecat63WriteMainheader(fp, &e63mhdr);
279
280 }
281
282 if(verbose>1) printf("closing file\n");
283 fclose(fp);
284
285 if(ret) {
286 fprintf(stderr, "Error: cannot write the mainheader.\n");
287 return(14);
288 }
289
290 if(verbose>0) fprintf(stdout, "Scan start: %s -> %s\n", tmp1, tmp2);
291 if(verbose>1) fprintf(stdout, "done.\n");
292
293 return(0);
294}
295/*****************************************************************************/
296
297/*****************************************************************************/
int get_datetime(char *str, struct tm *date, int verbose)
Definition datetime.c:322
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,...
Definition datetime.c:110
time_t timegm(struct tm *tm)
Inverse of gmtime, converting struct tm to time_t.
Definition datetime.c:69
void tmAdd(int s, struct tm *d)
Definition datetime.c:514
int isdatetime(char *str, char *intdate)
Definition datetime.c:280
int atof_with_check(char *double_as_string, double *result_value)
Definition decpoint.c:107
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
int ecat63WriteMainheader(FILE *fp, ECAT63_mainheader *h)
Definition ecat63w.c:24
void ecat7PrintMainheader(ECAT7_mainheader *h, FILE *fp)
Definition ecat7p.c:16
int ecat7ReadMainheader(FILE *fp, ECAT7_mainheader *h)
Definition ecat7r.c:15
int ecat7WriteMainheader(FILE *fp, ECAT7_mainheader *h)
Definition ecat7w.c:16
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
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition strext.c:245
int tpcHtmlUsage(const char *program, char *text[], const char *path)
Definition proginfo.c:213
size_t strlcat(char *dst, const char *src, size_t dstsize)
Definition strext.c:206
void tpcPrintBuild(const char *program, FILE *fp)
Definition proginfo.c:383
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
Definition proginfo.c:158
short int scan_start_month
short int scan_start_second
short int scan_start_year
short int scan_start_day
short int scan_start_minute
short int scan_start_hour
char magic_number[14]