1 |
|
2 |
/******************************************************* |
3 |
* |
4 |
* Copyright (c) 2003-2009 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 <escriptexport/EscriptDataset.h> |
15 |
#include <escriptexport/FinleyMesh.h> |
16 |
#include <escriptexport/DataVar.h> |
17 |
|
18 |
#if USE_SILO |
19 |
#include <silo.h> |
20 |
#endif |
21 |
|
22 |
#include <cstring> |
23 |
#include <fstream> |
24 |
|
25 |
using namespace std; |
26 |
using namespace escriptexport; |
27 |
|
28 |
string insertTimestep(const string& fString, int timeStep, int tsMultiplier) |
29 |
{ |
30 |
string s(fString); |
31 |
size_t pos; |
32 |
if ((pos = s.find("%")) != s.npos) { |
33 |
size_t endpos = pos+1; |
34 |
while (endpos<s.length() && s[endpos] != 'd') |
35 |
endpos++; |
36 |
string fmtStr = s.substr(pos, endpos-pos+1); |
37 |
char ts[255]; |
38 |
snprintf(ts, 255, fmtStr.c_str(), timeStep*tsMultiplier); |
39 |
s.replace(pos, endpos-pos+1, ts); |
40 |
} |
41 |
return s; |
42 |
} |
43 |
|
44 |
int main(int argc, char** argv) |
45 |
{ |
46 |
#if HAVE_MPI |
47 |
MPI_Init(&argc, &argv); |
48 |
#endif |
49 |
|
50 |
#if USE_SILO |
51 |
// turn off for debugging purposes |
52 |
bool writeMultiMesh = true; |
53 |
|
54 |
// whether time-varying datasets should use the same mesh (from T=0) |
55 |
// TODO: Add a command line option for this |
56 |
bool writeMeshOnce = true; |
57 |
|
58 |
if (argc < 2) { |
59 |
cerr << "Usage: " << argv[0] << " <file.esd>" << endl; fflush(stderr); |
60 |
return -1; |
61 |
} |
62 |
|
63 |
string esdFile(argv[1]); |
64 |
|
65 |
ifstream in(esdFile.c_str()); |
66 |
if (!in.is_open()) { |
67 |
cerr << "Could not open " << esdFile << "." << endl; |
68 |
return -1; |
69 |
} |
70 |
|
71 |
// first line (header) should be "#escript datafile Vx.y" |
72 |
char line[256]; |
73 |
in.getline(line, 256); |
74 |
int major, minor; |
75 |
if (sscanf(line, "#escript datafile V%d.%d", &major, &minor) != 2) { |
76 |
cerr << esdFile << " is not a valid escript datafile." << endl; |
77 |
in.close(); |
78 |
return -1; |
79 |
} |
80 |
|
81 |
int nParts=0, nTimesteps=1, tsMultiplier=1; |
82 |
string meshFile; |
83 |
StringVec varFiles; |
84 |
StringVec varNames; |
85 |
|
86 |
while (in.good()) { |
87 |
in.getline(line, 256); |
88 |
if (line[0] == '#' || strlen(line) == 0) |
89 |
continue; |
90 |
int iVal; |
91 |
char sVal[256]; |
92 |
if (sscanf(line, "N=%d", &iVal) == 1) |
93 |
nParts = iVal; |
94 |
else if (sscanf(line, "T=%d", &iVal) == 1) |
95 |
nTimesteps = iVal; |
96 |
else if (sscanf(line, "DT=%d", &iVal) == 1) |
97 |
tsMultiplier = iVal; |
98 |
else if (sscanf(line, "M=%s", sVal) == 1) |
99 |
meshFile = sVal; |
100 |
else if (sscanf(line, "V=%s", sVal) == 1 && strchr(sVal, ':')) { |
101 |
// split into filename and variable name |
102 |
char* colon = strchr(sVal, ':'); |
103 |
*colon = 0; |
104 |
varFiles.push_back(sVal); |
105 |
varNames.push_back(colon+1); |
106 |
} else { |
107 |
cerr << esdFile << " is not a valid escript datafile." << endl; |
108 |
in.close(); |
109 |
return -1; |
110 |
} |
111 |
} |
112 |
|
113 |
in.close(); |
114 |
|
115 |
if (nParts < 1 || meshFile == "" || nTimesteps < 1 || tsMultiplier < 1) { |
116 |
cerr << esdFile << " is not a valid escript datafile." << endl; |
117 |
return -1; |
118 |
} |
119 |
|
120 |
cout << "Converting " << esdFile << "..." << endl; |
121 |
|
122 |
MeshBlocks meshFromTzero; |
123 |
|
124 |
// load and save all timesteps |
125 |
for (int timeStep = 0; timeStep < nTimesteps; timeStep++) { |
126 |
StringVec varFilesTS; |
127 |
StringVec::const_iterator it; |
128 |
|
129 |
// look for "%d" in filename and replace by timestep*multiplier if found |
130 |
for (it=varFiles.begin(); it!=varFiles.end(); it++) { |
131 |
string v = insertTimestep(*it, timeStep, tsMultiplier); |
132 |
if (nParts > 1) |
133 |
v.append(".nc.%04d"); |
134 |
else |
135 |
v.append(".nc"); |
136 |
varFilesTS.push_back(v); |
137 |
} |
138 |
|
139 |
if (nTimesteps > 1) |
140 |
cout << "T = " << timeStep << endl; |
141 |
|
142 |
EscriptDataset* ds = new EscriptDataset(); |
143 |
|
144 |
if (writeMeshOnce && timeStep > 0) { |
145 |
if (!ds->loadNetCDF(meshFromTzero, varFilesTS, varNames)) { |
146 |
delete ds; |
147 |
break; |
148 |
} |
149 |
} else { |
150 |
string meshTS = insertTimestep(meshFile, timeStep, tsMultiplier); |
151 |
if (nParts > 1) |
152 |
meshTS.append(".nc.%04d"); |
153 |
else |
154 |
meshTS.append(".nc"); |
155 |
|
156 |
if (!ds->loadNetCDF(meshTS, varFilesTS, varNames, nParts)) { |
157 |
delete ds; |
158 |
break; |
159 |
} |
160 |
} |
161 |
|
162 |
string baseName(esdFile); |
163 |
size_t dot = baseName.rfind('.'); |
164 |
if (dot != baseName.npos) |
165 |
baseName.erase(dot, baseName.length()-dot); |
166 |
|
167 |
ostringstream siloFile; |
168 |
siloFile << baseName; |
169 |
if (nTimesteps > 1) |
170 |
siloFile << "." << timeStep; |
171 |
siloFile << ".silo"; |
172 |
|
173 |
ds->setCycleAndTime(timeStep, (double)timeStep); |
174 |
ds->saveSilo(siloFile.str(), writeMultiMesh); |
175 |
|
176 |
// keep mesh from first timestep if it should be reused |
177 |
if (writeMeshOnce && nTimesteps > 1 && timeStep == 0) { |
178 |
meshFromTzero = ds->extractMesh(); |
179 |
meshFile = siloFile.str(); |
180 |
MeshBlocks::iterator meshIt; |
181 |
for (meshIt = meshFromTzero.begin(); meshIt != meshFromTzero.end(); |
182 |
meshIt++) |
183 |
{ |
184 |
// Prepend Silo mesh paths with the filename of the mesh to be |
185 |
// used |
186 |
string fullSiloPath = meshFile + string(":"); |
187 |
fullSiloPath += (*meshIt)->getSiloPath(); |
188 |
(*meshIt)->setSiloPath(fullSiloPath); |
189 |
} |
190 |
} |
191 |
|
192 |
delete ds; |
193 |
} |
194 |
|
195 |
// clean up |
196 |
MeshBlocks::iterator meshIt; |
197 |
|
198 |
cout << "All done." << endl; |
199 |
#else // !USE_SILO |
200 |
cerr << "Error: escriptconvert was compiled without Silo support! Exiting." |
201 |
<< endl; |
202 |
#endif |
203 |
|
204 |
#if HAVE_MPI |
205 |
MPI_Finalize(); |
206 |
#endif |
207 |
|
208 |
return 0; |
209 |
} |
210 |
|