Repository navigation
Expand file tree
/
Copy pathH4File.java.txt
More file actions
724 lines (644 loc) · 21.3 KB
/
Copy pathH4File.java.txt
File metadata and controls
724 lines (644 loc) · 21.3 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
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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
/****************************************************************************
* 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.hdflib.*;
/**
* 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 H4File 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, path+name
*/
private final String fullFileName;
/**
* The root node of the tree structure of this file.
*/
private MutableTreeNode rootNode;
/**
* The list of unique (tag, ref) pairs.
* It is used to avoid duplicate objects in memory.
*/
private List objList;
/**
* The GR interface identifier.
* The identifier is returned by GRstart(fid), which initializes the GR
* interface for the file specified by the parameter. GRstart(fid) is an
* expensive call. It should be called only once. Calling GRstart(fid) in
* a loop should be avoided.
*/
private int grid;
/**
* The SDS interface identifier.
* The identifier is returned by SDstart(fname, flag), which initializes the
* SD interface for the file specified by the parameter. SDstart(fname, flag)
* is an expensive call. It should be called only once. Calling
* SDstart(fname, flag) in a loop should be avoided.
*/
private int sdid;
/**
* Creates an H4File with specific full file name and access flag.
* <p>
* @param pathname the full path of the file name.
* @param flag the file access flag, HDF provides several file access
* code definitions:
* <DL><DL>
* <DT> DFACC_READ <DD> Open for read only. If file does not exist,
* an error condition results.</DT>
* <DT> DFACC_CREATE <DD> If file exists, delete it, then open a new file
* for read/write.</DT>
* <DT> DFACC_WRITE <DD> Open for read/write. If file does not exist,
* create it.</DT>
* </DL></DL>
*/
public H4File(String pathname, int flag)
{
super(pathname);
this.fid = -1;
this.flag = flag;
this.fullFileName = pathname;
}
/**
* Checks if the given file is an HDF4 file.
* <p>
* @param filename the file to be checked.
* @return true if the given file is an HDF4 file; otherwise returns false.
*/
public static boolean isThisType(String filename)
{
boolean isH4 = false;
try {
isH4 = HDFLibrary.Hishdf(filename);
} catch (HDFException ex)
{
isH4 = false;
}
return isH4;
}
// Implementing FileFormat
public int open() throws Exception
{
if ( fid >=0 )
return fid; // file is openned already
// check for valid file access permission
if (flag < 0) // invalid access id
{
throw new HDFException("Invalid access identifer -- "+flag);
}
else if (flag == HDFConstants.DFACC_READ)
{
if (!exists())
throw new HDFException("File does not exist -- "+fullFileName);
else if (exists() && !canRead())
throw new HDFException("Cannot read file -- "+fullFileName);
}
else if (flag == HDFConstants.DFACC_WRITE ||
flag == HDFConstants.DFACC_CREATE)
{
if (exists() && !canWrite())
throw new HDFException("Cannot write file -- "+fullFileName);
}
try
{
fid = HDFLibrary.Hopen( fullFileName, flag);
}
catch (HDFException ex)
{
fid = -1;
}
if ( fid>=0 )
{
try
{
HDFLibrary.Vstart(fid);
grid = HDFLibrary.GRstart(fid);
sdid = HDFLibrary.SDstart(fullFileName, flag);
} catch (HDFException ex) {}
// load the file hierarchy
rootNode = loadTree();
}
return fid;
}
// Implementing FileFormat
public void close() throws HDFException
{
try { HDFLibrary.GRend(grid); } catch (HDFException ex) {}
try { HDFLibrary.SDend(sdid); } catch (HDFException ex) {}
try { HDFLibrary.Vend(fid); } catch (HDFException ex) {}
HDFLibrary.Hclose(fid);
fid = -1;
objList = null;
}
// Implementing FileFormat
public MutableTreeNode getRootNode()
{
return rootNode;
}
// Implementing FileFormat
public String getFilePath()
{
return fullFileName;
}
/**
* Retrieves and returns the file structure from disk.
* <p>
* First gets the top level objects or objects that do not belong to any groups.
* If a top level object is a group, call the depth_first() to retrieve
* the sub-tree of that group, recursively.
*
*/
private MutableTreeNode loadTree()
{
if (fid <0 ) return null;
long[] oid = {0, 0};
int n=0, tag=-1, ref=-1;
int[] argv = null;
MutableTreeNode node = null;
H4Group rootGroup = new H4Group(
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);
if (objList == null)
objList = new Vector(100);
DefaultMutableTreeNode root = new DefaultMutableTreeNode(rootGroup);
// get top level VGroup
int[] tmpN = new int[1];
int[] refs = null;
String[] vClass = new String[1];
try {
// first call to get the number of lone Vgroup
n = HDFLibrary.Vlone(fid, tmpN, 0);
refs = new int[n];
// second call to get the references of all lone Vgroup
n = HDFLibrary.Vlone(fid, refs, n);
} catch (HDFException ex) { n = 0; }
for (int i=0; i<n; i++)
{
ref = refs[i];
H4Group g = getVGroup(ref, HObject.separator, null, false);
if (g != null)
{
node = new DefaultMutableTreeNode(g);
root.add( node );
// recursively get the sub-tree
depth_first(node);
}
} // for (int i=0; i<n; i++)
// get the top level GR images
argv = new int[2];
boolean b = false;
try {
b = HDFLibrary.GRfileinfo(grid,argv);
} catch (HDFException ex)
{
b = false;
}
if ( b )
{
n = argv[0];
for (int i=0; i<n; i++)
{
// no duplicate object at top level
H4GRImage gr = getGRImage(i, HObject.separator, false);
if (gr != null)
{
node = new DefaultMutableTreeNode(gr);
root.add( node );
}
} // for (int i=0; i<n; i++)
} // if ( grid!=HDFConstants.FAIL && HDFLibrary.GRfileinfo(grid,argv) )
// get top level SDS
try {
b = HDFLibrary.SDfileinfo(sdid, argv);
} catch (HDFException ex)
{
b = false;
}
if (b)
{
n = argv[0];
for (int i=0; i<n; i++)
{
// no duplicate object at top level
H4SDS sds = getSDS(i, HObject.separator, false);
if (sds != null)
{
node = new DefaultMutableTreeNode(sds);
root.add( node );
}
} // for (int i=0; i<n; i++)
} // if (sdid != HDFConstants.FAIL && HDFLibrary.SDfileinfo(sdid, argv))
// get top level VData
try {
n = HDFLibrary.VSlone(fid, tmpN, 0);
refs = new int[n];
n = HDFLibrary.VSlone(fid, refs, n);
} catch (HDFException ex)
{
n = 0;
}
for (int i=0; i<n; i++)
{
ref = refs[i];
// no duplicate object at top level
H4Vdata vdata = getVdata(ref, HObject.separator, false);
if (vdata != null)
{
node = new DefaultMutableTreeNode(vdata);
root.add( node );
}
} // for (int i=0; i<n; i++)
return root;
}
/**
* Retrieves the tree structure of the file by depth-first order.
* The current implementation only retrieves group and dataset. It does
* not include named datatype and soft links.
* <p>
* @param parentNode the parent node.
*/
private void depth_first(MutableTreeNode parentNode)
{
//System.out.println("H4File.depth_first() pnode = "+parentNode);
int nelems=0, ref=-1, tag=-1, id=-1, index=-1;
int[] tags = null;
int[] refs = null;
MutableTreeNode node = null;
DefaultMutableTreeNode pnode = (DefaultMutableTreeNode)parentNode;
H4Group pgroup = (H4Group)(pnode.getUserObject());
String fullPath = pgroup.getPath()+pgroup.getName()+HObject.separator;
int gid = pgroup.open();
if (gid == HDFConstants.FAIL)
{
return;
}
try
{
nelems = HDFLibrary.Vntagrefs(gid);
tags = new int[nelems];
refs = new int[nelems];
nelems = HDFLibrary.Vgettagrefs(gid, tags, refs, nelems);
} catch (HDFException ex)
{
nelems = 0;
} finally
{
pgroup.close(gid);
}
// interate through all the objects under this group
for (int i=0; i<nelems; i++)
{
tag = tags[i];
ref = refs[i];
switch (tag)
{
case HDFConstants.DFTAG_RIG:
case HDFConstants.DFTAG_RI:
case HDFConstants.DFTAG_RI8:
try {
index = HDFLibrary.GRreftoindex(grid, (short)ref);
} catch (HDFException ex)
{
index = HDFConstants.FAIL;
}
if (index != HDFConstants.FAIL)
{
H4GRImage gr = getGRImage(index, fullPath, true);
if (gr != null)
{
node = new DefaultMutableTreeNode(gr);
pnode.add( node );
}
}
break;
case HDFConstants.DFTAG_SD:
case HDFConstants.DFTAG_SDG:
case HDFConstants.DFTAG_NDG:
try {
index = HDFLibrary.SDreftoindex(sdid, ref);
} catch (HDFException ex)
{
index = HDFConstants.FAIL;
}
if (index != HDFConstants.FAIL)
{
H4SDS sds = getSDS(index, fullPath, true);
if (sds != null)
{
node = new DefaultMutableTreeNode(sds);
pnode.add( node );
}
}
break;
case HDFConstants.DFTAG_VH:
case HDFConstants.DFTAG_VS:
H4Vdata vdata = getVdata(ref, fullPath, true);
if (vdata != null)
{
node = new DefaultMutableTreeNode(vdata);
pnode.add( node );
}
break;
case HDFConstants.DFTAG_VG:
H4Group vgroup = getVGroup(ref, fullPath, pgroup, true);
if (vgroup != null)
{
node = new DefaultMutableTreeNode(vgroup);
pnode.add( node );
// check for loops
boolean looped = false;
DefaultMutableTreeNode theNode = (DefaultMutableTreeNode)pnode;
while (theNode != null && !looped)
{
H4Group theGroup = (H4Group)theNode.getUserObject();
long[] oid = {tag, ref};
if (theGroup.equalsOID(oid))
looped = true;
else
theNode = (DefaultMutableTreeNode)theNode.getParent();
}
if (!looped)
depth_first(node);
}
break;
default:
break;
} //switch (tag)
} //for (int i=0; i<nelms; i++)
} // private depth_first()
/**
* Retrieve an GR image for the given GR image identifier and index.
* <p>
* @param index the index of the image.
* @param path the path of the image.
* @param copyAllowed The indicator if multiple copies of an object is allowed.
* @return the new H5GRImage if successful; otherwise returns null.
*/
private final H4GRImage getGRImage(int index, String path, boolean copyAllowed)
{
int id=-1, ref=-1;
H4GRImage gr = null;
String[] objName = {""};
int[] tmpInfo = new int[4];
int tag = HDFConstants.DFTAG_RIG;
try
{
id = HDFLibrary.GRselect(grid, index);
ref = HDFLibrary.GRidtoref(id);
HDFLibrary.GRgetiminfo(id, objName, tmpInfo, tmpInfo);
} catch (HDFException ex)
{
id = HDFConstants.FAIL;
}
finally
{
try { HDFLibrary.GRendaccess(id); }
catch (HDFException ex ) {}
}
if (id != HDFConstants.FAIL)
{
long oid[] = {tag, ref};
if (copyAllowed)
{
objList.add(oid);
} else if (find(oid))
{
return null;
}
gr = new H4GRImage(
this,
objName[0],
path,
oid);
}
return gr;
}
/**
* Retrieve a SDS for the given sds identifier and index.
* <p>
* @param sdid the SDS idendifier.
* @param index the index of the SDS.
* @param path the path of the SDS.
* @param copyAllowed The indicator if multiple copies of an object is allowed.
* @return the new H4SDS if successful; otherwise returns null.
*/
private final H4SDS getSDS(int index, String path, boolean copyAllowed)
{
int id=-1, ref=-1;
H4SDS sds = null;
String[] objName = {""};
int[] tmpInfo = new int[4];
int tag = HDFConstants.DFTAG_NDG;
boolean isCoordvar = false;
try
{
id = HDFLibrary.SDselect(sdid, index);
ref = HDFLibrary.SDidtoref(id);
HDFLibrary.SDgetinfo(id, objName, tmpInfo, tmpInfo);
isCoordvar = HDFLibrary.SDiscoordvar(id);
} catch (HDFException ex)
{
id = HDFConstants.FAIL;
}
finally
{
try { HDFLibrary.SDendaccess(id); }
catch (HDFException ex) {}
}
// check if the given SDS has dimension metadata
// Coordinate variables are not displayed. They are created to store
// metadata associated with dimensions. To ensure compatibility with
// netCDF, coordinate variables are implemented as data sets
if (id != HDFConstants.FAIL && !isCoordvar)
{
long oid[] = {tag, ref};
if (copyAllowed)
{
objList.add(oid);
} else if (find(oid))
{
return null;
}
sds = new H4SDS(
this,
objName[0],
path,
oid);
}
return sds;
}
/**
* Retrieve a Vdata for the given Vdata identifier and index.
* <p>
* @param ref the reference idendifier of the Vdata.
* @param path the path of the Vdata.
* @param copyAllowed The indicator if multiple copies of an object is allowed.
* @return the new H4Vdata if successful; otherwise returns null.
*/
private final H4Vdata getVdata(int ref, String path, boolean copyAllowed)
{
int id=-1;
H4Vdata vdata = null;
String[] objName = {""};
String[] vClass = {""};
int tag = HDFConstants.DFTAG_VS;
long oid[] = {tag, ref};
if (copyAllowed)
{
objList.add(oid);
} else if (find(oid))
{
return null;
}
try
{
id = HDFLibrary.VSattach(fid, ref, "r");
HDFLibrary.VSgetclass(id, vClass);
vClass[0] = vClass[0].trim();
HDFLibrary.VSgetname(id, objName);
} catch (HDFException ex)
{
id = HDFConstants.FAIL;
}
finally
{
try { HDFLibrary.VSdetach(id); }
catch (HDFException ex) {}
}
if (id != HDFConstants.FAIL &&
// do not display Vdata named "Attr0.0"
!vClass[0].equalsIgnoreCase(HDFConstants.HDF_ATTRIBUTE) &&
// do not display internal Vdata
!vClass[0].equalsIgnoreCase(HDFConstants.HDF_CHK_TBL))
{
vdata = new H4Vdata(
this,
objName[0],
path,
oid);
}
return vdata;
}
/**
* Retrieve a VGroup for the given VGroup identifier and index.
* <p>
* @param ref the reference idendifier of the VGroup.
* @param path the path of the VGroup.
* @param pgroup the parent group.
* @param copyAllowed The indicator if multiple copies of an object is allowed.
* @return the new H4VGroup if successful; otherwise returns null.
*/
private final H4Group getVGroup(int ref, String path, H4Group pgroup, boolean copyAllowed)
{
int id=-1;
H4Group vgroup = null;
String[] objName = {""};
String[] vClass = {""};
int tag = HDFConstants.DFTAG_VG;
long oid[] = {tag, ref};
if (copyAllowed)
{
objList.add(oid);
} else if (find(oid))
{
return null;
}
try
{
id = HDFLibrary.Vattach(fid, ref, "r");
HDFLibrary.Vgetclass(id, vClass);
vClass[0] = vClass[0].trim();
HDFLibrary.Vgetname(id, objName);
} catch (HDFException ex)
{
id = HDFConstants.FAIL;
}
finally
{
try { HDFLibrary.Vdetach(id); }
catch (HDFException ex) {}
}
// ignore the Vgroups created by the GR interface
if (id != HDFConstants.FAIL &&
// do not display Vdata named "Attr0.0"
!vClass[0].equalsIgnoreCase(HDFConstants.GR_NAME) &&
!vClass[0].equalsIgnoreCase(HDFConstants.RI_NAME) &&
!vClass[0].equalsIgnoreCase(HDFConstants.RIGATTRNAME) &&
!vClass[0].equalsIgnoreCase(HDFConstants.RIGATTRCLASS) &&
!vClass[0].equalsIgnoreCase(HDFConstants.HDF_CDF))
{
vgroup = new H4Group(
this,
objName[0],
path,
pgroup,
oid);
}
return vgroup;
}
/**
* Check if object already exists in memory by match the (tag, ref) pairs.
*/
private final boolean find(long[] oid)
{
boolean existed = false;
if (objList == null)
return false;
int n = objList.size();
long[] theOID = null;
for (int i=0; i<n; i++)
{
theOID = (long[])objList.get(i);
if (theOID[0]==oid[0] && theOID[1]==oid[1])
{
existed = true;
break;
}
}
if (!existed)
{
objList.add(oid);
}
return existed;
}
/**
* Returns the GR identifier, which is returned from GRstart(fid).
*/
int getGRAccessID()
{
return grid;
}
/**
* Returns the SDS identifier, which is returned from SDstart(fname, flag).
*/
int getSDAccessID()
{
return sdid;
}
}