1 |
|
2 |
/******************************************************* |
3 |
* |
4 |
* Copyright (c) 2003-2010 by University of Queensland |
5 |
* Earth Systems Science Computational Center (ESSCC) |
6 |
* http://www.uq.edu.au/esscc |
7 |
* |
8 |
* Primary Business: Queensland, Australia |
9 |
* Licensed under the Open Software License version 3.0 |
10 |
* http://www.opensource.org/licenses/osl-3.0.php |
11 |
* |
12 |
*******************************************************/ |
13 |
|
14 |
#include <weipa/EscriptDataset.h> |
15 |
#include <weipa/DataVar.h> |
16 |
|
17 |
#if USE_SILO |
18 |
#include <silo.h> |
19 |
#endif |
20 |
|
21 |
#include <cstring> |
22 |
#include <fstream> |
23 |
#include <iostream> |
24 |
#include <sstream> |
25 |
|
26 |
using namespace std; |
27 |
using namespace weipa; |
28 |
|
29 |
string insertTimestep(const string& fString, int timeStep, int tsMultiplier) |
30 |
{ |
31 |
string s(fString); |
32 |
size_t pos; |
33 |
if ((pos = s.find("%")) != s.npos) { |
34 |
size_t endpos = pos+1; |
35 |
while (endpos<s.length() && s[endpos] != 'd') |
36 |
endpos++; |
37 |
string fmtStr = s.substr(pos, endpos-pos+1); |
38 |
char ts[255]; |
39 |
sprintf(ts, fmtStr.c_str(), timeStep*tsMultiplier); |
40 |
s.replace(pos, endpos-pos+1, ts); |
41 |
} |
42 |
return s; |
43 |
} |
44 |
|
45 |
int usage() |
46 |
{ |
47 |
#if USE_SILO |
48 |
cerr << "Usage: escriptconvert {-vtk|-silo} <file.esd>" << endl; |
49 |
#else |
50 |
cerr << "Note: escriptconvert was compiled without Silo support!" << endl; |
51 |
cerr << "Usage: escriptconvert [-vtk] <file.esd>" << endl; |
52 |
#endif |
53 |
fflush(stderr); |
54 |
return -1; |
55 |
} |
56 |
|
57 |
void cleanup() |
58 |
{ |
59 |
#if HAVE_MPI |
60 |
MPI_Finalize(); |
61 |
#endif |
62 |
} |
63 |
|
64 |
int main(int argc, char** argv) |
65 |
{ |
66 |
#if HAVE_MPI |
67 |
MPI_Init(&argc, &argv); |
68 |
#endif |
69 |
|
70 |
// turn off for debugging purposes |
71 |
bool writeMultiMesh = true; |
72 |
|
73 |
// whether time-varying datasets should use the same domain (from T=0) |
74 |
// TODO: Add a command line option for this |
75 |
bool writeDomainOnce = true; |
76 |
bool doVTK = false, doSilo = false; |
77 |
string esdFile; |
78 |
|
79 |
#if USE_SILO |
80 |
if (argc != 3) { |
81 |
cleanup(); |
82 |
return usage(); |
83 |
} |
84 |
|
85 |
if (!strcmp(argv[1], "-vtk")) { |
86 |
doVTK = true; |
87 |
} else if (!strcmp(argv[1], "-silo")) { |
88 |
doSilo = true; |
89 |
} else { |
90 |
cleanup(); |
91 |
return usage(); |
92 |
} |
93 |
esdFile = string(argv[2]); |
94 |
|
95 |
#else // !USE_SILO |
96 |
if (argc == 2) { |
97 |
esdFile = string(argv[1]); |
98 |
} else if (argc == 3) { |
99 |
if (strcmp(argv[1], "-vtk")) { |
100 |
cleanup(); |
101 |
return usage(); |
102 |
} |
103 |
esdFile = string(argv[2]); |
104 |
} else { |
105 |
cleanup(); |
106 |
return usage(); |
107 |
} |
108 |
doVTK = true; |
109 |
#endif |
110 |
|
111 |
if (!doVTK && !doSilo) { |
112 |
cleanup(); |
113 |
return usage(); |
114 |
} |
115 |
|
116 |
ifstream in(esdFile.c_str()); |
117 |
if (!in.is_open()) { |
118 |
cerr << "Could not open " << esdFile << "." << endl; |
119 |
cleanup(); |
120 |
return -1; |
121 |
} |
122 |
|
123 |
// first line (header) should be "#escript datafile Vx.y" |
124 |
char line[256]; |
125 |
in.getline(line, 256); |
126 |
int major, minor; |
127 |
if (sscanf(line, "#escript datafile V%d.%d", &major, &minor) != 2) { |
128 |
cerr << esdFile << " is not a valid escript datafile." << endl; |
129 |
in.close(); |
130 |
cleanup(); |
131 |
return -1; |
132 |
} |
133 |
|
134 |
int nParts=0, nTimesteps=1, tsMultiplier=1; |
135 |
string domainFile; |
136 |
StringVec varFiles; |
137 |
StringVec varNames; |
138 |
|
139 |
while (in.good()) { |
140 |
in.getline(line, 256); |
141 |
if (line[0] == '#' || strlen(line) == 0) |
142 |
continue; |
143 |
int iVal; |
144 |
char sVal[256]; |
145 |
if (sscanf(line, "N=%d", &iVal) == 1) |
146 |
nParts = iVal; |
147 |
else if (sscanf(line, "T=%d", &iVal) == 1) |
148 |
nTimesteps = iVal; |
149 |
else if (sscanf(line, "DT=%d", &iVal) == 1) |
150 |
tsMultiplier = iVal; |
151 |
else if (sscanf(line, "M=%s", sVal) == 1) |
152 |
domainFile = sVal; |
153 |
else if (sscanf(line, "V=%s", sVal) == 1 && strchr(sVal, ':')) { |
154 |
// split into filename and variable name |
155 |
char* colon = strchr(sVal, ':'); |
156 |
*colon = 0; |
157 |
varFiles.push_back(sVal); |
158 |
varNames.push_back(colon+1); |
159 |
} else { |
160 |
cerr << esdFile << " is not a valid escript datafile." << endl; |
161 |
in.close(); |
162 |
cleanup(); |
163 |
return -1; |
164 |
} |
165 |
} |
166 |
|
167 |
in.close(); |
168 |
|
169 |
if (nParts < 1 || domainFile == "" || nTimesteps < 1 || tsMultiplier < 1) { |
170 |
cerr << esdFile << " is not a valid escript datafile." << endl; |
171 |
cleanup(); |
172 |
return -1; |
173 |
} |
174 |
|
175 |
cout << "Converting " << esdFile << "..." << endl; |
176 |
|
177 |
DomainChunks domainFromTzero; |
178 |
|
179 |
// load and save all timesteps |
180 |
for (int timeStep = 0; timeStep < nTimesteps; timeStep++) { |
181 |
StringVec varFilesTS; |
182 |
StringVec::const_iterator it; |
183 |
|
184 |
// look for "%d" in filename and replace by timestep*multiplier if found |
185 |
for (it=varFiles.begin(); it!=varFiles.end(); it++) { |
186 |
string v = insertTimestep(*it, timeStep, tsMultiplier); |
187 |
if (nParts > 1) |
188 |
v.append(".nc.%04d"); |
189 |
else |
190 |
v.append(".nc"); |
191 |
varFilesTS.push_back(v); |
192 |
} |
193 |
|
194 |
if (nTimesteps > 1) |
195 |
cout << "T = " << timeStep << endl; |
196 |
|
197 |
EscriptDataset* ds; |
198 |
#if HAVE_MPI |
199 |
ds = new EscriptDataset(MPI_COMM_WORLD); |
200 |
#else |
201 |
ds = new EscriptDataset(); |
202 |
#endif |
203 |
|
204 |
if (writeDomainOnce && timeStep > 0) { |
205 |
if (!ds->loadNetCDF(domainFromTzero, varFilesTS, varNames)) { |
206 |
delete ds; |
207 |
break; |
208 |
} |
209 |
} else { |
210 |
string domainTS = insertTimestep(domainFile, timeStep, tsMultiplier); |
211 |
if (nParts > 1) |
212 |
domainTS.append(".nc.%04d"); |
213 |
else |
214 |
domainTS.append(".nc"); |
215 |
|
216 |
if (!ds->loadNetCDF(domainTS, varFilesTS, varNames, nParts)) { |
217 |
delete ds; |
218 |
break; |
219 |
} |
220 |
} |
221 |
|
222 |
string baseName(esdFile); |
223 |
size_t dot = baseName.rfind('.'); |
224 |
if (dot != baseName.npos) |
225 |
baseName.erase(dot, baseName.length()-dot); |
226 |
|
227 |
ds->setCycleAndTime(timeStep, (double)timeStep); |
228 |
|
229 |
ostringstream outFilename; |
230 |
outFilename << baseName; |
231 |
if (nTimesteps > 1) |
232 |
outFilename << "." << timeStep; |
233 |
if (doSilo) { |
234 |
outFilename << ".silo"; |
235 |
ds->saveSilo(outFilename.str(), writeMultiMesh); |
236 |
} else { |
237 |
outFilename << ".vtu"; |
238 |
ds->saveVTK(outFilename.str()); |
239 |
} |
240 |
|
241 |
// keep domain from first timestep if it should be reused |
242 |
if (writeDomainOnce && nTimesteps > 1 && timeStep == 0) { |
243 |
domainFromTzero = ds->getConvertedDomain(); |
244 |
domainFile = outFilename.str(); |
245 |
DomainChunks::iterator domIt; |
246 |
if (doSilo) { |
247 |
for (domIt = domainFromTzero.begin(); |
248 |
domIt != domainFromTzero.end(); |
249 |
domIt++) |
250 |
{ |
251 |
// Prepend Silo mesh paths with the filename of the mesh |
252 |
// to be used |
253 |
string fullSiloPath = domainFile + string(":"); |
254 |
fullSiloPath += (*domIt)->getSiloPath(); |
255 |
(*domIt)->setSiloPath(fullSiloPath); |
256 |
} |
257 |
} |
258 |
} |
259 |
|
260 |
delete ds; |
261 |
} |
262 |
|
263 |
cout << "All done." << endl; |
264 |
cleanup(); |
265 |
|
266 |
return 0; |
267 |
} |
268 |
|