-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTestH5Object.java
More file actions
1576 lines (1344 loc) · 56.3 KB
/
Copy pathTestH5Object.java
File metadata and controls
1576 lines (1344 loc) · 56.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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package test.object;
import java.io.*;
import java.util.*;
import java.lang.reflect.Array;
import ncsa.hdf.object.*;
import ncsa.hdf.object.h5.*;
import ncsa.hdf.hdf5lib.*;
import ncsa.hdf.hdf5lib.exceptions.*;
/**
* Test object at the ncsa.hdf.object package.
* <p>
*
* @version 1.3.0 9/21/2006
* @author Peter X. Cao
*
*/
public class TestH5Object
{
private final static String FILE_NAME = "TestH5Object.h5";
private final static String FILE_NAME2 = "./samples/TestH5Obejct2.h5";
private final static String NAME_GROUP = "/g0";
private final static String NAME_GROUP_ATTR = "/g0_attr";
private final static String NAME_GROUP_SUB = "/g0/g00";
private final static String NAME_DATASET_INT = "/dataset_int";
private final static String NAME_DATASET_FLOAT = "/dataset_float";
private final static String NAME_DATASET_CHAR = "/dataset_byte";
private final static String NAME_DATASET_STR = "/dataset_str";
private final static String NAME_DATASET_ENUM = "/dataset_enum";
private final static String NAME_DATASET_ATTR = "/dataset_with_attr";
private final static String NAME_DATASET_COMPOUND = "/comp_dataset";
private final static String NAME_DATASET_SUB = "/g0/dataset_int";
private final static String NAME_DATASET_SUB_SUB = "/g0/g00/dataset_float";
private final static H5File H5FILE = new H5File();
private final static long DIM1 = 50;
private final static long DIM2 = 10;
private final static long[] DIMs = {DIM1, DIM2};
private final static long[] CHUNKs = {DIM1/2, DIM2/2};
private final static int RANK = 2;
private final static int STR_LEN = 20;
private final static int DIM_SIZE = (int)(DIM1*DIM2);;
/* testing data */
private final static int[] DATA_INT = new int[DIM_SIZE];
private final static float[] DATA_FLOAT = new float[DIM_SIZE];
private final static byte[] DATA_BYTE = new byte[DIM_SIZE];
private final static String[] DATA_STR = new String[DIM_SIZE];
private final static int[] DATA_ENUM = new int[DIM_SIZE];
private final static Vector DATA_COMP = new Vector(3);
private static PrintStream out = null;
/**
* Constructs an instance of TestH5Object.
* @param out_stream the out stream for printing the test results.
*/
public TestH5Object(final PrintStream print_stream)
{
if (print_stream == null) {
out = System.out;
} else {
out = print_stream;
}
for (int i=0; i<DIM_SIZE; i++) {
DATA_INT[i] = i;
DATA_FLOAT[i] = i+i/100.0f;
DATA_BYTE[i] = (byte)Math.IEEEremainder(i, 127);
DATA_STR[i] = "str"+i;
DATA_ENUM[i] = (int)Math.IEEEremainder(i, 2);
}
DATA_COMP.add(0, DATA_INT);
DATA_COMP.add(1, DATA_FLOAT);
DATA_COMP.add(2, DATA_STR);
}
private static final void passed(final String message) {
out.println("PASSED:\t\t"+message);
}
private static final void failed(final String message, final Exception err, final H5File file) {
out.println("FAILED***:\t"+message +"--"+err);
try { file.close(); } catch (final Exception ex) {}
}
/**
* Check if all the data values of two data buffers are the same
* @param buf1 the first buffer to compare
* @param buf2 the second buffer to compare
* @return true if all the vlaues equal; otherwise, returns false.
*/
private static final boolean dataEquals(final Object buf1, final Object buf2) {
// array cannot be null
if ((buf1 == null) || (buf2==null)) {
return false;
}
// must be array
if (!buf1.getClass().isArray() || !buf2.getClass().isArray()) {
return false;
}
// must be the same kind of array
final String cname = buf1.getClass().getName();
if (!cname.equals(buf2.getClass().getName())) {
return false;
}
// must have the same size
final int n = Array.getLength(buf1);
if (n != Array.getLength(buf2)) {
return false;
}
if (cname.equals("[I")) {
final int[] data1 = (int[])buf1;
final int[] data2 = (int[])buf2;
for (int i=0; i<n; i++) {
if (data1[i] != data2[i]) {
return false;
}
}
}
else if (cname.equals("[F")) {
final float[] data1 = (float[])buf1;
final float[] data2 = (float[])buf2;
for (int i=0; i<n; i++) {
if (data1[i] != data2[i]) {
return false;
}
}
}
else if (cname.equals("[B")) {
final byte[] data1 = (byte[])buf1;
final byte[] data2 = (byte[])buf2;
for (int i=0; i<n; i++) {
if (data1[i] != data2[i]) {
return false;
}
}
}
else if (cname.equals("[Ljava.lang.String;")) {
final String[] data1 = (String[])buf1;
final String[] data2 = (String[])buf2;
for (int i=0; i<n; i++) {
if (!data1[i].equals(data2[i])) {
return false;
}
}
} else {
return false;
}
return true;
}
/**
* Create a test file.
*
* @param fname the name of the file to open
* @return true if successful; otherwise returns false
*/
private static final boolean create_test_file(final String fname, String message)
{
H5File file=null;
Group g0, g1, g00;
message += "\tCreate a test file: "+fname;
try {
file = (H5File)H5FILE.open(fname, H5File.CREATE);
file.open();
} catch (final Exception ex) {failed(message, ex, file); return false;}
// create groups
try {
g0 = file.createGroup(NAME_GROUP, null);
g1 = file.createGroup(NAME_GROUP_ATTR, null);
g00 = file.createGroup(NAME_GROUP_SUB, null);
final long[] attrDims = {1};
final String attrName = "Test attribute";
final String[] attrValue = {"Test for group attribute"};
final Datatype attrType = new H5Datatype(Datatype.CLASS_STRING, attrValue[0].length()+1, -1, -1);
final Attribute attr = new Attribute(attrName, attrType, attrDims);
attr.setValue(attrValue);
g1.writeMetadata(attr);
} catch (final Exception ex) { failed(message, ex, file); return false;}
// create datasets
try {
file.createScalarDS(NAME_DATASET_INT, null, new H5Datatype(Datatype.CLASS_INTEGER, -1, -1, -1), DIMs, null, CHUNKs, 9, DATA_INT);
file.createScalarDS(NAME_DATASET_FLOAT, null, new H5Datatype(Datatype.CLASS_FLOAT, -1, -1, -1), DIMs, null, CHUNKs, 9, DATA_FLOAT);
file.createScalarDS(NAME_DATASET_CHAR, null, new H5Datatype(Datatype.CLASS_CHAR, -1, -1, -1), DIMs, null, CHUNKs, 9, DATA_BYTE);
file.createScalarDS(NAME_DATASET_STR, null, new H5Datatype(Datatype.CLASS_STRING, STR_LEN, -1, -1), DIMs, null, CHUNKs, 9, DATA_STR);
file.createScalarDS(NAME_DATASET_ENUM, null, new H5Datatype(Datatype.CLASS_ENUM, -1, -1, -1), DIMs, null, CHUNKs, 9, DATA_ENUM);
file.createScalarDS(NAME_DATASET_SUB, g0, new H5Datatype(Datatype.CLASS_INTEGER, -1, -1, -1), DIMs, null, CHUNKs, 9, DATA_INT);
file.createScalarDS(NAME_DATASET_SUB_SUB, g00, new H5Datatype(Datatype.CLASS_FLOAT, -1, -1, -1), DIMs, null, CHUNKs, 9, DATA_FLOAT);
file.createImage(NAME_DATASET_ATTR, null, new H5Datatype(Datatype.CLASS_INTEGER, 1, -1, -1), DIMs, null, CHUNKs, 9, 1, -1, DATA_BYTE);
final Datatype[] mdtypes = {new H5Datatype(Datatype.CLASS_INTEGER, -1, -1, -1), new H5Datatype(Datatype.CLASS_FLOAT, -1, -1, -1), new H5Datatype(Datatype.CLASS_STRING, STR_LEN, -1, -1)};
final String[] mnames = {"int", "float", "string"};
file.createCompoundDS(NAME_DATASET_COMPOUND, null, DIMs, null, CHUNKs, 9, mnames, mdtypes, null, DATA_COMP);
} catch (final Exception ex) { failed(message, ex, file); return false;}
try { file.close(); } catch (final Exception ex) {}
return true;
}
/**
* Test H5File create() function.
*
* @param fname the name of the file to create
* @return zero if successful; otherwise returns one
*/
private int test_H5File_create(final String fname)
{
H5File file=null;
String message = "";
message = "Create a new file -- new H5File("+fname+", H5File.CREATE)";
try {
file = new H5File(fname, H5File.CREATE);
file.open();
file.close();
} catch (final Exception ex) { failed(message, ex, file); return 1;}
passed(message);
message = "Create a new file -- H5File.create("+fname+")";
try {
file = (H5File)H5FILE.create(fname);
file.open();
file.close();
} catch (final Exception ex) { failed(message, ex, file); return 1;}
passed(message);
message = "Create a new file -- H5File.open("+fname+", H5File.CREATE)";
try {
file = (H5File)H5FILE.open(fname, H5File.CREATE);
file.open();
file.close();
} catch (final Exception ex) { failed(message, ex, file); return 1;}
passed(message);
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Test H5File open() function.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_H5File_open(final String fname)
{
H5File file=null;
String message = "";
message = "Testing open file with read/write access";
try {
file = new H5File(fname, H5File.CREATE);
file.open();
file.close();
} catch (final Exception ex) { failed(message, ex, file); return 1;}
message = "Open file with READ-ONLY access -- H5File.open("+fname+", H5File.READ)";
try {
file = (H5File)H5FILE.open(fname, H5File.READ);
file.close();
} catch (final Exception ex) { failed(message, ex, file); return 1;}
passed(message);
message = "Open file with WRITE access -- H5File.open("+fname+", H5File.WRITE)";
try {
file = (H5File)H5FILE.open(fname, H5File.WRITE);
file.close();
} catch (final Exception ex) { failed(message, ex, file); return 1;}
passed(message);
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Test H5File open() function with relative file path.
* The cwd may be changed at Dataset.read() by H5Dchdir_ext()
* to make it work for external datasets. We need to set it
* back before the file is closed/opened.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_H5File_open_relative_path(final String fname)
{
H5File file=null;
String message = "";
message = "H5File open() function with relative file path";
if (!create_test_file(fname, message)) {
return 1;
}
try {
// test open/close file and open/close dataset
file = new H5File(fname, H5File.READ);
Dataset dset = (Dataset)file.get(NAME_DATASET_ATTR);
dset.getData();
file.close();
file = new H5File(fname, H5File.READ);
dset = (Dataset)file.get(NAME_DATASET_ATTR);
dset.getData();
file.close();
// test open file and open multiple datasets
file = new H5File(fname, H5File.READ);
dset = (Dataset)file.get(NAME_DATASET_ATTR);
dset.getData();
dset = (Dataset)file.get(NAME_DATASET_COMPOUND);
dset.getData();
dset = (Dataset)file.get(NAME_DATASET_SUB);
dset.getData();
file.close();
} catch (final Exception ex) { failed(message, ex, file); return 1;}
passed(message);
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Test H5File createGroup() function.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_H5File_createGroup(final String fname)
{
H5File file=null;
String message = "";
file = new H5File(fname);
// create groups
Group g0 = null;
message = "Create a group at root -- H5File.createGroup and H5Group.create(\"/g0\", root_group)";
try {
g0 = file.createGroup("/g0", null);
} catch (final Exception ex) { failed(message, ex, file); return 1;}
passed(message);
Group g00 = null;
message = "Create a group with absolute path -- H5File.createGroup and H5Group.create(\"g0/g00\", root_group)";
try {
g00 = file.createGroup("g0/g00", null);
} catch (final Exception ex) { failed(message, ex, file); return 1;}
passed(message);
Group g01 = null;
message = "Create a group at non-root group -- H5File.createGroup and H5Group.create(\"/g0/g01/\", group_g0)";
try {
g01 = file.createGroup("/g0/g01/", g0);
} catch (final Exception ex) { failed(message, ex, file); return 1;}
passed(message);
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Test H5File createDatatype() function.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_H5File_createDatatype(final String fname)
{
H5File file=null;
String message = "";
final int N = 5;
final int dtype_cls[] = {Datatype.CLASS_INTEGER, Datatype.CLASS_FLOAT,
Datatype.CLASS_CHAR, Datatype.CLASS_STRING, Datatype.CLASS_ENUM};
final String dtype_names[] = {"INTEGER", "FLOAT", "CHAR", "STRING", "ENUM"};
final String msgs[] = { "H5File.createDatatype(..., "+dtype_names[0]+")",
"H5File.createDatatype(..., "+dtype_names[1]+")",
"H5File.createDatatype(..., "+dtype_names[2]+")",
"H5File.createDatatype(..., "+dtype_names[3]+")",
"H5File.createDatatype(..., "+dtype_names[4]+")"};
message = "Test creating named datatypes";
try {
file = (H5File)H5FILE.open(fname, H5File.CREATE);
file.open();
} catch (final Exception ex) {failed(message, ex, file); return 1;}
// create groups
Datatype dtype = null;
for (int i=0; i<N; i++)
{
message = "Create a named datatype -- "+msgs[i];
try {
dtype = file.createDatatype(dtype_cls[i],Datatype.NATIVE, Datatype.NATIVE, Datatype.NATIVE, dtype_names[i]);
} catch (final Exception ex) { failed(message, ex, file); return 1;}
passed(message);
}
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Test H5File createScalarDS function.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_H5File_createScalarDS(final String fname)
{
H5File file=null;
String message = "";
Group pgroup = null;
final int N=5;
final int dtype_cls[] = {Datatype.CLASS_INTEGER, Datatype.CLASS_FLOAT,
Datatype.CLASS_CHAR, Datatype.CLASS_STRING, Datatype.CLASS_ENUM};
final int dtype_sizes[] = {-1, -1, 1, 80, -1};
final String names[] = {"INTEGER", "FLOAT", "CHAR", "STRING", "ENUM"};
message = "Test creating ScalarDS";
try {
file = (H5File)H5FILE.open(fname, H5File.CREATE);
file.open();
pgroup = (Group)file.get("/");
} catch (final Exception ex) {failed(message, ex, file); return 1;}
final Object[] all_data = new Object[N];
all_data[0] = DATA_INT;
all_data[1] = DATA_FLOAT;
all_data[2] = DATA_BYTE;
all_data[3] = DATA_STR;
all_data[4] = DATA_ENUM;
// create groups
Datatype dtype = null;
Dataset dset = null;
Object data_read = null;
for (int i=0; i<N; i++)
{
message = "Create/read/write a H5ScalarDS -- H5File.createScalarDS and H5ScalarDS.create (..., "+names[i]+", ...) "+names[i];
try {
dtype = new H5Datatype(dtype_cls[i], dtype_sizes[i], -1, -1);
dset = file.createScalarDS(names[i], pgroup, dtype, DIMs, null, CHUNKs, 9, all_data[i]);
} catch (final Exception ex) { failed(message, ex, file); return 1;}
// test data valuse
try {
dset.init();
final long[] selectedDims = dset.getSelectedDims();
final long[] dims = dset.getDims();
final long[] starts = dset.getStartDims();
final int rank = dset.getRank();
// read all data
for (int j=0; j<rank; j++) {
starts[j] = 0;
selectedDims[j] = dims[j];
}
data_read = dset.read();
} catch (final Exception ex) { failed(message, ex, file); return 1;}
if ( !dataEquals(all_data[i], data_read) ) {
failed(message, new HDF5LibraryException("Incorrect data values in file"), file);
return 1;
}
passed(message);
}
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Test H5File createLink function.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_H5File_createLink(final String fname)
{
H5File file=null;
String message = "";
Group pgroup = null;
message = "Create a hard link -- H5File.createLink(Group parentGroup, String name, HObject currentObj)";
try {
file = (H5File)H5FILE.open(fname, H5File.CREATE);
file.open();
pgroup = (Group)file.get("/");
} catch (final Exception ex) {failed(message, ex, file); return 1;}
final String gname = "Group";
Group grp = null;
try {
grp = file.createGroup(gname, null);
} catch (final Exception ex) { failed(message, ex, file); return 1;}
HObject hobj = null;
try {
hobj = file.createLink(pgroup, "link to "+gname, grp);
} catch (final Exception ex) { failed(message, ex, file); return 1;}
final long oid[] = grp.getOID();
if (!hobj.equalsOID(oid))
{
failed(message, new HDF5LibraryException("link to the wrong object"), file);
return 1;
}
passed(message);
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Test H5File createImage function.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_H5File_createImage(final String fname)
{
H5File file=null;
String message = "";
Group pgroup = null;
message = "Ceate an image -- H5File.createImage(...) and H5File.createImageAttributes()";
try {
file = (H5File)H5FILE.open(fname, H5File.CREATE);
file.open();
pgroup = (Group)file.get("/");
} catch (final Exception ex) {failed(message, ex, file); return 1;}
// create groups
Datatype dtype = null;
Dataset dset = null;
Object data_read = null;
try {
dtype = new H5Datatype(Datatype.CLASS_INTEGER, 1, -1, -1);
dset = file.createImage("Image", pgroup, dtype, DIMs, null, CHUNKs, 9, 1, -1, DATA_BYTE);
} catch (final Exception ex) { failed(message, ex, file); return 1;}
// test data valuse
try {
data_read = dset.read();
} catch (final Exception ex) { failed(message, ex, file); return 1;}
if ( !dataEquals(DATA_BYTE, data_read) ) {
failed(message, new HDF5LibraryException("Incorrect data values in file"), file);
return 1;
}
passed(message);
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Test H5File createCompoundDS function.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_H5File_createCompoundDS(final String fname)
{
H5File file=null;
String message = "";
Group pgroup = null;
message = "Create/read/write a compound dataset -- H5File.createCompoundDS(...) and H5CompoundDS.create(...), H5CompoundDS.read(), H5CompoundDS.write() {INTEGER, FLOAT, STRING}";
try {
file = (H5File)H5FILE.open(fname, H5File.CREATE);
file.open();
pgroup = (Group)file.get("/");
} catch (final Exception ex) {failed(message, ex, file); return 1;}
final Vector data = new Vector();
data.add(0, DATA_INT);
data.add(1, DATA_FLOAT);
data.add(2, DATA_STR);
// create groups
final Datatype[] mdtypes = new H5Datatype[3];
final String[] mnames = {"int", "float", "string"};
Dataset dset = null;
try {
mdtypes[0] = new H5Datatype(Datatype.CLASS_INTEGER, -1, -1, -1);
mdtypes[1] = new H5Datatype(Datatype.CLASS_FLOAT, -1, -1, -1);
mdtypes[2] = new H5Datatype(Datatype.CLASS_STRING, STR_LEN, -1, -1);
dset = file.createCompoundDS("/CompoundDS", pgroup, DIMs, null, CHUNKs, 9, mnames, mdtypes, null, data);
} catch (final Exception ex) { failed(message, ex, file); return 1;}
// test data valuse
List data_read = null;
try {
data_read = (List)dset.read();
} catch (final Exception ex) { failed(message, ex, file); return 1;}
if ( !dataEquals(DATA_INT, data_read.get(0)) ||
!dataEquals(DATA_FLOAT, data_read.get(1)) ||
!dataEquals(DATA_STR, data_read.get(2))) {
failed(message, new HDF5LibraryException("Incorrect data values in file"), file);
return 1;
}
// tests for subset
final H5CompoundDS compDS = (H5CompoundDS)dset;
int rank = compDS.getRank();
try { if (rank<=0) {
compDS.init();
} } catch (final Exception ex) {}
rank = compDS.getRank();
// read only one column but all rows
compDS.setMemberSelection(false); //unselect all members
compDS.selectMember(1); // select the second column
try {
data_read = (List)dset.read();
} catch (final Exception ex) { failed(message, ex, file); return 1;}
if ( !dataEquals(DATA_FLOAT, data_read.get(0)) || (data_read.size()!=1)) {
failed(message, new HDF5LibraryException("incorrect data values from file"), file);
return 1;
}
// read only one row but all columns
compDS.setMemberSelection(true); //select all members, it is default
final int nmembers = compDS.getSelectedMemberCount();
final long[] count = compDS.getSelectedDims();
final long[] start = compDS.getStartDims();
for (int i=0; i<rank; i++) {
start[i] = 2; // start the third data point
count[i] = 1; // select only one row (the third row)
}
try {
data_read = (List)dset.read();
} catch (final Exception ex) { failed(message, ex, file); return 1;}
if (nmembers != compDS.getMemberCount())
{
failed(message, new HDF5LibraryException("incorrect members selection"), file);
return 1;
}
for (int i=0; i<nmembers; i++) {
if (Array.getLength(data_read.get(i)) != 1)
{
failed(message, new HDF5LibraryException("incorrect row selection"), file);
return 1;
}
}
// create dataset at non root group
Group g0 = null;
try {
g0 = file.createGroup("/gg0", null);
} catch (final Exception ex) { failed(message, ex, file); return 1;}
try {
mdtypes[0] = new H5Datatype(Datatype.CLASS_INTEGER, -1, -1, -1);
mdtypes[1] = new H5Datatype(Datatype.CLASS_FLOAT, -1, -1, -1);
mdtypes[2] = new H5Datatype(Datatype.CLASS_STRING, STR_LEN, -1, -1);
dset = file.createCompoundDS("/g0/CompoundDS/", g0, DIMs, null, CHUNKs, 9, mnames, mdtypes, null, data);
} catch (final Exception ex) { failed(message, ex, file); return 1;}
passed(message);
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Test H5File copy function.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_H5File_copy(final String fname)
{
H5File file=null;
String message = "";
Group pgroup = null;
message = "Copy dataset, group and attributes -- H5File.copy(...) and H5File.get(...)";
try {
file = (H5File)H5FILE.open(fname, H5File.CREATE);
file.open();
pgroup = (Group)file.get("/");
} catch (final Exception ex) {failed(message, ex, file); return 1;}
final int size = (int) (DIM1*DIM2);
final byte[] bdata = new byte[size];
for (int i=0; i<size; i++) {
bdata[i] = (byte)Math.IEEEremainder(i, 127);
}
Group grp = null;
Datatype dtype = null;
Dataset dset = null;
try {
grp = file.createGroup("/Group", null);
dtype = new H5Datatype(Datatype.CLASS_INTEGER, 1, -1, -1);
dset = file.createImage("Dataset", pgroup, dtype, DIMs, null, CHUNKs, 9, 1, -1, bdata);
} catch (final Exception ex) { failed(message, ex, file); return 1;}
try {
file.copy(dset, grp);
file.copy(grp, pgroup, "~Group");
} catch (final Exception ex) { failed(message, ex, file); return 1;}
passed(message);
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Test H5File getAttribute function.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_H5File_getAttribute(final String fname)
{
H5File file=null;
String message = "";
message = "Read/write attributes from a group/dataset";
if (!create_test_file(fname, message)) {
return 1;
}
try {
file = new H5File(fname);
final Dataset dset = (Dataset)file.get(NAME_DATASET_ATTR);
final int did = dset.open();
List attrs = H5File.getAttribute(did);
try {dset.close(did);} catch (final Exception ex2) {}
if ((attrs==null) || (attrs.size()<1)) {
failed(message, new HDF5LibraryException("failed to read attributes from dataset"), file);
return 1;
}
attrs.clear();
final Group grp = (Group)file.get(NAME_GROUP_ATTR);
final int gid = grp.open();
attrs = H5File.getAttribute(gid);
try {grp.close(gid);} catch (final Exception ex2) {}
if ((attrs==null) || (attrs.size()<1)) {
failed(message, new HDF5LibraryException("failed to read attributes from group"), file);
return 1;
}
} catch (final Exception ex) { failed(message, ex, file); return 1;}
passed(message);
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Test H5File getHObject() function.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_H5File_getHObject(final String fname)
{
final H5File file=null;
String message = "";
message = "Get a group for a given path -- H5File.getHObject(String filename, String path), H5File.getHObject(fname#//Dataset)";
if (!create_test_file(fname, message)) {
return 1;
}
try {
HObject obj = FileFormat.getHObject(fname, NAME_GROUP);
if (obj == null) {
failed(message, new HDF5Exception("Failed to get a group"), file);
return 1;
}
try { obj.getFileFormat().close(); } catch (final Exception ex2) {}
obj = FileFormat.getHObject(fname+"#//"+NAME_GROUP_SUB);
if (obj == null) {
failed(message, new HDF5Exception("Failed to get a group"), file);
return 1;
}
try { obj.getFileFormat().close(); } catch (final Exception ex2) {}
} catch (final Exception ex) { failed(message, ex, file); return 1;}
passed(message);
message = "Get a ScalarDS for a given path -- H5File.getHObject(String filename, String path), H5File.getHObject(fname#//Dataset)";
try {
HObject obj = FileFormat.getHObject(fname, NAME_DATASET_INT);
if (obj == null) {
failed(message, new HDF5Exception("Failed to get a dataset"), file);
return 1;
}
try { obj.getFileFormat().close(); } catch (final Exception ex2) {}
obj = FileFormat.getHObject(fname+"#//"+NAME_DATASET_FLOAT);
if (obj == null) {
failed(message, new HDF5Exception("Failed to get a dataset"), file);
return 1;
}
try { obj.getFileFormat().close(); } catch (final Exception ex2) {}
} catch (final Exception ex) { failed(message, ex, file); return 1;}
passed(message);
message = "Get a CompoundDS for a given path -- H5File.getHObject(String filename, String path), H5File.getHObject(fname#//Dataset)";
try {
final HObject obj = FileFormat.getHObject(fname, NAME_DATASET_COMPOUND);
if (obj == null) {
failed(message, new HDF5Exception("Failed to get a compound dataset"), file);
return 1;
}
try { obj.getFileFormat().close(); } catch (final Exception ex2) {}
} catch (final Exception ex) { failed(message, ex, file); return 1;}
passed(message);
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Test HObject getFID() function.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_HObject_getFID(final String fname)
{
H5File file=null;
String message = "";
int fid = 0;
Group pgroup = null;
message = "Get file identifier -- Group.getFID(), Dataset.getFID()";
try {
file = (H5File)H5FILE.open(fname, H5File.CREATE);
fid = file.open();
pgroup = (Group)file.get("/");
} catch (final Exception ex) {failed(message, ex, file); return 1;}
if (fid != pgroup.getFID()) {
failed(message, new HDF5LibraryException("wrong object ID in group"), file);
return 1;
}
Datatype dtype = null;
Dataset dset = null;
try {
dtype = new H5Datatype(Datatype.CLASS_INTEGER, 1, -1, -1);
dset = file.createScalarDS("Dataset", pgroup, dtype, DIMs, null, CHUNKs, 9, null);
} catch (final Exception ex) { failed(message, ex, file); return 1;}
if (fid != dset.getFID()) {
failed(message, new HDF5LibraryException("wrong object ID in dataset"), file);
return 1;
}
passed(message);
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Test HObject getName() function.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_HObject_getName(final String fname)
{
H5File file=null;
String message = "";
int fid = 0;
Group pgroup = null;
message = "Get object name and path -- Group.getName(), Group.getPath(), Group.getFullName(), Dataset.getName(), Dataset.getPath(), Dataset.getFullName()";
try {
file = (H5File)H5FILE.open(fname, H5File.CREATE);
fid = file.open();
pgroup = (Group)file.get("/");
} catch (final Exception ex) {failed(message, ex, file); return 1;}
Group grp = null;
try {
grp = file.createGroup("/Group", null);
} catch (final Exception ex) {failed(message, ex, file); return 1;}
Group grp2 = null;
try {
grp2 = file.createGroup("/Group/Group2", grp);
} catch (final Exception ex) { failed(message, ex, file); return 1;}
if (!grp2.getName().endsWith("Group2")) {
failed(message, new HDF5LibraryException("wrong name for the group"), file);
return 1;
}
if (!grp2.getPath().endsWith("/Group/")) {
failed(message, new HDF5LibraryException("wrong path for the group"), file);
return 1;
}
if (!grp2.getFullName().endsWith("/Group/Group2")) {
failed(message, new HDF5LibraryException("wrong full path for the group"), file);
return 1;
}
Datatype dtype = null;
Dataset dset = null;
try {
dtype = new H5Datatype(Datatype.CLASS_INTEGER, 1, -1, -1);
dset = file.createScalarDS("Dataset", pgroup, dtype, DIMs, null, CHUNKs, 9, null);
} catch (final Exception ex) { failed(message, ex, file); return 1;}
if (!dset.getName().endsWith("Dataset")) {
failed(message, new HDF5LibraryException("wrong name for the dataset"), file);
return 1;
}
if (!dset.getPath().endsWith("/")) {
failed(message, new HDF5LibraryException("wrong path for the dataset"), file);
return 1;
}
if (!dset.getFullName().endsWith("/Dataset")) {
failed(message, new HDF5LibraryException("wrong full path for the dataset"), file);
return 1;
}
passed(message);
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Test Group isRoot function.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_Group_isRoot(final String fname)
{
H5File file=null;
String message = "";
Group pgroup = null;
message = "Check root group -- Group.isRoot()";
try {
file = (H5File)H5FILE.open(fname, H5File.CREATE);
file.open();
pgroup = (Group)file.get("/");
} catch (final Exception ex) {failed(message, ex, file); return 1;}
if (!pgroup.isRoot())
{
failed(message, new HDF5LibraryException("failed to test root group"), file);
return 1;
}
Group grp = null;
try {
grp = file.createGroup("/Group", null);
} catch (final Exception ex) { failed(message, ex, file); return 1;}
try { file.close(); } catch (final Exception ex) {}
try {
grp = (Group)FileFormat.getHObject(fname, "/Group");
} catch (final Exception ex) {failed(message, ex, file); return 1;}
if (grp.isRoot())
{
failed(message, new HDF5LibraryException("failed to test non-root group"), file);
return 1;