8#include "tpcclibConfig.h"
21static char *info[] = {
22 "Get the data units of TAC files, or optionally set or convert the units.",
24 "Usage: @P [options] filename(s)",
28 " Set x (sample time) unit; x values are however not changed.",
30 " Set y (concentration) unit; y values are however not changed.",
32 " Conversion of x (sample time) values to specified unit.",
34 " Conversion of y (concentration) values to specified unit.",
36 " Print only the unit of x in stdout.",
38 " Print only the unit of y in stdout.",
39 " -density=<Density (g/mL)>",
40 " Density is needed when converting per mass to per volume units.",
42 " List available units and quit.",
45 "Example 1: convert sample times to minutes and concentrations to kBq/mL",
46 " @P -xconv=min -yconv=kBq/ml iea*.tac",
48 "Example 2: convert concentrations to the same units as in another file",
49 " (in Linux/UNIX/OS X)",
50 " unit=$( @P -y iea2285.tac )",
51 " @P -yconv=$unit iea2285.bld",
53 "See also: taclist, tac2suv, taccalc, imgunit, tacframe, dftmax",
55 "Keywords: TAC, tool, unit, time",
74int main(
int argc,
char **argv)
76 int ai, help=0, version=0, verbose=1;
79 int print_x=0, print_y=0;
80 int newxunit=-1, newyunit=-1;
81 int oldxunit=-1, oldyunit=-1;
84 double density=nan(
"");
85 char tacfile[FILENAME_MAX];
92 if(argc==1) {
tpcPrintUsage(argv[0], info, stderr);
return(1);}
96 for(ai=1; ai<argc; ai++)
if(*argv[ai]==
'-') {
98 char *cptr=argv[ai]+1;
if(*cptr==
'-') cptr++;
if(!*cptr)
continue;
99 if(strncasecmp(cptr,
"xset=", 5)==0) {
102 if(!cptr[0] || strcasecmp(cptr,
"UNKNOWN")==0)
continue;
103 }
else if(strncasecmp(cptr,
"yset=", 5)==0) {
106 if(!cptr[0] || strcasecmp(cptr,
"UNKNOWN")==0)
continue;
107 }
else if(strncasecmp(cptr,
"xconv=", 6)==0) {
110 if(!cptr[0] || strcasecmp(cptr,
"UNKNOWN")==0)
continue;
111 }
else if(strncasecmp(cptr,
"yconv=", 6)==0) {
114 if(!cptr[0] || strcasecmp(cptr,
"UNKNOWN")==0)
continue;
115 }
else if(strcasecmp(cptr,
"x")==0) {
117 }
else if(strcasecmp(cptr,
"y")==0) {
119 }
else if(strcasecmp(cptr,
"UNITS")==0) {
120 list_units=1;
continue;
121 }
else if(strncasecmp(cptr,
"DENSITY=", 8)==0) {
122 if(
atofCheck(cptr+8, &density)==0 && density>0.0)
continue;
124 fprintf(stderr,
"Error: invalid option '%s'\n", argv[ai]);
128 if(help==2) {
tpcHtmlUsage(argv[0], info,
"");
return(0);}
134 printf(
"list_units := %d\n", list_units);
135 printf(
"print_x := %d\n", print_x);
136 printf(
"print_y := %d\n", print_y);
137 printf(
"change_x := %d\n", change_x);
138 printf(
"change_y := %d\n", change_y);
139 if(!isnan(density)) printf(
"density := %g\n", density);
140 if(newxunit>=0) printf(
"newxunit := %s\n",
unitName(newxunit));
141 if(newyunit>=0) printf(
"newyunit := %s\n",
unitName(newyunit));
155 int n=0, nr_of_changes, err_nr=0;
156 for(; ai<argc; ai++) {
158 strcpy(tacfile, argv[ai]);
160 if(verbose>1) printf(
"reading %s\n", tacfile);
161 ret=
tacRead(&tac, tacfile, &status);
171 if(print_x || print_y) {
174 fflush(stdout);
tacFree(&tac);
continue;
177 if(newxunit==-1 && newyunit==-1) {
178 fprintf(stdout,
"# filename := %s\n", tacfile);
182 fflush(stdout);
tacFree(&tac);
continue;
186 if(newxunit>=0 && newxunit!=oldxunit) {
189 if(verbose>0) fprintf(stdout,
"%s: units '%s' replaced by '%s'\n",
195 if(verbose>0) fprintf(stdout,
"%s: conversion of units '%s' to '%s'\n",
200 fprintf(stderr,
"%s:\n", tacfile);
201 fprintf(stderr,
"Error: conversion from '%s' to '%s' not supported.\n",
208 if(newyunit>=0 && newyunit!=oldyunit) {
211 if(verbose>0) fprintf(stdout,
"%s: units '%s' replaced by '%s'\n",
217 if(verbose>0) fprintf(stdout,
"%s: conversion of units '%s' to '%s'\n",
220 }
else if(!isnan(density)) {
228 fprintf(stdout,
"%s: initial conversion of units '%s' to '%s'\n",
233 if(verbose>0) fprintf(stdout,
"%s: conversion of units '%s' to '%s'\n",
240 fprintf(stderr,
"%s:\n", tacfile);
241 fprintf(stderr,
"Error: conversion from '%s' to '%s' not supported.\n",
248 if(nr_of_changes==0) {
tacFree(&tac);
continue;}
250 if(verbose>1) printf(
"writing %s\n", tacfile);
251 FILE *fp; fp=fopen(tacfile,
"w");
253 fprintf(stderr,
"Error: cannot open file for writing (%s)\n", tacfile);
259 fprintf(stderr,
"Error (%d): %s\n", ret,
errorMsg(status.
error));
266 if(n==0) fprintf(stderr,
"Error: no TAC(s) were found.\n");
267 if(n==0 || err_nr>0)
return(2);
int atofCheck(const char *s, double *v)
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
int tpcHtmlUsage(const char *program, char *text[], const char *path)
void tpcPrintBuild(const char *program, FILE *fp)
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
void statusInit(TPCSTATUS *s)
char * errorMsg(tpcerror e)
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
int verbose
Verbose level, used by statusPrint() etc.
tpcerror error
Error code.
int tacRead(TAC *d, const char *fname, TPCSTATUS *status)
int tacWrite(TAC *tac, FILE *fp, tacformat format, int extra, TPCSTATUS *status)
int tacYUnitConvert(TAC *tac, const int u, TPCSTATUS *status)
int tacYUnitMass2Volume(TAC *tac, const double density, TPCSTATUS *status)
int tacXUnitConvert(TAC *tac, const int u, TPCSTATUS *status)
int tacYUnitVolume2Mass(TAC *tac, const double density, TPCSTATUS *status)
Header file for library libtpcextensions.
@ UNIT_UNKNOWN
Unknown unit.
int unitIdentify(const char *s)
char * unitName(int unit_code)
int unitDividerHasVolume(int u)
int unitDividerHasMass(int u)
Header file for library libtpcift.
Header file for library libtpctac.
@ TAC_FORMAT_UNKNOWN
Unknown format.