/[escript]/trunk/tools/escriptconvert/escriptconvert.cpp
ViewVC logotype

Contents of /trunk/tools/escriptconvert/escriptconvert.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6939 - (show annotations)
Mon Jan 20 03:37:18 2020 UTC (22 months, 1 week ago) by uqaeller
File size: 7508 byte(s)
Updated the copyright header.


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

  ViewVC Help
Powered by ViewVC 1.1.26