TPCCLIB
Loading...
Searching...
No Matches
taccross.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 <unistd.h>
14#include <math.h>
15/*****************************************************************************/
16#include "tpcextensions.h"
17#include "tpcift.h"
18#include "tpctac.h"
19#include "tpcli.h"
20/*****************************************************************************/
21
22/*****************************************************************************/
23static char *info[] = {
24 "Determines the time when TAC in file 2 crosses over the TAC in file 1.",
25 "The first time point (x) of file 2 after which at least the given nr of",
26 "samples (y values) become all lower or higher than the samples in file 1",
27 "is written to stdout.",
28 "Only the first TAC of data files is used.",
29 " ",
30 "Usage: @P [options] file1 file2 nr",
31 " ",
32 "Options:",
33 " -ift",
34 " Cross-over point is written in stdout with explanations.",
35 " -stdoptions", // List standard options like --help, -v, etc
36 " ",
37 "Example:",
38 " @P iea345_lvcav.bld iea345_vena_cava.bld 3",
39 " ",
40 "See also: taccut, tacsplit, tacadd, taccut, tacdel, tacsort",
41 " ",
42 "Keywords: TAC, input, tool",
43 0};
44/*****************************************************************************/
45
46/*****************************************************************************/
47/* Turn on the globbing of the command line, since it is disabled by default in
48 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
49 In Unix&Linux wildcard command line processing is enabled by default. */
50/*
51#undef _CRT_glob
52#define _CRT_glob -1
53*/
54int _dowildcard = -1;
55/*****************************************************************************/
56
57/*****************************************************************************/
61int main(int argc, char **argv)
62{
63 int ai, help=0, version=0, verbose=1;
64 int ret;
65 char *cptr, tacfile1[FILENAME_MAX], tacfile2[FILENAME_MAX];
66 TAC tac1, tac2;
67 int crossLimit=0;
69 int outputType=0;
70
71
72 /*
73 * Get arguments
74 */
75 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
76 tacInit(&tac1); tacInit(&tac2);
77 tacfile1[0]=tacfile2[0]=(char)0;
78 /* Options */
79 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
80 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
81 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
82 if(strcasecmp(cptr, "IFT")==0) {
83 outputType=1; continue;
84 }
85 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
86 return(1);
87 } else break; // tac name argument may start with '-'
88
89 /* Print help or version? */
90 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
91 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
92 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
93
94 /* In verbose mode print arguments and options */
95 if(verbose>1) {
96 printf("outputType := %d\n", outputType);
97 }
98
99 TPCSTATUS status; statusInit(&status);
100 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
101 status.verbose=verbose-5;
102
103 /* Arguments */
104 if(ai<argc) strlcpy(tacfile1, argv[ai++], FILENAME_MAX);
105 if(ai<argc) strlcpy(tacfile2, argv[ai++], FILENAME_MAX);
106 if(ai<argc) crossLimit=atoi(argv[ai++]);
107 if(ai<argc) {fprintf(stderr, "Error: invalid argument '%s'.\n", argv[ai]); return(1);}
108
109 /* Is something missing? */
110 if(!tacfile2[0] || crossLimit<1) {tpcPrintUsage(argv[0], info, stdout); return(1);}
111 if(crossLimit<1) {fprintf(stderr, "Error: missing or invalid cross number.\n"); return(1);}
112
113 /* In verbose mode print arguments and options */
114 if(verbose>1) {
115 for(ai=0; ai<argc; ai++) printf("%s ", argv[ai]);
116 printf("\n");
117 printf("tacfile1 := %s\n", tacfile1);
118 printf("tacfile2 := %s\n", tacfile2);
119 printf("crossLimit := %d\n", crossLimit);
120 fflush(stdout);
121 }
122
123
124 /*
125 * Read the files
126 */
127 if(verbose>1) printf("reading %s\n", tacfile1);
128 ret=tacRead(&tac1, tacfile1, &status);
129 if(ret!=TPCERROR_OK) {
130 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
131 tacFree(&tac1); return(2);
132 }
133 if(verbose>3) {
134 printf("fileformat1 := %s\n", tacFormattxt(tac1.format));
135 printf("tacNr1 := %d\n", tac1.tacNr);
136 printf("sampleNr1 := %d\n", tac1.sampleNr);
137 printf("xunit1 := %s\n", unitName(tac1.tunit));
138 printf("yunit1 := %s\n", unitName(tac1.cunit));
139 }
140 /* Use only the first TAC */
141 if(tac1.tacNr>1) tac1.tacNr=1;
142
143 if(verbose>1) printf("reading %s\n", tacfile2);
144 ret=tacRead(&tac2, tacfile2, &status);
145 if(ret!=TPCERROR_OK) {
146 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
147 tacFree(&tac1); tacFree(&tac2); return(2);
148 }
149 if(verbose>3) {
150 printf("fileformat2 := %s\n", tacFormattxt(tac2.format));
151 printf("tacNr2 := %d\n", tac2.tacNr);
152 printf("sampleNr2 := %d\n", tac2.sampleNr);
153 printf("xunit2 := %s\n", unitName(tac2.tunit));
154 printf("yunit2 := %s\n", unitName(tac2.cunit));
155 }
156 /* Use only the first TAC */
157 if(tac2.tacNr>1) tac2.tacNr=1;
158
159
160 /* Convert TAC 1 into units of TAC 2 */
161 ret=tacXUnitConvert(&tac1, tac2.tunit, &status);
162 if(ret!=TPCERROR_OK) {
163 fprintf(stderr, "Warning: could not convert times from '%s' to '%s'.\n",
164 unitName(tac1.tunit), unitName(tac2.tunit));
165 }
166 ret=tacYUnitConvert(&tac1, tac2.cunit, &status);
167 if(ret!=TPCERROR_OK) {
168 fprintf(stderr, "Warning: could not convert concentrations from '%s' to '%s'.\n",
169 unitName(tac1.cunit), unitName(tac2.cunit));
170 }
171
172 /* Interpolate TAC 1 to sample times of TAC 2 */
173 tacSortByTime(&tac1, &status);
174 tacSortByTime(&tac2, &status);
175 ret=tacAllocateMore(&tac2, 1);
176 if(ret==TPCERROR_OK)
177 ret=liInterpolate(tac1.x, tac1.c[0].y, tac1.sampleNr,
178 tac2.x, tac2.c[1].y, NULL, NULL, tac2.sampleNr,
179 4, 1, verbose-8);
180 if(ret!=0) {
181 fprintf(stderr, "Error: cannot interpolate data.\n");
182 tacFree(&tac1); tacFree(&tac2); return(3);
183 }
184 tacFree(&tac1);
185
186 /*
187 * Search for the crossing
188 */
189 int i, consecNr=0, crossNr=0, above=0;
190 i=0; if(tac2.c[0].y[i]>tac2.c[1].y[i]) above=1;
191 if(verbose>2) printf(" %d: %d %g %g\n", 1+i, above, tac2.c[0].y[i], tac2.c[1].y[i]);
192 for(i=1; i<tac2.sampleNr; i++) {
193 if(tac2.c[0].y[i]>tac2.c[1].y[i]) { // above
194 if(above) {consecNr++;} else {consecNr=0; crossNr++;}
195 above=1;
196 } else { // not above
197 if(!above) {consecNr++;} else {consecNr=0; crossNr++;}
198 above=0;
199 }
200 if(verbose>2) printf(" %d: %d %g %g\n", 1+i, above, tac2.c[0].y[i], tac2.c[1].y[i]);
201 if(crossNr>0 && consecNr>=crossLimit) break;
202 }
203 if(crossNr==0 || consecNr<crossLimit) {
204 fprintf(stderr, "Error: cannot determine cross-over time.\n");
205 tacFree(&tac2); return(4);
206 }
207 i-=consecNr;
208 if(verbose>2) printf("cross-over_point := %d\n", 1+i);
209 if(outputType==0) {
210 printf("%g\n", tac2.x[i]);
211 } else {
212 printf("x := %g\n", tac2.x[i]);
213 printf("y := %g\n", tac2.c[0].y[i]);
214 printf("index := %d\n", i);
215 }
216 tacFree(&tac2);
217
218 return(0);
219}
220/*****************************************************************************/
221
222/*****************************************************************************/
int liInterpolate(double *x, double *y, const int nr, double *newx, double *newy, double *newyi, double *newyii, const int newnr, const int se, const int ee, const int verbose)
Linear interpolation and/or integration with trapezoidal method.
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:89
TACC * c
Definition tpctac.h:117
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 tacAllocateMore(TAC *tac, int tacNr)
Definition tac.c:178
int tacRead(TAC *d, const char *fname, TPCSTATUS *status)
Definition tacio.c:413
char * tacFormattxt(tacformat c)
Definition tacio.c:98
int tacSortByTime(TAC *d, TPCSTATUS *status)
Definition tacorder.c:74
int tacYUnitConvert(TAC *tac, const int u, TPCSTATUS *status)
Definition tacunits.c:72
int tacXUnitConvert(TAC *tac, const int u, TPCSTATUS *status)
Definition tacunits.c:23
Header file for library libtpcextensions.
@ TPCERROR_OK
No error.
char * unitName(int unit_code)
Definition units.c:143
Header file for library libtpcift.
Header file for libtpcli.
Header file for library libtpctac.