TPCCLIB
Loading...
Searching...
No Matches
tacrange.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 "List the time range in TAC datafile(s) in DFT, CSV, PMOD, or SIF formats.",
23 " ",
24 "Usage: @P [options] filename(s)",
25 " ",
26 "Options:",
27 " -unit=<sec|min>",
28 " Times are shown in seconds, or minutes (default)",
29 " -stdoptions", // List standard options like --help, -v, etc
30 " ",
31 "See also: tacframe, tactime, dftmax, inpstart, tacunit, tacsetx",
32 " ",
33 "Keywords: TAC, SIF, time",
34 0};
35/*****************************************************************************/
36
37/*****************************************************************************/
38/* Turn on the globbing of the command line, since it is disabled by default in
39 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
40 In Unix&Linux wildcard command line processing is enabled by default. */
41/*
42#undef _CRT_glob
43#define _CRT_glob -1
44*/
45int _dowildcard = -1;
46/*****************************************************************************/
47
48/*****************************************************************************/
52int main(int argc, char **argv)
53{
54 int ai, help=0, version=0, verbose=1;
55 int ret, fileNr=0, ffi=0;
56 int tunit=UNIT_MIN;
57 char *cptr, tacfile[FILENAME_MAX];
58 TAC tac;
59 double xmin, xmax;
60 double min_xmin, max_xmax, max_xmin, min_xmax;
61
62
63 /*
64 * Get arguments
65 */
66 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
67 tacInit(&tac);
68 tacfile[0]=(char)0;
69 /* Options */
70 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
71 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
72 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
73 if(strncasecmp(cptr, "UNIT=", 5)==0) {
74 tunit=unitIdentify(cptr+5); if(unitIsTime(tunit)) continue;
75 } else if(strncasecmp(cptr, "TUNIT=", 6)==0) {
76 tunit=unitIdentify(cptr+6); if(unitIsTime(tunit)) 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 /* Print help or version? */
83 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
84 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
85 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
86
87 TPCSTATUS status; statusInit(&status);
88 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
89 status.verbose=verbose-1;
90
91 /* Process other arguments, starting from the first non-option */
92 fileNr=0;
93 for(; ai<argc; ai++) {
94 if(ffi<1) ffi=ai;
95 fileNr++;
96 }
97
98 /* Check that we got what we need */
99 if(fileNr<1) {tpcPrintUsage(argv[0], info, stderr); tacFree(&tac); return(1);}
100
101 /* In verbose mode print arguments and options */
102 if(verbose>1) {
103 printf("required_timeunit := %s\n", unitName(tunit));
104 printf("fileNr := %d\n", fileNr);
105 }
106
107
108 /*
109 * Process each file
110 */
111 fileNr=0;
112 for(ai=ffi; ai<argc; ai++) {
113
114 /* Read TAC file */
115 strcpy(tacfile, argv[ai]);
116 fprintf(stdout, "%s : \n", tacfile); fflush(stdout);
117 ret=tacRead(&tac, tacfile, &status);
118 if(ret!=TPCERROR_OK) {
119 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
120 tacFree(&tac); return(2);
121 }
122 if(verbose>1) {
123 printf("fileformat := %s\n", tacFormattxt(tac.format));
124 printf("tacNr := %d\n", tac.tacNr);
125 printf("sampleNr := %d\n", tac.sampleNr);
126 printf("original_timeunit := %s\n", unitName(tac.tunit));
127 }
128
129 /* Convert time unit */
130 ret=tacXUnitConvert(&tac, tunit, &status);
131 if(ret!=TPCERROR_OK) {
132 fprintf(stderr, "Warning: cannot convert time unit from %s to %s\n",
133 unitName(tac.tunit), unitName(tunit));
134 }
135
136 /* Get the time range */
137 ret=tacXRange(&tac, &xmin, &xmax);
138 if(ret!=0) {
139 fprintf(stderr, "Warning: cannot find time range in %s\n", tacfile);
140 if(verbose>1) printf("ret := %d\n", ret);
141 tacFree(&tac);
142 continue;
143 }
144
145 /* Print time range */
146 fprintf(stdout, "time_min := %g\n", xmin);
147 fprintf(stdout, "time_max := %g\n", xmax);
148 fflush(stdout);
149
150 /* Set min and max start time so far */
151 if(fileNr==0 || xmin<min_xmin) min_xmin=xmin;
152 if(fileNr==0 || xmin>max_xmin) max_xmin=xmin;
153 /* Set min and max end time so far */
154 if(fileNr==0 || xmax<min_xmax) min_xmax=xmax;
155 if(fileNr==0 || xmax>max_xmax) max_xmax=xmax;
156
157 tacFree(&tac);
158 fileNr++;
159 } // next TAC
160
161 if(fileNr==0) {
162 fprintf(stderr, "Error: no min/max could be determined.\n");
163 return(3);
164 }
165
166 /*
167 * Print the min and max of all files
168 */
169 if(fileNr>1) {
170 fprintf(stdout, "all_files :\n");
171 if(verbose>0) fprintf(stdout, "timeunit := %s\n", unitName(tunit));
172 fprintf(stdout, "min_time_min := %g\n", min_xmin);
173 fprintf(stdout, "max_time_max := %g\n", max_xmax);
174 fprintf(stdout, "max_time_min := %g\n", max_xmin);
175 fprintf(stdout, "min_time_max := %g\n", min_xmax);
176 }
177
178 return(0);
179}
180/*****************************************************************************/
181
182/*****************************************************************************/
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
Definition tpctac.h:87
tacformat format
Definition tpctac.h:93
int sampleNr
Definition tpctac.h:89
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 tacXUnitConvert(TAC *tac, const int u, TPCSTATUS *status)
Definition tacunits.c:23
int tacXRange(TAC *d, double *xmin, double *xmax)
Get the range of x values (times) in TAC structure.
Definition tacx.c:124
Header file for library libtpcextensions.
@ UNIT_MIN
minutes
@ TPCERROR_OK
No error.
int unitIdentify(const char *s)
Definition units.c:162
char * unitName(int unit_code)
Definition units.c:143
int unitIsTime(int u)
Definition units.c:359
Header file for library libtpcift.
Header file for library libtpctac.