66using System . Text ;
77using System . Threading . Tasks ;
88
9+ using HDF . PInvoke ;
10+ using System . Runtime . InteropServices ;
11+
912namespace DataDownload
1013{
1114 class Program
@@ -24,5 +27,125 @@ static void Main(string[] args)
2427 Console . WriteLine ( l . Item1 + l . Item2 ) ;
2528 }
2629 }
30+
31+ [ StructLayout ( LayoutKind . Sequential ) ]
32+ struct ComType
33+ {
34+ [ MarshalAs ( UnmanagedType . ByValTStr , SizeConst = 16 ) ]
35+ public string Name ;
36+ public int x_pos ;
37+ public int y_pos ;
38+ public float Mass ;
39+ public double Temperature ;
40+ }
41+
42+ static void Main_Read ( string [ ] args )
43+ {
44+ int DATA_ARRAY_LENGTH = 5 ;
45+
46+ //var h5 = H5F.open(@"E:\HDF5\HDF5DotNet-src\examples\CSharpExample\CSharpExample1\table.h5", H5F.ACC_RDONLY);
47+
48+ //var h5 = H5F.open(@"D:\test.h5", H5F.ACC_RDONLY);
49+ //var h5 = H5F.open(@"E:\HDF5\Hdf5DotnetTools-master\ToolTest\bin\Debug\table.h5", H5F.ACC_RDONLY);
50+ var h5 = H5F . open ( @"E:\HDF5\test_gzip.h5" , H5F . ACC_RDONLY ) ;
51+
52+ var dataset = H5D . open ( h5 , "trans_detail/20160929" ) ;
53+ var spaceid = H5D . get_space ( dataset ) ;
54+ var npoints = H5S . get_simple_extent_npoints ( spaceid ) ;
55+ //var dims = H5S.get_simple_extent_dims(spaceid);
56+ int rank = H5S . get_simple_extent_ndims ( spaceid ) ;
57+
58+ // 是不是不能用自己的type
59+ var dtype = H5D . get_type ( dataset ) ;
60+ var dtcls = H5T . get_class ( dtype ) ;
61+ var size = H5T . get_size ( dtype ) ;
62+ var sz = Marshal . SizeOf ( typeof ( ComType ) ) ;
63+
64+ var dtype_n = H5T . get_nmembers ( dtype ) ;
65+
66+ for ( uint i = 0 ; i < dtype_n ; ++ i )
67+ {
68+ var x = H5T . get_member_name ( dtype , i ) ;
69+ var x4 = Marshal . PtrToStringAnsi ( x ) ;
70+ var y = H5T . get_member_type ( dtype , i ) ;
71+ var z = H5T . get_class ( y ) ;
72+ var x1 = H5T . get_member_offset ( dtype , i ) ;
73+ var x3 = H5T . get_size ( y ) ;
74+
75+ Console . WriteLine ( x4 ) ;
76+ Console . WriteLine ( z ) ;
77+ Console . WriteLine ( x1 ) ;
78+ //var x2 = Marshal.OffsetOf(typeof(ComType), x4).ToInt32();
79+ //Console.WriteLine(x2);
80+ Console . WriteLine ( x3 ) ;
81+ }
82+
83+ int ss1 = Marshal . SizeOf ( typeof ( ComType ) ) ;
84+ IntPtr p = Marshal . AllocHGlobal ( ss1 * 11 ) ;
85+ H5D . read ( dataset , dtype , H5S . ALL , H5S . ALL , H5P . DEFAULT , p ) ;
86+ var s = Marshal . PtrToStructure ( p , typeof ( ComType ) ) ;
87+ Console . WriteLine ( s ) ;
88+ var s2 = Marshal . PtrToStructure ( p + ss1 , typeof ( ComType ) ) ;
89+ Console . WriteLine ( s2 ) ;
90+ var s3 = Marshal . PtrToStructure ( p + ss1 * 4 , typeof ( ComType ) ) ;
91+ Console . WriteLine ( s3 ) ;
92+ var s4 = Marshal . PtrToStructure ( p + ss1 * 5 , typeof ( ComType ) ) ;
93+ Console . WriteLine ( s4 ) ;
94+ var s6 = Marshal . PtrToStructure ( p + ss1 * 10 , typeof ( ComType ) ) ;
95+ Console . WriteLine ( s6 ) ;
96+ }
97+
98+ static void Main2222 ( string [ ] args )
99+ {
100+ var h5 = H5F . create ( @"D:\test.h5" , H5F . ACC_TRUNC ) ;
101+
102+ var typeId = H5T . create ( H5T . class_t . COMPOUND , new IntPtr ( 40 ) ) ;
103+
104+ var strtype = H5T . copy ( H5T . C_S1 ) ;
105+ H5T . set_size ( strtype , new IntPtr ( 16 ) ) ;
106+
107+ H5T . insert ( typeId , "Name" , new IntPtr ( 0 ) , strtype ) ;
108+ H5T . insert ( typeId , "x_pos" , new IntPtr ( 16 ) , H5T . NATIVE_INT32 ) ;
109+ H5T . insert ( typeId , "y_pos" , new IntPtr ( 20 ) , H5T . NATIVE_INT32 ) ;
110+ H5T . insert ( typeId , "Mass" , new IntPtr ( 24 ) , H5T . NATIVE_FLOAT ) ;
111+ H5T . insert ( typeId , "Temperature" , new IntPtr ( 32 ) , H5T . NATIVE_DOUBLE ) ;
112+
113+ ulong [ ] dims = new ulong [ ] { 10000 } ;
114+ ulong [ ] chunk_size = new ulong [ ] { 1000 } ;
115+
116+ var spaceid = H5S . create_simple ( dims . Length , dims , null ) ;
117+
118+
119+ var dcpl = H5P . create ( H5P . DATASET_CREATE ) ;
120+
121+ H5P . set_layout ( dcpl , H5D . layout_t . COMPACT ) ;
122+ H5P . set_deflate ( dcpl , 6 ) ;
123+
124+ H5P . set_chunk ( dcpl , chunk_size . Length , chunk_size ) ;
125+
126+
127+
128+
129+
130+ var datasetid = H5D . create ( h5 , "Table1" , typeId , spaceid , H5P . DEFAULT , dcpl ) ;
131+
132+ ComType ct = new ComType ( )
133+ {
134+ Name = "aabb" ,
135+ x_pos = 2 ,
136+ y_pos = 1 ,
137+ Mass = 1.24F ,
138+ Temperature = 45.7 ,
139+ } ;
140+
141+ IntPtr p = Marshal . AllocHGlobal ( 40 * ( int ) dims [ 0 ] ) ;
142+ Marshal . StructureToPtr ( ct , p , false ) ;
143+
144+
145+
146+ H5D . write ( datasetid , typeId , spaceid , H5S . ALL , H5P . DEFAULT , p ) ;
147+
148+ H5F . close ( h5 ) ;
149+ }
27150 }
28151}
0 commit comments