File: H5ObjectEx_G_Intermediate.java

package info (click to toggle)
jhdf 2.11.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 39,996 kB
  • ctags: 17,959
  • sloc: java: 100,416; ansic: 24,920; sh: 2,256; makefile: 957; cpp: 48
file content (117 lines) | stat: -rw-r--r-- 3,926 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
/************************************************************
This example shows how to create intermediate groups with
a single call to H5Gcreate.

This file is intended for use with HDF5 Library version 1.8
 ************************************************************/
package examples.groups;

import java.util.ArrayList;

import ncsa.hdf.hdf5lib.H5;
import ncsa.hdf.hdf5lib.HDF5Constants;
import ncsa.hdf.hdf5lib.callbacks.H5O_iterate_cb;
import ncsa.hdf.hdf5lib.callbacks.H5O_iterate_t;
import ncsa.hdf.hdf5lib.structs.H5O_info_t;
import ncsa.hdf.object.FileFormat;
import ncsa.hdf.object.h5.H5File;

public class H5ObjectEx_G_Intermediate {

    private static String FILE = "H5ObjectEx_G_Intermediate.h5";

    private void CreateGroup() throws Exception {
        H5File      file = null;
        int     file_id = -1;
        int     lcpl_id = -1;

        try {
            // Create a new file_id using the default properties.
            file = new H5File(FILE, FileFormat.CREATE);
            file_id = file.open();

            //Create group_id creation property list and set it to allow creation of intermediate group_ids.
            lcpl_id = H5.H5Pcreate (HDF5Constants.H5P_LINK_CREATE);
            H5.H5Pset_create_intermediate_group(lcpl_id, true);

            /*
             * Create the group_id /G1/G2/G3.  Note that /G1 and /G1/G2 do not
             * exist yet.  This call would cause an error if we did not use the
             * previously created property list.
             */
            file.createGroup("/G1/G2/G3", null, lcpl_id);
            //Print all the objects in the file_ids to show that intermediate group_ids have been created. 
            System.out.println("Objects in the file_id:");

            //H5O_iterate_t iter_data = null;
            H5O_iterate_t iter_data = new H5O_iter_data();
            H5O_iterate_cb iter_cb = new H5O_iter_callback();

            H5.H5Ovisit(file_id, HDF5Constants.H5_INDEX_NAME, HDF5Constants.H5_ITER_NATIVE, iter_cb, iter_data);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            //Close and release resources.
            if (lcpl_id >= 0)
                H5.H5Pclose (lcpl_id);
            file.close();
        }
    }

    public static void main(String[] args) {
        try {
            (new H5ObjectEx_G_Intermediate()).CreateGroup();
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
    }


    private class idata {
        public String link_name = null;
        public int link_type = -1;
        idata(String name, int type) {
            this.link_name = name;
            this.link_type = type;
        }
    }
    private class H5O_iter_data implements H5O_iterate_t {
        public ArrayList<idata> iterdata = new ArrayList<idata>();
    }


    private class H5O_iter_callback implements H5O_iterate_cb {
        public int callback(int group, String name, H5O_info_t info, H5O_iterate_t op_data) {
            idata id = new idata(name, info.type);
            ((H5O_iter_data)op_data).iterdata.add(id);

            System.out.print("/"); /* Print root group in object path */

            //Check if the current object is the root group, and if not print the full path name and type.

            if (name.charAt(0) == '.')         /* Root group, do not print '.' */
                System.out.println("  (Group)");
            else if(info.type == HDF5Constants.H5O_TYPE_GROUP ) 
                System.out.println(name + "  (Group)" );
            else if(info.type == HDF5Constants.H5O_TYPE_DATASET)
                System.out.println(name + "  (Dataset)");
            else if (info.type == HDF5Constants.H5O_TYPE_NAMED_DATATYPE )
                System.out.println(name + "  (Datatype)");
            else
                System.out.println(name + "  (Unknown)");

            return 0;
        }
    }

}