-
Notifications
You must be signed in to change notification settings - Fork 5
/
getdp.cpp
194 lines (167 loc) · 5.44 KB
/
getdp.cpp
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
192
193
194
/*********************************************************************\
* Program: getdp *
* Purpose: extract slice info from the GENESIS output result *
* Copyright (C) 2012 Tong Zhang *
* *
* This program is free software: you can distribute it and/or modify*
* it under the terms of GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not see <http://www.gnu.org/licenses/>.*
\*********************************************************************/
/*********************************************************************\
This program is written for parsing output file (TDP)
Author: Tong ZHANG
e-mail: tzhang@sinap.ac.cn
Created Time: 10:05, Dec. 9th, 2011
Last update: 15:18, May 26th, 2014
\*********************************************************************/
#include "getdp_fun.h"
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
checkParams(argc,argv);
string file1name, file2name;
int isOrder = 0, izOrder = 0;
int sFlag = 0, zFlag = 0;
int ishowRange = 0;
double dsPos = 0, dzPos = 0;
if (!parseOpts(argc, argv, file1name, file2name,
isOrder, dsPos, izOrder, dzPos, sFlag, zFlag, ishowRange))
{
cerr << "Unknown flags, please check!\n";
exit(1);
}
ifstream file1;
ofstream file2;
if (ishowRange)
{
showRange(file1name);
exit(1);
}
if (argc < 9 )
{
cout << "Not enough parameters!" << "\n";
exit(1);
}
string keystr1 = "history";
string keystr2 = "entries";
int totalSlices = (int)findKeywordValue(file1name, keystr1);
int totalZentri = (int)findKeywordValue(file1name, keystr2);
if (sFlag == 0 && zFlag == 0)
{
cout << "Please define the sFlag (--s) or zFlag (--z) value." << "\n";
exit(1);
}
if (sFlag)
{
if (dsPos) // given s-pos in meter
{
string keystr = "seperation"; // define the keyword string, to find its value
double slice_sepe = findKeywordValue(file1name, keystr);
isOrder = (int) (dsPos/slice_sepe+1.0); // transform s_pos into s-record order
if (isOrder < 1) isOrder = 1;
if (isOrder > totalSlices) isOrder = totalSlices;
}
file1.open(file1name.c_str(), ifstream::in);
file2.open(file2name.c_str(), ofstream::out);
if (!file1 || !file2) // if any one of files cannot open, return error, exit
{
cout << "Open file error!\n\n";
exit(1);
}
// locate the slice [s-order]
int count = 0; // read line by line, increase count value when encounter line starts with "*", untile count = s-order
while (!file1.eof()) // do not stop reading untile the end of file1
{
if (count==isOrder)break;
char buf[MAX_CHARS_PER_LINE];
file1.getline(buf, MAX_CHARS_PER_LINE);
char* token[MAX_TOKENS_PER_LINE] = {0};
token[0] = strtok(buf, DELIMITER.c_str());
if(token[0] && token[0][0]=='*')count++;
}
//pass next 5 useless lines
string line;
for (int i =1; i<=5; i++)
{
getline(file1, line);
//file2 << line << "\n";
}
//extract the [s-order]th slice, 'result v.s. z'
getline(file1, line);
while (!line.empty())
{
file2 << line << "\n";
getline(file1, line);
}
//close opened files
file1.close();
file2.close();
}
if (zFlag)
{
if (dzPos)
{
string keystr1 = "delz";
string keystr2 = "xlamd";
double delz = findMainKeyValue(file1name, keystr1);
double xlamd = findMainKeyValue(file1name, keystr2);
izOrder = (int)(dzPos/(delz*xlamd)+1.0); // transform z-pos into z-record order
if (izOrder < 1) izOrder = 1;
if (izOrder > totalZentri) izOrder = totalZentri;
}
file1.open(file1name.c_str(), ifstream::in);
file2.open(file2name.c_str(), ofstream::out);
if (!file1 || !file2) // if any one of files cannot open, return error, exit
{
cout << "Open file error!\n\n";
exit(1);
}
int count = 0; // read line by line, increase count value when encounter line starts with "*", means find one slice record
while (!file1.eof()) // do not stop reading untile the end of file1
{
// locate the beginning of every slice [s-order]
while (1)
{
char buf[MAX_CHARS_PER_LINE];
file1.getline(buf, MAX_CHARS_PER_LINE);
char* token[MAX_TOKENS_PER_LINE] = {0};
token[0] = strtok(buf, DELIMITER.c_str());
if (token[0] && token[0][0]=='*')
{
count++;
break;
}
}
//pass next 5 useless lines
string line;
for (int i =1; i<=5; i++)
{
getline(file1, line);
//file2 << line << "\n";
}
//extract the [slice_order]th z-record, given that line_count = z_order
int line_count = 0;
getline(file1, line);
while (!line.empty())
{
line_count++;
if (line_count==izOrder) file2 << line << "\n";
getline(file1, line);
}
}
//close opened files
file1.close();
file2.close();
}
return 0;
}