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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2813 - (show annotations)
Tue Dec 8 00:30:13 2009 UTC (9 years, 11 months ago) by caltinay
File size: 6046 byte(s)
Added missing include.

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 #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 = new EscriptDataset();
144
145 if (writeMeshOnce && timeStep > 0) {
146 if (!ds->loadNetCDF(meshFromTzero, varFilesTS, varNames)) {
147 delete ds;
148 break;
149 }
150 } else {
151 string meshTS = insertTimestep(meshFile, timeStep, tsMultiplier);
152 if (nParts > 1)
153 meshTS.append(".nc.%04d");
154 else
155 meshTS.append(".nc");
156
157 if (!ds->loadNetCDF(meshTS, varFilesTS, varNames, nParts)) {
158 delete ds;
159 break;
160 }
161 }
162
163 string baseName(esdFile);
164 size_t dot = baseName.rfind('.');
165 if (dot != baseName.npos)
166 baseName.erase(dot, baseName.length()-dot);
167
168 ostringstream siloFile;
169 siloFile << baseName;
170 if (nTimesteps > 1)
171 siloFile << "." << timeStep;
172 siloFile << ".silo";
173
174 ds->setCycleAndTime(timeStep, (double)timeStep);
175 ds->saveSilo(siloFile.str(), writeMultiMesh);
176
177 // keep mesh from first timestep if it should be reused
178 if (writeMeshOnce && nTimesteps > 1 && timeStep == 0) {
179 meshFromTzero = ds->extractMesh();
180 meshFile = siloFile.str();
181 MeshBlocks::iterator meshIt;
182 for (meshIt = meshFromTzero.begin(); meshIt != meshFromTzero.end();
183 meshIt++)
184 {
185 // Prepend Silo mesh paths with the filename of the mesh to be
186 // used
187 string fullSiloPath = meshFile + string(":");
188 fullSiloPath += (*meshIt)->getSiloPath();
189 (*meshIt)->setSiloPath(fullSiloPath);
190 }
191 }
192
193 delete ds;
194 }
195
196 // clean up
197 MeshBlocks::iterator meshIt;
198
199 cout << "All done." << endl;
200 #else // !USE_SILO
201 cerr << "Error: escriptconvert was compiled without Silo support! Exiting."
202 << endl;
203 #endif
204
205 #if HAVE_MPI
206 MPI_Finalize();
207 #endif
208
209 return 0;
210 }
211

  ViewVC Help
Powered by ViewVC 1.1.26