8#include "tpcclibConfig.h"
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.",
27 "Usage: @P [Options] ecatfile YYYY-MM-DD hh:mm:ss",
29 " @P [Options] ecatfile timedifference",
35 " @P a2345dy1.img 2003-12-25 13:15:03",
37 " @P a2345dy1.img 59.8",
39 "See also: egetstrt, lmhdr, ecattime, ecatcat, edecay, injdifft",
41 "Keywords: image, ECAT, input, tool, scan start time",
60int main(
int argc,
char **argv)
62 int ai, help=0, version=0, verbose=1;
65 char ecatfile[FILENAME_MAX], *cptr;
66 char datestr[256], timestr[256];
67 double diftime=nan(
"");
68 char tmp1[32], tmp2[32];
78 if(argc==1) {
tpcPrintUsage(argv[0], info, stderr);
return(1);}
79 ecatfile[0]=datestr[0]=timestr[0]=(char)0;
81 for(ai=1; ai<argc; ai++)
if(*argv[ai]==
'-') {
82 cptr=argv[ai]+1;
if(*cptr==
'-') cptr++;
if(cptr==NULL)
continue;
84 fprintf(stderr,
"Error: invalid option '%s'.\n", argv[ai]);
89 if(help==2) {
tpcHtmlUsage(argv[0], info,
"");
return(0);}
95 strlcpy(ecatfile, argv[ai++], FILENAME_MAX);
97 if(access(ecatfile, 0) == -1) {
98 fprintf(stderr,
"Error: file '%s' does not exist.\n", ecatfile);
104 if(ret==0) diftime*=60.0;
105 if(ret!=0 || diftime==0) {
106 fprintf(stderr,
"Error: invalid time difference.\n");
112 strlcpy(datestr, argv[ai++], 256);
113 strlcpy(timestr, argv[ai++], 256);
117 fprintf(stderr,
"Error: invalid date and time.\n");
120 if(verbose>4) printf(
"timestr='%s'\n", timestr);
123 fprintf(stderr,
"Error: invalid date and time.\n");
126 if(verbose>3) printf(
"new_hour := %d\n", stn.tm_hour);
129 fprintf(stderr,
"Error: invalid argument %s\n", argv[ai]);
134 if(!timestr[0] && isnan(diftime)) {
135 fprintf(stderr,
"Error: missing command-line argument; try %s --help\n",
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);
152 if(verbose>1) printf(
"opening file %s\n", ecatfile);
155 if((fp=fopen(ecatfile,
"r+b")) == NULL) {
156 fprintf(stderr,
"Error: cannot open file %s\n", ecatfile);
163 fprintf(stderr,
"Error (%d): cannot read main header.\n", ret);
164 fclose(fp);
return(3);
168 if(strncmp(e7mhdr.
magic_number, ECAT7V_MAGICNR, 7)==0) {
172 if(verbose>4) printf(
" identified as an ECAT 7 file.\n");
176 fprintf(stderr,
"Error (%d): cannot read main header.\n", ret);
177 fclose(fp);
return(3);
182 if(verbose>4) printf(
" identified as an ECAT 6.3 file.\n");
189 if(verbose>1) printf(
"setting scan start time\n");
199 if(!
ctime_r_int(&t, tmp1)) strcpy(tmp1,
"1900-01-01 00:00:00");
200 if(verbose>3) printf(
"tmp1 := '%s'\n", tmp1);
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);
217 if(!
ctime_r_int(&t, tmp2)) strcpy(tmp2,
"1900-01-01 00:00:00");
218 if(verbose>3) printf(
"tmp2 := '%s'\n", tmp2);
224 if(verbose>1) fprintf(stdout,
"writing main header\n");
230 int YYYY, MM, DD, hh, mm, ss;
234 if(verbose>3) printf(
"tmp1 := '%s'\n", tmp1);
240 ret=sscanf(tmp2,
"%d-%d-%d %d:%d:%d", &YYYY, &MM, &DD, &hh, &mm, &ss);
242 fprintf(stderr,
"Error: invalid date and time.\n");
243 fclose(fp);
return(3);
249 if(ret==0)
tmAdd((
int)diftime, &stn);
251 if(strftime(tmp2, 32,
"%Y-%m-%d %H:%M:%S", &stn)==0) {
252 strcpy(tmp2,
"1900-01-01 00:00:00");
255 if(verbose>3) printf(
"tmp2 := '%s'\n", tmp2);
258 if(sscanf(tmp2,
"%d-%d-%d %d:%d:%d", &YYYY, &MM, &DD, &hh, &mm, &ss)!=6)
262 fprintf(stderr,
"Error: new scan start time is not valid.\n");
263 fclose(fp);
return(5);
277 if(verbose>1) fprintf(stdout,
"writing main header\n");
282 if(verbose>1) printf(
"closing file\n");
286 fprintf(stderr,
"Error: cannot write the mainheader.\n");
290 if(verbose>0) fprintf(stdout,
"Scan start: %s -> %s\n", tmp1, tmp2);
291 if(verbose>1) fprintf(stdout,
"done.\n");
int get_datetime(char *str, struct tm *date, int verbose)
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,...
time_t timegm(struct tm *tm)
Inverse of gmtime, converting struct tm to time_t.
void tmAdd(int s, struct tm *d)
int isdatetime(char *str, char *intdate)
int atof_with_check(char *double_as_string, double *result_value)
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)
int ecat63WriteMainheader(FILE *fp, ECAT63_mainheader *h)
void ecat7PrintMainheader(ECAT7_mainheader *h, FILE *fp)
int ecat7ReadMainheader(FILE *fp, 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)
size_t strlcpy(char *dst, const char *src, size_t dstsize)
int tpcHtmlUsage(const char *program, char *text[], const char *path)
size_t strlcat(char *dst, const char *src, size_t dstsize)
void tpcPrintBuild(const char *program, FILE *fp)
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
short int scan_start_month
short int scan_start_second
short int scan_start_year
short int scan_start_minute
short int scan_start_hour