1
- import { assertDefined } from '@h5web/shared/guards' ;
1
+ import { assertDefined , isNumericType } from '@h5web/shared/guards' ;
2
2
import type {
3
3
Attribute ,
4
4
ChildEntity ,
5
5
DType ,
6
6
Group ,
7
- NumericType ,
8
7
ProvidedEntity ,
9
8
Shape ,
10
9
} from '@h5web/shared/hdf5-models' ;
11
- import { Endianness , EntityKind } from '@h5web/shared/hdf5-models' ;
10
+ import { Endianness , EntityKind , H5TClass } from '@h5web/shared/hdf5-models' ;
12
11
import {
13
12
arrayType ,
14
- boolType ,
13
+ bitfieldType ,
15
14
buildEntityPath ,
16
- compoundType ,
17
- cplxType ,
18
- enumType ,
15
+ compoundOrCplxType ,
16
+ enumOrBoolType ,
19
17
floatType ,
20
- intType ,
21
- isBoolEnumType ,
18
+ intOrUintType ,
19
+ opaqueType ,
20
+ referenceType ,
22
21
strType ,
23
- uintType ,
22
+ timeType ,
23
+ toCharSet ,
24
24
unknownType ,
25
25
} from '@h5web/shared/hdf5-utils' ;
26
26
import type { Metadata } from 'h5wasm' ;
@@ -32,17 +32,7 @@ import {
32
32
Group as H5WasmGroup ,
33
33
} from 'h5wasm' ;
34
34
35
- import {
36
- assertNumericMetadata ,
37
- isArrayMetadata ,
38
- isCompoundMetadata ,
39
- isEnumMetadata ,
40
- isFloatMetadata ,
41
- isIntegerMetadata ,
42
- isNumericMetadata ,
43
- isStringMetadata ,
44
- } from './guards' ;
45
- import type { H5WasmAttributes , H5WasmEntity , NumericMetadata } from './models' ;
35
+ import type { H5WasmAttributes , H5WasmEntity } from './models' ;
46
36
47
37
// https://github.com/h5wasm/h5wasm-plugins#included-plugins
48
38
// https://support.hdfgroup.org/services/contributions.html
@@ -162,83 +152,86 @@ function parseAttributes(h5wAttrs: H5WasmAttributes): Attribute[] {
162
152
} ) ;
163
153
}
164
154
165
- export function parseDTypeFromNumericMetadata (
166
- metadata : NumericMetadata ,
167
- ) : NumericType {
168
- const { signed, size : length , littleEndian } = metadata ;
169
- const size = length * 8 ;
170
- const endianness = littleEndian ? Endianness . LE : Endianness . BE ;
155
+ export function parseDType ( metadata : Metadata ) : DType {
156
+ const { type : h5tClass , size } = metadata ;
171
157
172
- if ( isIntegerMetadata ( metadata ) ) {
173
- return signed ? intType ( size , endianness ) : uintType ( size , endianness ) ;
158
+ if ( h5tClass === H5TClass . Integer ) {
159
+ const { signed, littleEndian } = metadata ;
160
+ return intOrUintType ( signed , size * 8 , toEndianness ( littleEndian ) ) ;
174
161
}
175
-
176
- if ( isFloatMetadata ( metadata ) ) {
177
- return floatType ( size , endianness ) ;
162
+ if ( h5tClass === H5TClass . Float ) {
163
+ const { littleEndian } = metadata ;
164
+ return floatType ( size * 8 , toEndianness ( littleEndian ) ) ;
178
165
}
179
166
180
- throw new Error ( 'Expected numeric metadata' ) ;
181
- }
182
-
183
- export function parseDType ( metadata : Metadata ) : DType {
184
- if ( isNumericMetadata ( metadata ) ) {
185
- return parseDTypeFromNumericMetadata ( metadata ) ;
167
+ if ( h5tClass === H5TClass . Time ) {
168
+ return timeType ( ) ;
186
169
}
187
170
188
- if ( isStringMetadata ( metadata ) ) {
189
- const { size, cset, vlen } = metadata ;
190
-
191
- return strType (
192
- cset === 1 ? 'UTF-8' : 'ASCII' ,
193
- // For variable-length string datatypes, the returned value is the size of the pointer to the actual string and
194
- // not the size of actual variable-length string data (https://portal.hdfgroup.org/display/HDF5/H5T_GET_SIZE)
195
- vlen ? undefined : size ,
196
- ) ;
171
+ if ( h5tClass === H5TClass . String ) {
172
+ const { cset, vlen } = metadata ;
173
+ return strType ( toCharSet ( cset ) , vlen ? undefined : size ) ;
197
174
}
198
175
199
- if ( isArrayMetadata ( metadata ) ) {
200
- const { array_type } = metadata ;
201
- assertDefined ( array_type ) ;
176
+ if ( h5tClass === H5TClass . Bitfield ) {
177
+ return bitfieldType ( ) ;
178
+ }
202
179
203
- return arrayType ( parseDType ( array_type ) , array_type . shape ) ;
180
+ if ( h5tClass === H5TClass . Opaque ) {
181
+ return opaqueType ( ) ;
204
182
}
205
183
206
- if ( isCompoundMetadata ( metadata ) ) {
184
+ if ( h5tClass === H5TClass . Compound ) {
207
185
const { compound_type } = metadata ;
208
- const { members, nmembers } = compound_type ;
209
-
210
- if ( nmembers === 2 && members [ 0 ] . name === 'r' && members [ 1 ] . name === 'i' ) {
211
- const [ realTypeMetadata , imagTypeMetadata ] = members ;
212
- assertNumericMetadata ( realTypeMetadata ) ;
213
- assertNumericMetadata ( imagTypeMetadata ) ;
186
+ assertDefined ( compound_type ) ;
214
187
215
- return cplxType (
216
- parseDTypeFromNumericMetadata ( realTypeMetadata ) ,
217
- parseDTypeFromNumericMetadata ( imagTypeMetadata ) ,
218
- ) ;
219
- }
220
-
221
- return compoundType (
188
+ return compoundOrCplxType (
222
189
Object . fromEntries (
223
- members . map ( ( member ) => [ member . name , parseDType ( member ) ] ) ,
190
+ compound_type . members . map ( ( member ) => [
191
+ member . name ,
192
+ parseDType ( member ) ,
193
+ ] ) ,
224
194
) ,
225
195
) ;
226
196
}
227
197
228
- if ( isEnumMetadata ( metadata ) ) {
198
+ if ( h5tClass === H5TClass . Reference ) {
199
+ return referenceType ( ) ;
200
+ }
201
+
202
+ if ( h5tClass === H5TClass . Enum ) {
229
203
const { enum_type } = metadata ;
230
- const { members : mapping , type : baseType } = enum_type ;
204
+ assertDefined ( enum_type ) ;
205
+ const { members, type } = enum_type ;
231
206
232
- const baseMetadata = { ...metadata , type : baseType } ;
233
- assertNumericMetadata ( baseMetadata ) ;
207
+ const baseType = parseDType ( { ...metadata , type } ) ;
208
+ if ( ! isNumericType ( baseType ) ) {
209
+ throw new Error ( 'Expected enum type to have numeric base type' ) ;
210
+ }
234
211
235
- const type = enumType ( parseDTypeFromNumericMetadata ( baseMetadata ) , mapping ) ;
236
- return isBoolEnumType ( type ) ? boolType ( ) : type ; // booleans stored as enums by h5py
212
+ return enumOrBoolType ( baseType , members ) ;
213
+ }
214
+
215
+ if ( h5tClass === H5TClass . Vlen ) {
216
+ // Not currently provided, so unable to know base type
217
+ // const { array_type } = metadata;
218
+ // assertDefined(array_type);
219
+ return arrayType ( unknownType ( ) ) ;
220
+ }
221
+
222
+ if ( h5tClass === H5TClass . Array ) {
223
+ const { array_type } = metadata ;
224
+ assertDefined ( array_type ) ;
225
+ return arrayType ( parseDType ( array_type ) , array_type . shape ) ;
237
226
}
238
227
239
228
return unknownType ( ) ;
240
229
}
241
230
231
+ function toEndianness ( littleEndian : boolean ) : Endianness {
232
+ return littleEndian ? Endianness . LE : Endianness . BE ;
233
+ }
234
+
242
235
export function convertSelectionToRanges (
243
236
dataset : H5WasmDataset ,
244
237
selection : string ,
0 commit comments