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

Annotation of /trunk/tools/escriptconvert/escriptconvert.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2845 - (hide annotations)
Fri Jan 15 03:10:42 2010 UTC (9 years, 8 months ago) by caltinay
File size: 6133 byte(s)
escriptconvert passes MPI communicator to EscriptDataset now so files are
read/written in parallel.

1 caltinay 2183
2     /*******************************************************
3     *
4 jfenwick 2548 * Copyright (c) 2003-2009 by University of Queensland
5 caltinay 2183 * 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 caltinay 2810 #include <escriptexport/EscriptDataset.h>
15     #include <escriptexport/FinleyMesh.h>
16     #include <escriptexport/DataVar.h>
17    
18     #if USE_SILO
19 caltinay 2183 #include <silo.h>
20 caltinay 2810 #endif
21    
22 caltinay 2183 #include <cstring>
23     #include <fstream>
24 caltinay 2813 #include <iostream>
25 caltinay 2183
26     using namespace std;
27 caltinay 2810 using namespace escriptexport;
28 caltinay 2183
29 caltinay 2525 string insertTimestep(const string& fString, int timeStep, int tsMultiplier)
30 caltinay 2194 {
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 caltinay 2525 snprintf(ts, 255, fmtStr.c_str(), timeStep*tsMultiplier);
40 caltinay 2194 s.replace(pos, endpos-pos+1, ts);
41     }
42     return s;
43     }
44    
45 caltinay 2183 int main(int argc, char** argv)
46     {
47 caltinay 2810 #if HAVE_MPI
48     MPI_Init(&argc, &argv);
49     #endif
50    
51     #if USE_SILO
52 caltinay 2183 // turn off for debugging purposes
53     bool writeMultiMesh = true;
54    
55     // whether time-varying datasets should use the same mesh (from T=0)
56 caltinay 2194 // TODO: Add a command line option for this
57 caltinay 2183 bool writeMeshOnce = true;
58    
59     if (argc < 2) {
60 caltinay 2810 cerr << "Usage: " << argv[0] << " <file.esd>" << endl; fflush(stderr);
61 caltinay 2183 return -1;
62     }
63    
64 caltinay 2194 string esdFile(argv[1]);
65    
66     ifstream in(esdFile.c_str());
67 caltinay 2183 if (!in.is_open()) {
68 caltinay 2194 cerr << "Could not open " << esdFile << "." << endl;
69 caltinay 2183 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 caltinay 2194 cerr << esdFile << " is not a valid escript datafile." << endl;
78 caltinay 2183 in.close();
79     return -1;
80     }
81    
82 caltinay 2525 int nParts=0, nTimesteps=1, tsMultiplier=1;
83 caltinay 2183 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 caltinay 2525 else if (sscanf(line, "DT=%d", &iVal) == 1)
98     tsMultiplier = iVal;
99 caltinay 2183 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 caltinay 2194 cerr << esdFile << " is not a valid escript datafile." << endl;
109 caltinay 2183 in.close();
110     return -1;
111     }
112     }
113    
114     in.close();
115    
116 caltinay 2525 if (nParts < 1 || meshFile == "" || nTimesteps < 1 || tsMultiplier < 1) {
117 caltinay 2194 cerr << esdFile << " is not a valid escript datafile." << endl;
118 caltinay 2183 return -1;
119     }
120    
121 caltinay 2194 cout << "Converting " << esdFile << "..." << endl;
122 caltinay 2183
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 caltinay 2525 // look for "%d" in filename and replace by timestep*multiplier if found
131 caltinay 2183 for (it=varFiles.begin(); it!=varFiles.end(); it++) {
132 caltinay 2525 string v = insertTimestep(*it, timeStep, tsMultiplier);
133 caltinay 2183 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 caltinay 2845 EscriptDataset* ds;
144     #if HAVE_MPI
145     ds = new EscriptDataset(MPI_COMM_WORLD);
146     #else
147     ds = new EscriptDataset();
148     #endif
149 caltinay 2183
150     if (writeMeshOnce && timeStep > 0) {
151 caltinay 2810 if (!ds->loadNetCDF(meshFromTzero, varFilesTS, varNames)) {
152 caltinay 2183 delete ds;
153     break;
154     }
155 caltinay 2194 } else {
156 caltinay 2525 string meshTS = insertTimestep(meshFile, timeStep, tsMultiplier);
157 caltinay 2194 if (nParts > 1)
158     meshTS.append(".nc.%04d");
159     else
160     meshTS.append(".nc");
161    
162 caltinay 2810 if (!ds->loadNetCDF(meshTS, varFilesTS, varNames, nParts)) {
163 caltinay 2194 delete ds;
164     break;
165     }
166 caltinay 2183 }
167    
168 caltinay 2194 string baseName(esdFile);
169 caltinay 2183 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 caltinay 2810 ds->setCycleAndTime(timeStep, (double)timeStep);
180     ds->saveSilo(siloFile.str(), writeMultiMesh);
181 caltinay 2183
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 caltinay 2810 #else // !USE_SILO
206     cerr << "Error: escriptconvert was compiled without Silo support! Exiting."
207     << endl;
208     #endif
209 caltinay 2183
210 caltinay 2810 #if HAVE_MPI
211     MPI_Finalize();
212     #endif
213    
214 caltinay 2183 return 0;
215     }
216    

  ViewVC Help
Powered by ViewVC 1.1.26