avtPFLOTRANFileFormat.C 43.3 KB
Newer Older
1 2
/*****************************************************************************
*
brugger's avatar
 
brugger committed
3
* Copyright (c) 2000 - 2011, Lawrence Livermore National Security, LLC
4
* Produced at the Lawrence Livermore National Laboratory
brugger's avatar
 
brugger committed
5
* LLNL-CODE-442911
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
* 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.
*
*****************************************************************************/

// ************************************************************************* //
ahern's avatar
ahern committed
40
//                            avtPFLOTRANFileFormat.C                           //
41 42
// ************************************************************************* //

ahern's avatar
ahern committed
43
#include <avtPFLOTRANFileFormat.h>
44 45

#include <DebugStream.h>
bonnell's avatar
bonnell committed
46
#include <snprintf.h>
47 48 49 50

#include <vtkDoubleArray.h>
#include <vtkRectilinearGrid.h>
#include <vtkUnstructuredGrid.h>
51 52 53 54
#include <vtkUnsignedCharArray.h>
#include <vtkIdList.h>
#include <vtkCellData.h>
#include <vtkIntArray.h>
55 56

#include <avtDatabaseMetaData.h>
57
#include <avtDatabase.h>
58
#include <avtMaterial.h>
59 60 61 62 63 64 65 66

#include <InvalidDBTypeException.h>

#include <DBOptionsAttributes.h>
#include <Expression.h>

#include <InvalidVariableException.h>

67 68 69 70
#ifdef PARALLEL
#include <avtParallel.h>
#endif

71 72 73 74 75
#include <algorithm>
#include <map>
#include <string>
#include <vector>

76 77
#include <visit-hdf5.h>

78
using std::map;
79
using std::string;
80
using std::vector;
81

82 83
static string coordNames[3] = { "X [m]", "Y [m]", "Z [m]" };
static string vecNames[3] = { "X-", "Y-", "Z-" };
84 85

// ****************************************************************************
ahern's avatar
ahern committed
86
//  Method: avtPFLOTRANFileFormat constructor
87
//
88
//  Programmer: Sean Ahern
89 90
//  Creation:   Thu Apr 24 14:00:58 PST 2008
//
91 92 93 94 95 96
//  Modifications:
//    Jeremy Meredith, Fri Apr  3 12:26:26 EDT 2009
//    All variables are supposed to be cell-centered.
//    Add support for old-style PFLOTRAN files where the coordinate
//    arrays represented cell centers.
//
97 98 99
//    Jeremy Meredith, Wed Mar 30 12:57:00 EDT 2011
//    Initialize various HDF5 ids to -1.
//
100 101
// ****************************************************************************

ahern's avatar
ahern committed
102
avtPFLOTRANFileFormat::avtPFLOTRANFileFormat(const char *fname):
103 104
    avtMTMDFileFormat(fname)
{
105 106 107 108 109
    fileID = -1;
    dimID[0] = -1;
    dimID[1] = -1;
    dimID[2] = -1;

110 111 112 113
    filename = strdup(fname);
    opened = false;
    nTime = 0;

114 115 116
    // a bit of a hack to fix up old files with cell-centered coordinates
    oldFileNeedingCoordFixup = false;

117 118 119 120 121
    // Turn off HDF5 error messages to the terminal.
    H5Eset_auto(NULL, NULL);
}

// ****************************************************************************
ahern's avatar
ahern committed
122
//  Method: avtPFLOTRANFileFormat destructor
123
//
124
//  Programmer: Sean Ahern
125 126
//  Creation:   Thu Apr 24 14:00:58 PST 2008
//
127 128 129 130 131 132 133 134 135 136 137 138
//  Modifications:
//    Jeremy Meredith, Wed Mar 30 12:30:09 EDT 2011
//    Close the data sets we opened earlier.  Also, we can now check for an
//    error during H5Fclose (due to file access property setting when opening
//    the file), and report it if we encountered it.
//
//    Jeremy Meredith, Wed Jun 15 14:55:51 EDT 2011
//    Move file-closing code here from FreeUpResources -- that's called
//    when changing time steps; we don't want to close the file then.
//    Also, don't make the check report an error; that's too severe.
//    Just print to cerr.
//
139 140
// ****************************************************************************

ahern's avatar
ahern committed
141
avtPFLOTRANFileFormat::~avtPFLOTRANFileFormat()
142 143 144
{
    free(filename);
    filename = NULL;
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

    if (dimID[0] > 0)
        H5Dclose(dimID[0]);
    if (dimID[1] > 0)
        H5Dclose(dimID[1]);
    if (dimID[2] > 0)
        H5Dclose(dimID[2]);
    dimID[0] = -1;
    dimID[1] = -1;
    dimID[2] = -1;

    if (fileID > 0)
    {
        herr_t err = H5Fclose(fileID);
        if (err < 0)
        {
            cerr <<    "avtPFLOTRANFileFormat::~avtPFLOTRANFileFormat: "
                       "Couldn't close the file properly; this is likely due "
                       "to not closing all open objects in the file.  Please "
                       "report this to a VisIt developer.\n";
        }
    }
    fileID = -1;
    opened = false;

170 171 172 173 174 175 176 177
}

// ****************************************************************************
//  Method: avtEMSTDFileFormat::GetNTimesteps
//
//  Purpose:
//      Tells the rest of the code how many timesteps there are in this file.
//
178
//  Programmer: Sean Ahern
179 180 181 182 183
//  Creation:   Thu Apr 24 14:00:58 PST 2008
//
// ****************************************************************************

int
ahern's avatar
ahern committed
184
avtPFLOTRANFileFormat::GetNTimesteps(void)
185 186 187 188 189 190 191
{
    LoadFile();

    return nTime;
}

// ****************************************************************************
ahern's avatar
ahern committed
192
//  Method: avtPFLOTRANFileFormat::LoadFile
193 194 195 196
//
//  Purpose:
//      Loads the basics of the file into memory.
//
197
//  Programmer: Sean Ahern
198 199
//  Creation:   Thu Apr 24 14:00:58 PST 2008
//
200 201 202 203 204
//  Modifications:
//    Jeremy Meredith, Tue Jul  1 12:43:11 EDT 2008
//    Added support for automatic parallel decomposition and parallel I/O
//    via hyperslab reading.
//
brugger's avatar
 
brugger committed
205 206 207 208
//    Eric Brugger, Fri Aug  8 13:53:01 PDT 2008
//    Added an explicit conversion from a char* to a string to make the
//    MIPSpro compiler happy.
//
209 210 211
//    Jeremy Meredith, Thu Jan  7 15:36:19 EST 2010
//    Close all open ids when returning an exception.
//
212 213 214 215
//    Jeremy Meredith, Wed Mar 30 12:33:56 EDT 2011
//    More ids left to close.  Also, open the file in a way that is more
//    strict about detecting unclosed files/ids.
//
216 217 218
//    Jeremy Meredith, Wed Jun 15 15:56:44 EDT 2011
//    Missed an ID to close.
//
219 220 221
// ****************************************************************************

void
ahern's avatar
ahern committed
222
avtPFLOTRANFileFormat::LoadFile(void)
223 224 225 226 227 228 229
{
    if (opened == true)
        return;

    // Initialize HDF5.
    H5open();

230 231 232 233 234 235 236 237 238 239 240 241
    // We want it to be aggressive about closing the file when we tell it to.
    hid_t fileAccessPropListID = H5Pcreate(H5P_FILE_ACCESS);
    if (fileAccessPropListID < 0)
    {
        EXCEPTION1(ImproperUseException, "Couldn't H5Pcreate");
    }
    herr_t err = H5Pset_fclose_degree(fileAccessPropListID, H5F_CLOSE_SEMI);
    if (err < 0)
    {
        EXCEPTION1(ImproperUseException, "Couldn't set file close access");
    }

242
    // Grab the file and begin.
243 244
    fileID = H5Fopen(filename, H5F_ACC_RDONLY, fileAccessPropListID);
    H5Pclose(fileAccessPropListID);
245 246
    if (fileID < 0)
    {
ahern's avatar
ahern committed
247
        debug4 << "avtPFLOTRANFileFormat::LoadFile: " << "Could not open <" << filename << ">" << endl;
248
        char error[1024];
bonnell's avatar
bonnell committed
249
        SNPRINTF(error, 1024, "Cannot be a PFLOTRAN file (%s) since it is not even an HDF5 file:",filename);
250 251 252 253 254 255 256 257
        EXCEPTION1(InvalidDBTypeException, error);
    }

    // Check the coordinates and pull out the mesh dimensions
    // while we're at it.
    hid_t coordsGID = H5Gopen(fileID, "Coordinates");
    if (coordsGID < 0)
    {
258
        H5Fclose(fileID);
ahern's avatar
ahern committed
259 260
        debug4 << "avtPFLOTRANFileFormat::LoadFile: " << "Could not open the Coordinates group in file " << filename << endl;
        EXCEPTION1(InvalidDBTypeException, "Cannot be a PFLOTRAN file since it does not have a Coordinates group.");
261 262 263 264 265 266
    }
    for (int dim=0;dim<3;dim++)
    {
        dimID[dim] = H5Dopen(coordsGID, coordNames[dim].c_str());
        if (dimID[dim] < 0)
        {
267
            H5Gclose(coordsGID);
268
            H5Fclose(fileID);
ahern's avatar
ahern committed
269 270
            debug4 << "avtPFLOTRANFileFormat::LoadFile: " << "Could not open the " << coordNames[dim] << "dataset in file " << filename << endl;
            EXCEPTION1(InvalidDBTypeException, "Cannot be a PFLOTRAN file since it does not have valid coordinates data.");
271 272 273 274
        }
        hid_t dimSpaceID = H5Dget_space(dimID[dim]);
        if (dimSpaceID < 0)
        {
275
            H5Gclose(coordsGID);
276
            H5Fclose(fileID);
ahern's avatar
ahern committed
277 278
            debug4 << "avtPFLOTRANFileFormat::LoadFile: " << "Could not get the space information for the " << coordNames[dim] << " coordinate in file " << filename << endl;
            EXCEPTION1(InvalidDBTypeException, "Cannot be a PFLOTRAN file since it does not have valid coordinates data.");
279 280 281 282
        }
        int ndims = H5Sget_simple_extent_ndims(dimSpaceID);
        if (ndims != 1)
        {
283
            H5Gclose(coordsGID);
284
            H5Fclose(fileID);
ahern's avatar
ahern committed
285 286
            debug4 << "avtPFLOTRANFileFormat::LoadFile: " << "The " << coordNames[dim] << " coordinate is not one dimensional" << endl;
            EXCEPTION1(InvalidDBTypeException, "Cannot be a PFLOTRAN file since some coordinate data is not one dimensional.");
287 288 289 290
        }
        hsize_t dims, maxdims;
        H5Sget_simple_extent_dims(dimSpaceID, &dims, &maxdims);

291
        globalDims[dim] = dims;
292
    }
293
    H5Gclose(coordsGID);
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310

    // Look for groups starting with "Time:".  They're our timesteps.
    hsize_t nObjs;
    H5Gget_num_objs(fileID, &nObjs);
    nTime = 0;
    times.clear();
    for(int i=0;i<nObjs;i++)
    {
        char name[256];
        H5Gget_objname_by_idx(fileID, i, name, 256);
        if (strncmp(name, "Coordinates", 11) == 0)
            continue;
        if (strncmp(name, "Time: ", 6) == 0)
        {
            nTime++;
            double time;
            sscanf(name, "Time: %lf h", &time);
brugger's avatar
 
brugger committed
311
            times.push_back(std::make_pair(time,std::string(name)));
312 313 314 315
        }
        else
        {
            // Unknown name.
ahern's avatar
ahern committed
316
            debug2 << "avtPFLOTRANFileFormat::LoadFile: Unknown object in file: " << name << endl;
317 318 319 320
        }
    }
    std::sort(times.begin(), times.end());

321 322
    DoDomainDecomposition();

323 324 325 326
    opened = true;
}

// ****************************************************************************
ahern's avatar
ahern committed
327
//  Method: avtPFLOTRANFileFormat::FreeUpResources
328 329 330 331 332 333 334
//
//  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.
//
335
//  Programmer: Sean Ahern
336 337
//  Creation:   Thu Apr 24 14:00:58 PST 2008
//
338
//  Modifications:
339 340 341
//    Jeremy Meredith, Wed Jun 15 14:55:51 EDT 2011
//    Move file-closing code from here to destructor.  This function is called
//    when changing time steps; we don't want to close the file then.
342
//
343 344 345
// ****************************************************************************

void
ahern's avatar
ahern committed
346
avtPFLOTRANFileFormat::FreeUpResources(void)
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 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546

// ****************************************************************************
//  Method:  avtPFLOTRANFileFormat::DoDomainDecomposition
//
//  Purpose:
//    Split the single big dataset into parallel chunks.
//
//  Arguments:
//    none
//
//  Programmer:  Jeremy Meredith
//  Creation:    July  1, 2008
//
// ****************************************************************************
void
avtPFLOTRANFileFormat::DoDomainDecomposition()
{
#ifdef PARALLEL
    int zoneDims[3] = {globalDims[0]-1,globalDims[1]-1,globalDims[2]-1};
    avtDatabase::ComputeRectilinearDecomposition(3,
                                                 PAR_Size(),
                                                 zoneDims[0],
                                                 zoneDims[1],
                                                 zoneDims[2],
                                                 &domainCount[0],
                                                 &domainCount[1],
                                                 &domainCount[2]);

    avtDatabase::ComputeDomainLogicalCoords(3, domainCount, PAR_Rank(),
                                            domainIndex);


    for (int axis = 0; axis < 3; axis++)
    {
        avtDatabase::ComputeDomainBounds(zoneDims[axis],
                                         domainCount[axis],
                                         domainIndex[axis],
                                         &domainGlobalStart[axis],
                                         &domainGlobalCount[axis]);
        domainGlobalCount[axis]++; // the returned bounds are zonal; we want nodal

        // start with the assumption that all cells are real
        localRealStart[axis] = 0;
        localRealCount[axis] = domainGlobalCount[axis];

        // now account for ghost cells by expanding the requested cells
        // and updating the local real cell indices
        if (domainIndex[axis] > 0)
        {
            localRealStart[axis]++;
            domainGlobalStart[axis]--;
            domainGlobalCount[axis]++;
        }
        if (domainIndex[axis] < domainCount[axis]-1)
        {
            domainGlobalCount[axis]++;
        }
        debug4 << "PFLOTRAN domain local indices: "
               << "start[" << axis << "] = " << domainGlobalStart[axis]
               << ",  count[" << axis << "] = " << domainGlobalCount[axis]
               << endl;
    }
#else
    domainGlobalStart[0] = 0;
    domainGlobalStart[1] = 0;
    domainGlobalStart[2] = 0;
    domainGlobalCount[0] = globalDims[0];
    domainGlobalCount[1] = globalDims[1];
    domainGlobalCount[2] = globalDims[2];
    localRealStart[0] = 0;
    localRealStart[1] = 0;
    localRealStart[2] = 0;
    localRealCount[0] = globalDims[0];
    localRealCount[1] = globalDims[1];
    localRealCount[2] = globalDims[2];
#endif
}


// ****************************************************************************
//  Method:  avtPFLOTRANFileFormat::AddGhostCellInfo
//
//  Purpose:
//    Add the ghost cell information and local-to-global translations.
//
//  Note:  some of this was borrowed from avtSiloFileFormat::GetQuadGhostZones
//
//  Arguments:
//    ds         the dataset to which we need add the informatio
//
//  Programmer:  Jeremy Meredith
//  Creation:    July  1, 2008
//
// ****************************************************************************
void
avtPFLOTRANFileFormat::AddGhostCellInfo(vtkDataSet *ds)
{
#ifndef PARALLEL
    return;
#endif

    int nx = domainGlobalCount[0];
    int ny = domainGlobalCount[1];
    int nz = domainGlobalCount[2];
    int nnodes = nx*ny*nz;

    bool *ghostPoints = new bool[nnodes];

    //
    // Initialize as all ghost levels
    //
    for (int ii = 0; ii < nnodes; ii++)
        ghostPoints[ii] = true; 

    //
    // Set real values
    //
    for (int k = 0; k < localRealCount[2]; k++)
        for (int j = 0; j < localRealCount[1]; j++)
            for (int i = 0; i < localRealCount[0]; i++)
            {
                int ii = localRealStart[0] + i;
                int jj = localRealStart[1] + j;
                int kk = localRealStart[2] + k;
                int index = kk*ny*nx + jj*nx + ii;
                ghostPoints[index] = false; 
            }

    //
    //  okay, now we have ghost points, but what we really want
    //  are ghost cells ... convert:  if all points associated with
    //  cell are 'real' then so is the cell.
    //
    unsigned char realVal = 0;
    unsigned char ghostVal = 0;
    avtGhostData::AddGhostZoneType(ghostVal, 
                                   DUPLICATED_ZONE_INTERNAL_TO_PROBLEM);

    int ncells = ds->GetNumberOfCells();
    vtkIdList *ptIds = vtkIdList::New();
    vtkUnsignedCharArray *ghostCells = vtkUnsignedCharArray::New();
    ghostCells->SetName("avtGhostZones");
    ghostCells->Allocate(ncells);
 
    for (int i = 0; i < ncells; i++)
    {
        ds->GetCellPoints(i, ptIds);
        bool ghost = false;
        for (int idx = 0; idx < ptIds->GetNumberOfIds(); idx++)
            ghost |= ghostPoints[ptIds->GetId(idx)];

        if (ghost)
            ghostCells->InsertNextValue(ghostVal);
        else
            ghostCells->InsertNextValue(realVal);
 
    } 
    ds->GetCellData()->AddArray(ghostCells);
    delete [] ghostPoints;
    ghostCells->Delete();
    ptIds->Delete();

    // Add the min/max local logical extents of this domain.  It's
    // an alternate way we label ghost zones for structured grids.
    vtkIntArray *realDims = vtkIntArray::New();
    realDims->SetName("avtRealDims");
    realDims->SetNumberOfValues(6);

    realDims->SetValue(0, localRealStart[0]);
    realDims->SetValue(1, localRealStart[0]+localRealCount[0]-1);

    realDims->SetValue(2, localRealStart[1]);
    realDims->SetValue(3, localRealStart[1]+localRealCount[1]-1);

    realDims->SetValue(4, localRealStart[2]);
    realDims->SetValue(5, localRealStart[2]+localRealCount[2]-1);

    ds->GetFieldData()->AddArray(realDims);
    ds->GetFieldData()->CopyFieldOn("avtRealDims");
    realDims->Delete();

    // And add base_index, the global index of the first real
    // zone in the domain.  This is how we convert from local
    // to global indices.
    vtkIntArray *baseIndex = vtkIntArray::New();
    baseIndex->SetName("base_index");
    baseIndex->SetNumberOfValues(3);
    baseIndex->SetValue(0, domainGlobalStart[0]+localRealStart[0]);
    baseIndex->SetValue(1, domainGlobalStart[1]+localRealStart[1]);
    baseIndex->SetValue(2, domainGlobalStart[2]+localRealStart[2]);
    ds->GetFieldData()->AddArray(baseIndex);
    ds->GetFieldData()->CopyFieldOn("base_index");    

    ds->SetUpdateGhostLevel(0);
}


547
// ****************************************************************************
ahern's avatar
ahern committed
548
//  Method: avtPFLOTRANFileFormat::PopulateDatabaseMetaData
549 550 551 552 553 554
//
//  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.
//
555
//  Programmer: Sean Ahern
556 557
//  Creation:   Thu Apr 24 14:00:58 PST 2008
//
558 559 560 561 562 563
//  Modifications:
//    Jeremy Meredith, Fri Apr  3 12:26:26 EDT 2009
//    All variables are supposed to be cell-centered.
//    Add support for old-style PFLOTRAN files where the coordinate
//    arrays represented cell centers.
//
564 565 566
//    Daniel Schep, Thu Aug 26 15:30:18 EDT 2010
//    Added support for returning vector and material data.
//
567 568 569
//    Jeremy Meredith, Wed Mar 30 12:58:02 EDT 2011
//    Close any HDF5 group/dataset we open.
//
570 571 572
//    Jeremy Meredith, Wed Jun 15 15:56:44 EDT 2011
//    More aggressive about closing ids, including spaces.
//
573 574 575
// ****************************************************************************

void
ahern's avatar
ahern committed
576
avtPFLOTRANFileFormat::PopulateDatabaseMetaData(avtDatabaseMetaData * md,
577 578 579 580
                                                 int timeState)
{
    LoadFile();

581 582
    md->SetFormatCanDoDomainDecomposition(true);

583 584 585 586
    // Mesh
    avtMeshMetaData *mesh = new avtMeshMetaData;
    mesh->name = "mesh";
    mesh->meshType = AVT_RECTILINEAR_MESH;
587
    mesh->numBlocks = 1;  // must be 1 for automatic decomposition
588 589 590 591 592 593 594 595 596 597 598 599 600
    mesh->blockOrigin = 1;
    mesh->cellOrigin = 0;
    mesh->spatialDimension = 3;
    mesh->topologicalDimension = 3;
    mesh->blockTitle = "blocks";
    mesh->blockPieceName = "block";
    mesh->hasSpatialExtents = false;
    //mesh->xUnits = "mm";
    //mesh->yUnits = "mm";
    //mesh->zUnits = "mm";
    md->Add(mesh);

    // Look in the timestep for the list of variables.
601
    hid_t timeGID = H5Gopen(fileID, times[timeState].second.c_str());
602
    hsize_t nObjs;
603
    H5Gget_num_objs(timeGID, &nObjs);
604 605 606
    for(int i=0;i<nObjs;i++)
    {
        char name[512];
607
        H5Gget_objname_by_idx(timeGID, i, name, 512);
608

609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
        // Check if variable is a Vector component
        char* vecMatch;
        for (int comp=0;comp<3;comp++)
            if (vecMatch = strstr(name,vecNames[comp].c_str())) break;
        
        // if so, add component to vector list
        if (vecMatch)
        {
            char vectorName[512];
            strncpy(vectorName, name, strlen(name)-strlen(vecMatch));
            strcpy(vectorName+strlen(name)-strlen(vecMatch), vecMatch+2);

            if (vectors.count(string(vectorName)) == 0)
                vectors[string(vectorName)] = vector<string>();
            vectors[string(vectorName)].push_back(string(name));

            continue; // Don't add vectors components as scalars.
        }

628
        hid_t ds = H5Dopen(timeGID, name);
629 630 631
        hid_t dsSpace = H5Dget_space(ds);
        int ndims = H5Sget_simple_extent_ndims(dsSpace);
        if (ndims != 3)
632 633 634
        {
            H5Sclose(dsSpace);
            H5Dclose(ds);
635
            continue; // skip it
636
        }
637

638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709

        // set metadata for Materials
        if(strstr(name, "Material_ID"))
        {
            hid_t slabSpace = H5Scopy(dsSpace);
            hsize_t start[] = {domainGlobalStart[0],domainGlobalStart[1],domainGlobalStart[2]};
            hsize_t count[] = {domainGlobalCount[0]-1,domainGlobalCount[1]-1,domainGlobalCount[2]-1};
            if (oldFileNeedingCoordFixup)
            {
                for (int dim=0; dim<3; dim++)
                    count[dim]++;
            }
            H5Sselect_hyperslab(slabSpace, H5S_SELECT_SET, start, NULL, count, NULL);

            hsize_t beg[3];
            hsize_t end[3];
            H5Sget_select_bounds(slabSpace, beg, end);
            int nx = end[0]-beg[0]+1;
            int ny = end[1]-beg[1]+1;
            int nz = end[2]-beg[2]+1;
            int nvals = nx*ny*nz;

            int dims[3];
            dims[0] = nx;
            dims[1] = ny;
            dims[2] = nz;

            hid_t memSpace = H5Screate_simple(3,count,NULL);

            int *matlist = new int[nvals];

            int *in = new int[nvals];
            herr_t err = H5Dread(ds, H5T_NATIVE_INT, memSpace, slabSpace,
                                 H5P_DEFAULT, in);
            // Input is in a different ordering (Fortran) than VTK wants (C).
            for (int i=0;i<nx;i++)
                for (int j=0;j<ny;j++)
                    for (int k=0;k<nz;k++)
                        matlist[k*nx*ny + j*nx + i] = in[k + j*nz + i*nz*ny];


            // find the different materials
            map<int,bool> matls;
            for (int i=0;i<nvals;i++)
                matls[matlist[i]] = true;

            int nmats = matls.size();
            char **names = new char*[nmats];
            int i = 0;
            for (map<int,bool>::iterator iter = matls.begin(); iter != matls.end();++iter)
            {
                names[i] = new char[8];
                sprintf(names[i], "%d", iter->first);
                ++i;
            }


            avtMaterialMetaData *material = new avtMaterialMetaData();
            material->name = name;
            material->meshName = "mesh";
            material->numMaterials = nmats;
            for(int i=0;i<nmats;++i)
                material->materialNames.push_back(names[i]);
            md->Add(material);

     
            delete [] in;
            delete [] matlist;
            for(int i=0;i<nmats;++i)
                delete [] names[i];
            delete [] names;

710 711 712 713
            H5Sclose(memSpace);
            H5Sclose(dsSpace);
            H5Sclose(slabSpace);
            H5Dclose(ds);
714 715 716
            continue; // Don't add materials as a scalar.
        }

717 718 719 720 721 722 723 724 725 726 727
        // We need to check if the coordinate array dims are the same
        // as the variables.  So well save off one of the variable's
        // dimensions and compare it with the mesh coordinate array later
        // to see if we need to recenter the coordinate arrays.
        if (i == 0)
        {
            hsize_t varDimsForOldFileTest[3];
            H5Sget_simple_extent_dims(dsSpace, varDimsForOldFileTest, NULL);
            oldFileNeedingCoordFixup = (globalDims[0]==varDimsForOldFileTest[0]);
        }        

728 729
        H5Dclose(ds);

730 731 732
        avtScalarMetaData *scalar = new avtScalarMetaData();
        scalar->name = name;
        scalar->meshName = "mesh";
733
        scalar->centering = AVT_ZONECENT;
734 735
        md->Add(scalar);
    }
736
    H5Gclose(timeGID);
737

738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
    //add metadata for vectors if they have 3 components
    for (map<string, vector<string> >::iterator iter=vectors.begin();
        iter!=vectors.end(); ++iter )
    {
        if (iter->second.size() == 3)
        {
            avtVectorMetaData *vector = new avtVectorMetaData();
            vector->name = iter->first;
            vector->meshName = "mesh";
            vector->centering = AVT_ZONECENT;
            md->Add(vector);
        }
        // ignore < 3-vectors
    }


754 755 756 757 758 759 760
#ifdef PARALLEL
    if (oldFileNeedingCoordFixup)
        EXCEPTION1(ImproperUseException,
                   "Can't support old PFLOTRAN files, needing coordinate "
                   "array recentering, in parallel.");
#endif

761
    // Set the time information.
762
    vector<double> t;
763 764 765 766 767 768 769 770
    for(int i=0;i<nTime;i++)
        t.push_back(times[i].first);
    md->SetTimes(t);
    md->SetTimesAreAccurate(true);
    md->SetTemporalExtents(times[0].first, times[nTime-1].first);
}

// ****************************************************************************
ahern's avatar
ahern committed
771
//  Method: avtPFLOTRANFileFormat::GetMesh
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
//
//  Purpose:
//      Gets the mesh associated with this file.  The mesh is returned as a
//      derived type of vtkDataSet (ie vtkRectilinearGrid, vtkStructuredGrid,
//      vtkUnstructuredGrid, etc).
//
//  Arguments:
//      timestate   The index of the timestate.  If GetNTimesteps returned
//                  'N' time steps, this is guaranteed to be between 0 and N-1.
//      domain      The index of the domain.  If there are NDomains, this
//                  value is guaranteed to be between 0 and NDomains-1,
//                  regardless of block origin.
//      meshname    The name of the mesh of interest.  This can be ignored if
//                  there is only one mesh.
//
787
//  Programmer: Sean Ahern
788 789
//  Creation:   Thu Apr 24 14:00:58 PST 2008
//
790 791 792 793 794
//  Modifications:
//    Jeremy Meredith, Tue Jul  1 12:43:11 EDT 2008
//    Added support for automatic parallel decomposition and parallel I/O
//    via hyperslab reading.
//
795 796 797 798 799
//    Jeremy Meredith, Fri Apr  3 12:26:26 EDT 2009
//    All variables are supposed to be cell-centered.
//    Add support for old-style PFLOTRAN files where the coordinate
//    arrays represented cell centers.
//
800 801 802
//    Jeremy Meredith, Wed Jun 15 15:56:44 EDT 2011
//    More aggressive about closing ids, including spaces.
//
803 804 805
// ****************************************************************************

vtkDataSet *
ahern's avatar
ahern committed
806
avtPFLOTRANFileFormat::GetMesh(int, int domain, const char *)
807 808 809 810 811
{
    // Make sure the file is opened and ready.
    LoadFile();

    // Read the coordinates arrays.
812
    vtkDoubleArray *localCoords[3];
813 814
    for (int dim=0;dim<3;dim++)
    {
815 816 817 818 819 820 821 822 823 824 825 826 827 828
        localCoords[dim] = vtkDoubleArray::New();
        localCoords[dim]->SetNumberOfTuples(domainGlobalCount[dim]);
        double *coords = (double*)localCoords[dim]->GetVoidPointer(0);

        hid_t arraySpace = H5Dget_space(dimID[dim]);
        hid_t slabSpace = H5Scopy(arraySpace);
        hsize_t start[] = {domainGlobalStart[dim]};
        hsize_t count[] = {domainGlobalCount[dim]};
        H5Sselect_hyperslab(slabSpace, H5S_SELECT_SET, start,NULL,count,NULL);

        hid_t memSpace = H5Screate_simple(1,count,NULL);

        herr_t err = H5Dread(dimID[dim], H5T_NATIVE_DOUBLE,
                             memSpace, slabSpace, H5P_DEFAULT, coords);
829 830 831 832

        H5Sclose(memSpace);
        H5Sclose(slabSpace);
        H5Sclose(arraySpace);
833
    }
834

835
    vtkRectilinearGrid *mesh = vtkRectilinearGrid::New();
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870

    if (oldFileNeedingCoordFixup)
    {
        int dims[3] = {domainGlobalCount[0],
                       domainGlobalCount[1],
                       domainGlobalCount[2]};
        for (int dim=0; dim<3; dim++)
        {
            if (dims[dim] < 2)
                continue;

            dims[dim]++;
            int d = dims[dim];
            vtkDoubleArray *oc = localCoords[dim];
            vtkDoubleArray *nc = vtkDoubleArray::New();

            nc->SetNumberOfTuples(oc->GetNumberOfTuples()+1);
            nc->SetTuple1(0, (oc->GetTuple1(0) -
                              (oc->GetTuple1(1)-oc->GetTuple1(0))/2.));
            for (int i=1; i<d-1; i++)
                nc->SetTuple1(i, (oc->GetTuple1(i-1)+oc->GetTuple1(i))/2.);
            nc->SetTuple1(d-1, (oc->GetTuple1(d-2) +
                                (oc->GetTuple1(d-2)-oc->GetTuple1(d-3))/2.));

            localCoords[dim] = nc;
            oc->Delete();
        }

        mesh->SetDimensions(dims);
    }
    else
    {
        mesh->SetDimensions(domainGlobalCount);
    }

871 872 873 874 875 876 877 878
    mesh->SetXCoordinates(localCoords[0]);
    mesh->SetYCoordinates(localCoords[1]);
    mesh->SetZCoordinates(localCoords[2]);
    localCoords[0]->Delete();
    localCoords[1]->Delete();
    localCoords[2]->Delete();

    AddGhostCellInfo(mesh);
879 880 881 882 883

    return mesh;
}

// ****************************************************************************
884
//
ahern's avatar
ahern committed
885
//  Method: avtPFLOTRANFileFormat::GetVar
886 887 888 889 890 891 892 893 894 895 896 897
//
//  Purpose:
//      Gets a scalar variable associated with this file.
//
//  Arguments:
//      timestate  The index of the timestate.  If GetNTimesteps returned
//                 'N' time steps, this is guaranteed to be between 0 and N-1.
//      domain     The index of the domain.  If there are NDomains, this
//                 value is guaranteed to be between 0 and NDomains-1,
//                 regardless of block origin.
//      varname    The name of the variable requested.
//
898
//  Programmer: Sean Ahern
899 900
//  Creation:   Thu Apr 24 14:00:58 PST 2008
//
901 902 903 904 905
//  Modifications:
//    Jeremy Meredith, Tue Jul  1 12:43:11 EDT 2008
//    Added support for automatic parallel decomposition and parallel I/O
//    via hyperslab reading.
//
906 907 908 909 910 911 912 913 914
//    Jeremy Meredith, Tue Mar 31 11:09:37 EDT 2009
//    Don't try reading integral data into doubles; instead read to ints,
//    the convert to double using the compiler.  At least in the version
//    of HDF5 I used, 1.6.5 x64, trying to read an integer array to a double
//    looked like it worked, but gave garbage for much of the last portion
//    of data.  The HDF5 docs for 1.6 claim they can't do this operation
//    at all, so while I'm surprised it looked even partly okay, I'm
//    confident reading to ints is the correct solution.
//
915 916 917 918 919
//    Jeremy Meredith, Fri Apr  3 12:26:26 EDT 2009
//    All variables are supposed to be cell-centered.
//    Add support for old-style PFLOTRAN files where the coordinate
//    arrays represented cell centers.
//
920 921 922
//    Jeremy Meredith, Wed Mar 30 12:58:02 EDT 2011
//    Close any HDF5 group/dataset we open.
//
923 924 925
//    Jeremy Meredith, Wed Jun 15 15:56:44 EDT 2011
//    More aggressive about closing ids, including spaces.
//
926 927 928
// ****************************************************************************

vtkDataArray *
ahern's avatar
ahern committed
929
avtPFLOTRANFileFormat::GetVar(int timestate, int, const char *varname)
930 931 932 933 934 935 936 937 938 939 940
{
    // Make sure the file is opened and ready.
    LoadFile();

    // Read the dataset from the file.
    hid_t ts = H5Gopen(fileID, times[timestate].second.c_str());
    hid_t ds = H5Dopen(ts, varname);
    hid_t dsSpace = H5Dget_space(ds);
    int ndims = H5Sget_simple_extent_ndims(dsSpace);
    if (ndims != 3)
    {
941
        H5Sclose(dsSpace);
942 943
        H5Dclose(ds);
        H5Gclose(ts);
944 945 946
        debug1 << "The variable " << varname << " had only " << ndims << " dimensions" << endl;
        EXCEPTION1(InvalidVariableException, varname);
    }
947 948 949

    hid_t slabSpace = H5Scopy(dsSpace);
    hsize_t start[] = {domainGlobalStart[0],domainGlobalStart[1],domainGlobalStart[2]};
950 951 952 953 954 955
    hsize_t count[] = {domainGlobalCount[0]-1,domainGlobalCount[1]-1,domainGlobalCount[2]-1};
    if (oldFileNeedingCoordFixup)
    {
        for (int dim=0; dim<3; dim++)
            count[dim]++;
    }
956 957 958 959 960 961 962 963 964 965 966 967
    H5Sselect_hyperslab(slabSpace, H5S_SELECT_SET, start, NULL, count, NULL);

    hsize_t beg[3];
    hsize_t end[3];
    H5Sget_select_bounds(slabSpace, beg, end);
    int nx = end[0]-beg[0]+1;
    int ny = end[1]-beg[1]+1;
    int nz = end[2]-beg[2]+1;
    int nvals = nx*ny*nz;

    hid_t memSpace = H5Screate_simple(3,count,NULL);

968 969
    // Set up the VTK object.
    vtkDoubleArray *array = vtkDoubleArray::New();
970
    array->SetNumberOfTuples(nvals);
971 972
    double *out = (double*)array->GetVoidPointer(0);

973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
    // Read the data -- read to ints or doubles directly, as
    // some versions of HDF5 seem to have problems converting
    // ints to doubles directly.
    hid_t intype = H5Dget_type(ds);
    if (H5Tequal(intype, H5T_NATIVE_FLOAT) ||
        H5Tequal(intype, H5T_NATIVE_DOUBLE) ||
        H5Tequal(intype, H5T_NATIVE_LDOUBLE))
    {        
        double *in = new double[nvals];
        herr_t err = H5Dread(ds, H5T_NATIVE_DOUBLE, memSpace, slabSpace,
                             H5P_DEFAULT, in);

        // Input is in a different ordering (Fortran) than VTK wants (C).
        for (int i=0;i<nx;i++)
            for (int j=0;j<ny;j++)
                for (int k=0;k<nz;k++)
                    out[k*nx*ny + j*nx + i] = in[k + j*nz + i*nz*ny];

        delete [] in;
    }
    else
    {
        int *in = new int[nvals];
        herr_t err = H5Dread(ds, H5T_NATIVE_INT, memSpace, slabSpace,
                             H5P_DEFAULT, in);
        // Input is in a different ordering (Fortran) than VTK wants (C).
        for (int i=0;i<nx;i++)
            for (int j=0;j<ny;j++)
                for (int k=0;k<nz;k++)
                    out[k*nx*ny + j*nx + i] = in[k + j*nz + i*nz*ny];

        delete [] in;
    }
1006

1007 1008 1009
    H5Sclose(memSpace);
    H5Sclose(slabSpace);
    H5Sclose(dsSpace);
1010 1011 1012
    H5Dclose(ds);
    H5Gclose(ts);

1013 1014 1015 1016
    return array;
}

// ****************************************************************************
ahern's avatar
ahern committed
1017
//  Method: avtPFLOTRANFileFormat::GetVectorVar
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
//
//  Purpose:
//      Gets a vector variable associated with this file.
//
//  Arguments:
//      timestate  The index of the timestate.  If GetNTimesteps returned
//                 'N' time steps, this is guaranteed to be between 0 and N-1.
//      domain     The index of the domain.  If there are NDomains, this
//                 value is guaranteed to be between 0 and NDomains-1,
//                 regardless of block origin.
//      varname    The name of the variable requested.
//
1030
//  Programmer: Sean Ahern
1031 1032
//  Creation:   Thu Apr 24 14:00:58 PST 2008
//
1033 1034 1035 1036
//  Modifications:
//    Daniel Schep, Thu Aug 26 15:30:18 EDT 2010
//    Implemented support to return vector data.
//
1037 1038 1039
//    Jeremy Meredith, Wed Mar 30 12:58:02 EDT 2011
//    Close any HDF5 group/dataset we open.
//
1040 1041 1042
//    Jeremy Meredith, Wed Jun 15 15:56:44 EDT 2011
//    More aggressive about closing ids, including spaces.
//
1043 1044 1045
// ****************************************************************************

vtkDataArray *
ahern's avatar
ahern committed
1046
avtPFLOTRANFileFormat::GetVectorVar(int timestate, int domain,
1047 1048
                                     const char *varname)
{
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
    // Make sure the file is opened and ready.
    LoadFile();

    // Get var component names
    vector<string> varnames = vectors[string(varname)];
    sort(varnames.begin(), varnames.end());


    hid_t ts = H5Gopen(fileID, times[timestate].second.c_str());
    vtkDoubleArray *array;
    for(int comp=0; comp<3; comp++)
1060
    {
1061 1062 1063 1064 1065
        hid_t ds = H5Dopen(ts, varnames[comp].c_str());
        hid_t dsSpace = H5Dget_space(ds);
        int ndims = H5Sget_simple_extent_ndims(dsSpace);
        if (ndims != 3)
        {
1066 1067
            H5Dclose(ds);
            H5Gclose(ts);
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
            debug1 << "The variable " << varname << " had only " << ndims << " dimensions" << endl;
            EXCEPTION1(InvalidVariableException, varname);
        }

        hid_t slabSpace = H5Scopy(dsSpace);
        hsize_t start[] = {domainGlobalStart[0],domainGlobalStart[1],domainGlobalStart[2]};
        hsize_t count[] = {domainGlobalCount[0]-1,domainGlobalCount[1]-1,domainGlobalCount[2]-1};
        if (oldFileNeedingCoordFixup)
        {
            for (int dim=0; dim<3; dim++)
                count[dim]++;
        }
        H5Sselect_hyperslab(slabSpace, H5S_SELECT_SET, start, NULL, count, NULL);

        hsize_t beg[3];
        hsize_t end[3];
        H5Sget_select_bounds(slabSpace, beg, end);
        int nx = end[0]-beg[0]+1;
        int ny = end[1]-beg[1]+1;
        int nz = end[2]-beg[2]+1;
        int nvals = nx*ny*nz;

        hid_t memSpace = H5Screate_simple(3,count,NULL);

        // Set up the VTK object.
        double *out;
        if (comp == 0)
        {
            array = vtkDoubleArray::New();
            array->SetNumberOfComponents(3);
            array->SetNumberOfTuples(nvals);
            out = (double*)array->GetVoidPointer(0);
        }

        // Read the data -- read to ints or doubles directly, as
        // some versions of HDF5 seem to have problems converting
        // ints to doubles directly.
        hid_t intype = H5Dget_type(ds);
        if (H5Tequal(intype, H5T_NATIVE_FLOAT) ||
            H5Tequal(intype, H5T_NATIVE_DOUBLE) ||
            H5Tequal(intype, H5T_NATIVE_LDOUBLE))
        {        
            double *in = new double[nvals];
            herr_t err = H5Dread(ds, H5T_NATIVE_DOUBLE, memSpace, slabSpace,
                                 H5P_DEFAULT, in);

            // Input is in a different ordering (Fortran) than VTK wants (C).
            for (int i=0;i<nx;i++)
                for (int j=0;j<ny;j++)
                    for (int k=0;k<nz;k++)
                        out[k*nx*ny*3 + j*nx*3 + i*3 + comp] = in[k + j*nz + i*nz*ny];

            delete [] in;
        }
        else
        {
            int *in = new int[nvals];
            herr_t err = H5Dread(ds, H5T_NATIVE_INT, memSpace, slabSpace,
                                 H5P_DEFAULT, in);
            // Input is in a different ordering (Fortran) than VTK wants (C).
            for (int i=0;i<nx;i++)
                for (int j=0;j<ny;j++)
                    for (int k=0;k<nz;k++)
                        out[k*nx*ny*3 + j*nx*3 + i*3 + comp] = in[k + j*nz + i*nz*ny];

            delete [] in;
        }
1135

1136 1137 1138
        H5Sclose(memSpace);
        H5Sclose(slabSpace);
        H5Sclose(dsSpace);
1139
        H5Dclose(ds);
1140 1141
    }

1142 1143
    H5Gclose(ts);

1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
    return array;
}


// ****************************************************************************
//  Method: avtPFLOTRANFileFormat::GetAuxiliaryData
//
//  Purpose:
//      Get Auxiliary Data associated with this file.
//
//  Arguments:
//      var        The variable of interest.
//      timestep   The index of the timestate.  If GetNTimesteps returned
//                 'N' time steps, this is guaranteed to be between 0 and N-1.
//      domain     The index of the domain.  If there are NDomains, this
//                 value is guaranteed to be between 0 and NDomains-1,
//                 regardless of block origin.
//      varname    The name of the variable requested.
//      type       The type of auxiliary data requested.
//      args       Optional arguments (unused)
//      df         Reference variable to a Destructor for the data returned.
//
//  Note: The only Auxiliary type supported is AUXILIARY_DATA_MATERIAL.
//
//  Programmer: Daniel Schep
//  Creation:   Thu Aug 26 14:14:09 EDT 2010
//
1171 1172 1173 1174
//  Modifications:
//    Jeremy Meredith, Wed Mar 30 13:17:54 EDT 2011
//    Close any HDF5 group/dataset we open.
//
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
// ****************************************************************************

void      *avtPFLOTRANFileFormat::GetAuxiliaryData(const char *var, int timestep, 
                                    int domain, const char *type, void *args, 
                                    DestructorFunction &df)
{
    void *retval = 0;

    if (strcmp(type, AUXILIARY_DATA_MATERIAL) == 0)
    {
        // Make sure the file is opened and ready.
        LoadFile();

        // Read the dataset from the file.
        hid_t ts = H5Gopen(fileID, times[timestep].second.c_str());
        hid_t ds = H5Dopen(ts, var);
        hid_t dsSpace = H5Dget_space(ds);
        int ndims = H5Sget_simple_extent_ndims(dsSpace);
        if (ndims != 3)
        {
1195 1196
            H5Dclose(ds);
            H5Gclose(ts);
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
            debug1 << "The variable " << var << " had only " << ndims << " dimensions" << endl;
            EXCEPTION1(InvalidVariableException, var);
        }

        hid_t slabSpace = H5Scopy(dsSpace);
        hsize_t start[] = {domainGlobalStart[0],domainGlobalStart[1],domainGlobalStart[2]};
        hsize_t count[] = {domainGlobalCount[0]-1,domainGlobalCount[1]-1,domainGlobalCount[2]-1};
        if (oldFileNeedingCoordFixup)
        {
            for (int dim=0; dim<3; dim++)
                count[dim]++;
        }
        H5Sselect_hyperslab(slabSpace, H5S_SELECT_SET, start, NULL, count, NULL);

        hsize_t beg[3];
        hsize_t end[3];
        H5Sget_select_bounds(slabSpace, beg, end);
        int nx = end[0]-beg[0]+1;
        int ny = end[1]-beg[1]+1;
        int nz = end[2]-beg[2]+1;
        int nvals = nx*ny*nz;

        int dims[3];
        dims[0] = nx;
        dims[1] = ny;
        dims[2] = nz;

        hid_t memSpace = H5Screate_simple(3,count,NULL);

        int *matlist = new int[nvals];

        int *in = new int[nvals];
        herr_t err = H5Dread(ds, H5T_NATIVE_INT, memSpace, slabSpace,
                             H5P_DEFAULT, in);
        // Input is in a different ordering (Fortran) than VTK wants (C).
        for (int i=0;i<nx;i++)
            for (int j=0;j<ny;j++)
                for (int k=0;k<nz;k++)
                    matlist[k*nx*ny + j*nx + i] = in[k + j*nz + i*nz*ny];


        map<int,bool> matls;
        for (int i=0;i<nvals;i++)
            matls[matlist[i]] = true;

        int nmats = matls.size();
        int *matnos = new int[nmats];
        char **names = new char*[nmats];
        int i = 0;
        for(map<int,bool>::iterator iter = matls.begin(); iter != matls.end();++iter)
        {
            matnos[i] = iter->first;
            names[i] = new char[8];
            sprintf(names[i], "%d", iter->first);
            ++i;
        }

        avtMaterial *mat = new avtMaterial(
            nmats,
            matnos,
            names,
            ndims,
            dims,
            0,
            matlist,
            0,0,0,0,0);


        delete [] in;
        delete [] matlist;
        delete [] matnos;
        for(int i=0;i<nmats;++i)
            delete [] names[i];
        delete [] names;

        retval = (void*)mat;
        df = avtMaterial::Destruct;
1274 1275 1276 1277

        H5Dclose(ds);
        H5Gclose(ts);

1278 1279 1280
    }

    return retval;
1281
}
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304


// ****************************************************************************
// Method:  avtPFLOTRANFileFormat::GetTimes
//
// Purpose:
//   We also set the times vector in the metadata directly above, but
//   that method doesn't work when time-grouping MT files.
//
// Arguments:
//   t          the time array to populate
//
// Programmer:  Jeremy Meredith
// Creation:    August 23, 2011
//
// ****************************************************************************
void
avtPFLOTRANFileFormat::GetTimes(std::vector<double> &t)
{
    int nt = times.size();
    for(int i=0;i<nt;i++)
        t.push_back(times[i].first);
}