Repository navigation
Expand file tree
/
Copy pathH5File.java.txt
More file actions
316 lines (278 loc) · 10.1 KB
/
Copy pathH5File.java.txt
File metadata and controls
316 lines (278 loc) · 10.1 KB
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
/****************************************************************************
* NCSA HDF *
* National Comptational Science Alliance *
* University of Illinois at Urbana-Champaign *
* 605 E. Springfield, Champaign IL 61820 *
* *
* For conditions of distribution and use, see the accompanying *
* hdf-java/COPYING file. *
* *
****************************************************************************/
package ncsa.hdf.object;
import java.io.File;
import java.util.*;
import javax.swing.tree.*;
import ncsa.hdf.object.*;
import ncsa.hdf.hdf5lib.*;
import ncsa.hdf.hdf5lib.exceptions.*;
/**
* This class provides file level APIs. File access APIs include retrieving the
* file hierarchy, opening and closing file, and writing file content to disk.
* <p>
* @version 1.0 12/12/2001
* @author Peter X. Cao, NCSA
*/
public class H5File extends File implements FileFormat
{
/**
* file identifier for the open file.
*/
private int fid;
/**
* the file access flag.
*/
private int flag;
/**
* The full name of this file; full path plus name
*/
private final String fullFileName;
/**
* The root node of the file hierearchy.
*/
private MutableTreeNode rootNode;
/**
* Creates an H5File object with specific file name and access flag.
* <p>
* @param pathname the full path name of the file.
* @param flag the file access flag, it takes one of two values below:
* <DL><DL>
* <DT> H5F_ACC_RDWR <DD> Allow read and write access to file.</DT>
* <DT> H5F_ACC_RDONLY <DD> Allow read-only access to file.</DT>
* </DL></DL>
*/
public H5File(String pathname, int flag)
{
super(pathname);
this.fid = -1;
this.flag = flag;
this.fullFileName = pathname;
}
/**
* Checks if a given file is an HDF5 file.
* <p>
* @param filename the file to be checked.
* @return true if the given file is an HDF5 file; otherwise returns false.
*/
public static boolean isThisType(String filename)
{
boolean isH5 = false;
try {
isH5 = H5.H5Fis_hdf5(filename);
} catch (HDF5Exception ex)
{
isH5 = false;
}
return isH5;
}
// Implementing FileFormat
public int open() throws Exception
{
if ( fid >=0 )
return fid; // file is openned already
// check for valid file access permission
if (!exists())
throw new HDF5Exception("File does not exist -- "+fullFileName);
else if ( flag < 0)
throw new HDF5Exception("Invalid access identifer -- "+flag);
else if ((flag == HDF5Constants.H5F_ACC_RDONLY) && !canRead())
throw new HDF5Exception("Cannot read file -- "+fullFileName);
else if ((flag == HDF5Constants.H5F_ACC_RDWR) && !canWrite())
throw new HDF5Exception("Cannot write file -- "+fullFileName);
fid = H5.H5Fopen( fullFileName, flag, HDF5Constants.H5P_DEFAULT);
if (fid>=0)
{
// load the hierearchy of the file
rootNode = loadTree();
}
return fid;
}
// Implementing FileFormat
public void close() throws HDF5Exception
{
H5.H5Fclose(fid);
fid = -1;
}
// Implementing FileFormat
public MutableTreeNode getRootNode()
{
return rootNode;
}
// Implementing FileFormat
public String getFilePath()
{
return fullFileName;
}
/**
* Retrieves and returns the tree structure from the file
*/
private MutableTreeNode loadTree()
{
if (fid <0 ) return null;
long[] oid = {0};
H5Group rootGroup = new H5Group(
this,
getName(), // set the name of the root node to the name of the file
null, // root node does not have a parent path
null, // root node does not have a parent node
oid);
MutableTreeNode root = new DefaultMutableTreeNode(rootGroup);
depth_first(root);
return root;
}
/**
* Retrieves the file structure by depth-first order, recursively.
* The current implementation retrieves group and dataset only. It does
* not include named datatype and soft links.
* <p>
* It also detects and stops loops. A loop is detected if there exists
* object with the same object ID by tracing path back up to the root.
* <p>
* @param parentNode the parent node.
*/
private void depth_first(MutableTreeNode parentNode)
{
//System.out.println("H5File.depth_first() pnode = "+parentNode);
int nelems;
MutableTreeNode node = null;
String fullPath = null;
String ppath = null;
DefaultMutableTreeNode pnode = (DefaultMutableTreeNode)parentNode;
H5Group pgroup = (H5Group)(pnode.getUserObject());
ppath = pgroup.getPath();
if (ppath == null)
{
fullPath = HObject.separator;
}
else
{
fullPath = ppath+pgroup.getName()+HObject.separator;
}
nelems = 0;
try {
nelems = H5.H5Gn_members(fid, fullPath);
} catch (HDF5Exception ex) {
nelems = -1;
}
if (nelems < 0 ) {
return;
}
int[] oType = new int[1];
long[] oid = null;
String [] oName = new String[1];
//Iterate through the file to see members of the group
for ( int i = 0; i < nelems; i++)
{
oName[0] = "";
oType[0] = -1;
oid = null;
try {
H5.H5Gget_obj_info_idx(fid, fullPath, i, oName, oType );
} catch (HDF5Exception ex) {
// do not stop if accessing one member fails
continue;
}
try
{
// retrieve the object ID.
byte[] ref_buf = H5.H5Rcreate(
fid,
fullPath+oName[0],
HDF5Constants.H5R_OBJECT,
-1);
long l = HDFNativeData.byteToLong(ref_buf, 0);
oid = new long[1];
oid[0] = l; // save the object ID
} catch (HDF5Exception ex) {}
if (oid == null)
continue; // do the next one, if the object is not identified.
switch (oType[0])
{
// create a new group
case HDF5Constants.H5G_GROUP:
H5Group g = new H5Group(
this,
oName[0],
fullPath,
pgroup,
oid);
node = new DefaultMutableTreeNode(g);
pnode.add( node );
// detect and stop loops
// a loop is detected if there exists object with the same
// object ID by tracing path back up to the root.
boolean hasLoop = false;
HObject tmpObj = null;
long[] tmpOID = null;
DefaultMutableTreeNode tmpNode = (DefaultMutableTreeNode)pnode;
while (tmpNode != null)
{
tmpObj = (HObject)tmpNode.getUserObject();
if (tmpObj.equalsOID(oid))
{
hasLoop = true;
break;
}
else
tmpNode = (DefaultMutableTreeNode)tmpNode.getParent();
}
// recursively go through the next group
// stops if it has loop.
if (!hasLoop)
depth_first(node);
break;
case HDF5Constants.H5G_DATASET:
int did=-1, tid=-1, tclass=-1;
try {
did = H5.H5Dopen(fid, fullPath+oName[0]);
tid = H5.H5Dget_type(did);
tclass = H5.H5Tget_class(tid);
if (tclass == HDF5Constants.H5T_ARRAY)
{
// for ARRAY, the type is determined by the base type
int btid = H5.H5Tget_super(tid);
tclass = H5.H5Tget_class(btid);
try { H5.H5Tclose(btid); } catch (HDF5Exception ex) {}
}
} catch (HDF5Exception ex) {}
finally {
try { H5.H5Tclose(tid); } catch (HDF5Exception ex) {}
try { H5.H5Dclose(did); } catch (HDF5Exception ex) {}
}
Dataset d = null;
if (tclass == HDF5Constants.H5T_COMPOUND)
{
// create a new compound dataset
d = new H5CompoundDS(
this,
oName[0],
fullPath,
oid);
}
else
{
// create a new scalar dataset
d = new H5ScalarDS(
this,
oName[0],
fullPath,
oid);
}
node = new DefaultMutableTreeNode(d);
pnode.add( node );
break;
default:
break;
} // switch (oType[0])
} // for ( i = 0; i < nelems; i++)
} // private depth_first()
}