TPCCLIB
Loading...
Searching...
No Matches
tacln.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 "tpctac.h"
17/*****************************************************************************/
18
19/*****************************************************************************/
20static char *info[] = {
21 "Calculates natural logarithm of y values (concentrations) in TAC file, or",
22 "optionally of TAC x values (sample times).",
23 " ",
24 "Usage: @P [options] tacfile outputfile",
25 " ",
26 "Options:",
27 " -x=<y|N>",
28 " Calculate (y) or do not calculate (n, default) natural logarithm of x",
29 " values.",
30 " -y=<Y|n>",
31 " Calculate (y, default) or do not calculate (n) natural logarithm of y",
32 " values.",
33 " -stdoptions", // List standard options like --help, -v, etc
34 " ",
35 "See also: taccalc, tacinv, tacadd, taccut, tacsetx, fit_line, fit_exp",
36 " ",
37 "Keywords: TAC, tool",
38 0};
39/*****************************************************************************/
40
41/*****************************************************************************/
42/* Turn on the globbing of the command line, since it is disabled by default in
43 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
44 In Unix&Linux wildcard command line processing is enabled by default. */
45/*
46#undef _CRT_glob
47#define _CRT_glob -1
48*/
49int _dowildcard = -1;
50/*****************************************************************************/
51
52/*****************************************************************************/
56int main(int argc, char **argv)
57{
58 int ai, help=0, version=0, verbose=1;
59 int ret;
60 char tacfile[FILENAME_MAX], outfile[FILENAME_MAX];
61 char *cptr;
62 TAC tac;
63 int xvals=0;
64 int yvals=1;
65
66
67 /*
68 * Get arguments
69 */
70 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
71 tacInit(&tac);
72 tacfile[0]=outfile[0]=(char)0;
73 /* Options */
74 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
75 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
76 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
77 if(strncasecmp(cptr, "X=", 2)==0) {
78 cptr+=2;
79 if(strncasecmp(cptr, "YES", 1)==0) {xvals=1; continue;}
80 else if(strncasecmp(cptr, "NO", 1)==0) {xvals=0; continue;}
81 } else if(strncasecmp(cptr, "Y=", 2)==0) {
82 cptr+=2;
83 if(strncasecmp(cptr, "YES", 1)==0) {yvals=1; continue;}
84 else if(strncasecmp(cptr, "NO", 1)==0) {yvals=0; continue;}
85 }
86 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
87 return(1);
88 } else break; // tac name argument may start with '-'
89
90 TPCSTATUS status; statusInit(&status);
91 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
92 status.verbose=verbose-1;
93
94 /* Print help or version? */
95 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
96 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
97 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
98
99 /* Arguments */
100 for(; ai<argc; ai++) {
101 if(!tacfile[0]) {
102 strcpy(tacfile, argv[ai]); continue;
103 } else if(!outfile[0]) {
104 strcpy(outfile, argv[ai]); continue;
105 }
106 fprintf(stderr, "Error: invalid argument '%s'.\n", argv[ai]);
107 return(1);
108 }
109
110 /* Is something missing? */
111 if(!outfile[0]) {tpcPrintUsage(argv[0], info, stdout); return(1);}
112
113
114 /* In verbose mode print arguments and options */
115 if(verbose>1) {
116 for(ai=0; ai<argc; ai++)
117 printf("%s ", argv[ai]);
118 printf("\n");
119 printf("tacfile := %s\n", tacfile);
120 printf("outfile := %s\n", outfile);
121 printf("xvals := %d\n", xvals);
122 printf("yvals := %d\n", yvals);
123 }
124 if(xvals==0 && yvals==0) {
125 fprintf(stderr, "Error: no transforms requested.\n");
126 return(2);
127 }
128
129
130 /*
131 * Read the file
132 */
133 if(verbose>1) printf("reading %s\n", tacfile);
134 ret=tacRead(&tac, tacfile, &status);
135 if(ret!=TPCERROR_OK) {
136 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
137 tacFree(&tac); return(2);
138 }
139 if(verbose>2) {
140 printf("fileformat := %s\n", tacFormattxt(tac.format));
141 printf("tacNr := %d\n", tac.tacNr);
142 printf("sampleNr := %d\n", tac.sampleNr);
143 printf("xunit := %s\n", unitName(tac.tunit));
144 printf("yunit := %s\n", unitName(tac.cunit));
145 }
146
147
148 int okNr=0, failNr=0;
149 /*
150 * Invert the y data, if required
151 */
152 if(yvals) {
153 for(int ri=0; ri<tac.tacNr; ri++) {
154 for(int fi=0; fi<tac.sampleNr; fi++) {
155 if(isnan(tac.c[ri].y[fi])) {failNr++; continue;}
156 if(tac.c[ri].y[fi]<1.0E-20) {
157 tac.c[ri].y[fi]=nan(""); failNr++; continue;}
158 tac.c[ri].y[fi]=log(tac.c[ri].y[fi]);
159 if(isfinite(tac.c[ri].y[fi])) okNr++;
160 else {tac.c[ri].y[fi]=nan(""); failNr++;}
161 }
162 }
163 }
164 /*
165 * Invert the x data, if required
166 */
167 if(xvals) {
168 if(tac.isframe==0) {
169 for(int fi=0; fi<tac.sampleNr; fi++) {
170 if(isnan(tac.x[fi])) {failNr++; continue;}
171 if(tac.x[fi]<1.0E-20) {tac.x[fi]=nan(""); failNr++; continue;}
172 tac.x[fi]=log(tac.x[fi]);
173 if(isfinite(tac.x[fi])) okNr++;
174 else {tac.x[fi]=nan(""); failNr++;}
175 }
176 } else {
177 for(int fi=0; fi<tac.sampleNr; fi++) {
178 if(isnan(tac.x1[fi])) {failNr++;}
179 else if(tac.x1[fi]<1.0E-20) {tac.x1[fi]=nan(""); failNr++;}
180 else {
181 tac.x1[fi]=log(tac.x1[fi]);
182 if(isfinite(tac.x1[fi])) okNr++;
183 else {tac.x1[fi]=nan(""); failNr++;}
184 }
185 if(isnan(tac.x2[fi])) {failNr++;}
186 else if(tac.x2[fi]<1.0E-20) {tac.x2[fi]=nan(""); failNr++;}
187 else {
188 tac.x2[fi]=log(tac.x2[fi]);
189 if(isfinite(tac.x2[fi])) okNr++;
190 else {tac.x2[fi]=nan(""); failNr++;}
191 }
192 }
193 }
194 }
195
196 if(verbose>1) {
197 printf(" failNr := %d\n", failNr);
198 printf(" okNr := %d\n", okNr);
199 }
200 if(okNr<1) {
201 fprintf(stderr, "Error: all transformations failed.\n");
202 tacFree(&tac); return(3);
203 }
204
205
206 /*
207 * Save data
208 */
209 if(verbose>1) printf("writing %s\n", outfile);
210 FILE *fp; fp=fopen(outfile, "w");
211 if(fp==NULL) {
212 fprintf(stderr, "Error: cannot open file for writing (%s)\n", outfile);
213 tacFree(&tac); return(11);
214 }
215 ret=tacWrite(&tac, fp, TAC_FORMAT_UNKNOWN, 1, &status);
216 fclose(fp); tacFree(&tac);
217 if(ret!=TPCERROR_OK) {
218 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
219 return(12);
220 }
221 if(verbose>=0) printf("%s saved.\n", outfile);
222
223 return(0);
224}
225/*****************************************************************************/
226
227/*****************************************************************************/
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
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
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
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
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