-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
H5ObjectEx_G_Visit.java
144 lines (122 loc) · 5.12 KB
/
H5ObjectEx_G_Visit.java
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
/************************************************************
This example shows how to recursively traverse a file
using H5Ovisit and H5Lvisit. The program prints all of
the objects in the file specified in FILE, then prints all
of the links in that file. The default file used by this
example implements the structure described in the User's
Guide.
************************************************************/
package groups;
import java.util.ArrayList;
import hdf.object.FileFormat;
import hdf.object.h5.H5File;
import hdf.hdf5lib.H5;
import hdf.hdf5lib.HDF5Constants;
import hdf.hdf5lib.callbacks.H5L_iterate_opdata_t;
import hdf.hdf5lib.callbacks.H5L_iterate_t;
import hdf.hdf5lib.callbacks.H5O_iterate_opdata_t;
import hdf.hdf5lib.callbacks.H5O_iterate_t;
import hdf.hdf5lib.structs.H5L_info_t;
import hdf.hdf5lib.structs.H5O_info_t;
public class H5ObjectEx_G_Visit {
private static String FILE = "examples/groups/h5ex_g_visit.h5";
public static void main(String[] args)
{
try {
(new H5ObjectEx_G_Visit()).VisitGroup();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private void VisitGroup() throws Exception
{
H5File file = null;
long file_id = -1;
try {
// Open file
file = new H5File(FILE, FileFormat.READ);
file_id = file.open();
// Begin iteration using H5Ovisit
System.out.println("Objects in the file:");
H5O_iterate_opdata_t iter_data = new H5O_iter_data();
H5O_iterate_t iter_cb = new H5O_iter_callback();
H5.H5Ovisit(file_id, HDF5Constants.H5_INDEX_NAME, HDF5Constants.H5_ITER_NATIVE, iter_cb,
iter_data);
System.out.println();
// Repeat the same process using H5Lvisit
H5L_iterate_opdata_t iter_data2 = new H5L_iter_data();
H5L_iterate_t iter_cb2 = new H5L_iter_callback();
System.out.println("Links in the file:");
H5.H5Lvisit(file_id, HDF5Constants.H5_INDEX_NAME, HDF5Constants.H5_ITER_NATIVE, iter_cb2,
iter_data2);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
file.close();
}
}
/************************************************************
* Operator function for H5Lvisit. This function simply
* retrieves the info for the object the current link points
* to, and calls the operator function for H5Ovisit.
************************************************************/
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 H5L_iter_data implements H5L_iterate_opdata_t {
public ArrayList<idata> iterdata = new ArrayList<>();
}
private class H5L_iter_callback implements H5L_iterate_t {
public int callback(long group, String name, H5L_info_t info, H5L_iterate_opdata_t op_data)
{
idata id = new idata(name, info.type);
((H5L_iter_data)op_data).iterdata.add(id);
H5O_info_t infobuf;
int ret = 0;
try {
// Get type of the object and display its name and type. The name of the object is passed to
// this function by the Library.
infobuf = H5.H5Oget_info_by_name(group, name, HDF5Constants.H5P_DEFAULT);
H5O_iterate_t iter_cbO = new H5O_iter_callback();
H5O_iterate_opdata_t iter_dataO = new H5O_iter_data();
ret = iter_cbO.callback(group, name, infobuf, iter_dataO);
}
catch (Exception e) {
e.printStackTrace();
}
return ret;
}
}
private class H5O_iter_data implements H5O_iterate_opdata_t {
public ArrayList<idata> iterdata = new ArrayList<>();
}
private class H5O_iter_callback implements H5O_iterate_t {
public int callback(long group, String name, H5O_info_t info, H5O_iterate_opdata_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;
}
}
}