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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2881 - (show annotations)
Thu Jan 28 02:03:15 2010 UTC (9 years, 7 months ago) by jfenwick
File size: 6133 byte(s)
Don't panic.
Updating copyright stamps

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

  ViewVC Help
Powered by ViewVC 1.1.26