TPCCLIB
Loading...
Searching...
No Matches
tacdelna.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 "Delete those TAC(s) inside a TAC file that have missing values.",
23 "If file contains only one TAC, then missing samples are deleted.",
24 " ",
25 "Usage: @P [options] file",
26 " ",
27 "Options:",
28 " -dry",
29 " Nothing is actually deleted, application only lists which TAC(s)",
30 " would be deleted.",
31 " -stdoptions", // List standard options like --help, -v, etc
32 " ",
33 "See also: taclist, tacdel, tacadd, taccut, taccuty, tacsplit, niinan",
34 " ",
35 "Keywords: TAC, tool",
36 0};
37/*****************************************************************************/
38
39/*****************************************************************************/
40/* Turn on the globbing of the command line, since it is disabled by default in
41 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
42 In Unix&Linux wildcard command line processing is enabled by default. */
43/*
44#undef _CRT_glob
45#define _CRT_glob -1
46*/
47int _dowildcard = -1;
48/*****************************************************************************/
49
50/*****************************************************************************/
54int main(int argc, char **argv)
55{
56 int ai, help=0, version=0, verbose=1;
57 int ret, i, dryMode=0;
58 char *cptr, tacfile[FILENAME_MAX];
59
60
61 /*
62 * Get arguments
63 */
64 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
65 tacfile[0]=(char)0;
66 /* Options */
67 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
68 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
69 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
70 if(strcasecmp(cptr, "DRY")==0) {
71 dryMode=1; continue;
72 }
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-3;
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 /* The first argument (non-option) is the filename */
87 if(ai<argc) {strlcpy(tacfile, argv[ai], FILENAME_MAX); ai++;}
88 else {fprintf(stderr, "Error: missing filename.\n"); return(1);}
89 if(ai<argc) {fprintf(stderr, "Error: extra command-line argument.\n"); return(1);}
90
91 /* In verbose mode print arguments and options */
92 if(verbose>1) {
93 printf("tacfile := %s\n", tacfile);
94 printf("dryMode := %d\n", dryMode);
95 }
96
97 /* Read the file */
98 if(verbose>1) printf("reading %s\n", tacfile);
99 TAC tac; tacInit(&tac);
100 ret=tacRead(&tac, tacfile, &status);
101 if(ret!=TPCERROR_OK) {
102 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
103 tacFree(&tac); return(2);
104 }
105 if(verbose>2) {
106 printf("fileformat := %s\n", tacFormattxt(tac.format));
107 printf("tacNr := %d\n", tac.tacNr);
108 printf("sampleNr := %d\n", tac.sampleNr);
109 }
110
111 /* Are there any NaNs in y values? */
112 if(tacYNaNs(&tac, -1)==0) {
113 if(verbose>0) fprintf(stdout, "File does not have any missing values.\n");
114 tacFree(&tac); return(0);
115 }
116
117 /* If only one TAC, then delete the samples with NaN */
118 if(tac.tacNr==1) {
119 int origSampleNr=tac.sampleNr;
120 if(verbose>1) printf("one TAC only; deleting missing samples...\n");
121 if(tacDeleteMissingSamples(&tac)!=TPCERROR_OK || tac.sampleNr<1) {
122 fprintf(stderr, "Error: cannot delete missing samples.\n");
123 tacFree(&tac); return(3);
124 }
125 if(dryMode) {
126 printf("%d sample(s) were marked to be deleted.\n", origSampleNr-tac.sampleNr);
127 tacFree(&tac); return(0);
128 }
129 /* Save data */
130 if(verbose>1) printf("writing %s\n", tacfile);
131 FILE *fp; fp=fopen(tacfile, "w");
132 if(fp==NULL) {
133 fprintf(stderr, "Error: cannot open file for writing (%s)\n", tacfile);
134 tacFree(&tac); return(11);
135 }
136 ret=tacWrite(&tac, fp, TAC_FORMAT_UNKNOWN, 1, &status);
137 fclose(fp);
138 if(ret!=TPCERROR_OK) {
139 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
140 tacFree(&tac); return(12);
141 }
142 if(verbose>=0) printf("%d missing sample(s) deleted.\n", origSampleNr-tac.sampleNr);
143 tacFree(&tac);
144 return(0);
145 }
146
147 /* Select the TACs which contain NaN(s) as concentration value */
148 int n=0;
149 for(int i=0; i<tac.tacNr; i++) {
150 if(tacYNaNs(&tac, i)>0) {tac.c[i].sw=1; n++;}
151 }
152 if(verbose>1) printf("%d tac(s) with missing values found\n", n);
153 if(n==tac.tacNr) {
154 fprintf(stderr, "Warning: all TACs were selected for removal; aborted.\n");
155 tacFree(&tac); return(3);
156 }
157
158 /*
159 * Delete the selected TACs
160 */
161 i=tac.tacNr-1; n=0; ret=TPCERROR_OK;
162 while(i>=0) {
163 if(tac.c[i].sw) {
164 if(dryMode) {
165 printf("marked for deletion: %s\n", tac.c[i].name); n++;
166 } else {
167 if(verbose>=0) printf("%s deleted\n", tac.c[i].name);
168 ret=tacDeleteTACC(&tac, i); if(ret!=TPCERROR_OK) break; else n++;
169 }
170 }
171 i--;
172 }
173 if(ret!=TPCERROR_OK) {
174 fprintf(stderr, "Error: cannot delete TAC.\n");
175 tacFree(&tac); return(5);
176 }
177 if(dryMode) {
178 printf("%d TAC(s) were marked to be deleted.\n", n);
179 tacFree(&tac); return(0);
180 }
181
182 /*
183 * Save data
184 */
185 if(verbose>1) printf("writing %s\n", tacfile);
186 FILE *fp; fp=fopen(tacfile, "w");
187 if(fp==NULL) {
188 fprintf(stderr, "Error: cannot open file for writing (%s)\n", tacfile);
189 tacFree(&tac); return(11);
190 }
191 ret=tacWrite(&tac, fp, TAC_FORMAT_UNKNOWN, 1, &status);
192 fclose(fp); tacFree(&tac);
193 if(ret!=TPCERROR_OK) {
194 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
195 return(12);
196 }
197
198 if(verbose>=0) printf("%d TAC(s) are deleted.\n", n);
199 return(0);
200}
201/*****************************************************************************/
202
203/*****************************************************************************/
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
char sw
Definition tpctac.h:77
char name[MAX_TACNAME_LEN+1]
Definition tpctac.h:81
Definition tpctac.h:87
tacformat format
Definition tpctac.h:93
int sampleNr
Definition tpctac.h:89
TACC * c
Definition tpctac.h:117
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 tacYNaNs(TAC *tac, const int i)
Definition tacnan.c:47
int tacDeleteTACC(TAC *d, int i)
Definition tacorder.c:310
int tacDeleteMissingSamples(TAC *d)
Delete those samples (time frames) from TAC structure, which contain only missing y values,...
Definition tacx.c:450
Header file for library libtpcextensions.
@ TPCERROR_OK
No error.
Header file for library libtpcift.
Header file for library libtpctac.
@ TAC_FORMAT_UNKNOWN
Unknown format.
Definition tpctac.h:28