TPCCLIB
Loading...
Searching...
No Matches
tacfr2x.c
Go to the documentation of this file.
1
8/*****************************************************************************/
9#include "tpcclibConfig.h"
10/*****************************************************************************/
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <math.h>
15/*****************************************************************************/
16#include "tpcextensions.h"
17#include "tpctac.h"
18/*****************************************************************************/
19
20/*****************************************************************************/
21static char *info[] = {
22 "Transform TAC x1 and x2 (frame start and end times) to x at each x1 and x2",
23 "to retain TAC AUC.",
24 " ",
25 "Usage: @P [Options] tacfile outputfile",
26 " ",
27 "Options:",
28 " -stdoptions", // List standard options like --help, -v, etc
29 " ",
30 "See also: tac4frpl, tacframe, simframe, tac2svg, tacformat, tacsetx",
31 " ",
32 "Keywords: TAC, AUC, simulation, time",
33 0};
34/*****************************************************************************/
35
36/*****************************************************************************/
37/* Turn on the globbing of the command line, since it is disabled by default in
38 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
39 In Unix&Linux wildcard command line processing is enabled by default. */
40/*
41#undef _CRT_glob
42#define _CRT_glob -1
43*/
44int _dowildcard = -1;
45/*****************************************************************************/
46
47/*****************************************************************************/
51int main(int argc, char **argv)
52{
53 int ai, help=0, version=0, verbose=1;
54 char tacfile1[FILENAME_MAX], tacfile2[FILENAME_MAX];
55 int ret;
56
57 /*
58 * Get arguments
59 */
60 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
61 tacfile1[0]=tacfile2[0]=(char)0;
62 /* Options */
63 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
64 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
65 //char *cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
66 fprintf(stderr, "Error: invalid option '%s'\n", argv[ai]);
67 return(1);
68 } else break; // tac name argument may start with '-'
69
70 TPCSTATUS status; statusInit(&status);
71 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
72 status.verbose=verbose-1;
73
74 /* Print help or version? */
75 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
76 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
77 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
78
79 /* Arguments */
80 if(ai<argc) {strlcpy(tacfile1, argv[ai++], FILENAME_MAX);}
81 if(ai<argc) {strlcpy(tacfile2, argv[ai++], FILENAME_MAX);}
82 if(ai<argc) {fprintf(stderr, "Error: invalid argument '%s'.\n", argv[ai]); return(1);}
83
84 /* Is something missing? */
85 if(!tacfile2[0]) {tpcPrintUsage(argv[0], info, stdout); return(1);}
86
87 /* In verbose mode print arguments and options */
88 if(verbose>1) {
89 for(ai=0; ai<argc; ai++) printf("%s ", argv[ai]);
90 printf("\n");
91 printf("tacfile1 := %s\n", tacfile1);
92 printf("tacfile2 := %s\n", tacfile2);
93 fflush(stdout);
94 }
95
96
97 /*
98 * Read the file
99 */
100 if(verbose>1) printf("reading %s\n", tacfile1);
101 TAC tac1; tacInit(&tac1);
102 ret=tacRead(&tac1, tacfile1, &status);
103 if(ret!=TPCERROR_OK) {
104 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
105 if(verbose>1) printf("ret := %d\n", ret);
106 tacFree(&tac1); return(2);
107 }
108 if(verbose>2) {
109 printf("fileformat := %s\n", tacFormattxt(tac1.format));
110 printf("tacNr := %d\n", tac1.tacNr);
111 printf("sampleNr := %d\n", tac1.sampleNr);
112 printf("xunit := %s\n", unitName(tac1.tunit));
113 printf("yunit := %s\n", unitName(tac1.cunit));
114 printf("isframe := %d\n", tac1.isframe);
115 }
116
117 /* Check the data */
118 if(tac1.sampleNr<1 || tac1.tacNr<1) {
119 fprintf(stderr, "Error: invalid data.\n");
120 tacFree(&tac1); return(3);
121 }
122 /* Make sure that frame start and end times are present */
123 if(tac1.isframe==0) {
124 fprintf(stderr, "Error: file does not contain frame start and end times.\n");
125 tacFree(&tac1); return(3);
126 }
127 if(tacSortByTime(&tac1, &status)!=TPCERROR_OK) {
128 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
129 if(verbose>1) printf("ret := %d\n", ret);
130 tacFree(&tac1); return(3);
131 }
132 /* Fix any gaps and overlap in data if possible and if not then quit with error */
133 ret=tacSetXContiguous(&tac1);
134 if(ret!=TPCERROR_OK) {
135 fprintf(stderr, "Error: %s\n", errorMsg(ret));
136 return(ret);
137 }
138
139 /*
140 * Do it
141 */
142 TAC tac2; tacInit(&tac2);
143 ret=tacFramesToSteps(&tac1, &tac2, &status);
144 if(ret!=0) {
145 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
146 tacFree(&tac1); tacFree(&tac2); return(6);
147 }
148
149 tacFree(&tac1);
150
151 /*
152 * Save data
153 */
154 if(verbose>2) printf("writing %s\n", tacfile2);
155 FILE *fp; fp=fopen(tacfile2, "w");
156 if(fp==NULL) {
157 fprintf(stderr, "Error: cannot open file for writing (%s)\n", tacfile2);
158 tacFree(&tac2); return(11);
159 }
160 ret=tacWrite(&tac2, fp, TAC_FORMAT_UNKNOWN, 1, &status);
161 fclose(fp); tacFree(&tac2);
162 if(ret!=TPCERROR_OK) {
163 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
164 return(12);
165 }
166
167 return(0);
168}
169/*****************************************************************************/
170
171/*****************************************************************************/
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
Definition proginfo.c:47
int tpcHtmlUsage(const char *program, char *text[], const char *path)
Definition proginfo.c:169
void tpcPrintBuild(const char *program, FILE *fp)
Definition proginfo.c:339
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
Definition proginfo.c:114
void statusInit(TPCSTATUS *s)
Definition statusmsg.c:104
char * errorMsg(tpcerror e)
Definition statusmsg.c:68
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition stringext.c:632
Definition tpctac.h:87
unit cunit
Definition tpctac.h:105
tacformat format
Definition tpctac.h:93
int sampleNr
Definition tpctac.h:89
int isframe
Definition tpctac.h:95
unit tunit
Definition tpctac.h:109
int tacNr
Definition tpctac.h:91
int verbose
Verbose level, used by statusPrint() etc.
tpcerror error
Error code.
void tacFree(TAC *tac)
Definition tac.c:106
void tacInit(TAC *tac)
Definition tac.c:24
int tacRead(TAC *d, const char *fname, TPCSTATUS *status)
Definition tacio.c:413
char * tacFormattxt(tacformat c)
Definition tacio.c:98
int tacWrite(TAC *tac, FILE *fp, tacformat format, int extra, TPCSTATUS *status)
Definition tacio.c:332
int tacSortByTime(TAC *d, TPCSTATUS *status)
Definition tacorder.c:74
int tacFramesToSteps(TAC *inp, TAC *out, TPCSTATUS *status)
Transform TAC with frames into TAC with frames represented with stepwise changing dot-to-dot data.
Definition tacx.c:942
int tacSetXContiguous(TAC *d)
Set PET TAC frame times contiguous, without even tiny overlap or gaps in between.
Definition tacx.c:1166
Header file for library libtpcextensions.
@ TPCERROR_OK
No error.
char * unitName(int unit_code)
Definition units.c:143
Header file for library libtpctac.
@ TAC_FORMAT_UNKNOWN
Unknown format.
Definition tpctac.h:28