-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfilter.cpp
More file actions
191 lines (173 loc) · 5.1 KB
/
filter.cpp
File metadata and controls
191 lines (173 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Purpose: Filter data file according to numeric criteria
Changes log:
- Apr/7-JZ:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/
#include <dynamo.h>
int main(int argc,char *argv[])
{
//////////////////////////////////////////////////////////////
//PROGRAM VARIBALES
//////////////////////////////////////////////////////////////
char datafile[FSIZE]="",filterfile[FSIZE]="",eq[100]="",cmd[LSIZE]="";
real min=-MAXREAL,max=MAXREAL,eqval;
int numcols=0,col=0;
bool eqcmp=true;
//////////////////////////////////////////////////////////////
//INITIALIZE
//////////////////////////////////////////////////////////////
TITLE(stdout,'*',"FILTER DATA FILE");
//////////////////////////////////////////////////////////////
//SET OPTIONS AND USAGE
//////////////////////////////////////////////////////////////
SET_OPTIONS(":hvVf:s:n:c:m:M:e:");
SET_USAGE(
"=======================================================================================\n"
"Usage:\n\n"
"\t./program -f <datafile> [-s <filterfile>] -n <numcols>\n"
"\t -c <col> [-m <min>] [-M <Max>] | [-e <equal>]\n"
"\n"
"Filter <datafile> according to column <col> with the limits defined by <min>\n"
"and <max> or with the exact value <equal>.\n"
"\n"
"=======================================================================================\n"
);
//////////////////////////////////////////////////////////////
//READ OPTIONS
//////////////////////////////////////////////////////////////
while(ITEROPTIONS){
switch(OPTION){
case 'f':
strcpy(datafile,optarg);
break;
case 's':
strcpy(filterfile,optarg);
break;
case 'n':
numcols=atoi(optarg);
break;
case 'c':
col=atoi(optarg);
break;
case 'm':
min=atof(optarg);
break;
case 'M':
max=atof(optarg);
break;
case 'e':
strcpy(eq,optarg);
break;
//========================================
//COMMON
//========================================
case 'v':
VERBOSITY=1;
break;
case 'V':
VERBOSITY=2;
break;
//DETECT ERRORS
OPTION_ERRORS;
}
}
//////////////////////////////////////////////////////////////
//VALIDATE OPTIONS
//////////////////////////////////////////////////////////////
if(isBlank(datafile)){
fprintf(stderr,"Error: No datafile was provided\n");
PRINT_USAGE;
EXIT;
}
if(!fileExists(datafile)){
fprintf(stderr,"Error: Datafile '%s' does not exist\n",datafile);
PRINT_USAGE;
EXIT;
}
if(numcols<1){
fprintf(stderr,"Error: The number of columns should be different from 0\n");
PRINT_USAGE;
EXIT;
}
if(isBlank(filterfile)){
sprintf(filterfile,"%s.fil",datafile);
}
if(col==0){
fprintf(stderr,"Error: You must provide a column number\nColumns available:");
sprintf(cmd,"grep '#1:' %s",datafile);
system(cmd);
PRINT_USAGE;
EXIT;
}
if(isBlank(eq)){
eqcmp=false;
}else{
eqval=atof(eq);
if(eqval<min || eqval>max){
fprintf(stderr,"Error: Value to compare '%g' is outside the input range [%g,%g]\n",
eqval,min,max);
PRINT_USAGE;
EXIT;
}
}
//////////////////////////////////////////////////////////////
//REPORT INPUT INFORMATION
//////////////////////////////////////////////////////////////
if(VERBOSE(1)){
fprintf(stdout,"Datafile: %s\n",datafile);
fprintf(stdout,"Filterfile: %s\n",filterfile);
fprintf(stdout,"Number of columns: %d\n",numcols);
fprintf(stdout,"Selected column: %d\n",col);
fprintf(stdout,"Minimum: %+14.7e\n",min);
fprintf(stdout,"Maximum: %+14.7e\n",max);
if(eqcmp)
fprintf(stdout,"Equal: %+14.7e\n",eqval);
}
//////////////////////////////////////////////////////////////
//PROGRAM
//////////////////////////////////////////////////////////////
STPRINTF("Filtering datafile '%s'...\n",datafile);
file fd=fileOpen(datafile,"r");
file fs=fileOpen(filterfile,"w");
real value,*line;
char linea[LSIZE];
//SELECT DATA FROM DATAFILE
line=(real*)malloc(numcols*sizeof(real));
//Read file content
int r=0;
int i=0;
int c=col-1;
while(1){
fgets(linea,sizeof linea,fd);
if(feof(fd)) break;
//Check line with comment
if(linea[0]=='#'){
fprintf(fs,"%s",linea);
continue;
}
r++;
//fprintf(stdout,"LINEA %d: %s",r,linea);
//Read values in line
readLine(linea,line,numcols);
value=line[c];
//fprintf(stdout,"\tCOLUMN %d VALUE: %+14.7e\n",col,value);
//Check if filter is for an equal value
if(!eqcmp) eqval=value;
//Filter
if(EQUAL(value,eqval) && value>=min && value<=max){
fprintf(fs,"%s",linea);
//fprintf(stdout,"ROW SELECTED\n");
i++;
}else{
//fprintf(stdout,"VALUE NOT MATCHING (eq:%e,min:%e,max:%e) = %e\n",eqval,min,max,value);
}
}
STPRINTF("\t%d lines read...\n",r);
STPRINTF("\t%d lines selected...\n",i);
fclose(fs);
fclose(fd);
STPRINTF("Filtered file '%s' saved...\n",filterfile);
return 0;
}