TPCCLIB
Loading...
Searching...
No Matches
tacadd0.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 "Adds a zero sample to the beginning of TAC data.",
23 "If data samples already start from time (x) zero, then nothing is done.",
24 " ",
25 "Usage: @P [options] tacfile [outputfile]",
26 " ",
27 "Options:",
28 " -x=<time>",
29 " Add late time point with zero value instead.",
30 " -stdoptions", // List standard options like --help, -v, etc
31 " ",
32 "See also: tacsetx, tacsety, inpstart, tacframe, taccat, taccut",
33 " ",
34 "Keywords: TAC, time, tool",
35 0};
36/*****************************************************************************/
37
38/*****************************************************************************/
39/* Turn on the globbing of the command line, since it is disabled by default in
40 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
41 In Unix&Linux wildcard command line processing is enabled by default. */
42/*
43#undef _CRT_glob
44#define _CRT_glob -1
45*/
46int _dowildcard = -1;
47/*****************************************************************************/
48
49/*****************************************************************************/
53int main(int argc, char **argv)
54{
55 int ai, help=0, version=0, verbose=1;
56 int ret;
57 char *cptr, tacfile[FILENAME_MAX], outfile[FILENAME_MAX];
58 double latetime=nan("");
59 TAC tac;
60
61
62 /*
63 * Get arguments
64 */
65 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
66 tacInit(&tac);
67 tacfile[0]=outfile[0]=(char)0;
68 /* Options */
69 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
70 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
71 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
72 if(strncasecmp(cptr, "X=", 2)==0) {
73 if(atofCheck(cptr+2, &latetime)==0 && latetime>0.0) continue;
74 }
75 fprintf(stderr, "Error: invalid option '%s'\n", argv[ai]);
76 return(1);
77 } else break; // tac name argument may start with '-'
78
79 TPCSTATUS status; statusInit(&status);
80 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
81 status.verbose=verbose-1;
82
83 /* Print help or version? */
84 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
85 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
86 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
87
88 /* Arguments */
89 if(ai<argc) strlcpy(tacfile, argv[ai++], FILENAME_MAX);
90 if(ai<argc) strlcpy(outfile, argv[ai++], FILENAME_MAX);
91 if(ai<argc) {
92 fprintf(stderr, "Error: too many arguments.\n");
93 return(1);
94 }
95
96 /* Is something missing? */
97 if(!tacfile[0]) {tpcPrintUsage(argv[0], info, stdout); return(1);}
98 if(!outfile[0]) strcpy(outfile, tacfile);
99
100 /* In verbose mode print arguments and options */
101 if(verbose>1) {
102 for(ai=0; ai<argc; ai++)
103 printf("%s ", argv[ai]);
104 printf("\n");
105 printf("tacfile := %s\n", tacfile);
106 printf("outfile := %s\n", outfile);
107 if(!isnan(latetime)) printf("latetime := %g\n", latetime);
108 }
109
110
111 /*
112 * Read the file
113 */
114 if(verbose>1) printf("reading %s\n", tacfile);
115 ret=tacRead(&tac, tacfile, &status);
116 if(ret!=TPCERROR_OK) {
117 fprintf(stderr, "Error: %s\n", 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 }
127 /* Sort the data by sample times (x) */
128 ret=tacSortByTime(&tac, &status);
129 if(ret!=TPCERROR_OK) {
130 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
131 tacFree(&tac); return(2);
132 }
133
134
135 /*
136 * Check whether zero or late sample exists already
137 */
138 if(isnan(latetime)) {
139 if((tac.isframe && tac.x1[0]<=0.0) || (!tac.isframe && tac.x[0]<=0.0)) {
140 fprintf(stderr, "Notice: zero sample exists; nothing to do.\n");
141 tacFree(&tac); return(0);
142 }
143 } else {
144 if((tac.isframe && tac.x2[tac.sampleNr-1]>=latetime) || (!tac.isframe && tac.x[tac.sampleNr-1]>=latetime)) {
145 fprintf(stderr, "Notice: late sample exists; nothing to do.\n");
146 tacFree(&tac); return(0);
147 }
148 }
149
150
151 /*
152 * Determine the new sample x values
153 */
154 double newx, newx1, newx2;
155 newx=newx1=newx2=nan("");
156 if(tac.isframe) {
157 if(verbose>1) {
158 int i; if(isnan(latetime)) i=0; else i=tac.sampleNr-1;
159 printf("x1 := %g\n", tac.x1[i]);
160 printf("x2 := %g\n", tac.x2[i]);
161 }
162 if(isnan(latetime) && tac.x1[0]>0.0) {
163 newx2=tac.x1[0];
164 newx1=newx2-(tac.x2[0]-tac.x1[0]);
165 if(newx1<0.0) newx1=0.0;
166 newx=0.5*(newx1+newx2);
167 }
168 if(!isnan(latetime)) {
169 int i=tac.sampleNr-1;
170 newx=latetime;
171 newx1=tac.x2[i];
172 newx2=latetime+(latetime-newx1);
173 }
174 } else {
175 if(verbose>1) {
176 int i; if(isnan(latetime)) i=0; else i=tac.sampleNr-1;
177 printf("x[%d] := %g\n", i, tac.x[i]);
178 if(tac.sampleNr>1) {
179 if(isnan(latetime)) printf("x[1] := %g\n", tac.x[1]);
180 else printf("x[%d] := %g\n", i-1, tac.x[i-1]);
181 }
182 }
183 if(isnan(latetime) && tac.x[0]>0.0) {
184 if(tac.sampleNr>1) {
185 newx=tac.x[0]-(tac.x[1]-tac.x[0]);
186 if(newx<0.0) newx=0.0;
187 } else {
188 newx=0.0;
189 }
190 }
191 if(!isnan(latetime)) {
192 newx=latetime;
193 }
194 }
195 if(verbose>1) {
196 if(tac.isframe) {
197 printf("newx1 := %g\n", newx1);
198 printf("newx2 := %g\n", newx2);
199 } else {
200 printf("newx := %g\n", newx);
201 }
202 }
203 if(tac.isframe && (isnan(newx1) || isnan(newx2))) {
204 fprintf(stderr, "Error: cannot add zero sample.\n");
205 tacFree(&tac); return(3);
206 }
207 if(!tac.isframe && isnan(newx)) {
208 fprintf(stderr, "Error: cannot add zero sample.\n");
209 tacFree(&tac); return(3);
210 }
211
212 /*
213 * Add the zero sample
214 */
215 if(verbose>2) printf("adding zero sample\n");
216 if(isnan(latetime)) {
217 ret=tacAddZeroSample(&tac, &status);
218 } else {
219 ret=tacAllocateMoreSamples(&tac, 1);
220 }
221 if(ret!=TPCERROR_OK) {
222 fprintf(stderr, "Error: cannot add zero sample.\n");
223 tacFree(&tac); return(4);
224 }
225 if(isnan(latetime)) {
226 tac.x1[0]=newx1;
227 tac.x2[0]=newx2;
228 tac.x[0]=newx;
229 } else {
230 tac.sampleNr++;
231 tac.x1[tac.sampleNr-1]=newx1;
232 tac.x2[tac.sampleNr-1]=newx2;
233 tac.x[tac.sampleNr-1]=newx;
234 for(int i=0; i<tac.tacNr; i++) tac.c[i].y[tac.sampleNr-1]=0.0;
235 }
236
237
238 /*
239 * Save data
240 */
241 if(verbose>1) printf("writing %s\n", outfile);
242 FILE *fp; fp=fopen(outfile, "w");
243 if(fp==NULL) {
244 fprintf(stderr, "Error: cannot open file for writing (%s)\n", outfile);
245 tacFree(&tac); return(11);
246 }
247 ret=tacWrite(&tac, fp, TAC_FORMAT_UNKNOWN, 1, &status);
248 fclose(fp); tacFree(&tac);
249 if(ret!=TPCERROR_OK) {
250 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
251 return(12);
252 }
253 if(verbose>0) printf("%s saved.\n", outfile);
254
255 return(0);
256}
257/*****************************************************************************/
258
259/*****************************************************************************/
int atofCheck(const char *s, double *v)
Definition decpoint.c:94
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
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 tacAllocateMoreSamples(TAC *tac, int addNr)
Allocate memory for more samples in TAC data.
Definition tac.c:435
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 tacSortByTime(TAC *d, TPCSTATUS *status)
Definition tacorder.c:74
int tacAddZeroSample(TAC *d, TPCSTATUS *status)
Add an initial sample to TAC(s) with zero time and concentration.
Definition tacx.c:366
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 library libtpctac.
@ TAC_FORMAT_UNKNOWN
Unknown format.
Definition tpctac.h:28