TPCCLIB
Loading...
Searching...
No Matches
tacmsamp.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 <string.h>
13#include <math.h>
14/*****************************************************************************/
15#include "tpcextensions.h"
16#include "tpcift.h"
17#include "tpctac.h"
18/*****************************************************************************/
19
20/*****************************************************************************/
21static char *info[] = {
22 "Inspect TAC file for multiple samples with the same sample time.",
23 " ",
24 "Usage: @P [options] file",
25 " ",
26 "Options:",
27 " -mean",
28 " Replace multiple samples with their mean.",
29 " -stdoptions", // List standard options like --help, -v, etc
30 " ",
31 "Example 1. Check all TAC files for multiple samples in bash shell:",
32 " for file in ./*.tac; do @P $file; done",
33 " ",
34 "Example 2. Combine multiple samples inside TAC files in Windows command",
35 " prompt window: ",
36 " for %g in (*.tac) do @P -mean %g",
37 " ",
38 "See also: interpol, tacblend, taccut, taccat, tacsetx, tacframe",
39 " ",
40 "Keywords: TAC, tool, time",
41 0};
42/*****************************************************************************/
43
44/*****************************************************************************/
45/* Turn on the globbing of the command line, since it is disabled by default in
46 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
47 In Unix&Linux wildcard command line processing is enabled by default. */
48/*
49#undef _CRT_glob
50#define _CRT_glob -1
51*/
52int _dowildcard = -1;
53/*****************************************************************************/
54
55/*****************************************************************************/
59int main(int argc, char **argv)
60{
61 int ai, help=0, version=0, verbose=1;
62 int fixMode=0; // 0= change nothing, 1=replace with mean
63 char tacfile[FILENAME_MAX];
64
65
66 /*
67 * Get arguments
68 */
69 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
70 tacfile[0]=(char)0;
71 /* Options */
72 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
73 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
74 char *cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
75 if(strcasecmp(cptr, "MEAN")==0) {
76 fixMode=1; continue;
77 }
78 fprintf(stderr, "Error: invalid option '%s'\n", argv[ai]);
79 return(1);
80 } else break; // tac name argument may start with '-'
81
82 TPCSTATUS status; statusInit(&status);
83 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
84 status.verbose=verbose-3;
85
86 /* Print help or version? */
87 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
88 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
89 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
90
91 /* The first argument (non-option) is the filename */
92 if(ai<argc) {strlcpy(tacfile, argv[ai], FILENAME_MAX); ai++;}
93 else {fprintf(stderr, "Error: missing filename.\n"); return(1);}
94 if(ai<argc) {fprintf(stderr, "Error: extra command-line argument.\n"); return(1);}
95
96 /* In verbose mode print arguments and options */
97 if(verbose>1) {
98 printf("tacfile := %s\n", tacfile);
99 printf("fixMode := %d\n", fixMode);
100 fflush(stdout);
101 }
102
103 /*
104 * Read the file
105 */
106 if(verbose>1) printf("reading %s\n", tacfile);
107 TAC tac; tacInit(&tac);
108 if(tacRead(&tac, tacfile, &status)!=TPCERROR_OK) {
109 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
110 tacFree(&tac); return(2);
111 }
112 if(verbose>2) {
113 printf("fileformat := %s\n", tacFormattxt(tac.format));
114 printf("tacNr := %d\n", tac.tacNr);
115 printf("sampleNr := %d\n", tac.sampleNr);
116 }
117
118 /*
119 * Check for multiple samples
120 */
121 int isMultiple=tacMultipleSamples(&tac, 0, NULL, verbose-1);
122 if(fixMode==0 || verbose>0) {
123 printf("%s : ", tacfile);
124 if(isMultiple) printf("multiple samples found\n"); else printf("no multiple samples\n");
125 fflush(stdout);
126 }
127 if(fixMode==0 || !isMultiple) {tacFree(&tac); return(0);}
128
129 /*
130 * Fix multiple samples
131 */
132 TAC tac2; tacInit(&tac2);
133 if(tacMultipleSamples(&tac, fixMode, &tac2, verbose-1)!=0) {
134 fprintf(stderr, "Error: cannot fix multiple samples.\n");
135 tacFree(&tac); tacFree(&tac2); return(9);
136 }
137 /* Compare the sample numbers */
138 if(tac.sampleNr==tac2.sampleNr) {
139 printf("%s : no multiple samples fixed\n", tacfile);
140 tacFree(&tac); tacFree(&tac2); return(0);
141 }
142 if(verbose>0) printf("%d samples reduced to %d\n", tac.sampleNr, tac2.sampleNr);
143 tacFree(&tac);
144
145
146 /*
147 * Save data
148 */
149 {
150 if(verbose>1) printf("writing %s\n", tacfile);
151 FILE *fp; fp=fopen(tacfile, "w");
152 if(fp==NULL) {
153 fprintf(stderr, "Error: cannot open file for writing (%s)\n", tacfile);
154 tacFree(&tac2); return(11);
155 }
156 int ret=tacWrite(&tac2, fp, TAC_FORMAT_UNKNOWN, 1, &status);
157 fclose(fp); tacFree(&tac2);
158 if(ret!=TPCERROR_OK) {
159 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
160 return(12);
161 }
162 }
163
164 return(0);
165}
166/*****************************************************************************/
167
168/*****************************************************************************/
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
tacformat format
Definition tpctac.h:93
int sampleNr
Definition tpctac.h:89
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 tacMultipleSamples(TAC *d1, const int fixMode, TAC *d2, const int verbose)
Check TAC data for multiple samples with the same sample time. Optionally replace the multiple sample...
Definition tacorder.c:336
Header file for library libtpcextensions.
@ TPCERROR_OK
No error.
Header file for library libtpcift.
Header file for library libtpctac.
@ TAC_FORMAT_UNKNOWN
Unknown format.
Definition tpctac.h:28