File: hdiff_list.c

package info (click to toggle)
libhdf4 4.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 29,892 kB
  • sloc: ansic: 128,688; sh: 14,969; fortran: 12,444; java: 5,864; xml: 1,305; makefile: 900; yacc: 678; pascal: 418; perl: 360; javascript: 203; lex: 163; csh: 41
file content (1339 lines) | stat: -rw-r--r-- 43,257 bytes parent folder | download
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
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
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
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF.  The full HDF copyright notice, including       *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://support.hdfgroup.org/ftp/HDF/releases/.  *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include "hdf.h"
#include "mfhdf.h"
#include "hdiff_list.h"

static int   is_reserved(char *vg_class);
static char *get_path(char *path_name, char *obj_name);
static int   insert_an_data(int32 file_id, int32 ref_in, int32 tag_in, ann_type type, char *path);

/*-------------------------------------------------------------------------
 * Function: hdiff_list
 *
 * Purpose: locate all HDF objects in the file and return a list of them
 *
 * Return: number of objects in the file
 *
 * Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu
 *
 * Date: August 21, 2003
 *
 * Description:
 *
 * A main loop is used to locate all the objects in the file. This loop preserves the
 * hierarchy of the file. The algorithm used is
 * 1) Obtain the number of lone VGroups in the HDF file.
 * 2) Do a loop for each one of these groups. In each iteration a table is updated
 *    with the tag/reference pair of an object.
 *    2.1) Obtain the pairs of tag/references for the group
 *    2.2) Switch between the tag of the current object. Four cases are possible:
 *         1) Object is a group: recursively repeat the process (obtain the pairs of
 *            tag/references for this group and do another tag switch).
 *            Add the object to the table.
 *         2) Object is a dataset: Add the object to the table.
 *         3) Object is an image: Add the object to the table.
 *         4) Object is a vdata: Add the object to the table.
 * 3) Read all the HDF interfaces (SDS, GR and VS), checking for objects that are
 *    already in the table (meaning they belong to a previous inspected group,
 *    and should not be added).  These objects belong to a root group.
 * 4) Read all global attributes and annotations.
 *
 *-------------------------------------------------------------------------
 */

uint32
hdiff_list(const char *fname, dtable_t *table, diff_dim_table_t *td1, diff_dim_table_t *td2, int *err)
{
    int32 file_id = -1, sd_id = -1, gr_id = -1;

    /* open the file for read */
    if ((file_id = Hopen(fname, DFACC_READ, (int16)0)) == FAIL) {
        printf("Cannot open file <%s>\n", fname);
        goto out;
    }

    /* initialize the SD interface */
    if ((sd_id = SDstart(fname, DFACC_READ)) == FAIL) {
        printf("Could not start SD for <%s>\n", fname);
        goto out;
    }

    /* initialize the GR interface */
    if ((gr_id = GRstart(file_id)) == FAIL) {
        printf("Could not start GR for <%s>\n", fname);
        goto out;
    }

    /* iterate tru HDF interfaces */
    if (hdiff_list_vg(fname, file_id, sd_id, gr_id, table, td1, td2) < 0)
        goto out;
    if (hdiff_list_gr(file_id, gr_id, table) < 0)
        goto out;
    if (hdiff_list_sds(file_id, sd_id, table, td1, td2) < 0)
        goto out;
    if (hdiff_list_vs(file_id, table) < 0)
        goto out;
    if (hdiff_list_glb(sd_id, gr_id) < 0)
        goto out;
    if (hdiff_list_an(file_id) < 0)
        goto out;

    /* close */
    if (GRend(gr_id) == FAIL) {
        printf("Failed to close GR interface <%s>\n", fname);
        goto out;
    }
    if (SDend(sd_id) == FAIL) {
        printf("Failed to close SD interface <%s>\n", fname);
        goto out;
    }
    if (Hclose(file_id) == FAIL) {
        printf("Failed to close file <%s>\n", fname);
        goto out;
    }

    *err = 0;
    return table->nobjs;

out:

    if (sd_id != -1)
        SDend(sd_id);
    if (gr_id != -1)
        GRend(gr_id);
    if (file_id != -1)
        Hclose(file_id);

    *err = 1;
    return 0;
}

/*-------------------------------------------------------------------------
 * Function: hdiff_list_vg
 *
 * Purpose: locate all lone Vgroups in the file
 *
 * Return: SUCCEED, FAIL
 *
 *-------------------------------------------------------------------------
 */

int
hdiff_list_vg(const char *fname, int32 file_id, int32 sd_id, /* SD interface identifier */
              int32             gr_id,                       /* GR interface identifier */
              dtable_t         *table,                       /* all objects table */
              diff_dim_table_t *td1,                         /* dimension table 1 */
              diff_dim_table_t *td2)                         /* dimension table 2 */
{
    int32  vg_id;            /* vgroup identifier */
    int32  nlones = 0;       /* number of lone vgroups */
    int32  ntagrefs;         /* number of tag/ref pairs in a vgroup */
    int32 *ref_array = NULL; /* buffer to hold the ref numbers of lone vgroups   */
    int32 *tags      = NULL; /* buffer to hold the tag numbers of vgroups   */
    int32 *refs      = NULL; /* buffer to hold the ref numbers of vgroups   */
    int32  tag_vg;
    int32  ref_vg;
    char  *vg_name  = NULL;
    char  *vg_class = NULL;
    uint16 name_len;
    int32  i;

    /* initialize the V interface */
    if (Vstart(file_id) == FAIL) {
        printf("Error: Could not start group interface in <%s>\n", fname);
        return FAIL;
    }

    /*
     * get the names and class names of all the lone vgroups.
     * first, call Vlone with nlones set to 0 to get the number of
     * lone vgroups in the file, but not to get their reference numbers.
     */
    nlones = Vlone(file_id, NULL, nlones);

    if (nlones > 0) {
        /*
         * use the nlones returned to allocate sufficient space for the
         * buffer ref_array to hold the reference numbers of all lone vgroups,
         */
        ref_array = (int32 *)malloc(sizeof(int32) * nlones);

        /*
         * and call Vlone again to retrieve the reference numbers into
         * the buffer ref_array.
         */
        nlones = Vlone(file_id, ref_array, nlones);

        /*
         * iterate tru each lone vgroup.
         */
        for (i = 0; i < nlones; i++) {

            int32 ref = ref_array[i];
            /*
             * attach to the current vgroup then get its
             * name and class. note: the current vgroup must be detached before
             * moving to the next.
             */
            if ((vg_id = Vattach(file_id, ref, "r")) == FAIL) {
                printf("Error: Could not attach group with ref <%d>\n", ref);
                goto out;
            }

            if (Vgetnamelen(vg_id, &name_len) == FAIL) {
                printf("Error: Could not get name length for group with ref <%d>\n", ref);
                goto out;
            }

            free(vg_name);
            vg_name = (char *)malloc(sizeof(char) * (name_len + 1));

            if (Vgetname(vg_id, vg_name) == FAIL) {
                printf("Error: Could not get name for group with ref <%d>\n", ref);
                goto out;
            }

            if (Vgetclassnamelen(vg_id, &name_len) == FAIL) {
                printf("Error: Could not get classname length for group with ref <%d>\n", ref);
                goto out;
            }

            free(vg_class);
            vg_class = (char *)malloc(sizeof(char) * (name_len + 1));

            if (Vgetclass(vg_id, vg_class) == FAIL) {
                printf("Error: Could not get class for group with ref <%d>\n", ref);
                goto out;
            }

            /* ignore reserved HDF groups/vdatas */
            if (is_reserved(vg_class)) {
                if (Vdetach(vg_id) == FAIL) {
                    printf("Error: Could not detach group <%s>\n", vg_class);
                    goto out;
                }
                continue;
            }

            if (strcmp(vg_name, GR_NAME) == 0) {
                if (Vdetach(vg_id) == FAIL) {
                    printf("Error: Could not detach group <%s>\n", vg_class);
                    goto out;
                }
                continue;
            }

            /* get ref and tag */
            if ((ref_vg = VQueryref(vg_id)) == FAIL) {
                printf("Failed to get ref for <%s>\n", vg_name);
                goto out;
            }

            if ((tag_vg = VQuerytag(vg_id)) == FAIL) {
                printf("Failed to get tag for <%s>\n", vg_name);
                goto out;
            }

            assert(tag_vg == DFTAG_VG);

            /* add object to table */
            dtable_add(table, tag_vg, ref_vg, vg_name);

            insert_vg_attrs(vg_id, vg_name);
            insert_vg_an(file_id, vg_id, vg_name);

            /* insert objects for this group */
            ntagrefs = Vntagrefs(vg_id);
            if (ntagrefs > 0) {
                tags = (int32 *)malloc(sizeof(int32) * ntagrefs);
                refs = (int32 *)malloc(sizeof(int32) * ntagrefs);
                Vgettagrefs(vg_id, tags, refs, ntagrefs);

                insert_vg(fname, file_id, sd_id, gr_id, vg_name, tags, refs, ntagrefs, table, td1, td2);

                free(tags);
                free(refs);
            }

            if (Vdetach(vg_id) == FAIL) {
                printf("Error: Could not detach group <%s>\n", vg_name);
                goto out;
            }

            free(vg_name);
            vg_name = NULL;
            free(vg_class);
            vg_class = NULL;
        } /* for */

        /* free the space allocated */
        free(ref_array);
    } /* if */
    free(vg_name);
    free(vg_class);

    /* terminate access to the V interface */
    if (Vend(file_id) == FAIL) {
        printf("Error: Could not end group interface\n");
    }

    return 0;

out:
    free(vg_name);
    free(vg_class);
    free(tags);
    free(refs);
    free(ref_array);

    Vend(file_id);
    return FAIL;
}

/*-------------------------------------------------------------------------
 * Function: insert_vg
 *
 * Purpose: recursive function to locate objects in Vgroups
 *
 *-------------------------------------------------------------------------
 */

int
insert_vg(const char *fname, int32 file_id, int32 sd_id, /* SD interface identifier */
          int32             gr_id,                       /* GR interface identifier */
          char             *path_name,                   /* absolute path for input group name */
          int32            *in_tags,                     /* tag list for parent group */
          int32            *in_refs,                     /* ref list for parent group */
          int               npairs,                      /* number tag/ref pairs for parent group */
          dtable_t         *table,                       /* all objects table */
          diff_dim_table_t *td1,                         /* dimension table 1 */
          diff_dim_table_t *td2)                         /* dimension table 2 */

{
    int32 vg_id,            /* vgroup identifier */
        ntagrefs,           /* number of tag/ref pairs in a vgroup */
        tag,                /* temporary tag */
        ref;                /* temporary ref */
    int32 *tags     = NULL; /* buffer to hold the tag numbers of vgroups   */
    int32 *refs     = NULL; /* buffer to hold the ref numbers of vgroups   */
    char  *vg_name  = NULL;
    char  *vg_class = NULL;
    char  *path     = NULL;
    int    i;
    uint16 name_len;

    for (i = 0; i < npairs; i++) {
        tag = in_tags[i];
        ref = in_refs[i];

        switch (tag) {
                /*-------------------------------------------------------------------------
                 * VG
                 *-------------------------------------------------------------------------
                 */
            case DFTAG_VG:

                /* check if already inserted */
                if (dtable_search(table, DFTAG_VG, ref) >= 0) {
                    break;
                }

                vg_id = Vattach(file_id, ref, "r");
                if (Vgetnamelen(vg_id, &name_len) == FAIL) {
                    printf("Error: Could not get name length for group with ref <%d>\n", ref);
                    break;
                }

                free(vg_name);
                vg_name = (char *)malloc(sizeof(char) * (name_len + 1));

                Vgetname(vg_id, vg_name);

                if (Vgetclassnamelen(vg_id, &name_len) == FAIL) {
                    printf("Error: Could not get classname length for group with ref <%d>\n", ref);
                    break;
                }

                free(vg_class);
                vg_class = (char *)malloc(sizeof(char) * (name_len + 1));

                Vgetclass(vg_id, vg_class);

                /* ignore reserved HDF groups/vdatas */
                if (is_reserved(vg_class)) {
                    Vdetach(vg_id);
                    break;
                }

                if (strcmp(vg_name, GR_NAME) == 0) {
                    Vdetach(vg_id);
                    break;
                }

                /* initialize path */
                path = get_path(path_name, vg_name);

                /* add object to table */
                dtable_add(table, tag, ref, path);

                insert_vg_attrs(vg_id, path);
                insert_vg_an(file_id, vg_id, path);

                /* get objects for this group */
                ntagrefs = Vntagrefs(vg_id);
                if (ntagrefs > 0) {
                    tags = (int32 *)malloc(sizeof(int32) * ntagrefs);
                    refs = (int32 *)malloc(sizeof(int32) * ntagrefs);
                    Vgettagrefs(vg_id, tags, refs, ntagrefs);

                    /* recurse */
                    insert_vg(fname, file_id, sd_id, gr_id, path, tags, refs, ntagrefs, table, td1, td2);

                    free(tags);
                    free(refs);
                }
                if (Vdetach(vg_id) == FAIL) {
                    printf("Error: Could not detach group <%s>\n", vg_name);
                }
                free(path);

                break;

                /*-------------------------------------------------------------------------
                 * SDS
                 *-------------------------------------------------------------------------
                 */

            case DFTAG_SD:  /* Scientific Data */
            case DFTAG_SDG: /* Scientific Data Group */
            case DFTAG_NDG: /* Numeric Data Group */

                insert_sds(file_id, sd_id, tag, ref, path_name, table, td1, td2);

                break;

                /*-------------------------------------------------------------------------
                 * Image
                 *-------------------------------------------------------------------------
                 */
            case DFTAG_RI:  /* Raster Image */
            case DFTAG_CI:  /* Compressed Image */
            case DFTAG_RIG: /* Raster Image Group */
            case DFTAG_RI8: /* Raster-8 image */
            case DFTAG_CI8: /* RLE compressed 8-bit image */
            case DFTAG_II8: /* IMCOMP compressed 8-bit image */

                insert_gr(file_id, gr_id, tag, ref, path_name, table);

                break;

                /*-------------------------------------------------------------------------
                 * Vdata
                 *-------------------------------------------------------------------------
                 */
            case DFTAG_VH: /* Vdata Header */

                insert_vs(file_id, ref, path_name, table, 0);

                break;
        }
    }
    free(vg_name);
    free(vg_class);

    return 0;
}

/*-------------------------------------------------------------------------
 * Function: hdiff_list_gr
 *
 * Purpose: get top level GR images
 *
 * Return:  SUCCEED, FAIL
 *
 *-------------------------------------------------------------------------
 */

int
hdiff_list_gr(int32 file_id, int32 gr_id, /* GR interface identifier */
              dtable_t *table)
{
    int32 ri_id,               /* raster image identifier */
        n_rimages,             /* number of raster images in the file */
        n_file_attrs,          /* number of file attributes */
        ri_index,              /* index of a image */
        gr_ref,                /* reference number of the GR image */
        dim_sizes[2],          /* dimensions of an image */
        n_comps,               /* number of components an image contains */
        interlace_mode,        /* interlace mode of an image */
        data_type,             /* number type of an image */
        n_attrs;               /* number of attributes belong to an image */
    char name[H4_MAX_GR_NAME]; /* name of an image */

    /* determine the contents of the file */
    if (GRfileinfo(gr_id, &n_rimages, &n_file_attrs) < 0) {
        return FAIL;
    }

    for (ri_index = 0; ri_index < n_rimages; ri_index++) {
        ri_id = GRselect(gr_id, ri_index);
        GRgetiminfo(ri_id, name, &n_comps, &data_type, &interlace_mode, dim_sizes, &n_attrs);

        gr_ref = GRidtoref(ri_id);

        /* check if already inserted in Vgroup; search all image tags */
        if (dtable_search(table, DFTAG_RI, gr_ref) >= 0 || dtable_search(table, DFTAG_CI, gr_ref) >= 0 ||
            dtable_search(table, DFTAG_RIG, gr_ref) >= 0 || dtable_search(table, DFTAG_RI8, gr_ref) >= 0 ||
            dtable_search(table, DFTAG_CI8, gr_ref) >= 0 || dtable_search(table, DFTAG_II8, gr_ref) >= 0) {
            GRendaccess(ri_id);
            continue;
        }

        /* insert GR  */
        insert_gr(file_id, gr_id, DFTAG_RI, gr_ref, 0, table);

        /* terminate access to the current raster image */
        GRendaccess(ri_id);
    }

    return SUCCEED;
}

/*-------------------------------------------------------------------------
 * Function: hdiff_list_sds
 *
 * Purpose: get top level SDS
 *
 * Return: SUCCEED, FAIL
 *
 *-------------------------------------------------------------------------
 */

int
hdiff_list_sds(int32 file_id, int32 sd_id, /* SD interface identifier */
               dtable_t         *table,    /* all objects table */
               diff_dim_table_t *td1,      /* dimension table 1 */
               diff_dim_table_t *td2)      /* dimension table 2 */

{
    int32 sds_id,                   /* dataset identifier */
        n_datasets,                 /* number of datasets in the file */
        n_file_attrs,               /* number of file attributes */
        index,                      /* index of a dataset */
        sds_ref,                    /* reference number */
        dim_sizes[H4_MAX_VAR_DIMS], /* dimensions of an image */
        data_type,                  /* number type  */
        rank,                       /* rank */
        n_attrs;                    /* number of attributes */
    char name[H4_MAX_GR_NAME];      /* name of dataset */

    /* determine the number of data sets in the file and the number of file attributes */
    if (SDfileinfo(sd_id, &n_datasets, &n_file_attrs) < 0) {
        return FAIL;
    }

    for (index = 0; index < n_datasets; index++) {
        sds_id = SDselect(sd_id, index);
        SDgetinfo(sds_id, name, &rank, dim_sizes, &data_type, &n_attrs);
        sds_ref = SDidtoref(sds_id);

        /* check if already inserted in Vgroup; search all SDS tags */
        if (dtable_search(table, DFTAG_SD, sds_ref) >= 0 || dtable_search(table, DFTAG_SDG, sds_ref) >= 0 ||
            dtable_search(table, DFTAG_NDG, sds_ref) >= 0) {
            SDendaccess(sds_id);
            continue;
        }

        /* insert SDS  */
        insert_sds(file_id, sd_id, DFTAG_NDG, sds_ref, 0, table, td1, td2);

        /* terminate access to the current dataset */
        SDendaccess(sds_id);
    }

    return 0;
}

/*-------------------------------------------------------------------------
 * Function: hdiff_list_vs
 *
 * Purpose: get top level VS
 *
 *-------------------------------------------------------------------------
 */

int
hdiff_list_vs(int32 file_id, dtable_t *table)
{
    int32 nlones = 0, /* number of lone vdatas */
        *ref_array,   /* buffer to hold the ref numbers of lone vdatas   */
        ref;          /* temporary ref number  */
    int i;

    /* initialize the VS interface */
    Vstart(file_id);

    /*
     * get and print the names and class names of all the lone vdatas.
     * first, call Vlone with nlones set to 0 to get the number of
     * lone vdatas in the file, but not to get their reference numbers.
     */
    nlones = VSlone(file_id, NULL, nlones);

    if (nlones > 0) {
        /*
         * use the nlones returned to allocate sufficient space for the
         * buffer ref_array to hold the reference numbers of all lone vgroups,
         */
        ref_array = (int32 *)malloc(sizeof(int32) * nlones);

        /*
         * and call VSlone again to retrieve the reference numbers into
         * the buffer ref_array.
         */
        nlones = VSlone(file_id, ref_array, nlones);

        /*
         * iterate tru each lone vdata.
         */
        for (i = 0; i < nlones; i++) {
            /*
             * attach to the current vdata then get its
             * name and class. note: the current vdata must be detached before
             * moving to the next.
             */
            ref = ref_array[i];

            /* check if already inserted in Vgroup*/
            if (dtable_search(table, DFTAG_VH, ref) >= 0) {
                continue;
            }

            /* insert VS */
            insert_vs(file_id, ref, 0, table, 1);

        } /* for */

        /* free the space allocated */
        free(ref_array);
    } /* if */

    /* terminate access to the VS interface */
    Vend(file_id);
    return 0;
}

/*-------------------------------------------------------------------------
 * Function: insert_vg_attrs
 *
 * Purpose: insert VG attributes
 *
 *-------------------------------------------------------------------------
 */

int
insert_vg_attrs(int32 vg_in, char *path)
{
    int   n_attrs;
    int32 data_type, size, n_values;
    char  attr_name[H4_MAX_NC_NAME];
    int   i;

    /* Get the number of attributes attached to this vgroup.  */
    if ((n_attrs = Vnattrs2(vg_in)) == FAIL) {
        printf("Failed to get attributes for <%s>\n", path);
        return -1;
    }

    for (i = 0; i < n_attrs; i++) {
        if ((Vattrinfo2(vg_in, i, attr_name, &data_type, &n_values, &size, NULL, NULL)) == FAIL) {
            printf("Failed to get attribute %d of <%s>\n", i, path);
            continue;
        }
    }
    return 0;
}

/*-------------------------------------------------------------------------
 * Function: hdiff_list_glb
 *
 * Purpose: list/insert global SDS attributes, global GR attributes
 *
 *-------------------------------------------------------------------------
 */

int
hdiff_list_glb(int32 sd_id, /* SD interface identifier */
               int32 gr_id) /* GR interface identifier */
{
    int32 n_datasets, /* number of datasets in the file */
        n_file_attrs; /* number of file attributes */

    /*-------------------------------------------------------------------------
     * insert SDS global attributes
     *-------------------------------------------------------------------------
     */

    /* determine the number of data sets in the file and the number of file attributes */
    SDfileinfo(sd_id, &n_datasets, &n_file_attrs);

    insert_sds_attrs(sd_id, n_file_attrs);

    /*-------------------------------------------------------------------------
     * insert GR global attributes
     *-------------------------------------------------------------------------
     */

    /* determine the number of data sets in the file and the number of file attributes */
    GRfileinfo(gr_id, &n_datasets, &n_file_attrs);

    insert_gr_attrs(gr_id, n_file_attrs);

    return 0;
}

/*-------------------------------------------------------------------------
 * Function: hdiff_list_an
 *
 * Purpose: list/insert AN FILE objects
 *
 *-------------------------------------------------------------------------
 */

int
hdiff_list_an(int32 file_id)
{
    int32 an_id, /* AN interface identifier */
        ann_id,  /* an annotation identifier */
        i,       /* position of an annotation in all of the same type*/
        n_file_labels, n_file_descs, n_data_labels, n_data_descs;

    /* Initialize the AN interface  */
    an_id = ANstart(file_id);

    /*
     * Get the annotation information, e.g., the numbers of file labels, file
     * descriptions, data labels, and data descriptions.
     */
    ANfileinfo(an_id, &n_file_labels, &n_file_descs, &n_data_labels, &n_data_descs);

    /*-------------------------------------------------------------------------
     * AN_FILE_LABEL
     *-------------------------------------------------------------------------
     */

    for (i = 0; i < n_file_labels; i++) {
        /* Get the identifier of the current data label */
        ann_id = ANselect(an_id, i, AN_FILE_LABEL);

        /* Terminate access to the current data label */
        ANendaccess(ann_id);
    }

    /*-------------------------------------------------------------------------
     * AN_FILE_DESC
     *-------------------------------------------------------------------------
     */

    for (i = 0; i < n_file_descs; i++) {
        /* Get the identifier of the current data label */
        ann_id = ANselect(an_id, i, AN_FILE_DESC);

        /* Terminate access to the current data label */
        ANendaccess(ann_id);
    }

    /* Terminate access to the AN interface */
    ANend(an_id);

    return 0;
}

/*-------------------------------------------------------------------------
 * Function: insert_vg_an
 *
 * Purpose: insert Vgroup ANs
 *
 *-------------------------------------------------------------------------
 */

int
insert_vg_an(int32 file_id, int32 vg_id, char *path)
{
    int32 ref_in, tag_in;

    if ((ref_in = VQueryref(vg_id)) == FAIL) {
        printf("Failed to get ref for <%s>\n", path);
        return -1;
    }

    if ((tag_in = VQuerytag(vg_id)) == FAIL) {
        printf("Failed to get tag for <%s>\n", path);
        return -1;
    }

    insert_an(file_id, ref_in, tag_in, path);

    return 0;
}

/*-------------------------------------------------------------------------
 * Function: insert_vs_an
 *
 * Purpose: insert Vdata ANs
 *
 *-------------------------------------------------------------------------
 */

int
insert_vs_an(int32 file_id, int32 vdata_id, char *path)
{
    int32 ref_in, tag_in;

    if ((ref_in = VSQueryref(vdata_id)) == FAIL) {
        printf("Failed to get ref for <%s>\n", path);
        return -1;
    }
    if ((tag_in = VSQuerytag(vdata_id)) == FAIL) {
        printf("Failed to get tag for <%s>\n", path);
        return -1;
    }

    insert_an(file_id, ref_in, tag_in, path);

    return 1;
}

/*-------------------------------------------------------------------------
 * Function: insert_an_data
 *
 * Purpose: insert DATA ANs
 *
 *-------------------------------------------------------------------------
 */

static int
insert_an_data(int32 file_id, int32 ref_in, int32 tag_in, ann_type type, char *path)
{
    int32 an_id, /* AN interface identifier */
        ann_id,  /* an annotation identifier */
        i,       /* position of an annotation */
        n_anno;

    /* Initialize the AN interface  */
    an_id = ANstart(file_id);

    /* Get the number of ANs in this object  */
    if ((n_anno = ANnumann(an_id, type, (uint16)tag_in, (uint16)ref_in)) == FAIL) {
        printf("Failed to get annotations for <%s>\n", path);
        return -1;
    }

    for (i = 0; i < n_anno; i++) {
        if ((ann_id = ANselect(an_id, i, type)) == FAIL) {
            printf("Failed to select AN %d of <%s>\n", i, path);
            continue;
        }

        if (ANendaccess(ann_id) == FAIL) {
            printf("Failed to end AN %d of <%s>\n", i, path);
            continue;
        }
    }

    /* Terminate access to the AN interface */
    ANend(an_id);

    return 0;
}

/*-------------------------------------------------------------------------
 * Function: insert_an
 *
 * Purpose: insert DATA ANs (AN_DATA_LABEL and AN_DATA_DESC)
 *
 *-------------------------------------------------------------------------
 */

int
insert_an(int32 file_id, int32 ref_in, int32 tag_in, char *path)
{

    insert_an_data(file_id, ref_in, tag_in, AN_DATA_LABEL, path);
    insert_an_data(file_id, ref_in, tag_in, AN_DATA_DESC, path);

    return 0;
}

/*-------------------------------------------------------------------------
 * Function: insert_sds
 *
 * Purpose: insert an SDS into file object list
 *
 * Return: 0, -1 for error
 *
 * Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu
 *
 * Date: August 22, 2003
 *
 * Modifications: pvn. July the 13 (Friday), 2007
 *  Add support for lone dimensions
 *
 *-------------------------------------------------------------------------
 */

int
insert_sds(int32 file_id, int32 sd_id, int32 tag, /* tag of input SDS */
           int32             ref,                 /* ref of input SDS */
           char             *path_name,           /* absolute path for input group name */
           dtable_t         *table,               /* all objects table */
           diff_dim_table_t *td1,                 /* dimension table 1 */
           diff_dim_table_t *td2)                 /* dimension table 2 */
{
    int32 sds_id,                  /* data set identifier */
        sds_index,                 /* index number of the data set */
        dtype,                     /* SDS data type */
        dimsizes[H4_MAX_VAR_DIMS], /* dimensional size of SDS */
        nattrs,                    /* number of SDS attributes */
        rank,                      /* rank of SDS */
        dim_size,                  /* dimension size */
        dim_id;                    /* dimension ID */
    char  sds_name[H4_MAX_NC_NAME];
    char  dim_name[H4_MAX_NC_NAME];
    char *path = NULL;
    int   i;

    sds_index = SDreftoindex(sd_id, ref);
    sds_id    = SDselect(sd_id, sds_index);

    /*obtain name,rank,dimsizes,datatype and num of attributes of sds */
    SDgetinfo(sds_id, sds_name, &rank, dimsizes, &dtype, &nattrs);

    /* check if the given SDS is a dimension scale, return 0 for no table add */
    if (SDiscoordvar(sds_id)) {

        /* add SDS coordinate variable to dimension table 1 */
        diff_dim_table_add(td1, ref, sds_name);
        SDendaccess(sds_id);

        return 0;
    }

    /* initialize path */
    path = get_path(path_name, sds_name);

    /* add object to table */
    dtable_add(table, tag, ref, path);

    /*-------------------------------------------------------------------------
     * insert attributes
     *-------------------------------------------------------------------------
     */

    insert_sds_attrs(sds_id, nattrs);

    /*-------------------------------------------------------------------------
     * dimension scales
     *-------------------------------------------------------------------------
     */

    /* loop through each dimension up to rank of SDS */
    for (i = 0; i < rank; i++) {
        /* get dimension handle for input dimension */
        if ((dim_id = SDgetdimid(sds_id, i)) == FAIL) {
            printf("Failed to get dimension %d of SDS <%s>\n", i, path);
            continue;
        }
        /* get dimension information for input dimension */
        if (SDdiminfo(dim_id, dim_name, &dim_size, &dtype, &nattrs) == FAIL) {
            printf("Failed to get info for dimension %d of SDS <%s>\n", i, path);
            continue;
        }
        /* attributes */
        if (nattrs && insert_sds_attrs(dim_id, nattrs) == FAIL) {
            printf("Failed to copy attributes for dimension %d of of SDS <%s>\n", i, path);
            continue;
        }

        /* add dimension name to dimension scales table 2 */
        diff_dim_table_add(td2, -1, dim_name);
    }

    /*-------------------------------------------------------------------------
     * insert ANs
     *-------------------------------------------------------------------------
     */

    insert_an(file_id, ref, tag, path);

    /*-------------------------------------------------------------------------
     * terminate access to the SDSs
     *-------------------------------------------------------------------------
     */

    SDendaccess(sds_id);

    free(path);

    return 0;
}

/*-------------------------------------------------------------------------
 * Function: insert_sds_attrs
 *
 * Purpose: insert SDS attributes
 *   used for global, dataset and dimension attributes
 *
 *-------------------------------------------------------------------------
 */

int
insert_sds_attrs(int32 id_in, int32 nattrs)
{
    int32 dtype, /* SDS data type */
        nelms;   /* number of elements */
    char attr_name[H4_MAX_NC_NAME];
    int  i;

    /* loop through attributes in input SDS */
    for (i = 0; i < nattrs; i++) {
        if (SDattrinfo(id_in, i, attr_name, &dtype, &nelms) == FAIL) {
            printf("Cannot get info for attribute number %d\n", i);
            continue;
        }
    }

    return 0;
}

/*-------------------------------------------------------------------------
 * Function: insert_gr_attrs
 *
 * Purpose: insert GR attributes
 *
 *-------------------------------------------------------------------------
 */

int
insert_gr_attrs(int32 ri_id, int32 nattrs)
{
    int32 dtype, /* SDS data type */
        nelms;   /* number of elements */
    char attr_name[H4_MAX_NC_NAME];
    int  i;

    /* loop through attributes in input GR */
    for (i = 0; i < nattrs; i++) {
        if (GRattrinfo(ri_id, i, attr_name, &dtype, &nelms) == FAIL) {
            printf("Cannot get info for attribute number %d\n", i);
            continue;
        }
    }

    return 0;
}

/*-------------------------------------------------------------------------
 * Function: insert_vs_attrs
 *
 * Purpose: insert VS attributes
 *
 *-------------------------------------------------------------------------
 */

int
insert_vs_attrs(int32 in, int32 findex, intn attrindex)
{
    char  attr_name[H4_MAX_NC_NAME];
    int32 n_values, attr_size, attr_type;

    /* Get attribute information */
    VSattrinfo(in, findex, attrindex, attr_name, &attr_type, &n_values, &attr_size);

    return 0;
}

/*-------------------------------------------------------------------------
 * Function: insert_gr
 *
 * Purpose: insert a GR
 *
 *-------------------------------------------------------------------------
 */

int
insert_gr(int32 file_id, int32 gr_in, int32 tag, /* tag of input GR */
          int32     ref,                         /* ref of input GR */
          char     *path_name,                   /* absolute path for input group name */
          dtable_t *table)
{
    int32 ri_id,        /* raster image identifier */
        ri_index,       /* index of a image */
        dimsizes[2],    /* dimensions of an image */
        n_comps,        /* number of components an image contains */
        interlace_mode, /* interlace mode of an image */
        dtype,          /* number type of an image */
        n_attrs;        /* number of attributes belong to an image */

    int32 pal_id, /* palette identifier */
        r_num_entries, r_data_type, r_ncomp, r_interlace_mode;
    char  gr_name[H4_MAX_GR_NAME];
    char *path    = NULL;
    int   has_pal = 0;

    ri_index = GRreftoindex(gr_in, (uint16)ref);
    ri_id    = GRselect(gr_in, ri_index);

    GRgetiminfo(ri_id, gr_name, &n_comps, &dtype, &interlace_mode, dimsizes, &n_attrs);

    /* initialize path */
    path = get_path(path_name, gr_name);

    /* add object to table */
    dtable_add(table, tag, ref, path);

    /*-------------------------------------------------------------------------
     * insert attributes
     *-------------------------------------------------------------------------
     */

    insert_gr_attrs(ri_id, n_attrs);

    /*-------------------------------------------------------------------------
     * check for palette
     *-------------------------------------------------------------------------
     */

    pal_id = GRgetlutid(ri_id, 0);
    GRgetlutinfo(pal_id, &r_ncomp, &r_data_type, &r_interlace_mode, &r_num_entries);

    /*check if there is palette data */
    has_pal = ((r_ncomp == 0) || (r_interlace_mode < 0) || (r_num_entries == 0)) ? 0 : 1;

    if (has_pal == 1) {

    } /* has_pal==1 */

    /*-------------------------------------------------------------------------
     * insert ANs
     *-------------------------------------------------------------------------
     */

    insert_an(file_id, ref, DFTAG_RIG, path);
    insert_an(file_id, ref, DFTAG_RI, path);

    /*-------------------------------------------------------------------------
     * terminate access to the GR
     *-------------------------------------------------------------------------
     */

    /* terminate access to the GRs */
    GRendaccess(ri_id);

    free(path);

    return 0;
}

/*-------------------------------------------------------------------------
 * Function: insert_vs
 *
 * Purpose: insert a VS
 *
 *-------------------------------------------------------------------------
 */

int
insert_vs(int32 file_id, int32 ref, /* ref of input VS */
          char     *path_name,      /* absolute path for input group name */
          dtable_t *table, int is_lone)
{
    int32 vdata_id, /* vdata identifier */
        tag_vs, ref_vs;
    int   n_fields, n_attrs;
    char  vdata_name[VSNAMELENMAX], vdata_class[VSNAMELENMAX];
    char *path = NULL;
    int   i, j, ret = 1;

    /*-------------------------------------------------------------------------
     * attach the vdata, gets its name and class
     *-------------------------------------------------------------------------
     */

    if ((vdata_id = VSattach(file_id, ref, "r")) == FAIL) {
        printf("Failed to attach vdata ref %d\n", ref);
        return -1;
    }

    if (VSgetname(vdata_id, vdata_name) == FAIL) {
        printf("Failed to name for vdata ref %d\n", ref);
        return -1;
    }

    if (VSgetclass(vdata_id, vdata_class) == FAIL) {
        printf("Failed to name for vdata ref %d\n", ref);
        return -1;
    }

    /* ignore reserved HDF groups/vdatas; they are lone ones */
    if (is_lone == 1 && vdata_class[0] == '\0') {
        if (is_reserved(vdata_class)) {
            if (VSdetach(vdata_id) == FAIL)
                printf("Failed to detach vdata <%s>\n", path_name);
            return 0;
        }
    }

    if ((ref_vs = VSQueryref(vdata_id)) == FAIL) {
        printf("Failed to get ref for <%s>\n", vdata_name);
    }
    if ((tag_vs = VSQuerytag(vdata_id)) == FAIL) {
        printf("Failed to get tag for <%s>\n", vdata_name);
    }

    /* initialize path */
    path = get_path(path_name, vdata_name);

    /* add object to table */
    dtable_add(table, tag_vs, ref_vs, path);

    /*-------------------------------------------------------------------------
     * fields
     *-------------------------------------------------------------------------
     */

    if ((n_fields = VFnfields(vdata_id)) == FAIL) {
        printf("Failed getting fields for VS <%s>\n", path);
        ret = -1;
        goto out;
    }

    /*-------------------------------------------------------------------------
     * insert attributes
     *-------------------------------------------------------------------------
     */

    if ((n_attrs = VSfnattrs(vdata_id, -1)) == FAIL) {
        printf("Failed getting attributes for VS <%s>\n", path);
        ret = -1;
        goto out;
    }

    for (i = 0; i < n_attrs; i++) {
        insert_vs_attrs(vdata_id, -1, i);
    }

    /*-------------------------------------------------------------------------
     * insert field attributes
     *-------------------------------------------------------------------------
     */

    for (i = 0; i < n_fields; i++) {
        if ((n_attrs = VSfnattrs(vdata_id, i)) == FAIL) {
            printf("Failed getting fields for VS <%s>\n", path);
            ret = -1;
            goto out;
        }
        for (j = 0; j < n_attrs; j++) {
            insert_vs_attrs(vdata_id, i, j);
        }
    }

    /*-------------------------------------------------------------------------
     * insert ANs
     *-------------------------------------------------------------------------
     */

    insert_vs_an(file_id, vdata_id, path);

    /*-------------------------------------------------------------------------
     * terminate access to the VSs
     *-------------------------------------------------------------------------
     */

out:
    VSdetach(vdata_id);

    free(path);

    return ret;
}

/*-------------------------------------------------------------------------
 * Function: is_reserved
 *
 * Purpose: check for reserved Vgroup/Vdata class/names
 *
 * Return: 1 if reserved, 0 if not
 *
 *-------------------------------------------------------------------------
 */

static int
is_reserved(char *vg_class)
{
    int ret = 0;

    /* ignore reserved HDF groups/vdatas */
    if (vg_class != NULL) {
        if ((strcmp(vg_class, _HDF_ATTRIBUTE) == 0) || (strcmp(vg_class, _HDF_VARIABLE) == 0) ||
            (strcmp(vg_class, _HDF_DIMENSION) == 0) || (strcmp(vg_class, _HDF_UDIMENSION) == 0) ||
            (strcmp(vg_class, DIM_VALS) == 0) || (strcmp(vg_class, DIM_VALS01) == 0) ||
            (strcmp(vg_class, _HDF_CDF) == 0) || (strcmp(vg_class, GR_NAME) == 0) ||
            (strcmp(vg_class, RI_NAME) == 0) || (strcmp(vg_class, RIGATTRNAME) == 0) ||
            (strcmp(vg_class, RIGATTRCLASS) == 0)) {
            ret = 1;
        }

        /* class and name(partial) for chunk table i.e. Vdata */
        if ((strncmp(vg_class, "_HDF_CHK_TBL_", 13) == 0)) {
            ret = 1;
        }
    }

    return ret;
}

/*-------------------------------------------------------------------------
 * Function: get_path
 *
 * Purpose: return absolute path for an object
 *
 * Return: path
 *
 *-------------------------------------------------------------------------
 */

static char *
get_path(char *path_name, char *obj_name)
{
    char *path = NULL;
    /* initialize path */
    if (path_name != NULL) {
        path = (char *)malloc(strlen(path_name) + strlen(obj_name) + 2);
        strcpy(path, path_name);
        strcat(path, "/");
        strcat(path, obj_name);
    }
    else {
        path = (char *)malloc(strlen(obj_name) + 1);
        strcpy(path, obj_name);
    }
    return path;
}