TPCCLIB
Loading...
Searching...
No Matches
tacp2f.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 "Converts y values in TAC file from percentages to fractions.",
23 "If none of y values exceeds 1 then no conversion is done.",
24 " ",
25 "Usage: @P [options] tacfile [outputfile]",
26 " ",
27 "Options:",
28 " -stdoptions", // List standard options like --help, -v, etc
29 " ",
30 "Example 1. Convert all dat files in current directory in bash shell:",
31 " for file in ./*.dat; do @P $file; done",
32 " ",
33 "Example 2. Convert all dat files in Windows command prompt window:",
34 " for %g in (*.dat) do @P %g",
35 " ",
36 "See also: taccalc, tacunit, tacsety, tac2svg",
37 " ",
38 "Keywords: TAC, tool",
39 0};
40/*****************************************************************************/
41
42/*****************************************************************************/
43/* Turn on the globbing of the command line, since it is disabled by default in
44 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
45 In Unix&Linux wildcard command line processing is enabled by default. */
46/*
47#undef _CRT_glob
48#define _CRT_glob -1
49*/
50int _dowildcard = -1;
51/*****************************************************************************/
52
53/*****************************************************************************/
57int main(int argc, char **argv)
58{
59 int ai, help=0, version=0, verbose=1;
60 char tacfile[FILENAME_MAX], outfile[FILENAME_MAX];
61 int ret;
62
63
64 /*
65 * Get arguments
66 */
67 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
68 tacfile[0]=outfile[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 fprintf(stderr, "Error: invalid option '%s'\n", argv[ai]);
74 return(1);
75 } else break; // tac name argument may start with '-'
76
77 TPCSTATUS status; statusInit(&status);
78 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
79 status.verbose=verbose-1;
80
81 /* Print help or version? */
82 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
83 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
84 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
85
86 /* Process other arguments, starting from the first non-option */
87 if(ai<argc) {strlcpy(tacfile, argv[ai++], FILENAME_MAX);}
88 if(ai<argc) {strlcpy(outfile, argv[ai++], FILENAME_MAX);}
89 if(ai<argc) {
90 fprintf(stderr, "Error: too many arguments: '%s'.\n", argv[ai]);
91 return(1);
92 }
93
94 /* Is something missing? */
95 if(!tacfile[0]) {tpcPrintUsage(argv[0], info, stdout); return(1);}
96 /* If output filename was not given, then edit the input file */
97 if(!outfile[0]) strcpy(outfile, tacfile);
98
99
100 /* In verbose mode print arguments and options */
101 if(verbose>1) {
102 for(ai=0; ai<argc; ai++) printf("%s ", argv[ai]);
103 printf("\n");
104 printf("tacfile := %s\n", tacfile);
105 printf("outfile := %s\n", outfile);
106 fflush(stdout);
107 }
108
109
110 /*
111 * Read the file
112 */
113 if(verbose>1) printf("reading %s\n", tacfile);
114 TAC tac; tacInit(&tac);
115 ret=tacRead(&tac, tacfile, &status);
116 if(ret!=TPCERROR_OK) {
117 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
118 tacFree(&tac); return(2);
119 }
120 if(verbose>2) {
121 printf("fileformat := %s\n", tacFormattxt(tac.format));
122 printf("tacNr := %d\n", tac.tacNr);
123 printf("sampleNr := %d\n", tac.sampleNr);
124 printf("xunit := %s\n", unitName(tac.tunit));
125 printf("yunit := %s\n", unitName(tac.cunit));
126 fflush(stdout);
127 }
128
129
130 /*
131 * Get the max y value to check whether any of y values exceeds 1.
132 */
133 double ymax;
134 ret=tacYRange(&tac, -1, NULL, &ymax, NULL, NULL, NULL, NULL);
135 if(ret!=TPCERROR_OK) {
136 fprintf(stderr, "Error: cannot determine max value in %s\n", tacfile);
137 tacFree(&tac); return(3);
138 }
139 if(verbose>1) printf("y_max := %g\n", ymax);
140
141
142 /*
143 * Convert y values from percentages to fractions when necessary
144 */
145 if(ymax>1.0) {
146 int fi, ri;
147 for(ri=0; ri<tac.tacNr; ri++) {
148 for(fi=0; fi<tac.sampleNr; fi++) {
149 if(isnan(tac.c[ri].y[fi])) {continue;}
150 tac.c[ri].y[fi]*=0.01;
151 }
152 }
154 } else {
155 if(verbose>0) printf("no conversion necessary for %s\n", tacfile);
156 }
157
158
159 /*
160 * Save data
161 */
162 if(verbose>1) printf("writing %s\n", outfile);
163 FILE *fp; fp=fopen(outfile, "w");
164 if(fp==NULL) {
165 fprintf(stderr, "Error: cannot open file for writing (%s)\n", outfile);
166 tacFree(&tac); return(11);
167 }
168 ret=tacWrite(&tac, fp, TAC_FORMAT_UNKNOWN, 1, &status);
169 fclose(fp); tacFree(&tac);
170 if(ret!=TPCERROR_OK) {
171 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
172 return(12);
173 }
174 if(verbose>=0) printf("%s saved.\n", outfile);
175
176 return(0);
177}
178/*****************************************************************************/
179
180/*****************************************************************************/
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
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 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 tacYRange(TAC *d, int i, double *ymin, double *ymax, int *smin, int *smax, int *imin, int *imax)
Get the range of y values (concentrations) in TAC struct.
Definition tacy.c:26
Header file for library libtpcextensions.
@ UNIT_UNITLESS
Unitless.
@ TPCERROR_OK
No error.
char * unitName(int unit_code)
Definition units.c:143
Header file for library libtpcift.
Header file for library libtpctac.
@ TAC_FORMAT_UNKNOWN
Unknown format.
Definition tpctac.h:28