File: avtAUXFileFileFormat.C

package info (click to toggle)
paraview 4.0.1-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 526,572 kB
  • sloc: cpp: 2,284,430; ansic: 816,374; python: 239,936; xml: 70,162; tcl: 48,295; fortran: 39,116; yacc: 5,466; java: 3,518; perl: 3,107; lex: 1,620; sh: 1,555; makefile: 932; asm: 471; pascal: 228
file content (378 lines) | stat: -rw-r--r-- 11,010 bytes parent folder | download | duplicates (4)
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*****************************************************************************
*
* Copyright (c) 2000 - 2010, Lawrence Livermore National Security, LLC
* Produced at the Lawrence Livermore National Laboratory
* LLNL-CODE-400124
* All rights reserved.
*
* This file is  part of VisIt. For  details, see https://visit.llnl.gov/.  The
* full copyright notice is contained in the file COPYRIGHT located at the root
* of the VisIt distribution or at http://www.llnl.gov/visit/copyright.html.
*
* Redistribution  and  use  in  source  and  binary  forms,  with  or  without
* modification, are permitted provided that the following conditions are met:
*
*  - Redistributions of  source code must  retain the above  copyright notice,
*    this list of conditions and the disclaimer below.
*  - Redistributions in binary form must reproduce the above copyright notice,
*    this  list of  conditions  and  the  disclaimer (as noted below)  in  the
*    documentation and/or other materials provided with the distribution.
*  - Neither the name of  the LLNS/LLNL nor the names of  its contributors may
*    be used to endorse or promote products derived from this software without
*    specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT  HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR  IMPLIED WARRANTIES, INCLUDING,  BUT NOT  LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE
* ARE  DISCLAIMED. IN  NO EVENT  SHALL LAWRENCE  LIVERMORE NATIONAL  SECURITY,
* LLC, THE  U.S.  DEPARTMENT OF  ENERGY  OR  CONTRIBUTORS BE  LIABLE  FOR  ANY
* DIRECT,  INDIRECT,   INCIDENTAL,   SPECIAL,   EXEMPLARY,  OR   CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT  LIMITED TO, PROCUREMENT OF  SUBSTITUTE GOODS OR
* SERVICES; LOSS OF  USE, DATA, OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER
* CAUSED  AND  ON  ANY  THEORY  OF  LIABILITY,  WHETHER  IN  CONTRACT,  STRICT
* LIABILITY, OR TORT  (INCLUDING NEGLIGENCE OR OTHERWISE)  ARISING IN ANY  WAY
* OUT OF THE  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*****************************************************************************/

// ************************************************************************* //
//                            avtAUXFileFileFormat.C                             //
// ************************************************************************* //

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>


#include <avtAUXFileFileFormat.h>

#include <string>

#include <vtkFloatArray.h>
#include <vtkRectilinearGrid.h>

#include <avtDatabaseMetaData.h>

#include <Expression.h>
#include <StringHelpers.h>
#include <avtDatabase.h>

#include <InvalidVariableException.h>
#include <InvalidFilesException.h>

#include <FileFunctions.h>

using     std::string;


// ****************************************************************************
//  Method: avtAUXFile constructor
//
//  Programmer: miller -- generated by xml2avt
//  Creation:   Tue Mar 15 08:29:20 PDT 2005
//
//  Modifications:
//    Jeremy Meredith, Thu Jan  7 12:30:10 EST 2010
//    Refactored out file reading.  Initialize the flag for it to false.
//
// ****************************************************************************

avtAUXFileFileFormat::avtAUXFileFileFormat(const char *filename)
    : avtSTSDFileFormat(filename)
{
    fileName = filename;
    fileBuf = 0;
    sizeX = -1;
    sizeY = -1;
    fluence = 0;
    fileRead = false;
}


// ****************************************************************************
//  Method: avtAUXFileFileFormat::FreeUpResources
//
//  Purpose:
//      When VisIt is done focusing on a particular timestep, it asks that
//      timestep to free up any resources (memory, file descriptors) that
//      it has associated with it.  This method is the mechanism for doing
//      that.
//
//  Programmer: miller -- generated by xml2avt
//  Creation:   Tue Mar 15 08:29:20 PDT 2005
//
// ****************************************************************************

void
avtAUXFileFileFormat::FreeUpResources(void)
{
    if (fileBuf)
    {
        delete [] fileBuf;
        fileBuf = 0;
    }
    if (fluence)
    {
        fluence->Delete();
        fluence = 0;
    }
}


// ****************************************************************************
//  Method: avtAUXFileFileFormat::PopulateDatabaseMetaData
//
//  Purpose:
//      This database meta-data object is like a table of contents for the
//      file.  By populating it, you are telling the rest of VisIt what
//      information it can request from you.
//
//  Programmer: miller -- generated by xml2avt
//  Creation:   Tue Mar 15 08:29:20 PDT 2005
//
//  Modifications:
//    Jeremy Meredith, Thu Jan  7 12:30:10 EST 2010
//    Refactored out file reading.  Read it before populating the metadata.
//
// ****************************************************************************

void
avtAUXFileFileFormat::PopulateDatabaseMetaData(avtDatabaseMetaData *md)
{
    if (!fileRead)
        ReadFile();

    AddMeshToMetaData(md, "mesh", AVT_RECTILINEAR_MESH, NULL, 1, 0, 2, 2);

    AddScalarVarToMetaData(md, "fluence", "mesh", AVT_NODECENT);
}


// ****************************************************************************
//  Method: avtAUXFileFileFormat::GetMesh
//
//  Purpose:
//      Gets the mesh associated with this file.  The mesh is returned as a
//      derived type of vtkDataSet (ie vtkRectilinearGrid, vtkStructuredGrid,
//      vtkUnstructuredGrid, etc).
//
//  Arguments:
//      meshname    The name of the mesh of interest.  This can be ignored if
//                  there is only one mesh.
//
//  Programmer: miller -- generated by xml2avt
//  Creation:   Tue Mar 15 08:29:20 PDT 2005
//
//  Modifications:
//    Brad Whitlock, Fri Jun 17 14:53:02 PST 2005
//    Modified so it works on Windows.
//
//    Mark C. Miller, Thu Mar 30 16:45:35 PST 2006
//    Made it use VisItStat instead of stat
//
//    Jeremy Meredith, Thu Jan  7 12:31:54 EST 2010
//    Refactored out file reading code.
//
// ****************************************************************************

vtkDataSet *
avtAUXFileFileFormat::GetMesh(const char *meshname)
{
    if (!fileRead)
        ReadFile();

    int i, j;
    char dummyChar;
    int dummyInt;

    vtkFloatArray   *coords[3];

    const char *p = fileBuf;

    //
    // Line 1: scan size information
    //
    if (sscanf(p, "%d %d %d %d %d %d %d \n", &sizeX, &sizeY,
            &dummyInt, &dummyInt, &dummyInt, &dummyInt, &dummyInt) != 7)
    {
        EXCEPTION1(InvalidFilesException, fileName.c_str());
    }
    while (*p != '\n')
        p++;
    p++;

    //
    // Line 2: scan 'X='
    //
    if ((sscanf(p, "X%c\n", &dummyChar) != 1) || (dummyChar != '='))
    {
        EXCEPTION1(InvalidFilesException, fileName.c_str());
    }
    while (*p != '\n')
        p++;
    p++;

    //
    // scan X values
    //
    coords[0] = vtkFloatArray::New();
    coords[0]->SetNumberOfTuples(sizeX);
    for (i = 0; i < sizeX; i++)
    {
        float xVal;
        if (sscanf(p, "%f", &xVal) != 1)
        {
            EXCEPTION1(InvalidFilesException, fileName.c_str());
        }
        coords[0]->SetComponent(i, 0, xVal);
        while (isspace(*p))
            p++;
        while (!isspace(*p))
            p++;
    }
    while (*p != '\n')
        p++;
    p++;

    //
    // Loop to scan y values
    //
    coords[1] = vtkFloatArray::New();
    coords[1]->SetNumberOfTuples(sizeY);
    fluence = vtkFloatArray::New(); 
    fluence->SetNumberOfTuples(sizeX * sizeY);
    for (i = 0; i < sizeY; i++)
    {
        //
        // Line i'th Y entry: scan 'Y='
        //
        float yVal;
        if (sscanf(p, " Y= %f\n", &yVal) != 1)
        {
            EXCEPTION1(InvalidFilesException, fileName.c_str());
        }
        coords[1]->SetComponent(i, 0, yVal);
        while (*p != '\n')
            p++;
        p++;

        //
        // scan fluence values for this Y entry
        //
        for (j = 0; j < sizeX; j++)
        {
            float tmp;
            if (sscanf(p, "%f", &tmp) != 1)
            {
                EXCEPTION1(InvalidFilesException, fileName.c_str());
            }
            fluence->SetComponent(i * sizeX + j, 0, tmp);
            while (isspace(*p))
                p++;
            while (!isspace(*p))
                p++;
        }
        while (*p != '\n')
            p++;
        p++;
    }

    //
    // Deal with the z coordinate
    //
    coords[2] = vtkFloatArray::New();
    coords[2]->SetNumberOfTuples(1);
    coords[2]->SetComponent(0, 0, 0.0);

    //
    // Build rectilinear grid
    //
    vtkRectilinearGrid *rgrid = vtkRectilinearGrid::New();
    int dims[3] = {sizeX, sizeY, 1};
    rgrid->SetDimensions(dims);
    rgrid->SetXCoordinates(coords[0]);
    coords[0]->Delete();
    rgrid->SetYCoordinates(coords[1]);
    coords[1]->Delete();
    rgrid->SetZCoordinates(coords[2]);
    coords[2]->Delete();

    return rgrid;
}


// ****************************************************************************
//  Method: avtAUXFileFileFormat::GetVar
//
//  Purpose:
//      Gets a scalar variable associated with this file.  Although VTK has
//      support for many different types, the best bet is vtkFloatArray, since
//      that is supported everywhere through VisIt.
//
//  Arguments:
//      varname    The name of the variable requested.
//
//  Programmer: miller -- generated by xml2avt
//  Creation:   Tue Mar 15 08:29:20 PDT 2005
//
// ****************************************************************************

vtkDataArray *
avtAUXFileFileFormat::GetVar(const char *varname)
{
    return fluence;
}


// ****************************************************************************
// Method:  avtAUXFileFileFormat::ReadFile
//
// Purpose:
//   Read the file.
//
// Arguments:
//   none
//
// Programmer:  Jeremy Meredith
// Creation:    January  6, 2010
//
// ****************************************************************************
void
avtAUXFileFileFormat::ReadFile()
{
    //
    // Read the whole file's contents into a buffer
    //
    VisItStat_t statbuf;
    VisItStat(fileName.c_str(), &statbuf);
    VisItOff_t fileSize = statbuf.st_size;

    // for the metadata server, only check the first 10k bytes
    if (avtDatabase::OnlyServeUpMetaData())
        fileSize = 10000;

    FILE *fd = fopen(fileName.c_str(), "r");
    if(fd == 0)
    {
        EXCEPTION1(InvalidFilesException, fileName.c_str());
    }

    fileBuf = new char[fileSize];
    if (fread((void *)fileBuf, 1, fileSize, fd) != fileSize)
    {
        fclose(fd);
        delete [] fileBuf;
        fileBuf = 0;

        EXCEPTION1(InvalidFilesException, fileName.c_str());
    }
    fclose(fd);

    if (avtDatabase::OnlyServeUpMetaData())
    {
        if (!StringHelpers::IsPureASCII(fileBuf, fileSize))
            EXCEPTION2(InvalidFilesException, fileName.c_str(), "Not ASCII.");
    }
}