summaryrefslogtreecommitdiffstats
path: root/tools/h5dump.c
blob: 5901c09318ae36027bdf061402b17e44a58bd4de (plain)
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
#include <H5private.h>
#include <h5dump.h>

static int indent = 0;

/*-------------------------------------------------------------------------
 * Function:    usage
 *
 * Purpose:     Print the usage message about dumper
 *
 * Return:      void
 *
 * Programmer:  Ruey-Hsia Li
 *
 * Modifications:
 *
 *-----------------------------------------------------------------------*/
static void
usage()
{

printf("Usage: dumper <filename>\n");

/*
    printf(" Usage: h5dumper [options] <file>\n");
    printf(" options\n");
    printf("  -H         Print a usage message \n");
    printf("  -nobb      No boot block\n");
    printf("  -nodata    No data\n");
    printf("  -g <name>  Dump everything in the group with name <name>\n");
    printf("  -d <name>  Dump everything in the dataset with name <name>\n");
    printf("  -l <name>  Dump the target of the link with name <link>\n");
*/
}


/*-------------------------------------------------------------------------
 * Function:    indentation
 *
 * Purpose:     Print spaces for indentation
 *
 * Return:      void
 *
 * Programmer:  Ruey-Hsia Li
 *
 * Modifications:
 *
 *-----------------------------------------------------------------------*/
static void indentation(int x) {

  while (x>0) {
         printf(" "); 
         x--;
  }

}


/*-------------------------------------------------------------------------
 * Function:    datatype_name
 *
 * Purpose:     Returns the name of data type. 
 *
 * Return:      Returns a pointer to a string. 
 *
 * Programmer:  Ruey-Hsia Li
 *
 * Modifications:
 *
 *-----------------------------------------------------------------------*/
static char*
datatype_name(hid_t type) {

    switch (H5Tget_class(type)) {

    case H5T_INTEGER:

        if (H5Tequal(type, H5T_NATIVE_CHAR)) 
            return "H5T_NATIVE_CHAR";
        else if (H5Tequal(type, H5T_NATIVE_UCHAR))
            return "H5T_NATIVE_UCHAR";
        else if (H5Tequal(type, H5T_NATIVE_SHORT))
            return "H5T_NATIVE_SHORT";
        else if (H5Tequal(type, H5T_NATIVE_USHORT))
            return "H5T_NATIVE_USHORT";
        else if (H5Tequal(type, H5T_NATIVE_INT))
            return "H5T_NATIVE_INT";
        else if (H5Tequal(type, H5T_NATIVE_UINT))
            return "H5T_NATIVE_UINT";
        else if (H5Tequal(type, H5T_NATIVE_LONG))
            return "H5T_NATIVE_LONG";
        else if (H5Tequal(type, H5T_NATIVE_ULONG))
            return "H5T_NATIVE_ULONG";
        else if (H5Tequal(type, H5T_NATIVE_LLONG))
            return "H5T_NATIVE_LLONG";
        else if (H5Tequal(type, H5T_NATIVE_ULLONG))
            return "H5T_NATIVE_ULLONG";
        else if (H5Tequal(type, H5T_STD_I8BE))
            return "H5T_STD_I8BE";
        else if (H5Tequal(type, H5T_STD_I8LE))
            return "H5T_STD_I8LE";
        else if (H5Tequal(type, H5T_STD_I16BE))
            return "H5T_STD_I16BE";
        else if (H5Tequal(type, H5T_STD_I16LE))
            return "H5T_STD_I16LE";
        else if (H5Tequal(type, H5T_STD_I32BE))
            return "H5T_STD_I32BE";
        else if (H5Tequal(type, H5T_STD_I32LE))
            return "H5T_STD_I32LE";
        else if (H5Tequal(type, H5T_STD_I64BE))
            return "H5T_STD_I64BE";
        else if (H5Tequal(type, H5T_STD_I64LE))
            return "H5T_STD_I64LE";
        else if (H5Tequal(type, H5T_STD_U8BE))
            return "H5T_STD_U8BE";
        else if (H5Tequal(type, H5T_STD_U8LE))
            return "H5T_STD_U8LE";
        else if (H5Tequal(type, H5T_STD_U16BE))
            return "H5T_STD_U16BE";
        else if (H5Tequal(type, H5T_STD_U16LE))
            return "H5T_STD_U16LE";
        else if (H5Tequal(type, H5T_STD_U32BE))
            return "H5T_STD_U32BE";
        else if (H5Tequal(type, H5T_STD_U32LE))
            return "H5T_STD_U32LE";
        else if (H5Tequal(type, H5T_STD_U64BE))
            return "H5T_STD_U64BE";
        else if (H5Tequal(type, H5T_STD_U64LE))
            return "H5T_STD_U64LE";
        else return "unknown integer";

    case H5T_FLOAT: 

        if (H5Tequal(type, H5T_NATIVE_FLOAT))
            return "H5T_NATIVE_FLOAT";
        else if (H5Tequal(type, H5T_NATIVE_DOUBLE))
            return "H5T_NATIVE_DOUBLE";
        else if (H5Tequal(type, H5T_NATIVE_LDOUBLE))
            return "H5T_NATIVE_LDOUBLE";
        else if (H5Tequal(type, H5T_IEEE_F32BE))
            return "H5T_IEEE_F32BE";
        else if (H5Tequal(type, H5T_IEEE_F32LE))
            return "H5T_IEEE_F32LE";
        else if (H5Tequal(type, H5T_IEEE_F64BE))
            return "H5T_IEEE_F64BE";
        else if (H5Tequal(type, H5T_IEEE_F64LE))
            return "H5T_IEEE_F64LE";
        else return "unknown float";

    case H5T_TIME: return "time: not yet implemented";

    case H5T_STRING: 

        if (H5Tequal(type,H5T_C_S1))
            return "H5T_C_S1";
        else if (H5Tequal(type,H5T_FORTRAN_S1))
            return "H5T_FORTRAN_S1";
        else return "unknown string";

    case H5T_BITFIELD: 

        if (H5Tequal(type, H5T_STD_B8BE))
            return "H5T_STD_B8BE";
        else if (H5Tequal(type, H5T_STD_B8LE))
            return "H5T_STD_B8LE";
        else if (H5Tequal(type, H5T_STD_B16BE))
            return "H5T_STD_B16BE";
        else if (H5Tequal(type, H5T_STD_B16LE))
            return "H5T_STD_B16LE";
        else if (H5Tequal(type, H5T_STD_B32BE))
            return "H5T_STD_B32BE";
        else if (H5Tequal(type, H5T_STD_B32LE))
            return "H5T_STD_B32LE";
        else if (H5Tequal(type, H5T_STD_B64BE))
            return "H5T_STD_B64BE";
        else if (H5Tequal(type, H5T_STD_B64LE))
            return "H5T_STD_B64LE";
        else return "unknown bitfield";

    case H5T_OPAQUE: return "opaque: not yet implemented";

    case H5T_COMPOUND: 

         return "compound: not yet implemented";

    default:
            return "unknown data type";
    }

}


/*-------------------------------------------------------------------------
 * Function:    dump_bb
 *
 * Purpose:     Dump the boot block
 *
 * Return:      void
 *
 * Programmer:  Ruey-Hsia Li
 *
 * Modifications:
 *
 *-----------------------------------------------------------------------*/
static void
dump_bb() {

    printf ("%s %s %s\n", BOOT_BLOCK, BEGIN, END);

}


/*-------------------------------------------------------------------------
 * Function:    dump_datatype
 *
 * Purpose:     Dump the data type. Data type can be HDF5 predefined
 *              atomic data type, named data type or compound data type. 
 *
 * Return:      void 
 *
 * Programmer:  Ruey-Hsia Li
 *
 * Modifications:
 *
 *-----------------------------------------------------------------------*/
static void
dump_datatype (hid_t type) {
char *pt;
char *fname ;
hid_t nmembers, mtype;
int i, ndims, perm[512]; 
size_t dims[512];

    indent += col;
    indentation (indent);
    pt = datatype_name(type);
    printf ("%s %s \"%s\" %s\n", DATATYPE, BEGIN, pt, END);
    indent -= col;
}


/*-------------------------------------------------------------------------
 * Function:    dump_dataspace
 *
 * Purpose:     Dump the data space. Data space can be named data space,
 *              array, or others.
 *
 * Return:      void    
 *
 * Programmer:  Ruey-Hsia Li
 *
 * Modifications:
 *
 *-----------------------------------------------------------------------*/
static void
dump_dataspace (hid_t dataset_id, hid_t space) {
hsize_t size[64];
hsize_t maxsize[64];  /* check max dims size */
int ndims = H5Sextent_dims(space, size, maxsize);
int i;

    indent += col;

    indentation (indent);

    printf("%s ", DATASPACE);

    if (H5Sis_simple(space)) {

        HDfprintf (stdout, "%s %s ( %Hu",BEGIN, ARRAY, size[0]);
        for (i = 1; i < ndims; i++) 
             HDfprintf (stdout, ", %Hu", size[i]);
        printf(" ) ");
           
        if (maxsize[0]==H5S_UNLIMITED)
            HDfprintf (stdout, "( %s", "H5S_UNLIMITED");
        else
            HDfprintf (stdout, "( %Hu", maxsize[0]);

        for (i = 1; i < ndims; i++) 
             if (maxsize[i]==H5S_UNLIMITED)
                 HDfprintf (stdout, ", %s", "H5S_UNLIMITED");
             else
                 HDfprintf (stdout, ", %Hu", maxsize[i]);

        printf(" ) %s\n", END);

    } else

        printf("%s not yet implemented %s\n", BEGIN, END);

    indent -= col;

}


/*-------------------------------------------------------------------------
 * Function:    dump_attr
 *
 * Purpose:     dump the attribute
 *
 * Return:      Success:        SUCCEED
 *
 *              Failure:        FAIL
 *
 * Programmer:  Ruey-Hsia Li
 *
 * Modifications:
 *
 *-----------------------------------------------------------------------*/
static herr_t
dump_attr (hid_t attr, const char *attr_name, void __unused__ *op_data)
{
hid_t  attr_id, type, space;

    indentation(indent);
    begin_obj (ATTRIBUTE, attr_name); 

    if ((attr_id = H5Aopen_name (attr, attr_name))>= 0) {

        type = H5Aget_type(attr_id);
        space = H5Aget_space(attr_id);
        dump_datatype(type);
        dump_dataspace(attr_id, space);
        dump_data(attr_id, ATTRIBUTE_DATA);
        H5Tclose(type);
        H5Sclose(space);
	H5Aclose (attr_id);

    } else {
        indent += col;
        indentation (indent);
        printf("Unable to open attribute.\n", attr_name);
        indent -= col;
        return FAIL;
    }

    indentation (indent);
    end_obj();

    return SUCCEED;

}


/*-------------------------------------------------------------------------
 * Function:    dump all
 *
 * Purpose:     Dump everything in the specified object
 *
 * Return:      Success:        SUCCEED
 *
 *              Failure:        FAIL
 *
 * Programmer:  Ruey-Hsia Li
 *
 * Modifications:
 *
 *-----------------------------------------------------------------------*/
static herr_t
dump_all (hid_t group, const char *name, void __unused__ *op_data)
{
hid_t obj;
hid_t (*func)(void*);
void *edata;
char buf[512]; /* Is 512 large enough? */

    /* Disable error reporting */
    H5Eget_auto (&func, &edata);
    H5Eset_auto (NULL, NULL);


    if (H5Gget_linkval (group, name, sizeof(buf), buf)>=0) {  

        indentation (indent);
        begin_obj(SOFTLINK, name);
        indent += col;
        indentation (indent);
        printf ("linktarget \"%s\"\n", buf);
        indent -= col;
        indentation (indent);
        end_obj();

    } else if ((obj=H5Dopen (group, name))>=0) {              

        dump_dataset (obj, name);
        H5Dclose (obj);

    } else if ((obj=H5Gopen (group, name))>=0) {

           dump_group (obj, name);
           H5Gclose (obj);

    } else if ((obj=H5Topen(group, name))>=0) {
    
           dump_named_datatype (obj, name);
           H5Tclose(obj);

    } else {

        printf ("Unknown Object %s\n", name);
        H5Eset_auto (func, edata);
        return FAIL;

    }

    /* Restore error reporting */
    H5Eset_auto (func, edata);

    return SUCCEED;

}


/*-------------------------------------------------------------------------
 * Function:    dump_named_datatype
 *
 * Purpose:     Dump named data type
 *
 * Return:      void
 *
 * Programmer:  Ruey-Hsia Li
 *
 * Modifications:
 *
 *-----------------------------------------------------------------------*/
static void
dump_named_datatype (hid_t type_id, const char *name) {
char *fname ;
hid_t nmembers, mtype;
int i, ndims, perm[512]; /* dimensionality */
size_t dims[512];

    indentation (indent);
    begin_obj(DATATYPE, name);

    indent += col;
    indentation(indent);
    printf("named data type: not yet implemented.\n");
    indent -= col;

    indentation (indent);
    end_obj();

}


/*-------------------------------------------------------------------------
 * Function:    dump_group
 *
 * Purpose:     Dump everything within the specified group
 *
 * Return:      void
 *
 * Programmer:  Ruey-Hsia Li
 *
 * Modifications:
 *
 *-----------------------------------------------------------------------*/
static void
dump_group (hid_t gid, const char *name) {

    indentation (indent);
    begin_obj(GROUP, name);
    indent += col;
    H5Aiterate (gid, NULL, dump_attr, NULL);
    H5Giterate (gid, ".", NULL, dump_all, NULL);
    indent -= col;
    indentation (indent);
    end_obj();

}


/*-------------------------------------------------------------------------
 * Function:    dump_dataset
 *
 * Purpose:     Dump the specified data set
 *
 * Return:      void
 *
 * Programmer:  Ruey-Hsia Li
 *
 * Modifications:
 *
 *-----------------------------------------------------------------------*/
static void
dump_dataset (hid_t did, const char *name) {
hid_t  type, space;

    indentation (indent);
    begin_obj(DATASET, name);
    indent += col;
    H5Aiterate (did, NULL, dump_attr, NULL);
    indent -= col;
    type = H5Dget_type (did);
    space = H5Dget_space (did);
    dump_datatype(type);
    dump_dataspace(did, space);

    switch (H5Tget_class(type)) {
    case H5T_INTEGER:
         dump_data(did, DATASET_DATA);
         break;
    case H5T_FLOAT:
         dump_data(did, DATASET_DATA);
         break;
    case H5T_TIME:
         indent += col;
         indentation (indent);
         indent -= col;
         printf("DATA{ not yet implemented.}\n");
         break;
    case H5T_STRING:
         indent += col;
         indentation (indent);
         indent -= col;
         printf("DATA{ not yet implemented.}\n");
         break;
    case H5T_BITFIELD:
         indent += col;
         indentation (indent);
         indent -= col;
         printf("DATA{ not yet implemented.}\n");
         break;
    case H5T_OPAQUE:
         indent += col;
         indentation (indent);
         indent -= col;
         printf("DATA{ not yet implemented.}\n");
         break;
    case H5T_COMPOUND:
         indent += col;
         indentation (indent);
         indent -= col;
         printf("DATA{ not yet implemented.}\n");
         break;
    default: break;
    }

    H5Tclose(type);
    H5Sclose(space);
    indentation (indent);
    end_obj();
    
}


/*-------------------------------------------------------------------------
 * Function:    dump_data
 *
 * Purpose:     Dump attribute or dataset data
 *
 * Return:      void
 *
 * Programmer:  Ruey-Hsia Li
 *
 * Modifications:
 *
 *-----------------------------------------------------------------------*/
static void
dump_data (hid_t obj_id, int obj_data) {

    hid_t               f_type ;
    size_t              size ;
    h5dump_t            info;

    indent += col;
    indentation (indent);
    printf("%s %s", DATA, BEGIN);

    if (obj_data == DATASET_DATA)
        f_type = H5Dget_type(obj_id);
    else
        f_type = H5Aget_type(obj_id);

    size = H5Tget_size(f_type);

    /* Set to all default values and then override */
    memset(&info, 0, sizeof info);
    info.idx_fmt = "        (%s) ";
    info.line_ncols = 80;

    /*
     * If the dataset is a 1-byte integer type then format it as an ASCI
     * character string instead of integers.
     */
    if (1==size && H5T_INTEGER==H5Tget_class(f_type)) {
        info.elmt_suf1 = "";
        info.elmt_suf2 = "";
        info.idx_fmt = "        (%s) \"";
        info.line_suf = "\"";
        printf("\"");
    }

   
    /*
     * Print all the values.
     */

    if (h5dump1(stdout, &info, obj_id, -1, obj_data)<0) {
        printf("Unable to print data.");
    }

    if (1==size && H5T_INTEGER==H5Tget_class(f_type)) 
        printf("\"");

    printf("%s\n", END);
    indent -= col;

    H5Tclose(f_type);

}


/*-------------------------------------------------------------------------
 * Function:    main
 *
 * Purpose:     HDF5 dumper
 *
 * Return:      Success:	0
 *              Failure:	1
 *
 * Programmer:  Ruey-Hsia Li
 *
 * Modifications:
 *
 *-----------------------------------------------------------------------*/
int
main(int argc, char *argv[]) {
hid_t fid, gid;
hid_t plist=H5P_DEFAULT;
const char *fname = NULL;

    if (argc > 2 || argc <= 1) {
        usage();
        exit(1);
    }

    /* arguments */
    fname = argv[1];

    /* open file */
    if ((fid = H5Fopen (fname, H5F_ACC_RDONLY, plist))<0) exit(1);
   
    begin_obj("HDF5", fname);

    gid = H5Gopen (fid, "/");

    dump_group(gid, "/");

    end_obj();
    
    if (H5Gclose (gid) < 0) exit(1);

    if (H5Fclose (fid) < 0) exit(1);
    

    return 0;

}