File: H5ObjectEx_G_Phase.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 (206 lines) | stat: -rw-r--r-- 7,087 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
/************************************************************
  This example shows how to set the conditions for
  conversion between compact and dense (indexed) groups.

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

import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;

import ncsa.hdf.hdf5lib.H5;
import ncsa.hdf.hdf5lib.HDF5Constants;
import ncsa.hdf.hdf5lib.exceptions.HDF5Exception;
import ncsa.hdf.hdf5lib.structs.H5G_info_t;
import ncsa.hdf.object.FileFormat;
import ncsa.hdf.object.Group;
import ncsa.hdf.object.h5.H5File;
import ncsa.hdf.object.h5.H5Group;


public class H5ObjectEx_G_Phase {
    private static String FILE = "H5ObjectEx_G_Phase.h5";
    private static int MAX_GROUPS = 7;
    private static int MAX_COMPACT = 5;
    private static int MIN_DENSE = 3;

    enum H5G_storage {
        H5G_STORAGE_TYPE_UNKNOWN(-1),
        H5G_STORAGE_TYPE_SYMBOL_TABLE(0),
        H5G_STORAGE_TYPE_COMPACT(1),
        H5G_STORAGE_TYPE_DENSE(2);

        private static final Map<Integer, H5G_storage> lookup = new HashMap<Integer, H5G_storage>();

        static {
            for (H5G_storage s : EnumSet.allOf(H5G_storage.class))
                lookup.put(s.getCode(), s);
        }

        private int code;

        H5G_storage(int layout_type) {
            this.code = layout_type;
        }

        public int getCode() {
            return this.code;
        }

        public static H5G_storage get(int code) {
            return lookup.get(code);
        }
    }

    private static void CreateGroup() {
        H5File         file = null;
        H5Group        grp = null;
        int            group_id = -1;
        int            fapl_id = -1;
        int            gcpl_id = -1;
        H5G_info_t     ginfo;
        String         name = "G0";    // Name of subgroup_id
        int            i;

        //Set file access property list to allow the latest file format.This will allow the library to create new format groups.
        try {
            fapl_id = H5.H5Pcreate(HDF5Constants.H5P_FILE_ACCESS);
            if(fapl_id >= 0)
                H5.H5Pset_libver_bounds (fapl_id, HDF5Constants.H5F_LIBVER_LATEST, HDF5Constants.H5F_LIBVER_LATEST);
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        //Create a new file using the default properties.
        try {
            if(fapl_id >= 0) {
                file = new H5File(FILE, FileFormat.CREATE);
                file.open(fapl_id);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        //Create primary group.
        try {
            //Create group creation property list and set the phase change conditions.  
            gcpl_id = file.createGcpl(0, MAX_COMPACT, MIN_DENSE);
            if(gcpl_id >= 0) {
                grp = (H5Group) file.createGroup(name, null, HDF5Constants.H5P_DEFAULT, gcpl_id);
                gcpl_id = -1; //Create closes the group creation property list
                group_id = grp.open();
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        //Add subgroups to "group" one at a time, print the storage type for "group" after each subgroup is created.
        for (i = 1; i <= MAX_GROUPS; i++) {
            //Define the subgroup name and create the subgroup.
            char append = (char) (((char)i) + '0');
            name = name + append; /* G1, G2, G3 etc. */
            try {
                file.createGroup(name, grp);
            }
            catch (Exception e) {
                e.printStackTrace();
            }

            //Obtain the group info and print the group storage type
            try {
                if(group_id >= 0) {
                    ginfo = H5.H5Gget_info (group_id);
                    System.out.print(ginfo.nlinks + " Group"+(ginfo.nlinks == 1 ? " " : "s") + ": Storage type is ");
                    switch (H5G_storage.get(ginfo.storage_type)) {
                    case H5G_STORAGE_TYPE_COMPACT:
                        System.out.println("H5G_STORAGE_TYPE_COMPACT"); // New compact format
                        break;
                    case H5G_STORAGE_TYPE_DENSE:
                        System.out.println("H5G_STORAGE_TYPE_DENSE"); //New dense (indexed) format
                        break;
                    case H5G_STORAGE_TYPE_SYMBOL_TABLE:
                        System.out.println("H5G_STORAGE_TYPE_SYMBOL_TABLE"); //Original format 
                    }
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }

        System.out.println();

        //Delete subgroups one at a time, print the storage type for "group" after each subgroup is deleted.
        for (i = MAX_GROUPS; i >= 1; i--) {
            //Define the subgroup name and delete the subgroup.
            try {
                Group subgrp = (Group) file.get("/G0/" + name);
                if(subgrp == null)
                    throw new HDF5Exception(
                            "The subgroup - "+name+" - object could not be found.");
                file.delete(subgrp);
            }
            catch(Exception e) {
                e.printStackTrace();
            }
            name = name.substring(0, i+1);

            //Obtain the group info and print the group storage type
            try {
                if(group_id >= 0){
                    ginfo = H5.H5Gget_info(group_id);
                    System.out.print(ginfo.nlinks + " Group"+(ginfo.nlinks == 1 ? " " : "s") + ": Storage type is ");
                    switch (H5G_storage.get(ginfo.storage_type)) {
                    case H5G_STORAGE_TYPE_COMPACT:
                        System.out.println("H5G_STORAGE_TYPE_COMPACT"); // New compact format
                        break;
                    case H5G_STORAGE_TYPE_DENSE:
                        System.out.println("H5G_STORAGE_TYPE_DENSE"); //New dense (indexed) format
                        break;
                    case H5G_STORAGE_TYPE_SYMBOL_TABLE:
                        System.out.println("H5G_STORAGE_TYPE_SYMBOL_TABLE"); //Original format 
                    }
                }
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }

        //Close and release resources
        try {
            if(fapl_id >= 0)
                H5.H5Pclose (fapl_id);
        }
        catch(Exception e) {
            e.printStackTrace();
        }

        //Close the group
        try {
            if(group_id >= 0)
                grp.close (group_id);
        }
        catch(Exception e) {
            e.printStackTrace();
        }

        //Close the file
        try {
            file.close();
        }
        catch(Exception e) {
            e.printStackTrace();
        }

    }
    public static void main(String[] args) {
        H5ObjectEx_G_Phase.CreateGroup();
    }

}