-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDebugHDF.java
More file actions
4006 lines (3266 loc) · 148 KB
/
Copy pathDebugHDF.java
File metadata and controls
4006 lines (3266 loc) · 148 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.misc;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.BitSet;
import java.util.HashMap;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.tree.DefaultMutableTreeNode;
import ncsa.hdf.hdf5lib.H5;
import ncsa.hdf.hdf5lib.HDF5Constants;
import ncsa.hdf.hdf5lib.HDFNativeData;
import ncsa.hdf.hdf5lib.exceptions.HDF5Exception;
import ncsa.hdf.object.Attribute;
import ncsa.hdf.object.CompoundDS;
import ncsa.hdf.object.Dataset;
import ncsa.hdf.object.Datatype;
import ncsa.hdf.object.FileFormat;
import ncsa.hdf.object.Group;
import ncsa.hdf.object.HObject;
import ncsa.hdf.object.h4.H4File;
import ncsa.hdf.object.h4.H4SDS;
import ncsa.hdf.object.h5.H5CompoundDS;
import ncsa.hdf.object.h5.H5Datatype;
import ncsa.hdf.object.h5.H5File;
import ncsa.hdf.object.h5.H5Group;
import ncsa.hdf.object.h5.H5ScalarDS;
public class DebugHDF {
public static void main(final String[] args) {
try {
int[] libversion = {0, 0, 0};
H5.H5get_libversion(libversion);
System.out.println(libversion[0]+"."+libversion[1]+"."+libversion[2]);
} catch (Exception ex) {ex.printStackTrace();}
// try { create_debug_file();} catch(Exception ex) {}
// try { createStrDataset( "G:\\temp\\H5DatasetCreate.h5"); } catch(Exception ex) {}
// try { createDataset( "E:\\temp\\H5DatasetCreate.h5"); } catch(Exception ex) {ex.printStackTrace();}
// try { testchunkchche(); } catch (Exception ex) {ex.printStackTrace();}
// try { TestHDFcompound(); } catch (Exception ex) {ex.printStackTrace();}
// try { TestHDFdelete( "E:\\temp\\H5DatasetDelete.h5"); } catch(Exception ex) {ex.printStackTrace();}/
// try { TestHDFcomment( "E:\\temp\\H5DatasetComment.h5"); } catch(Exception ex) {ex.printStackTrace();}
// try { TestHDFgenotype( "E:\\temp\\genotypes_chr22_CEU.h5"); } catch(Exception ex) {ex.printStackTrace();}
// try { TestHDFvector( "E:\\temp\\TestVector.h5"); } catch(Exception ex) {ex.printStackTrace();}
// try { testSizeof(); } catch(Exception ex) {ex.printStackTrace();}
// try { testSDgetchunkinfo("E:\\temp\\MOD021KM.A2006016.0942.hdf"); } catch(Exception ex) {ex.printStackTrace();}
// try { testEnum("E:\\temp\\MOD021KM.A2006016.0942.hdf"); } catch(Exception ex) {ex.printStackTrace();}
// try { testHDF5OpenClose(); } catch(Exception ex) {ex.printStackTrace();}
// try { testHDF5Write("E:\\temp\\TestHDF5Write.h5"); } catch(Exception ex) {ex.printStackTrace();}
// try { TestHDF5Misc("E:\\hdf-files\\TestHDF5Misc.h5"); } catch(Exception ex) {ex.printStackTrace();}
// try { TestHDF5Get("E:\\hdf-files\\TestHDF5Get.h5"); } catch(Exception ex) {ex.printStackTrace();}
// try { TestHDF5Copy("d:\\hdf-files\\hdf5_test.h5", "/arrays/Vdata with mixed types"); } catch(Exception ex) {ex.printStackTrace();}
// try { TestHDF5Copy("d:\\hdf-files\\hdf5_test.h5", "/arrays"); } catch(Exception ex) {ex.printStackTrace();}
// try { TestHDF5Copy("d:\\hdf-files\\hdf5_test.h5", "/datatypes/H5T_NATIVE_INT"); } catch(Exception ex) {ex.printStackTrace();}
// try { testGetObjID(); } catch(Exception ex) {ex.printStackTrace();}
// try { testFillValue( "E:\\temp\\TestFillValue.h5"); } catch(Exception ex) {}
// try { TestGetOneRow("E:\\hdf-files\\hdf5_test.h5", "/arrays/Vdata with mixed types", 0); } catch(Exception ex) {ex.printStackTrace();}
// try { TestGetOneRow("E:\\hdf-files\\HDF5FileDAOTest.h5", "/Group0/1/Table0"); } catch(Exception ex) {ex.printStackTrace();}
// try { TestH5OpenClose("TestH5Object.h5"); } catch(Exception ex) {ex.printStackTrace();}
// try { checkMemory(); } catch(Exception ex) {ex.printStackTrace();}
// try { testCompressedStrings("G:\\temp\\test.h5"); } catch(Exception ex) {ex.printStackTrace();}
// try { testCreateLongPath("G:\\temp\\test_hdf5_5_group_levels.h5"); } catch(Exception ex) {ex.printStackTrace();}
// try { TestH5Bug847("d:\\hdf-files\\h5bug847.h5"); } catch(Exception ex) {ex.printStackTrace();}
// try { TestH5ReadChunk("d:\\hdf-files\\ExampleHDF5.hdf5"); } catch(Exception ex) {ex.printStackTrace();}
// try { TestH5Bug863("d:\\hdf-files\\bug863.h5"); } catch(final Exception ex) {ex.printStackTrace();}
// try { checkMemory(); } catch(Exception ex) {ex.printStackTrace();}
// try { testConvertFromUnsignedC();} catch(Exception ex) {ex.printStackTrace();}
// try { TestH5ReadPerf("d:\\hdf-files\\ushort_8kx8k_fast_order.h5"); } catch(Exception ex) {ex.printStackTrace();}
// try { TestH5ReadPerf("d:\\hdf-files\\ushort_8kx8k_fast_order.h5"); } catch(Exception ex) {ex.printStackTrace();}
// try { TestH5WriteFloats("g:\\temp\\t.h5"); } catch(Exception ex) {ex.printStackTrace();}
// try { TestH5Vlen("g:\\temp\\t.h5"); } catch(Exception ex) {ex.printStackTrace();}
// try { TestH5Array("g:\\temp\\t.h5"); } catch(Exception ex) {ex.printStackTrace();}
// try { TestH5DreadNIO("d:\\hdf-files\\ushort_8kx8k_fast_order.h5"); } catch(Exception ex) {ex.printStackTrace();}
// try { TestH5Compound2000Fields("g:\\temp\\h5comp2k.h5"); } catch(Exception ex) {ex.printStackTrace();}
// try { TestMemoryLeakOpenClose("D:\\hdf-files\\SAFNWC_MSG2_TPW__200807281015_CoMd.h5"); } catch(Exception ex) {ex.printStackTrace();}
// try { testTofwerkReaderBug1213("D:\\hdf-files\\bug1213_GCxGC_dummyData.h5"); } catch(Exception ex) {ex.printStackTrace();}
// try { testTofwerkReaderBug1213("D:\\hdf-files\\bug1213_GCxGC_dummyData_chunk100x200.h5"); } catch(Exception ex) {ex.printStackTrace();}
// try { testTofwerkReaderBug1213("G:\\Projects\\Java\\Release\\hdfview_release_test_files\\bug1213_GCxGC_dummyData_chunk10x20.h5"); } catch(Exception ex) {ex.printStackTrace();}
// try { TestMemoryLeak("D:\\hdf-files\\debug_memory_leak.h5"); } catch(Exception ex) {ex.printStackTrace();}
// try { TestBEAttr("G:\\temp\\TestBEAttr.h5"); } catch(Exception ex) {ex.printStackTrace();}
// try { TestVlenRead("d:\\hdf-files\\test_vlen.h5"); } catch(Exception ex) {ex.printStackTrace();}
// try { launchBrowser("http://www.armchairgeneral.com/tactics-101-021-intelligence-preparation-of-the-battlefield-in-urban-operations.htm"); } catch(Exception ex) {ex.printStackTrace();}
// try { launchBrowser("G:\\Projects\\ERDC\\Data\\Hetereogenous_Objects\\Events_Intel-Report.pdf"); } catch(Exception ex) {ex.printStackTrace();}
// try { launchBrowser("G:\\Projects\\ERDC\\Data\\Hetereogenous_Objects\\IPB_URL-Reference.txt"); } catch(Exception ex) {ex.printStackTrace();}
// try { launchBrowser("G:\\Projects\\ERDC\\Data\\Hetereogenous_Objects\\Recon_Immersive-Video.avi"); } catch(Exception ex) {ex.printStackTrace();}
// try { launchBrowser("G:\\Projects\\ERDC\\Data\\Hetereogenous_Objects\\Situation_Weather-XLS.xls"); } catch(Exception ex) {ex.printStackTrace();}
// try { launchBrowser("G:\\Projects\\ERDC\\Data\\Hetereogenous_Objects\\Software_External-Link.txt"); } catch(Exception ex) {ex.printStackTrace();}
// try { TestVlen("d:\\hdf-files\\test_vlen.h5"); } catch(Exception ex) {ex.printStackTrace();}
// try { TestPinning("G:\\Projects\\Rosetta\\debug\\test_pinning.h5"); } catch(Exception ex) {ex.printStackTrace();}
// try { createDataset("g:\\temp\\testDataset.h5"); } catch(Exception ex) {ex.printStackTrace();}
// try { TestVlen("d:\\hdf-files\\test_vlen.h5", FileFormat.WRITE); } catch(Exception ex) {ex.printStackTrace();}
// try { TestVlen("d:\\hdf-files\\test_vlen_org.h5", FileFormat.READ); } catch(Exception ex) {ex.printStackTrace();}
// try { TestVlen("g:\\temp\\test_vlen.h5", FileFormat.WRITE); } catch(Exception ex) {ex.printStackTrace();}
// TestBit64();
// TestBitmask();
// TestBinaryWrite(1, 1);
// TestBinaryWrite(9, 1);
// TestBinaryWrite(15, 1);
// TestBinaryWrite(127, 2);
// TestBinaryWrite(2147483647, 2);
// TestBinaryWrite(2147483647, 4);
// TestBinaryWrite(9123456789123456789L, 8);
//
// try { TestBug1523("G:\\Projects\\HUGS\\data\\testfile02.h5.corrupt"); } catch(Exception ex) {ex.printStackTrace();}
// try { TestBug1523("G:\\Projects\\HUGS\\data\\testfile02.h5"); } catch(Exception ex) {ex.printStackTrace();}
// try { createINF("G:\\temp\\inf.h5"); } catch(Exception ex) {ex.printStackTrace();}
// try { createNaN_INF("G:\\temp\\nan_inf.h5"); } catch(Exception ex) {ex.printStackTrace();}
// try { testStrings("G:\\temp\\strs.h5"); } catch(Exception ex) {ex.printStackTrace();}
// testVariableArity("null argument", null);
// testVariableArity("no argument");
// testVariableArity("1 argument", 1);
// testVariableArity("2 argument", 1,"string");
// testVariableArity("3 argument", 1,"string",2.59);
// try { readDatatype(); } catch(Exception ex) {ex.printStackTrace();}
// try { readTextFile("G:\\temp\\vlarsizes.txt"); } catch(Exception ex) {ex.printStackTrace();}
// try {processa8apis(); } catch (Exception ex) {}
// try {convertByte2Long(); } catch (Exception ex) {ex.printStackTrace();}
// try {testH5IO("G:\\temp\\test.h5"); } catch (Exception ex) {ex.printStackTrace();}
// try {testH5Core("G:\\temp\\test.h5"); } catch (Exception ex) {ex.printStackTrace();}
// try {test1Dstrings("G:\\temp\\test.h5"); } catch (Exception ex) {ex.printStackTrace();}
// try {testUpdateAttr("G:\\temp\\test.h5"); } catch (Exception ex) {ex.printStackTrace();}
// try {testCreateVlenStr("G:\\temp\\test.h5"); } catch (Exception ex) {ex.printStackTrace();}
// try {testH5TconvertStr(); } catch (Exception ex) {ex.printStackTrace();}
// try {testH5DeleteDS("g:\\temp\\strs.h5"); } catch (Exception ex) {ex.printStackTrace();}
// try {testExtendData("g:\\temp\\extended.h5", "dset", 1000, 1500); } catch (Exception ex) {ex.printStackTrace();}
// try {createNestedcompound("g:\\temp\\nested_cmp.h5", "dset"); } catch (Exception ex) {ex.printStackTrace();}
// try { testH5Vlen("G:\\temp\\str.h5") ; } catch (Exception ex) {ex.printStackTrace();}
// try { testH5VlenObj("G:\\temp\\str2.h5") ; } catch (Exception ex) {ex.printStackTrace();}
// try { testH5VlenAttr("G:\\temp\\vlen_str_attr.h5") ; } catch (Exception ex) {ex.printStackTrace();}
// try {testRefData("g:\\temp\\refs.h5", "refs"); } catch (Exception ex) {ex.printStackTrace();}
// try {testH5WriteDouble("g:\\temp\\double.h5"); } catch (Exception ex) {ex.printStackTrace();}
// try {testGroupMemoryLeak("G:\\temp\\mem_leak.h5"); } catch (Exception ex) {ex.printStackTrace();}
// try { testH5OflushCrash("G:\\temp\\H5Oflush_crash.h5"); } catch (Exception ex) {ex.printStackTrace();}
// testPrintData();
//try { testObjReadData("g:\\temp\\dset.h5", "dset"); } catch (Exception ex) {ex.printStackTrace();}
//try { testH5FileGet("g:\\temp\\dset.h5", "/dset/"); } catch (Exception ex) {ex.printStackTrace();}
// try {
// String fname = "g:\\temp\\dset.h5";
// new File(fname).delete(); // clean up existing file
//
// for (int i=0; i<10; i++)
// testCreateDS(fname, "dset"+i);
//
// } catch (Exception ex) {ex.printStackTrace();}
// try { testH5Write2D("g:\\temp\\dset.h5"); } catch (Exception ex) {ex.printStackTrace();}
// try { testHDF4("g:\\temp\\test_hdf4.hdf"); } catch (Exception ex) {ex.printStackTrace();}
//try { test3DHDF4("g:\\temp\\hdf3d.hdf", "3dint"); } catch (Exception ex) {ex.printStackTrace();}
try { testH5DataType("G:\\temp\\H5Datatype.h5"); } catch (Exception ex) {ex.printStackTrace();}
}
public static void testRefData(String fname, String dname)throws Exception
{
int size = 10;
long dims[] = {size};
long[] maxdims = {HDF5Constants.H5S_UNLIMITED};
byte[][] ref_buf = new byte[2][8];
float data[] = new float[size];
for (int i=0; i<size; i++)
data[i] = i;
H5File file = new H5File(fname, H5File.CREATE);
file.open();
// create a ref dataset
Group grp = file.createGroup("grp", null);
Dataset ds = file.createScalarDS("dset", grp, new H5Datatype(Datatype.CLASS_FLOAT, -1, -1, -1), dims, maxdims, null, 0, data);
ref_buf[0] = H5.H5Rcreate(file.getFID(), grp.getFullName(), HDF5Constants.H5R_OBJECT, -1);
ref_buf[1] = H5.H5Rcreate(file.getFID(), ds.getFullName(), HDF5Constants.H5R_OBJECT, -1);
ds = file.createScalarDS(dname, null, new H5Datatype(Datatype.CLASS_REFERENCE, -1, -1, -1), new long[] {2}, null, null, 0, ref_buf);
// create ref attributes
Datatype attr_dtype = file.createDatatype( Datatype.CLASS_REFERENCE, Datatype.NATIVE, Datatype.NATIVE, Datatype.NATIVE);
Attribute attr = new Attribute("ref", attr_dtype, new long[] {1});
attr.setValue(ds.getFullName());
file.writeAttribute(ds, attr, false);
attr = new Attribute("refs", attr_dtype, new long[] {2});
attr.setValue(ref_buf);
ds.writeMetadata(attr);
file.close();
// open the file and the dataset with refs
file.open();
ds = (H5ScalarDS)file.get(dname);
long[] refs = (long[])ds.getData();
// use low level API function, H5.H5Rget_name
String[] name = {""};;
for (int i=0; i<refs.length; i++) {
H5.H5Rget_name(file.getFID(), HDF5Constants.H5R_OBJECT, HDFNativeData.longToByte(refs[i]), name, 32);
System.out.println(name[0]);
}
// if file.open() was called, search objects in memory by high level function, findObject()
long[] oid = new long[1];
for (int i=0; i<refs.length; i++) {
oid[0] = refs[i];
HObject obj = FileFormat.findObject(file, oid);
System.out.println(obj.getFullName());
}
file.close();
}
private static void testH5VlenAttr( String fname) throws Exception
{
FileFormat fileFormat = FileFormat.getFileFormat(FileFormat.FILE_TYPE_HDF5);
if (fileFormat == null)
{
System.err.println("Cannot find HDF5 FileFormat.");
return;
}
H5File testFile = (H5File)fileFormat.create(fname);
if (testFile == null)
{
System.err.println("Failed to create file:"+fname);
return;
}
testFile.open();
Group root = (Group)((javax.swing.tree.DefaultMutableTreeNode)testFile.getRootNode()).getUserObject();
String[] data = {"abc de fghi jk lmn op qrst uvw xyz\n0 12 345 6 789"};
long[] data_dims = {1};
Datatype dtype = testFile.createDatatype(
Datatype.CLASS_STRING, data[0].length(),
Datatype.NATIVE, Datatype.NATIVE);
Dataset dataset = testFile.createScalarDS
("text", root, dtype, data_dims, null, null, 0, data);
long[] attr_dims = {3};
String[] attr_value = {"a", "ab", "abc"};
Datatype attr_dtype = testFile.createDatatype(
Datatype.CLASS_STRING, 5,
Datatype.NATIVE, Datatype.NATIVE);
Attribute attr = new Attribute("foo", attr_dtype, attr_dims);
//byte[] bvalue = Dataset.stringToByte(attr_value, 5);
attr.setValue(attr_value);
dataset.writeMetadata(attr);
testFile.close();
// read attributes back
testFile = (H5File)fileFormat.open(fname, FileFormat.READ);
if (testFile == null)
{
System.err.println("Failed to open file:"+fname);
return;
}
testFile.open();
root = (Group)((javax.swing.tree.DefaultMutableTreeNode)testFile.getRootNode()).getUserObject();
dataset = (Dataset)root.getMemberList().get(0);
List attrList = dataset.getMetadata();
attr = (Attribute)attrList.get(0);
String[] value = (String [])attr.getValue();
testFile.close();
}
/**
* Create a nested compound of {index, location{Lon, Lat}}
*
* @param fname name of the file
* @param dname name of the dataset
* @throws Exception
*/
private static void createNestedcompound(String fname, String dname) throws Exception
{
int DIM1 = 50;
long[] dims = {DIM1};
int cmpSize = 20;
int fid=-1, did=-1, tid = -1, tid_nested=-1, sid=-1;
int indexData[] = new int[DIM1];
double lonData[] = new double[DIM1];
double latData[] = new double[DIM1];
for (int i=0; i<DIM1; i++) {
indexData[i] = i;
lonData[i] = 5200.1+i;
latData[i] = 10.2+i;
}
fid = H5.H5Fcreate(fname, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
sid = H5.H5Screate_simple(1, dims, null);
tid_nested = H5.H5Tcreate(HDF5Constants.H5T_COMPOUND, 16);
H5.H5Tinsert(tid_nested, "Lon", 0, HDF5Constants.H5T_NATIVE_DOUBLE);
H5.H5Tinsert(tid_nested, "Lat", 8, HDF5Constants.H5T_NATIVE_DOUBLE);
tid = H5.H5Tcreate(HDF5Constants.H5T_COMPOUND, cmpSize);
H5.H5Tinsert(tid, "index", 0, HDF5Constants.H5T_NATIVE_INT32);
H5.H5Tinsert(tid, "location", 4, tid_nested);
did = H5.H5Dcreate(fid, dname, tid, sid, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
// write the first field "index"
int tid_tmp = H5.H5Tcreate(HDF5Constants.H5T_COMPOUND, 4);
H5.H5Tinsert(tid_tmp, "index", 0, HDF5Constants.H5T_NATIVE_INT32);
H5.H5Dwrite(did, tid_tmp, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, HDF5Constants.H5P_DEFAULT, indexData);
H5.H5Tclose(tid_tmp);
// write the first field of the nested compound, "location"->"Lon"
tid_tmp = H5.H5Tcreate(HDF5Constants.H5T_COMPOUND, 8);
H5.H5Tinsert(tid_tmp, "Lon", 0, HDF5Constants.H5T_NATIVE_DOUBLE);
int tid_tmp_nested = H5.H5Tcreate(HDF5Constants.H5T_COMPOUND, 8);
H5.H5Tinsert(tid_tmp_nested, "location", 0, tid_tmp);
H5.H5Dwrite(did, tid_tmp_nested, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, HDF5Constants.H5P_DEFAULT, lonData);
H5.H5Tclose(tid_tmp_nested);
H5.H5Tclose(tid_tmp);
// write the second field of the nested compound, "location"->"Lat"
tid_tmp = H5.H5Tcreate(HDF5Constants.H5T_COMPOUND, 8);
H5.H5Tinsert(tid_tmp, "Lat", 0, HDF5Constants.H5T_NATIVE_DOUBLE);
tid_tmp_nested = H5.H5Tcreate(HDF5Constants.H5T_COMPOUND, 8);
H5.H5Tinsert(tid_tmp_nested, "location", 0, tid_tmp);
H5.H5Dwrite(did, tid_tmp_nested, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, HDF5Constants.H5P_DEFAULT, latData);
H5.H5Tclose(tid_tmp_nested);
H5.H5Tclose(tid_tmp);
H5.H5Tclose(tid);
H5.H5Tclose(tid_nested);
H5.H5Sclose(sid);
H5.H5Dclose(did);
H5.H5Fclose(fid);
}
public static void testExtendData(String fname, String dname, int size, int newSize)throws Exception
{
long dims[] = {size};
long[] maxdims = {HDF5Constants.H5S_UNLIMITED};
long newDims[] = {newSize};
int extended = newSize - size;
if (extended<=0)
return; // nothing to extended
float data[] = new float[size];
for (int i=0; i<size; i++)
data[i] = i;
float extendedData[] = new float[extended];
for (int i=0; i<extended; i++)
extendedData[i] = size+i;
H5File file = new H5File(fname, H5File.CREATE);
file.open();
file.createScalarDS(dname, null, new H5Datatype(Datatype.CLASS_FLOAT, -1, -1, -1), dims, maxdims, null, 0, data);
file.close();
// reopen the file
file.open();
H5ScalarDS ds = (H5ScalarDS)file.get(dname);
ds.init();
ds.extend(newDims);
long [] start = ds.getStartDims();
long [] count = ds.getSelectedDims();
start[0] = size;
count[0] = extended;
ds.write(extendedData);
file.close();
}
private static final void testH5DeleteDS(String fname) throws Exception
{
String dname = "strs";
int nloops=1000, rank=1, strLen=1, fid=-1, tid=-1, sid=-1, did=-1;
String[] strData = { "1", "2", "3", "4" };
long[] dims = { strData.length };
byte[] byteData = new byte[strData.length*strLen];
for (int i=0; i<strData.length; i++)
System.arraycopy(strData[i].getBytes(), 0, byteData, i*strLen, strLen);
fid = H5.H5Fcreate(fname, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
tid = H5.H5Tcopy(HDF5Constants.H5T_C_S1);
H5.H5Tset_size(tid, 1);
sid = H5.H5Screate_simple(rank, dims, null);
did = H5.H5Dcreate(fid, dname, tid, sid, HDF5Constants.H5P_DEFAULT);
H5.H5Dwrite(did, tid, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, HDF5Constants.H5P_DEFAULT, byteData);
H5.H5Dclose(did);
H5.H5Fclose(fid);
for (int loop=0; loop<nloops; loop++) {
fid = H5.H5Fopen(fname, HDF5Constants.H5F_ACC_RDWR, HDF5Constants.H5P_DEFAULT);
H5.H5Ldelete( fid, dname, HDF5Constants.H5P_DEFAULT);
strLen = loop+1;
H5.H5Tset_size(tid, strLen);
for (int i=0; i<strData.length; i++) {
strData[i] = "";
for (int j=0; j<=loop; j++) {
strData[i] += (i+1);
}
}
byteData = new byte[strData.length*strLen];
for (int i=0; i<strData.length; i++) {
System.arraycopy(strData[i].getBytes(), 0, byteData, i*strLen, strLen);
}
did = H5.H5Dcreate(fid, dname, tid, sid, HDF5Constants.H5P_DEFAULT);
H5.H5Dwrite(did, tid, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, HDF5Constants.H5P_DEFAULT, byteData);
H5.H5Dclose(did);
H5.H5Fclose(fid);
}
H5.H5Sclose(sid);
H5.H5Tclose(tid);
}
private static final void testH5TconvertStr() throws Exception
{
String[] strs = {"a1234","b1234"};
int srcLen=5, dstLen=10, srcId=-1, dstId=-1, dimSize=strs.length;
byte[] buf = new byte[dimSize*dstLen];
for (int i=0; i<dimSize; i++)
System.arraycopy(strs[i].getBytes(), 0, buf, i*srcLen, 5);
for (int i=0; i<dimSize; i++)
System.out.println(new String(buf, i*srcLen, srcLen));
srcId = H5.H5Tcopy(HDF5Constants.H5T_C_S1);
H5.H5Tset_size(srcId, srcLen);
dstId = H5.H5Tcopy(HDF5Constants.H5T_C_S1);
H5.H5Tset_size(dstId, dstLen);
H5.H5Tconvert(srcId, dstId, dimSize, buf, null, HDF5Constants.H5P_DEFAULT);
H5.H5Tclose(srcId);
H5.H5Tclose(dstId);
for (int i=0; i<dimSize; i++) {
String str = new String(buf, i*dstLen, dstLen);
System.out.println(str);
System.out.println(str.startsWith(strs[i]));
}
}
private static final void testCreateVlenStr(String fname) throws Exception
{
String dname = "DS1";
int file_id=-1, type_id=-1, dataspace_id =-1, dataset_id = -1;
String[] strData = { "Parting", "is such", "sweet", "sorrow." };
int rank = 1;
long[] dims = { strData.length };
// Create a new file using default properties.
try {
file_id = H5.H5Fcreate(fname, HDF5Constants.H5F_ACC_TRUNC,
HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
}
catch (Exception e) {
e.printStackTrace();
}
try {
type_id = H5.H5Tcopy(HDF5Constants.H5T_C_S1);
H5.H5Tset_size(type_id, HDF5Constants.H5T_VARIABLE);
}
catch (Exception e) {
e.printStackTrace();
}
// Create dataspace. Setting maximum size to NULL sets the maximum
// size to be the current size.
try {
dataspace_id = H5.H5Screate_simple(rank, dims, null);
}
catch (Exception e) {
e.printStackTrace();
}
// Create the dataset and write the string data to it.
try {
if ((file_id >= 0) && (type_id >= 0) && (dataspace_id >= 0))
dataset_id = H5.H5Dcreate(file_id, dname, type_id,
dataspace_id, HDF5Constants.H5P_DEFAULT);
}
catch (Exception e) {
e.printStackTrace();
}
// Write the data to the dataset.
try {
if ((dataset_id >= 0) && (type_id >= 0))
H5.H5DwriteString(dataset_id, type_id, HDF5Constants.H5S_ALL,
HDF5Constants.H5S_ALL, HDF5Constants.H5P_DEFAULT, strData);
}
catch (Exception e) {
e.printStackTrace();
}
// Write the data to the dataset.
try {
if ((dataset_id >= 0) && (type_id >= 0)) {
String[] buf = new String[strData.length];
H5.H5Dread(dataset_id, H5.H5Dget_type(dataset_id), HDF5Constants.H5S_ALL,
HDF5Constants.H5S_ALL, HDF5Constants.H5P_DEFAULT, buf);
for (int i =0; i<strData.length; i++)
System.out.println(buf[i]);
}
}
catch (Exception e) {
e.printStackTrace();
}
// End access to the dataset and release resources used by it.
try {
if (dataset_id >= 0)
H5.H5Dclose(dataset_id);
}
catch (Exception e) {
e.printStackTrace();
}
// Terminate access to the data space.
try {
if (dataspace_id >= 0)
H5.H5Sclose(dataspace_id);
}
catch (Exception e) {
e.printStackTrace();
}
// Terminate access to the file type.
try {
if (type_id >= 0)
H5.H5Tclose(type_id);
}
catch (Exception e) {
e.printStackTrace();
}
// Close the file.
try {
if (file_id >= 0)
H5.H5Fclose(file_id);
}
catch (Exception e) {
e.printStackTrace();
}
}
private static void testUpdateAttr(String fname) throws Exception
{
int data[] = {1,2,3,4,5,6};
long[] dims = {data.length};
String dname = "/dset";
// create a new file and a new dataset
H5File file = new H5File(fname, FileFormat.CREATE);
Datatype dtype = file.createDatatype(Datatype.CLASS_INTEGER, 4, Datatype.NATIVE, Datatype.NATIVE);
Dataset dataset = file.createScalarDS (dname, null, dtype, dims, null, null, 0, data);
// create and write an attribute to the dataset
long[] attrDims = {2};
int[] attrValue = {0, 10000};
Attribute attr = new Attribute("range", dtype, attrDims);
attr.setValue(attrValue); // set the attribute value
dataset.writeMetadata(attr);
// close the file
file.close();
// open the file with read and write access
file = new H5File(fname, FileFormat.WRITE);
// retrieve the dataset and attribute
dataset = (Dataset)file.get(dname);
attr = (Attribute)dataset.getMetadata().get(0);
// change the attribute value
if (attr!=null) {
attrValue[0] = 100;
attr.setValue(attrValue);
dataset.writeMetadata(attr);
}
// close file resource
file.close();
}
public static void test1DExtendStrings(String fname) throws Exception {
//row count of my dataset
int rowCount = 5;
//max string length
int maxStringLength = 5;
//buffer of test data to write
String[] data = {"12345","12345","12345","12345","12345"};
byte[][] buffer = new byte[5][5];
for(int i=0; i<data.length; i++){
buffer[i] = data[i].getBytes();
}
//create my file
File file = new File("fname");
int fileId = H5.H5Fcreate(file.getAbsolutePath(),
HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT,
HDF5Constants.H5P_DEFAULT);
int RANK = 1;
long[] dataset_dims = { rowCount };
long[] max_dims = { HDF5Constants.H5S_UNLIMITED };
long[] chunk_dims = { rowCount };
int strtypeId = H5.H5Tcopy(HDF5Constants.H5T_C_S1);
H5.H5Tset_size(strtypeId, maxStringLength);
int memtypeId = H5.H5Tcopy(HDF5Constants.H5T_C_S1);
H5.H5Tset_size(memtypeId, maxStringLength);
int dataspaceId = H5.H5Screate_simple(RANK, dataset_dims, max_dims);
int memspaceId = H5.H5Screate_simple(RANK, chunk_dims, chunk_dims);
int dcplId = H5.H5Pcreate(HDF5Constants.H5P_DATASET_CREATE);
H5.H5Pset_deflate(dcplId, 9);
H5.H5Pset_chunk(dcplId, 1, chunk_dims);
int datasetId = H5.H5Dcreate(fileId, "/ds0",
strtypeId, dataspaceId, dcplId);
long[] hyperslab_dims = { rowCount};
memspaceId = H5.H5Screate_simple(1, hyperslab_dims, hyperslab_dims);
H5.H5Sselect_hyperslab(dataspaceId, HDF5Constants.H5S_SELECT_SET,
new long[] { 0 }, new long[] { 1 },
new long[] { rowCount }, new long[] { 1});
H5.H5Dwrite(datasetId, memtypeId, memspaceId,
dataspaceId, HDF5Constants.H5P_DEFAULT, buffer);
H5.H5Sclose(memspaceId);
H5.H5Sclose(dataspaceId);
H5.H5Tclose(strtypeId);
H5.H5Tclose(memtypeId);
H5.H5Dclose(datasetId);
H5.H5Fclose(fileId);
}
private static void testH5Core(final String filename) throws Exception {
int fapl_id = H5.H5Pcreate(HDF5Constants.H5P_FILE_ACCESS);
H5.H5Pset_fapl_core(fapl_id, 1024, true);
}
private static void testH5IO(final String filename) throws Exception {
int SIZE = 10*1024*1024;
int fid = -1, did = -1, sid = -1, rank = 1;
long[] dims = { SIZE };
float[] buf = new float[SIZE];
// for (int i = 0; i < buf.length; i++)
// buf[i] = i;
//
// try {
// fid = H5.H5Fcreate(filename, HDF5Constants.H5F_ACC_TRUNC,
// HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
// sid = H5.H5Screate_simple(rank, dims, null);
// did = H5.H5Dcreate(fid, "test", HDF5Constants.H5T_NATIVE_FLOAT,
// sid, HDF5Constants.H5P_DEFAULT);
// } finally {
// try {
// H5.H5Sclose(sid);
// } catch (HDF5Exception ex) {
// }
// try {
// H5.H5Dclose(did);
// } catch (HDF5Exception ex) {
// }
//
// try {
// H5.H5Fclose(fid);
// } catch (HDF5Exception ex) {
// }
// }
//
// try {
//
// fid = H5.H5Fopen(filename, HDF5Constants.H5F_ACC_RDWR,
// HDF5Constants.H5P_DEFAULT);
// did = H5.H5Dopen(fid, "test");
//
// long t0 = System.currentTimeMillis();
// H5.H5Dwrite_float(did, HDF5Constants.H5T_NATIVE_FLOAT,
// HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL,
// HDF5Constants.H5P_DEFAULT, buf);
// long t1 = System.currentTimeMillis();
// System.out.println("Time on writing (40MB): " + (t1 - t0));
//
// } finally {
// try {
// H5.H5Sclose(sid);
// } catch (HDF5Exception ex) {
// }
// try {
// H5.H5Dclose(did);
// } catch (HDF5Exception ex) {
// }
//
// try {
// H5.H5Fclose(fid);
// } catch (HDF5Exception ex) {
// }
// }
try {
fid = H5.H5Fopen(filename, HDF5Constants.H5F_ACC_RDWR,
HDF5Constants.H5P_DEFAULT);
did = H5.H5Dopen(fid, "test");
long t0 = System.currentTimeMillis();
H5.H5Dread_float(did, HDF5Constants.H5T_NATIVE_FLOAT,
HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL,
HDF5Constants.H5P_DEFAULT, buf);
System.out.println(buf[SIZE-1]);
long t1 = System.currentTimeMillis();
System.out.println("Time on reading (40MB): " + (t1 - t0));
} finally {
try {
H5.H5Sclose(sid);
} catch (HDF5Exception ex) {
}
try {
H5.H5Dclose(did);
} catch (HDF5Exception ex) {
}
try {
H5.H5Fclose(fid);
} catch (HDF5Exception ex) {
}
}
}
public static void convertByte2Long() throws Exception {
long[] la = {1000000000000000001L, 1000000000000000002L, 1000000000000000003L};
byte[] ba = HDFNativeData.longToByte(0, la.length, la);
if (ba.length != la.length*8) {
System.out.println ("Failed to convert from byte[] to long[]");
return ;
}
// // only need the two lines below to convert byte[] to long[]
// ByteBuffer bb = ByteBuffer.wrap(ba);
// long[] la2 = (bb.asLongBuffer()).array();
//
// if (la2.length != la.length) {
// System.out.println ("Failed to convert from long[] to byte[]");
// return;
// }
long[] la2 = HDFNativeData.byteToLong(ba);
for (int i=0; i<la.length; i++) {
if (la[i] != la2[i]) {
System.out.println ("Failed to convert from long[] to byte[]");
return;
}
}
System.out.println ("OK: convert from byte[] to long[].");
}
public static void processa8apis() throws Exception {
String[] newAPIs = new String[200];
String[] allAPIs = new String[500];
int nNew=0, nAll=0, idx=0, idx2=0;
BufferedReader in_all18 = new BufferedReader(new FileReader("G:\\Projects\\Java\\1.8Support\\hdf5_1_8.txt"));
BufferedReader in_new18 = new BufferedReader(new FileReader("G:\\Projects\\Java\\1.8Support\\hdf5_1_8_new.txt"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("G:\\Projects\\Java\\1.8Support\\hdf5_1_8_processed.txt")));
String line = in_new18.readLine();
while (line != null) {
line = line.trim();
if (line !=null && line.length()>0) {
idx = line.indexOf(' ');
idx2 = line.indexOf('(');
newAPIs[nNew++] = line.substring(idx+1, idx2+1);
}
line = in_new18.readLine();
}
idx = 0;
for (int i=0; i<nNew; i++) {
line = newAPIs[i];
if (line==null || line.length()<=0)
continue;
for (int j=i+1; j<nNew; j++) {
if (line.equals(newAPIs[j])) {
idx++;
newAPIs[j] = null;
}
}
}
System.out.println("No. of new APIs = "+(nNew - idx));
for (int i=0; i<nNew; i++)
System.out.println(newAPIs[i]);
line = in_all18.readLine();
while (line != null) {
line = line.trim();
if (line !=null && line.length()>0)
allAPIs[nAll++] = line;
line = in_all18.readLine();
}
// make sure no repeated elements
// for (int i=0; i<nNew; i++) {
// line = newAPIs[i];
// if (line==null || line.length()<=0)
// continue;
//
// for (int j=i+1; j<nNew; j++) {
// if (line.equals(newAPIs[j])) {
// System.out.println(i + " and "+ j + " REPEATED !!!!!");
// }
// }
// }
//
// for (int i=0; i<nAll; i++) {
// line = allAPIs[i];
// for (int j=i+1; j<nAll; j++) {
// if (line.endsWith(allAPIs[j])) {
// System.out.println(i + " and "+ j + " REPEATED !!!!!");
// }
// }
// }
int isNew, isFunc;
String apiName, newName;
for(int i=0; i<nAll; i++) {
idx = allAPIs[i].indexOf(' ');
apiName = allAPIs[i].substring(idx+1);
isNew = isFunc = 0;
for (int j=0; j<nNew; j++) {
if (newAPIs[j] == null || newAPIs[j].length()<=0)
continue;
if (apiName.startsWith(newAPIs[j]) ||
apiName.startsWith("*"+newAPIs[j])) {
isNew = 1;
break;
}
}
if (allAPIs[i].indexOf("func")>0 || allAPIs[i].indexOf("*op_data")>0)
isFunc = 1;
line = isFunc+ "\t"+ isNew+ "\t"+ allAPIs[i].substring(0, idx) + "\t" +apiName;
out.println(line);
}
in_new18.close();
in_all18.close();
out.close();
}
public static void readTextFile(String fname) throws Exception {
BufferedReader in = new BufferedReader(new FileReader(fname));
String line = in.readLine();
StringTokenizer st = new StringTokenizer(line, ",") ;
int n = st.countTokens();
PrintWriter out
= new PrintWriter(new BufferedWriter(new FileWriter(fname+".out")));
while(st.hasMoreTokens()) {
out.print(st.nextToken());
out.println();
}
in.close();
out.close();
}
public static void writeDatatype() throws Exception{
String fname = "g:\\temp\\t1.h5";
long[] dims0D = {1};
// retrieve an instance of H5File
FileFormat fileFormat = FileFormat
.getFileFormat(FileFormat.FILE_TYPE_HDF5);
if (fileFormat == null) {
System.err.println("Cannot find HDF5 FileFormat.");
return;
}
// create a new file with a given file name.
H5File testFile = (H5File) fileFormat.create(fname);
if (testFile == null) {
System.err.println("Failed to create file:" + fname);
return;
}
// open the file and retrieve the root group
testFile.open();
H5Group root = (H5Group)
((javax.swing.tree.DefaultMutableTreeNode) testFile
.getRootNode()).getUserObject();
/** add an Attribute */
Datatype attrType = testFile.createDatatype(
Datatype.CLASS_INTEGER, 4, Datatype.NATIVE,
Datatype.NATIVE);
Attribute attr = new Attribute("attribut int", attrType,dims0D);
int[] attrValue = {15}; // attribute value
attr.setValue(attrValue); // set the attribute value
root.writeMetadata(attr);