File: escriptconvert.cpp

package info (click to toggle)
python-escript 5.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 144,252 kB
  • sloc: python: 592,062; cpp: 136,909; ansic: 18,675; javascript: 9,411; xml: 3,384; sh: 740; makefile: 203
file content (272 lines) | stat: -rw-r--r-- 7,508 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272

/*****************************************************************************
*
* Copyright (c) 2003-2020 by The University of Queensland
* http://www.uq.edu.au
*
* Primary Business: Queensland, Australia
* Licensed under the Apache License, version 2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Development until 2012 by Earth Systems Science Computational Center (ESSCC)
* Development 2012-2013 by School of Earth Sciences
* Development from 2014-2017 by Centre for Geoscience Computing (GeoComp)
* Development from 2019 by School of Earth and Environmental Sciences
**
*****************************************************************************/

#include <weipa/EscriptDataset.h>
#include <weipa/DataVar.h>

#ifdef ESYS_HAVE_SILO
#include <silo.h>
#endif

#include <cstring>
#include <fstream>
#include <iostream>
#include <sstream>

using namespace std;
using namespace weipa;

string insertTimestep(const string& fString, int timeStep, int tsMultiplier)
{
    string s(fString);
    size_t pos;
    if ((pos = s.find("%")) != s.npos) {
        size_t endpos = pos+1;
        while (endpos<s.length() && s[endpos] != 'd')
            endpos++;
        string fmtStr = s.substr(pos, endpos-pos+1);
        char ts[255];
        sprintf(ts, fmtStr.c_str(), timeStep*tsMultiplier);
        s.replace(pos, endpos-pos+1, ts);
    }
    return s;
}

int usage()
{
#ifdef ESYS_HAVE_SILO
    cerr << "Usage: escriptconvert {-vtk|-silo} <file.esd>" << endl;
#else
    cerr << "Note: escriptconvert was compiled without Silo support!" << endl;
    cerr << "Usage: escriptconvert [-vtk] <file.esd>" << endl;
#endif
    fflush(stderr);
    return -1;
}

void cleanup()
{
#if HAVE_MPI
    MPI_Finalize();
#endif
}

int main(int argc, char** argv)
{
#if HAVE_MPI
    MPI_Init(&argc, &argv);
#endif

    // turn off for debugging purposes
    bool writeMultiMesh = true;

    // whether time-varying datasets should use the same domain (from T=0)
    // TODO: Add a command line option for this
    bool writeDomainOnce = true;
    bool doVTK = false, doSilo = false;
    string esdFile;

#if ESYS_HAVE_SILO
    if (argc != 3) {
        cleanup();
        return usage();
    }

    if (!strcmp(argv[1], "-vtk")) {
        doVTK = true;
    } else if (!strcmp(argv[1], "-silo")) {
        doSilo = true;
    } else {
        cleanup();
        return usage();
    }
    esdFile = string(argv[2]);
    
#else // !ESYS_HAVE_SILO
    if (argc == 2) {
        esdFile = string(argv[1]);
    } else if (argc == 3) {
        if (strcmp(argv[1], "-vtk")) {
            cleanup();
            return usage();
        }
        esdFile = string(argv[2]);
    } else {
        cleanup();
        return usage();
    }
    doVTK = true;
#endif

    if (!doVTK && !doSilo) {
        cleanup();
        return usage();
    }
    
    ifstream in(esdFile.c_str());
    if (!in.is_open()) {
        cerr << "Could not open " << esdFile << "." << endl;
        cleanup();
        return -1;
    }

    // first line (header) should be "#escript datafile Vx.y"
    char line[256];
    in.getline(line, 256);
    int major, minor;
    if (sscanf(line, "#escript datafile V%d.%d", &major, &minor) != 2) {
        cerr << esdFile << " is not a valid escript datafile." << endl;
        in.close();
        cleanup();
        return -1;
    }

    int nParts=0, nTimesteps=1, tsMultiplier=1;
    string domainFile;
    StringVec varFiles;
    StringVec varNames;

    while (in.good()) {
        in.getline(line, 256);
        if (line[0] == '#' || strlen(line) == 0)
            continue;
        int iVal;
        char sVal[256];
        if (sscanf(line, "N=%d", &iVal) == 1)
            nParts = iVal;
        else if (sscanf(line, "T=%d", &iVal) == 1)
            nTimesteps = iVal;
        else if (sscanf(line, "DT=%d", &iVal) == 1)
            tsMultiplier = iVal;
        else if (sscanf(line, "M=%s", sVal) == 1)
            domainFile = sVal;
        else if (sscanf(line, "V=%s", sVal) == 1 && strchr(sVal, ':')) {
            // split into filename and variable name
            char* colon = strchr(sVal, ':');
            *colon = 0;
            varFiles.push_back(sVal);
            varNames.push_back(colon+1);
        } else {
            cerr << esdFile << " is not a valid escript datafile." << endl;
            in.close();
            cleanup();
            return -1;
        }
    }

    in.close();
    
    if (nParts < 1 || domainFile == "" || nTimesteps < 1 || tsMultiplier < 1) {
        cerr << esdFile << " is not a valid escript datafile." << endl;
        cleanup();
        return -1;
    }

    cout << "Converting " << esdFile << "..." << endl;

    DomainChunks domainFromTzero;

    // load and save all timesteps
    for (int timeStep = 0; timeStep < nTimesteps; timeStep++) {
        StringVec varFilesTS;
        StringVec::const_iterator it;

        // look for "%d" in filename and replace by timestep*multiplier if found
        for (it=varFiles.begin(); it!=varFiles.end(); it++) {
            string v = insertTimestep(*it, timeStep, tsMultiplier);
            if (nParts > 1)
                v.append(".nc.%04d");
            else
                v.append(".nc");
            varFilesTS.push_back(v);
        }

        if (nTimesteps > 1)
            cout << "T = " << timeStep << endl;

        EscriptDataset* ds;
#if HAVE_MPI
        ds = new EscriptDataset(MPI_COMM_WORLD);
#else
        ds = new EscriptDataset();
#endif

        if (writeDomainOnce && timeStep > 0) {
            if (!ds->loadNetCDF(domainFromTzero, varFilesTS, varNames)) {
                delete ds;
                break;
            }
        } else {
            string domainTS = insertTimestep(domainFile, timeStep, tsMultiplier);
            if (nParts > 1)
                domainTS.append(".nc.%04d");
            else
                domainTS.append(".nc");

            if (!ds->loadNetCDF(domainTS, varFilesTS, varNames, nParts)) {
                delete ds;
                break;
            }
        }

        string baseName(esdFile);
        size_t dot = baseName.rfind('.');
        if (dot != baseName.npos)
            baseName.erase(dot, baseName.length()-dot);

        ds->setCycleAndTime(timeStep, (double)timeStep);

        ostringstream outFilename;
        outFilename << baseName;
        if (nTimesteps > 1)
            outFilename << "." << timeStep;
        if (doSilo) {
            outFilename << ".silo";
            ds->saveSilo(outFilename.str(), writeMultiMesh);
        } else {
            outFilename << ".vtu";
            ds->saveVTK(outFilename.str());
        }

        // keep domain from first timestep if it should be reused
        if (writeDomainOnce && nTimesteps > 1 && timeStep == 0) {
            domainFromTzero = ds->getConvertedDomain();
            domainFile = outFilename.str();
            DomainChunks::iterator domIt;
            if (doSilo) {
                for (domIt = domainFromTzero.begin();
                        domIt != domainFromTzero.end();
                        domIt++)
                {
                    // Prepend Silo mesh paths with the filename of the mesh
                    // to be used
                    string fullSiloPath = domainFile + string(":");
                    fullSiloPath += (*domIt)->getSiloPath();
                    (*domIt)->setSiloPath(fullSiloPath);
                }
            }
        }

        delete ds;
    }
    
    cout << "All done." << endl;
    cleanup();

    return 0;
}