/[escript]/branches/stage3.1/tools/escriptconvert/escriptconvert.cpp
ViewVC logotype

Contents of /branches/stage3.1/tools/escriptconvert/escriptconvert.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2945 - (show annotations)
Wed Feb 24 00:17:46 2010 UTC (8 years, 11 months ago) by jfenwick
File size: 6998 byte(s)
Bringing release stage up to trunk version 2944

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

  ViewVC Help
Powered by ViewVC 1.1.26