/[escript]/trunk/tools/libescriptreader/src/escript2silo.cpp
ViewVC logotype

Contents of /trunk/tools/libescriptreader/src/escript2silo.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2183 - (show annotations)
Fri Dec 19 03:52:50 2008 UTC (11 years, 11 months ago) by caltinay
File size: 5367 byte(s)
Added escriptreader library with tools. For more information refer to README
file or wait for user documentation.

1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2008 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 //
15 // escript2silo.cpp
16 //
17 #include <escriptreader/MPDataSet.h>
18 #include <escriptreader/MeshWithElements.h>
19 #include <escriptreader/DataVar.h>
20 #include <silo.h>
21 #include <netcdf.hh>
22 #include <cstring>
23 #include <fstream>
24
25 using namespace std;
26
27
28 int main(int argc, char** argv)
29 {
30 // turn off for debugging purposes
31 bool writeMultiMesh = true;
32
33 // whether time-varying datasets should use the same mesh (from T=0)
34 bool writeMeshOnce = true;
35
36 if (argc < 2) {
37 cerr << "Usage: " << argv[0] << " <file.esd>" << endl;
38 return -1;
39 }
40
41 ifstream in(argv[1]);
42 if (!in.is_open()) {
43 cerr << "Could not open " << argv[1] << "." << endl;
44 return -1;
45 }
46
47 // first line (header) should be "#escript datafile Vx.y"
48 char line[256];
49 in.getline(line, 256);
50 int major, minor;
51 if (sscanf(line, "#escript datafile V%d.%d", &major, &minor) != 2) {
52 cerr << argv[1] << " is not a valid escript datafile." << endl;
53 in.close();
54 return -1;
55 }
56
57 int nParts=0, nTimesteps=1;
58 string meshFile;
59 StringVec varFiles;
60 StringVec varNames;
61
62 while (in.good()) {
63 in.getline(line, 256);
64 if (line[0] == '#' || strlen(line) == 0)
65 continue;
66 int iVal;
67 char sVal[256];
68 if (sscanf(line, "N=%d", &iVal) == 1)
69 nParts = iVal;
70 else if (sscanf(line, "T=%d", &iVal) == 1)
71 nTimesteps = iVal;
72 else if (sscanf(line, "M=%s", sVal) == 1)
73 meshFile = sVal;
74 else if (sscanf(line, "V=%s", sVal) == 1 && strchr(sVal, ':')) {
75 // split into filename and variable name
76 char* colon = strchr(sVal, ':');
77 *colon = 0;
78 varFiles.push_back(sVal);
79 varNames.push_back(colon+1);
80 } else {
81 cerr << argv[1] << " is not a valid escript datafile." << endl;
82 in.close();
83 return -1;
84 }
85 }
86
87 in.close();
88
89 if (nParts < 1 || meshFile == "" || nTimesteps < 1) {
90 cerr << argv[1] << " is not a valid escript datafile." << endl;
91 return -1;
92 }
93
94 cout << "Converting " << argv[1] << "..." << endl;
95 if (nParts > 1)
96 meshFile.append(".nc.%04d");
97 else
98 meshFile.append(".nc");
99
100 MeshBlocks meshFromTzero;
101
102 // load and save all timesteps
103 for (int timeStep = 0; timeStep < nTimesteps; timeStep++) {
104 StringVec varFilesTS;
105 StringVec::const_iterator it;
106
107 // look for "%d" in filename and replace by timestep if found
108 for (it=varFiles.begin(); it!=varFiles.end(); it++) {
109 string v(*it);
110 size_t pos;
111 if ((pos = v.find("%")) != v.npos) {
112 size_t endpos = pos+1;
113 while (endpos<v.length() && v[endpos] != 'd')
114 endpos++;
115 string fmtStr = v.substr(pos, endpos-pos+1);
116 char ts[255];
117 snprintf(ts, 255, fmtStr.c_str(), timeStep);
118 v.replace(pos, endpos-pos+1, ts);
119 }
120 if (nParts > 1)
121 v.append(".nc.%04d");
122 else
123 v.append(".nc");
124 varFilesTS.push_back(v);
125 }
126
127 if (nTimesteps > 1)
128 cout << "T = " << timeStep << endl;
129
130 MPDataSet* ds = new MPDataSet();
131
132 if (writeMeshOnce && timeStep > 0) {
133 if (!ds->load(meshFromTzero, meshFile, varFilesTS, varNames)) {
134 delete ds;
135 break;
136 }
137 } else if (!ds->load(meshFile, varFilesTS, varNames, nParts)) {
138 delete ds;
139 break;
140 }
141
142 string baseName(argv[1]);
143 size_t dot = baseName.rfind('.');
144 if (dot != baseName.npos)
145 baseName.erase(dot, baseName.length()-dot);
146
147 ostringstream siloFile;
148 siloFile << baseName;
149 if (nTimesteps > 1)
150 siloFile << "." << timeStep;
151 siloFile << ".silo";
152
153 ds->saveAsSilo(siloFile.str(), writeMultiMesh);
154
155 // keep mesh from first timestep if it should be reused
156 if (writeMeshOnce && nTimesteps > 1 && timeStep == 0) {
157 meshFromTzero = ds->extractMesh();
158 meshFile = siloFile.str();
159 MeshBlocks::iterator meshIt;
160 for (meshIt = meshFromTzero.begin(); meshIt != meshFromTzero.end();
161 meshIt++)
162 {
163 // Prepend Silo mesh paths with the filename of the mesh to be
164 // used
165 string fullSiloPath = meshFile + string(":");
166 fullSiloPath += (*meshIt)->getSiloPath();
167 (*meshIt)->setSiloPath(fullSiloPath);
168 }
169 }
170
171 delete ds;
172 }
173
174 // clean up
175 MeshBlocks::iterator meshIt;
176 for (meshIt = meshFromTzero.begin(); meshIt != meshFromTzero.end(); meshIt++)
177 delete *meshIt;
178
179 cout << "All done." << endl;
180
181 return 0;
182 }
183

  ViewVC Help
Powered by ViewVC 1.1.26