TPCCLIB
Loading...
Searching...
No Matches
tac4frpl.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 file into suitable format for plotting with bars",
23 "representing both frame start and end times.",
24 " ",
25 "Usage: @P [Options] tacfile outputfile",
26 " ",
27 "Options:",
28 " -stdoptions", // List standard options like --help, -v, etc
29 " ",
30 "See also: tacfr2x, tacframe, simframe, tac2svg, tacformat, tacsetx",
31 " ",
32 "Keywords: TAC, plotting, 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
133#if(1)
134 TAC tac2; tacInit(&tac2);
135 ret=tacToBars(&tac1, &tac2);
136 if(ret!=0) {
137 fprintf(stderr, "Error: cannot produce meaningful bar data for plotting.\n");
138 tacFree(&tac1); tacFree(&tac2); return(4);
139 }
140
141#else
142
143 /* Make a copy of the TAC data in memory */
144 if(verbose>1) printf("allocating memory for new TAC\n");
145 TAC tac2; tacInit(&tac2);
146 ret=tacDuplicate(&tac1, &tac2);
147 if(ret!=0) {
148 fprintf(stderr, "Error: cannot allocate memory.\n");
149 tacFree(&tac1); return(4);
150 }
151 /* Add room for more frames; we plot each frame as its own bar
152 and therefore we may need 4x the original space */
153 ret=tacAllocateMoreSamples(&tac2, 3*tac1.sampleNr);
154 if(ret!=0) {
155 fprintf(stderr, "Error: cannot allocate memory.\n");
156 tacFree(&tac1); return(4);
157 }
158 if(verbose>2) printf("_sampleNr := %d\n", tac2._sampleNr);
159
160 /* Make separate 'samples' for each frame start and end time */
161 int ri, fi, fj;
162 tac2.isframe=0;
163
164 for(fi=0, fj=0; fi<tac1.sampleNr; fi++) {
165 /* Do nothing if missing values */
166 if(isnan(tac1.x1[fi]) || isnan(tac1.x2[fi])) continue;
167 if(tac1.tacNr==1 && isnan(tac1.c[0].y[fi])) continue;
168 /* Otherwise make bar of each frame */
169 tac2.x[fj]=tac1.x1[fi];
170 for(ri=0; ri<tac2.tacNr; ri++) tac2.c[ri].y[fj]=0.0;
171 fj++;
172 tac2.x[fj]=tac1.x1[fi];
173 for(ri=0; ri<tac2.tacNr; ri++) tac2.c[ri].y[fj]=tac1.c[ri].y[fi];
174 fj++;
175 tac2.x[fj]=tac1.x2[fi];
176 for(ri=0; ri<tac2.tacNr; ri++) tac2.c[ri].y[fj]=tac1.c[ri].y[fi];
177 fj++;
178 tac2.x[fj]=tac1.x2[fi];
179 for(ri=0; ri<tac2.tacNr; ri++) tac2.c[ri].y[fj]=0.0;
180 fj++;
181 }
182 tac2.sampleNr=fj;
183#endif
184
185 tacFree(&tac1);
186
187 /*
188 * Save data
189 */
190 if(verbose>2) printf("writing %s\n", tacfile2);
191 FILE *fp; fp=fopen(tacfile2, "w");
192 if(fp==NULL) {
193 fprintf(stderr, "Error: cannot open file for writing (%s)\n", tacfile2);
194 tacFree(&tac2); return(11);
195 }
196 ret=tacWrite(&tac2, fp, TAC_FORMAT_UNKNOWN, 1, &status);
197 fclose(fp); tacFree(&tac2);
198 if(ret!=TPCERROR_OK) {
199 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
200 return(12);
201 }
202
203 return(0);
204}
205/*****************************************************************************/
206
207/*****************************************************************************/
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
double * y
Definition tpctac.h:75
Definition tpctac.h:87
double * x
Definition tpctac.h:97
unit cunit
Definition tpctac.h:105
tacformat format
Definition tpctac.h:93
int _sampleNr
Definition tpctac.h:121
int sampleNr
Definition tpctac.h:89
int isframe
Definition tpctac.h:95
TACC * c
Definition tpctac.h:117
double * x2
Definition tpctac.h:101
unit tunit
Definition tpctac.h:109
double * x1
Definition tpctac.h:99
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
int tacDuplicate(TAC *tac1, TAC *tac2)
Make a duplicate of TAC structure.
Definition tac.c:356
void tacInit(TAC *tac)
Definition tac.c:24
int tacAllocateMoreSamples(TAC *tac, int addNr)
Allocate memory for more samples in TAC data.
Definition tac.c:435
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 tacToBars(TAC *tac1, TAC *tac2)
Transform TAC data with frame start and end times into suitable form for plotting with frames as bars...
Definition tacx.c:888
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