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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2910 - (show annotations)
Wed Feb 3 03:22:31 2010 UTC (9 years, 6 months ago) by caltinay
File size: 6152 byte(s)
Added a libescriptreader.a target which does not depend on escript or finley
at runtime. This will be used for the VisIt plugin so is not built by default.

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

  ViewVC Help
Powered by ViewVC 1.1.26