File: avtLODIParticleFileFormat.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 (1185 lines) | stat: -rw-r--r-- 36,094 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
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
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
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
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
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
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
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
1135
1136
1137
1138
1139
1140
1141
1142
1143
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
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
/*****************************************************************************
*
* 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.
*
*****************************************************************************/

#include <avtLODIParticleFileFormat.h>
#include <NETCDFFileObject.h>
#include "vtk_netcdf.h"

#include <avtDatabaseMetaData.h>
#include <avtMaterial.h>
#include <avtMTSDFileFormatInterface.h>
#include <avtTypes.h>
#include <avtVariableCache.h>

#include <vtkCellType.h>
#include <vtkUnsignedCharArray.h>
#include <vtkShortArray.h>
#include <vtkIntArray.h>
#include <vtkLongArray.h>
#include <vtkFloatArray.h>
#include <vtkDoubleArray.h>
#include <vtkPoints.h>
#include <vtkUnstructuredGrid.h>

#include <InvalidVariableException.h>
#include <DebugStream.h>
#include <snprintf.h>

// ****************************************************************************
// Method: avtLODIParticleFileFormat::Identify
//
// Purpose: 
//   This method checks to see if the file is a LODI Particle file.
//
// Arguments:
//   fileObject : The file to check.
//
// Returns:    True if the file is a particle file; False otherwise.
//
// Note:       
//
// Programmer: Brad Whitlock
// Creation:   Mon Aug 15 18:04:15 PST 2005
//
// Modifications:
//   
// ****************************************************************************

bool
avtLODIParticleFileFormat::Identify(NETCDFFileObject *fileObject)
{
    bool isLODIParticle = false;

    std::string create_code;
    if(fileObject->ReadStringAttribute("create_code", create_code))
    {
        bool isLODI = create_code == "LODI";
        std::string create_type;
        if(isLODI && fileObject->ReadStringAttribute("create_type", create_type))
        {
            isLODIParticle = create_type == "particle data";
        }
    }

    return isLODIParticle;
}

// ****************************************************************************
// Method: avtLODIParticleFileFormat::CreateInterface
//
// Purpose: 
//   This method creates a MTSD file format interface containing LODI Particle
//   file format readers.
//
// Programmer: Brad Whitlock
// Creation:   Mon Aug 15 18:05:07 PST 2005
//
// Modifications:
//    Jeremy Meredith, Thu Jan 28 12:28:07 EST 2010
//    MTSD now accepts grouping multiple files into longer sequences, so
//    its interface has changed to accept both a number of timestep groups
//    and a number of blocks.
//   
// ****************************************************************************

avtFileFormatInterface *
avtLODIParticleFileFormat::CreateInterface(NETCDFFileObject *f,
    const char *const *list, int nList, int nBlock)
{
    int nTimestepGroups = nList / nBlock;
    avtMTSDFileFormat ***ffl = new avtMTSDFileFormat**[nTimestepGroups];
    for (int i = 0 ; i < nTimestepGroups ; i++)
    {
        ffl[i] = new avtMTSDFileFormat*[nBlock];
        for (int j = 0 ; j < nBlock ; j++)
        {
            ffl[i][j] = NULL;
        }
    }

    TRY
    {
        for (int i = 0 ; i < nTimestepGroups ; i++)
        {
            for (int j = 0 ; j < nBlock ; j++)
            {
                ffl[i][j] = new avtLODIParticleFileFormat(list[i*nBlock+j],
                                                          (i==0) ? f : NULL);
            }
        }
    }
    CATCH(VisItException)
    {
        for (int i = 0 ; i < nTimestepGroups ; i++)
        {
            for (int j = 0 ; j < nBlock ; j++)
            {
                if(ffl[i][j] != 0)
                    delete ffl[i][j];
            }
            delete [] ffl[i];
        }
        delete [] ffl;
        RETHROW;
    }
    ENDTRY

    return new avtMTSDFileFormatInterface(ffl, nTimestepGroups, nBlock);
}

// ****************************************************************************
// Method: avtLODIParticleFileFormat::avtLODIParticleFileFormat
//
// Purpose: 
//   Constructor for the avtLODIParticleFileFormat class.
//
// Arguments:
//   filename : The name of the file being read.
//   f        : The file object to use.
//
// Programmer: Brad Whitlock
// Creation:   Mon Aug 15 18:05:46 PST 2005
//
// Modifications:
//   
// ****************************************************************************

avtLODIParticleFileFormat::avtLODIParticleFileFormat(const char *filename) :
    avtMTSDFileFormat(&filename, 1), times(), sourceids()
{
    fileObject = new NETCDFFileObject(filename);
    timesRead = false;
}

avtLODIParticleFileFormat::avtLODIParticleFileFormat(const char *filename,
    NETCDFFileObject *f) : avtMTSDFileFormat(&filename, 1), times(), sourceids()
{
    fileObject = f;
    timesRead = false;
}

// ****************************************************************************
// Method: avtLODIParticleFileFormat::~avtLODIParticleFileFormat
//
// Purpose: 
//   Destructor for the avtLODIParticleFileFormat class.
//
// Arguments:
//
// Returns:    
//
// Note:       
//
// Programmer: Brad Whitlock
// Creation:   Mon Aug 15 18:06:30 PST 2005
//
// Modifications:
//   
// ****************************************************************************

avtLODIParticleFileFormat::~avtLODIParticleFileFormat()
{
    FreeUpResources();

    delete fileObject;
}

// ****************************************************************************
// Method: avtLODIParticleFileFormat::ActivateTimestep
//
// Purpose: 
//   Activates the specified time step.
//
// Arguments:
//   ts : The time step to activate.
//
// Programmer: Brad Whitlock
// Creation:   Mon Aug 15 18:06:45 PST 2005
//
// Modifications:
//   
// ****************************************************************************

void
avtLODIParticleFileFormat::ActivateTimestep(int ts)
{
    debug4 << "avtLODIParticleFileFormat::ActivateTimestep: ts=" << ts << endl;
    ReadTimes();
}

// ****************************************************************************
// Method: avtLODIParticleFileFormat::FreeUpResources
//
// Purpose: 
//   Frees up the resources used by this object.
//
// Programmer: Brad Whitlock
// Creation:   Mon Aug 15 18:07:08 PST 2005
//
// Modifications:
//   
// ****************************************************************************

void
avtLODIParticleFileFormat::FreeUpResources()
{
    debug4 << "avtLODIParticleFileFormat::FreeUpResources" << endl;
    fileObject->Close();
}

// ****************************************************************************
// Method: avtLODIParticleFileFormat::ReadTimes
//
// Purpose: 
//   Reads in the times to be returned by this reader.
//
// Returns:    True if the times were read; False otherwise.
//
// Programmer: Brad Whitlock
// Creation:   Mon Aug 15 18:07:27 PST 2005
//
// Modifications:
//   
// ****************************************************************************

bool
avtLODIParticleFileFormat::ReadTimes()
{
    const char *mName = "avtLODIParticleFileFormat::ReadTimes: ";
    debug4 << mName << endl;
    if(!timesRead)
    {
        // Set the times
        TypeEnum t = NO_TYPE;
        int ndims = 0, *dims = 0;
        void *values = 0;
        if(fileObject->ReadVariable("elapsed_time", &t, &ndims, &dims, &values))
        {
            if(ndims == 1 && t == DOUBLEARRAY_TYPE)
            {
                debug4 << mName << "times={";
                double *dptr = (double *)values;
                intVector cycles;
                for(int i = 0; i < dims[0]; ++i)
                {
                    debug4 << ", " << *dptr;
                    cycles.push_back(i);
                    times.push_back(*dptr++);
                }
                debug4 << "}" << endl;
                timesRead = true;
            }
            else
            {
                debug4 << mName << "elapsed_time was read but it was the "
                       << "wrong type." << endl;
            }

            delete [] dims;
            free_void_mem(values, t);
        }
        else
        {
            debug4 << mName << "Could not read elapsed_time array!" << endl;
        }
    }

    return timesRead;
}

// ****************************************************************************
// Method: avtLODIParticleFileFormat::GetNTimesteps
//
// Purpose: 
//   Returns the number of time states in this database.
//
// Returns:    The number of time states.
//
// Programmer: Brad Whitlock
// Creation:   Mon Aug 15 18:08:06 PST 2005
//
// Modifications:
//   
// ****************************************************************************

int
avtLODIParticleFileFormat::GetNTimesteps()
{
    const char *mName = "avtLODIParticleFileFormat::GetNTimesteps: ";
    debug4 << mName << endl;
    ReadTimes();
    debug4 << mName << "returning " << times.size() << endl;
    return times.size();
}

// ****************************************************************************
// Method: avtLODIParticleFileFormat::GetTimes
//
// Purpose: 
//   Returns the times in the file.
//
// Arguments:
//   t : The times to be returned.
//
// Programmer: Brad Whitlock
// Creation:   Mon Aug 15 18:08:32 PST 2005
//
// Modifications:
//   
// ****************************************************************************

void
avtLODIParticleFileFormat::GetTimes(doubleVector &t)
{
    debug4 << "avtLODIParticleFileFormat::GetTimes" << endl;
    ReadTimes();
    t = times;
}

// ****************************************************************************
// Method: avtLODIParticleFileFormat::PopulateDatabaseMetaData
//
// Purpose: 
//   Populates the list of variables for this file.
//
// Arguments:
//   md : The metadata object to populate.
//
// Note:       Creates a title and a mesh name.
//
// Programmer: Brad Whitlock
// Creation:   Mon Aug 15 18:09:03 PST 2005
//
// Modifications:
//    Jeremy Meredith, Thu Aug 25 12:55:29 PDT 2005
//    Added group origin to mesh metadata constructor.
//   
//    Hank Childs, Fri Feb 23 09:22:20 PST 2007
//    Fix memory leak.
//
//    Mark C. Miller, Wed Apr 22 13:48:13 PDT 2009
//    Changed interface to DebugStream to obtain current debug level.
// ****************************************************************************

void
avtLODIParticleFileFormat::PopulateDatabaseMetaData(avtDatabaseMetaData *md)
{
    const char *mName = "avtLODIParticleFileFormat::PopulateDatabaseMetaData: ";
    debug4 << mName << endl;
    if(DebugStream::Level4())
        fileObject->PrintFileContents(DebugStream::Stream4());

    // Assemble a database title.
    std::string comment(GetType()), titleString, create_version, 
                create_date_time;
    if(fileObject->ReadStringAttribute("title", titleString))
    {
        comment += (std::string(" database: title=") + titleString);

        if(fileObject->ReadStringAttribute("create_version", create_version))
            comment += (std::string(", create_version=") + create_version);

        if(fileObject->ReadStringAttribute("create_date_time", create_date_time))
            comment += (std::string(", create_date_time=") + create_date_time);

        md->SetDatabaseComment(comment);
    }

    //
    // Add a point mesh for the particles
    //
    avtMeshMetaData *mmd = new avtMeshMetaData("particles", 1, 0, 1, 0, 3, 0,
        AVT_POINT_MESH);
    // Read the mesh units
    std::string meshUnits;
    if(fileObject->ReadStringAttribute("part_posn", "units", meshUnits))
    {
        mmd->xUnits = meshUnits;
        mmd->yUnits = meshUnits;
        mmd->zUnits = meshUnits;
    }
    // Read the mesh labels
    TypeEnum t = NO_TYPE;
    int ndims = 0, *dims = 0;
    void *values = 0;
    debug4 << mName << "Trying to read dimnum_labels" << endl;
    if(fileObject->ReadVariable("dimnum_labels", &t, &ndims, &dims, &values))
    {
        if(ndims == 2 && t == CHARARRAY_TYPE)
        {
            int len = dims[1];
            char *cptr = (char *)values;

            char *labels = new char[3 * (len+1)];
            char *xL = labels;
            char *yL = labels + len + 1;
            char *zL = yL + len + 1;
            memset(labels, 0, 3 * (len+1));
            memcpy(xL, cptr,       len);
            memcpy(yL, cptr+len,   len);
            memcpy(zL, cptr+len*2, len);

            mmd->xLabel = xL;
            debug4 << mName << "xLabel = " << xL << endl;

            mmd->yLabel = yL;
            debug4 << mName << "yLabel = " << yL << endl;

            mmd->zLabel = zL;
            debug4 << mName << "zLabel = " << zL << endl;

            delete [] labels;
        }

        delete [] dims;
        free_void_mem(values, t);
    }
    md->Add(mmd);

    //
    // Read the sourceid variable and create material names from it.
    //
    sourceids.clear();
    if(fileObject->ReadVariable("sourceid", &t, &ndims, &dims, &values))
    {
        if(t == CHARARRAY_TYPE && ndims == 2)
        {
            int nsrcs = dims[0];
            int len = dims[1];
            char *name = new char[len+1];
            char *start = (char *)values;

            debug4 << mName << "sourceid={";
            for(int i = 0; i < nsrcs; ++i)
            {
                char *namestart = start + len * i;
                memcpy((void*)name, (void*)namestart, len);
                name[len] = '\0';

                char *end = name + len - 1;
                while(end >= name && *end == ' ')
                    *end-- = '\0';

                sourceids.push_back(name);

                if(i > 0)
                    debug4 << ", ";
                debug4 << name;
            }
            debug4 << "}" << endl;
            delete [] name;

            // Add the material to the metadata.
            avtMaterialMetaData *matmd = new avtMaterialMetaData("sourceid",
                "particles", sourceids.size(), sourceids);
            md->Add(matmd);
        }

        delete [] dims;
        free_void_mem(values, t);
    }

    //
    // Look for variables defined on the particles mesh.
    //
    if(fileObject->InqVariable("part_posn", &t, &ndims, &dims))
    {
        // Iterate over all of the variables and pick those that have
        // the same number of elements as nPts.
        int status, nDims, nVars, nGlobalAtts, unlimitedDimension;
        status = nc_inq(fileObject->GetFileHandle(), &nDims, &nVars, &nGlobalAtts,
                        &unlimitedDimension);
        if(status != NC_NOERR)
        {
            fileObject->HandleError(status);
            return;
        }

        // Get the sizes of all dimensions.
        int i;
        size_t *dimSizes = new size_t[nDims];
        for(i = 0; i < nDims; ++i)
        {
            int status = nc_inq_dimlen(fileObject->GetFileHandle(), i, &dimSizes[i]);
            if(status != NC_NOERR)
                fileObject->HandleError(status);
        }

        // Determine the maximum number of points in the particle mesh.
        int nElems = 1;
        for(i = 0; i < ndims; ++i)
            nElems *= dims[i];
        int nPts = nElems / 3;
        delete [] dims;

        // Look for variables with the same number of values as nPts.
        for(i = 0; i < nVars; ++i)
        {
            char varname[NC_MAX_NAME+1];
            nc_type vartype;
            int  varndims;
            int  vardims[NC_MAX_VAR_DIMS];
            int  varnatts;
            if((status = nc_inq_var(fileObject->GetFileHandle(), i, varname,
                                    &vartype, &varndims, 
                                    vardims, &varnatts)) == NC_NOERR)
            {
                nElems = 1;
                for(int j = 0; j < varndims; ++j)
                    nElems *= dimSizes[vardims[j]];
                if(nElems == nPts)
                {
                    avtScalarMetaData *smd = new avtScalarMetaData(varname,
                        "particles", AVT_NODECENT);
                    smd->hasUnits = fileObject->ReadStringAttribute(
                        varname, "units", smd->units);
                    md->Add(smd);
                }
            }
        }

        delete [] dimSizes;
    }
}

// ****************************************************************************
// Method: avtLODIParticleFileFormat::GetMesh
//
// Purpose: 
//   Retrieves the mesh for the specified time state.
//
// Arguments:
//   ts  : The time state.
//   var : The name of the mesh to get.
//
// Returns:    A VTK dataset.
//
// Note:       We use the fill_value attribute of the part_posn field to
//             restrict points that should not be added to the final point mesh.
//
// Programmer: Brad Whitlock
// Creation:   Mon Aug 15 18:09:47 PST 2005
//
// Modifications:
//   
// ****************************************************************************

vtkDataSet *
avtLODIParticleFileFormat::GetMesh(int ts, const char *var)
{
    const char *mName = "avtLODIParticleFileFormat::GetMesh: ";
    debug4 << mName << "ts=" << ts
           << ", var=" << var << endl;

    vtkDataSet *retval = 0;
    bool err = true;
    if(strcmp(var, "particles") == 0)
    {
        TypeEnum t = NO_TYPE;
        int ndims = 0, *dims = 0;
        if(fileObject->InqVariable("part_posn", &t, &ndims, &dims))
        {
            size_t *starts = new size_t[ndims];
            size_t *counts = new size_t[ndims];
            int nElems = 1;
            for(int i = 1; i < ndims; ++i)
            {
                starts[i] = 0;
                counts[i] = dims[i];
                nElems *= dims[i];
            }
            starts[0] = ts;
            counts[0] = 1;

            int nPts = nElems / 3, varId = 0;
            vtkPoints *pts = vtkPoints::New();
            fileObject->GetVarId("part_posn", &varId);

            //
            // Get the particle mask for the current time step.
            //
            bool *particleMask = GetParticleMask(ts);
            if(particleMask != 0)
            {
                debug4 << mName << "Adding particles using the "
                       << "particle mask." << endl;
                pts->Allocate(nPts);
                float *points = new float[nElems];
                int status = nc_get_vara_float(fileObject->GetFileHandle(),
                                               varId, starts, counts, points);
                if(status == NC_NOERR)
                {
                    vtkUnstructuredGrid *ugrid = vtkUnstructuredGrid::New(); 
                    ugrid->SetPoints(pts);
                    ugrid->Allocate(nPts);
                    vtkIdType onevertex[1];
                    float *fptr = points;
                    vtkIdType ptIndex = 0;
                    for (int i = 0 ; i < nPts ; i++)
                    {
                        if(particleMask[i])
                        {
                            pts->InsertNextPoint(fptr);
                            onevertex[0] = ptIndex++;
                            ugrid->InsertNextCell(VTK_VERTEX, 1, onevertex);
                        }
                        fptr += 3;
                    }
                    debug4 << mName << "Added " << ptIndex
                           << " points to the mesh." << endl;
                    err = false;
                    retval = ugrid;
                }
                else
                    fileObject->HandleError(status);

                delete [] points;
            }
            else
            {
                debug4 << mName << "Adding all particles." << endl;

                //
                // Read the coordinates into the VTK points array.
                //
                int nPts = nElems / 3, varId = 0;
                vtkPoints *pts = vtkPoints::New();
                pts->SetNumberOfPoints(nPts);
                float *fptr = (float *)pts->GetVoidPointer(0);
                int status = nc_get_vara_float(fileObject->GetFileHandle(),
                                               varId, starts, counts, fptr);
                if(status == NC_NOERR)
                {
                    vtkUnstructuredGrid *ugrid = vtkUnstructuredGrid::New(); 
                    ugrid->SetPoints(pts);
                    ugrid->Allocate(nPts);
                    vtkIdType onevertex[1];
                    for (int i = 0 ; i < nPts ; i++)
                    {
                        onevertex[0] = i;
                        ugrid->InsertNextCell(VTK_VERTEX, 1, onevertex);
                    }

                    err = false;
                    retval = ugrid;
                }
                else
                    fileObject->HandleError(status);
            }

            pts->Delete();
            delete [] starts;
            delete [] counts;
            delete [] dims;
        }
    }

    if(err)
    {
        EXCEPTION1(InvalidVariableException, var);
    }

    return retval;
}

// ****************************************************************************
// Method: avtLODIParticleFileFormat::GetVar
//
// Purpose: 
//   Returns the a variable
//
// Arguments:
//   ts  : The time state.
//   var : The name of the variable to get.
//
// Returns:    Nothing yet.
//
// Note:       
//
// Programmer: Brad Whitlock
// Creation:   Mon Aug 15 18:11:21 PST 2005
//
// Modifications:
//   
// ****************************************************************************

vtkDataArray *
avtLODIParticleFileFormat::GetVar(int ts, const char *var)
{
    debug4 << "avtLODIParticleFileFormat::GetVar: ts=" << ts
           << ", var=" << var << endl;

    vtkDataArray *retval = 0;

    // Try and get the particle mask.
    bool *mask = GetParticleMask(ts);

    TypeEnum t = NO_TYPE;
    int ndims = 0, *dims = 0;
    if(mask != 0 && fileObject->InqVariable(var, &t, &ndims, &dims))
    {
        // Figure out the size of the chunk that we want to read.
        size_t *starts = new size_t[ndims];
        size_t *counts = new size_t[ndims];
        int nPts = 1;
        for(int i = 1; i < ndims; ++i)
        {
            starts[i] = 0;
            counts[i] = dims[i];
            nPts *= dims[i];
        }
        delete [] dims;
        starts[0] = ts;
        counts[0] = 1;

        int varId;
        fileObject->GetVarId(var, &varId);

#define READVAR(VTK, T, FUNC) \
        {\
            T *values = new T[nPts];\
            int status = FUNC(fileObject->GetFileHandle(),\
                              varId, starts, counts, values);\
            VTK *arr = VTK::New();\
            arr->Allocate(nPts);\
            vtkIdType index = 0;\
            for(int j = 0; j < nPts; ++j)\
            {\
                if(mask[j])\
                    arr->SetValue(index++, values[j]);\
            }\
            delete [] values;\
            arr->SetNumberOfTuples(index); \
            arr->Squeeze();\
            retval = arr;\
        }

        if(t == CHARARRAY_TYPE)
            READVAR(vtkUnsignedCharArray, char, nc_get_vara_text)
        else if(t == UCHARARRAY_TYPE)
            READVAR(vtkUnsignedCharArray, unsigned char, nc_get_vara_uchar)
        else if(t == SHORTARRAY_TYPE)
            READVAR(vtkShortArray, short, nc_get_vara_short)
        else if(t == INTEGERARRAY_TYPE)
            READVAR(vtkIntArray, int, nc_get_vara_int)
        else if(t == LONGARRAY_TYPE)
            READVAR(vtkLongArray, long, nc_get_vara_long)
        else if(t == FLOATARRAY_TYPE)
            READVAR(vtkFloatArray, float, nc_get_vara_float)
        else if(t == DOUBLEARRAY_TYPE)
            READVAR(vtkDoubleArray, double, nc_get_vara_double)

        delete [] starts;
        delete [] counts;
    }
    else
    {
        EXCEPTION1(InvalidVariableException, var);
    }

    return retval;
}

// ****************************************************************************
// Method: avtLODIParticleFileFormat::GetAuxiliaryData
//
// Purpose: 
//   Gets the material object for the particles.
//
// Arguments:
//
// Returns:    
//
// Note:       
//
// Programmer: Brad Whitlock
// Creation:   Tue Aug 16 18:05:10 PST 2005
//
// Modifications:
//   
// ****************************************************************************

void *
avtLODIParticleFileFormat::GetAuxiliaryData(const char *var, int ts,
    const char *type, void *args, DestructorFunction &df)
{
    avtMaterial *retval = 0;

    if(strcmp(type, AUXILIARY_DATA_MATERIAL) == 0 &&
       strcmp(var, "sourceid") == 0)
    {
        df = avtMaterial::Destruct;

        // Figure out the max number of particles per time state.
        int nPts = 1;
        TypeEnum t = NO_TYPE;
        int ndims = 0, *dims = 0;
        int maxPointsPerSource = -1;
        if(fileObject->InqVariable("in_grid", &t, &ndims, &dims))
        {
            for(int i = 1; i < ndims; ++i)
                nPts *= dims[i];
            maxPointsPerSource = dims[ndims-1];
            delete [] dims;
        }
        if(maxPointsPerSource == -1)
            maxPointsPerSource = nPts;

        // Get the particle mask, which tells us which particles to keep
        // and which to throw out.
        bool *mask = GetParticleMask(ts);
        if(mask != 0)
        {
            // Use the particle mask to build a list of materials.
            intVector matlist;
            matlist.reserve(nPts);
            for(int i = 0; i < nPts; ++i)
            {
                if(mask[i])
                    matlist.push_back(i / maxPointsPerSource + 1);
            }

            // Create matnos and names arrays so we can create an avtMaterial.
            int *matnos = new int[sourceids.size()];
            char **names = new char *[sourceids.size()];
            for(int i = 0; i < sourceids.size(); ++i)
            {
                matnos[i] = i + 1;
                names[i] = (char *)sourceids[i].c_str();
            }

            // Create the avtMaterial.
            int mdims[1];
            mdims[0] = matlist.size();
            retval = new avtMaterial(
                sourceids.size(),
                matnos,
                names,
                1,
                mdims,
                0,
                &matlist[0],
                0,
                0,
                0,
                0,
                0
                );

            delete [] matnos;
            delete [] names;
        }
    }

    return retval;
}

// ****************************************************************************
// Method: avtLODIParticleFileFormat::GetFillValue
//
// Purpose: 
//   Returns the fill value that is used to blank out points.
//
// Arguments:
//   fill_value : The return fill value.
//
// Returns:    True if the fill value was read; False otherwise.
//
// Note:       
//
// Programmer: Brad Whitlock
// Creation:   Tue Aug 16 18:19:19 PST 2005
//
// Modifications:
//   
// ****************************************************************************

bool
avtLODIParticleFileFormat::GetFillValue(float &fill_value)
{
    const char *mName = "avtLODIFileFormat::GetFillValue: ";
    bool haveFillValue = false;
    TypeEnum t = NO_TYPE;
    int ndims = 0, *dims = 0;
    void *values = 0;
    if(fileObject->ReadAttribute("part_posn", "fill_value", &t,
       &ndims, &dims, &values))
    {
        if(ndims == 1 && t == DOUBLEARRAY_TYPE)
        {
            double *d = (double *)values;
            fill_value = float(*d);
            haveFillValue = true;
            debug4 << mName << "part_posn fill value is: "
                   << fill_value << endl;
        }
        delete [] dims;
        free_void_mem(values, t);
    }

    return haveFillValue;
}

// ****************************************************************************
// Method: avtLODIParticleFileFormat::CreateParticleMask
//
// Purpose: 
//   Returns an array of bools (1 per point) indicating which points are good
//   and should remain in the visualization.
//
// Arguments:
//   ts : The time state for which we want the particle mask.
//
// Returns:    An array of bools or 0 if we could not create it.
//
// Note:       We check the part_posn array for values that have been blanked
//             using the fill_value and we check the in_grid array to make sure
//             that a point's value is 1.
//
// Programmer: Brad Whitlock
// Creation:   Tue Aug 16 18:20:06 PST 2005
//
// Modifications:
//   
// ****************************************************************************

bool *
avtLODIParticleFileFormat::CreateParticleMask(int ts)
{
    const char *mName = "avtLODIParticleFileFormat::CreateParticleMask: ";
    bool *mask = 0;

    TypeEnum t = NO_TYPE;
    int ndims = 0, *dims = 0;
    if(fileObject->InqVariable("part_posn", &t, &ndims, &dims))
    {
        // Figure out the size of the chunk that we want to read.
        size_t *starts = new size_t[ndims];
        size_t *counts = new size_t[ndims];
        int nElems = 1;
        for(int i = 1; i < ndims; ++i)
        {
            starts[i] = 0;
            counts[i] = dims[i];
            nElems *= dims[i];
        }
        delete [] dims;
        starts[0] = ts;
        counts[0] = 1;
        int nPts = nElems / 3;

        //
        // Read the fill_value attribute if we can and use it to
        // eliminate points that should not be added to the mesh.
        //
        float fill_value;
        bool haveFillValue = GetFillValue(fill_value);

        if(haveFillValue)
        {
            int varId;
            fileObject->GetVarId("part_posn", &varId);
            float *points = new float[nElems];
            int status = nc_get_vara_float(fileObject->GetFileHandle(),
                                           varId, starts, counts, points);
            if(status == NC_NOERR)
            {
                debug4 << mName << "Using part_posn and fill_value to fill "
                       << "in particle mask for ts=" << ts << endl;
                mask = new bool[nPts];

                float *fptr = points;
                for (int i = 0 ; i < nPts ; i++)
                {
                    mask[i] = (fptr[0] != fill_value &&
                               fptr[1] != fill_value &&
                               fptr[2] != fill_value);
                    fptr += 3;
                }
            }
            else
                fileObject->HandleError(status);

            delete [] points;
        }

        if(mask == 0)
        {
            mask = new bool[nPts];
            for(int i = 0; i < nPts; ++i)
                mask[i] = true;
        }

        delete [] starts;
        delete [] counts;
    }

#ifdef USE_IN_GRID_IN_PARTICLE_MASK
    if(fileObject->InqVariable("in_grid", &t, &ndims, &dims))
    {
        // Figure out the size of the chunk that we want to read.
        size_t *starts = new size_t[ndims];
        size_t *counts = new size_t[ndims];
        int nElems = 1;
        for(int i = 1; i < ndims; ++i)
        {
            starts[i] = 0;
            counts[i] = dims[i];
            nElems *= dims[i];
        }
        delete [] dims;
        starts[0] = ts;
        counts[0] = 1;
        int nPts = nElems;

        int varId;
        fileObject->GetVarId("in_grid", &varId);
        int *in_grid = new int[nElems];
        int status = nc_get_vara_int(fileObject->GetFileHandle(),
                                     varId, starts, counts, in_grid);
        if(status != NC_NOERR)
            fileObject->HandleError(status);
        
        if(mask == 0)
        {
            mask = new bool[nPts];
            for(int i = 0; i < nPts; ++i)
                mask[i] = true;
        }

        // If we were able to read in_grid, only admit those that are
        // in the grid and are not set to the fill_value.
        if(in_grid != 0)
        {
            debug4 << mName << "Using in_grid to further fill "
                   << "in particle mask for ts=" << ts << endl;

            for(int i = 0; i < nPts; ++i)
                mask[i] &= (in_grid[i] == 1);
        }

        delete [] in_grid;
        delete [] starts;
        delete [] counts;
    }
#endif
    return mask;
}

// ****************************************************************************
// Method: avtLODIParticleFileFormat::GetParticleMask
//
// Purpose: 
//   Gets the particle mask for the specified time state, creating it and
//   caching it as necessary.
//
// Arguments:
//   ts : The time state for which to return the particle mask.
//
// Returns:    The particle mask array or 0.
//
// Note:       
//
// Programmer: Brad Whitlock
// Creation:   Tue Aug 16 18:21:52 PST 2005
//
// Modifications:
//   
// ****************************************************************************

bool *
avtLODIParticleFileFormat::GetParticleMask(int ts)
{
    const char *mName = "avtLODIParticleFileFormat::GetParticleMask: ";
    const char *MASK_KEY = "mask key";

    char key[1000];
    SNPRINTF(key, 1000, "%s_particle_mask_ts=%d",
        fileObject->GetName().c_str(), ts);
    bool *retval = 0;

    void_ref_ptr vr = cache->GetVoidRef(key, MASK_KEY, ts, -1);
    if(*vr != 0)
    {
        debug4 << mName << "Found a cached particle mask for "
               << key << " at: " << (*vr) << endl;
        retval = (bool *)(*vr);
    }
    else
    {
        retval = CreateParticleMask(ts);
        if(retval != 0)
        {
            debug4 << mName << "Created a new particle mask for "
                   << key << endl;

            // Store it in the cache.
            void_ref_ptr vr2 = void_ref_ptr(retval, ParticleMaskDestruct);
            cache->CacheVoidRef(key, MASK_KEY, ts, -1, vr2);
        }
        else
             debug4 << mName << "Could not create particle mask" << endl;
    }

    return retval;
}

// ****************************************************************************
// Method: avtLODIParticleFileFormat::ParticleMaskDestruct
//
// Purpose: 
//   This method is called when the cache wants to delete its cached particle
//   masks.
//
// Arguments:
//   ptr : The particle mask array to delete.
//
// Programmer: Brad Whitlock
// Creation:   Tue Aug 16 18:22:44 PST 2005
//
// Modifications:
//   
// ****************************************************************************

void
avtLODIParticleFileFormat::ParticleMaskDestruct(void *ptr)
{
    debug4 << "avtLODIParticleFileFormat::ParticleMaskDestruct: " << ptr << endl;
    bool *bptr = (bool *)ptr;
    delete [] bptr;
}