/
h5t.pyx
1997 lines (1503 loc) · 54.4 KB
/
h5t.pyx
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
# This file is part of h5py, a Python interface to the HDF5 library.
#
# http://www.h5py.org
#
# Copyright 2008-2013 Andrew Collette and contributors
#
# License: Standard 3-clause BSD; see "license.txt" for full license terms
# and contributor agreement.
"""
HDF5 "H5T" data-type API
This module contains the datatype identifier class TypeID, and its
subclasses which represent things like integer/float/compound identifiers.
The majority of the H5T API is presented as methods on these identifiers.
"""
# Pyrex compile-time imports
include "config.pxi"
from _objects cimport pdefault
from numpy cimport dtype, ndarray
from h5r cimport Reference, RegionReference
from utils cimport emalloc, efree, \
require_tuple, convert_dims, convert_tuple
# Runtime imports
import codecs
from collections import namedtuple
import sys
import operator
from warnings import warn
from h5 import get_config
import numpy as np
from ._objects import phil, with_phil
from .h5py_warnings import H5pyDeprecationWarning
import platform
try:
from collections.abc import Mapping
except ImportError:
from collections import Mapping
cfg = get_config()
PY3 = sys.version_info[0] == 3
MACHINE = platform.machine()
# === Custom C API ============================================================
cpdef TypeID typewrap(hid_t id_):
cdef H5T_class_t cls
cls = H5Tget_class(id_)
if cls == H5T_INTEGER:
pcls = TypeIntegerID
elif cls == H5T_FLOAT:
pcls = TypeFloatID
elif cls == H5T_TIME:
pcls = TypeTimeID
elif cls == H5T_STRING:
pcls = TypeStringID
elif cls == H5T_BITFIELD:
pcls = TypeBitfieldID
elif cls == H5T_OPAQUE:
pcls = TypeOpaqueID
elif cls == H5T_COMPOUND:
pcls = TypeCompoundID
elif cls == H5T_REFERENCE:
pcls = TypeReferenceID
elif cls == H5T_ENUM:
pcls = TypeEnumID
elif cls == H5T_VLEN:
pcls = TypeVlenID
elif cls == H5T_ARRAY:
pcls = TypeArrayID
else:
pcls = TypeID
return pcls(id_)
cdef object lockid(hid_t id_in):
cdef TypeID tid
tid = typewrap(id_in)
tid.locked = 1
return tid
# === Public constants and data structures ====================================
# Enumeration H5T_class_t
NO_CLASS = H5T_NO_CLASS
INTEGER = H5T_INTEGER
FLOAT = H5T_FLOAT
TIME = H5T_TIME
STRING = H5T_STRING
BITFIELD = H5T_BITFIELD
OPAQUE = H5T_OPAQUE
COMPOUND = H5T_COMPOUND
REFERENCE = H5T_REFERENCE
ENUM = H5T_ENUM
VLEN = H5T_VLEN
ARRAY = H5T_ARRAY
# Enumeration H5T_sign_t
SGN_NONE = H5T_SGN_NONE
SGN_2 = H5T_SGN_2
# Enumeration H5T_order_t
ORDER_LE = H5T_ORDER_LE
ORDER_BE = H5T_ORDER_BE
ORDER_VAX = H5T_ORDER_VAX
ORDER_NONE = H5T_ORDER_NONE
DIR_DEFAULT = H5T_DIR_DEFAULT
DIR_ASCEND = H5T_DIR_ASCEND
DIR_DESCEND = H5T_DIR_DESCEND
# Enumeration H5T_str_t
STR_NULLTERM = H5T_STR_NULLTERM
STR_NULLPAD = H5T_STR_NULLPAD
STR_SPACEPAD = H5T_STR_SPACEPAD
# Enumeration H5T_norm_t
NORM_IMPLIED = H5T_NORM_IMPLIED
NORM_MSBSET = H5T_NORM_MSBSET
NORM_NONE = H5T_NORM_NONE
# Enumeration H5T_cset_t:
CSET_ASCII = H5T_CSET_ASCII
# Enumeration H5T_pad_t:
PAD_ZERO = H5T_PAD_ZERO
PAD_ONE = H5T_PAD_ONE
PAD_BACKGROUND = H5T_PAD_BACKGROUND
if sys.byteorder == "little": # Custom python addition
ORDER_NATIVE = H5T_ORDER_LE
else:
ORDER_NATIVE = H5T_ORDER_BE
# For conversion
BKG_NO = H5T_BKG_NO
BKG_TEMP = H5T_BKG_TEMP
BKG_YES = H5T_BKG_YES
# --- Built-in HDF5 datatypes -------------------------------------------------
# IEEE floating-point
IEEE_F32LE = lockid(H5T_IEEE_F32LE)
IEEE_F32BE = lockid(H5T_IEEE_F32BE)
IEEE_F64LE = lockid(H5T_IEEE_F64LE)
IEEE_F64BE = lockid(H5T_IEEE_F64BE)
# Signed 2's complement integer types
STD_I8LE = lockid(H5T_STD_I8LE)
STD_I16LE = lockid(H5T_STD_I16LE)
STD_I32LE = lockid(H5T_STD_I32LE)
STD_I64LE = lockid(H5T_STD_I64LE)
STD_I8BE = lockid(H5T_STD_I8BE)
STD_I16BE = lockid(H5T_STD_I16BE)
STD_I32BE = lockid(H5T_STD_I32BE)
STD_I64BE = lockid(H5T_STD_I64BE)
# Unsigned integers
STD_U8LE = lockid(H5T_STD_U8LE)
STD_U16LE = lockid(H5T_STD_U16LE)
STD_U32LE = lockid(H5T_STD_U32LE)
STD_U64LE = lockid(H5T_STD_U64LE)
STD_U8BE = lockid(H5T_STD_U8BE)
STD_U16BE = lockid(H5T_STD_U16BE)
STD_U32BE = lockid(H5T_STD_U32BE)
STD_U64BE = lockid(H5T_STD_U64BE)
# Native types by bytesize
NATIVE_INT8 = lockid(H5T_NATIVE_INT8)
NATIVE_UINT8 = lockid(H5T_NATIVE_UINT8)
NATIVE_INT16 = lockid(H5T_NATIVE_INT16)
NATIVE_UINT16 = lockid(H5T_NATIVE_UINT16)
NATIVE_INT32 = lockid(H5T_NATIVE_INT32)
NATIVE_UINT32 = lockid(H5T_NATIVE_UINT32)
NATIVE_INT64 = lockid(H5T_NATIVE_INT64)
NATIVE_UINT64 = lockid(H5T_NATIVE_UINT64)
NATIVE_FLOAT = lockid(H5T_NATIVE_FLOAT)
NATIVE_DOUBLE = lockid(H5T_NATIVE_DOUBLE)
NATIVE_LDOUBLE = lockid(H5T_NATIVE_LDOUBLE)
# Unix time types
UNIX_D32LE = lockid(H5T_UNIX_D32LE)
UNIX_D64LE = lockid(H5T_UNIX_D64LE)
UNIX_D32BE = lockid(H5T_UNIX_D32BE)
UNIX_D64BE = lockid(H5T_UNIX_D64BE)
# Reference types
STD_REF_OBJ = lockid(H5T_STD_REF_OBJ)
STD_REF_DSETREG = lockid(H5T_STD_REF_DSETREG)
# Null terminated (C) and Fortran string types
C_S1 = lockid(H5T_C_S1)
FORTRAN_S1 = lockid(H5T_FORTRAN_S1)
VARIABLE = H5T_VARIABLE
# Character sets
CSET_ASCII = H5T_CSET_ASCII
CSET_UTF8 = H5T_CSET_UTF8
# Mini (or short) floats
IEEE_F16BE = IEEE_F32BE.copy()
IEEE_F16BE.set_fields(15, 10, 5, 0, 10)
IEEE_F16BE.set_size(2)
IEEE_F16BE.set_ebias(15)
IEEE_F16BE.lock()
IEEE_F16LE = IEEE_F16BE.copy()
IEEE_F16LE.set_order(H5T_ORDER_LE)
IEEE_F16LE.lock()
# Quad floats
IEEE_F128BE = IEEE_F64BE.copy()
IEEE_F128BE.set_size(16)
IEEE_F128BE.set_precision(128)
IEEE_F128BE.set_fields(127, 112, 15, 0, 112)
IEEE_F128BE.set_ebias(16383)
IEEE_F128BE.lock()
IEEE_F128LE = IEEE_F128BE.copy()
IEEE_F128LE.set_order(H5T_ORDER_LE)
IEEE_F128LE.lock()
LDOUBLE_LE = NATIVE_LDOUBLE.copy()
LDOUBLE_LE.set_order(H5T_ORDER_LE)
LDOUBLE_LE.lock()
LDOUBLE_BE = NATIVE_LDOUBLE.copy()
LDOUBLE_BE.set_order(H5T_ORDER_BE)
LDOUBLE_BE.lock()
# Custom Python object pointer type
cdef hid_t H5PY_OBJ = H5Tcreate(H5T_OPAQUE, sizeof(PyObject*))
H5Tset_tag(H5PY_OBJ, "PYTHON:OBJECT")
H5Tlock(H5PY_OBJ)
PYTHON_OBJECT = lockid(H5PY_OBJ)
# Translation tables for HDF5 -> NumPy dtype conversion
cdef dict _order_map = { H5T_ORDER_NONE: '|', H5T_ORDER_LE: '<', H5T_ORDER_BE: '>'}
cdef dict _sign_map = { H5T_SGN_NONE: 'u', H5T_SGN_2: 'i' }
# Available floating point types
cdef tuple _get_available_ftypes():
cdef str floating_typecodes = np.typecodes["Float"]
cdef str ftc
cdef dtype fdtype
cdef list available_ftypes = []
for ftc in floating_typecodes:
fdtype = dtype(ftc)
available_ftypes.append((
<object>(fdtype.typeobj), np.finfo(fdtype), fdtype.itemsize
))
return tuple(available_ftypes)
cdef tuple _available_ftypes = _get_available_ftypes()
# Old code to inform about floating point changes
class _DeprecatedMapping(Mapping):
"""
Mapping class which warns when members are accessed
"""
def __init__(self, mapping, message):
self._mapping = mapping
self._message = message
def __len__(self):
warn(self._message, H5pyDeprecationWarning)
return len(self._mapping)
def __iter__(self):
warn(self._message, H5pyDeprecationWarning)
return iter(self._mapping)
def __getitem__(self, key):
warn(self._message, H5pyDeprecationWarning)
return self._mapping[key]
available_ftypes = dict()
for ftype in np.typeDict.values():
if np.issubdtype(ftype, np.floating):
available_ftypes[np.dtype(ftype).itemsize] = np.finfo(ftype)
available_ftypes = _DeprecatedMapping(available_ftypes,
("Do not use available_ftypes, this is not part of the public API of "
"h5py. See https://github.com/h5py/h5py/pull/926 for details.")
)
cdef (int, int, int) _correct_float_info(ftype_, finfo):
nmant = finfo.nmant
maxexp = finfo.maxexp
minexp = finfo.minexp
# workaround for numpy's buggy finfo on float128 on ppc64 archs
if ftype_ == np.longdouble and MACHINE == 'ppc64':
# values reported by hdf5
nmant = 116
maxexp = 1024
minexp = -1022
elif ftype_ == np.longdouble and MACHINE == 'ppc64le':
# values reported by hdf5
nmant = 52
maxexp = 1024
minexp = -1022
elif nmant == 63 and finfo.nexp == 15:
# This is an 80-bit float, correct mantissa size
nmant += 1
return nmant, maxexp, minexp
# === General datatype operations =============================================
@with_phil
def create(int classtype, size_t size):
"""(INT classtype, UINT size) => TypeID
Create a new HDF5 type object. Legal class values are
COMPOUND and OPAQUE. Use enum_create for enums.
"""
# HDF5 versions 1.6.X segfault with anything else
if classtype != H5T_COMPOUND and classtype != H5T_OPAQUE:
raise ValueError("Class must be COMPOUND or OPAQUE.")
return typewrap(H5Tcreate(<H5T_class_t>classtype, size))
@with_phil
def open(ObjectID group not None, char* name):
"""(ObjectID group, STRING name) => TypeID
Open a named datatype from a file.
"""
return typewrap(H5Topen(group.id, name))
@with_phil
def array_create(TypeID base not None, object dims_tpl):
"""(TypeID base, TUPLE dimensions) => TypeArrayID
Create a new array datatype, using and HDF5 parent type and
dimensions given via a tuple of positive integers. "Unlimited"
dimensions are not allowed.
"""
cdef hsize_t rank
cdef hsize_t *dims = NULL
require_tuple(dims_tpl, 0, -1, "dims_tpl")
rank = len(dims_tpl)
dims = <hsize_t*>emalloc(sizeof(hsize_t)*rank)
try:
convert_tuple(dims_tpl, dims, rank)
return TypeArrayID(H5Tarray_create(base.id, rank, dims, NULL))
finally:
efree(dims)
@with_phil
def enum_create(TypeID base not None):
"""(TypeID base) => TypeID
Create a new enumerated type based on an (integer) parent type.
"""
return typewrap(H5Tenum_create(base.id))
@with_phil
def vlen_create(TypeID base not None):
"""(TypeID base) => TypeID
Create a new variable-length datatype, using any HDF5 type as a base.
Although the Python interface can manipulate these types, there is no
provision for reading/writing vlen data.
"""
return typewrap(H5Tvlen_create(base.id))
@with_phil
def decode(char* buf):
"""(STRING buf) => TypeID
Unserialize an HDF5 type. You can also do this with the native
Python pickling machinery.
"""
return typewrap(H5Tdecode(<unsigned char*>buf))
# === Base type class =========================================================
cdef class TypeID(ObjectID):
"""
Base class for type identifiers (implements common operations)
* Hashable: If committed; in HDF5 1.8.X, also if locked
* Equality: Logical H5T comparison
"""
def __hash__(self):
with phil:
if self._hash is None:
try:
# Try to use object header first
return ObjectID.__hash__(self)
except TypeError:
# It's a transient type object
if self.locked:
self._hash = hash(self.encode())
else:
raise TypeError("Only locked or committed types can be hashed")
return self._hash
def __richcmp__(self, object other, int how):
cdef bint truthval = 0
with phil:
if how != 2 and how != 3:
return NotImplemented
if isinstance(other, TypeID):
truthval = self.equal(other)
if how == 2:
return truthval
return not truthval
def __copy__(self):
cdef TypeID cpy
with phil:
cpy = ObjectID.__copy__(self)
return cpy
property dtype:
""" A Numpy-style dtype object representing this object.
"""
def __get__(self):
with phil:
return self.py_dtype()
cdef object py_dtype(self):
raise TypeError("No NumPy equivalent for %s exists" % self.__class__.__name__)
@with_phil
def commit(self, ObjectID group not None, char* name, ObjectID lcpl=None):
"""(ObjectID group, STRING name, PropID lcpl=None)
Commit this (transient) datatype to a named datatype in a file.
If present, lcpl may be a link creation property list.
"""
H5Tcommit2(group.id, name, self.id, pdefault(lcpl),
H5P_DEFAULT, H5P_DEFAULT)
@with_phil
def committed(self):
"""() => BOOL is_comitted
Determine if a given type object is named (T) or transient (F).
"""
return <bint>(H5Tcommitted(self.id))
@with_phil
def copy(self):
"""() => TypeID
Create a copy of this type object.
"""
return typewrap(H5Tcopy(self.id))
@with_phil
def equal(self, TypeID typeid):
"""(TypeID typeid) => BOOL
Logical comparison between datatypes. Also called by
Python's "==" operator.
"""
return <bint>(H5Tequal(self.id, typeid.id))
@with_phil
def lock(self):
"""()
Lock this datatype, which makes it immutable and indestructible.
Once locked, it can't be unlocked.
"""
H5Tlock(self.id)
self.locked = 1
@with_phil
def get_class(self):
"""() => INT classcode
Determine the datatype's class code.
"""
return <int>H5Tget_class(self.id)
@with_phil
def set_size(self, size_t size):
"""(UINT size)
Set the total size of the datatype, in bytes.
"""
H5Tset_size(self.id, size)
@with_phil
def get_size(self):
""" () => INT size
Determine the total size of a datatype, in bytes.
"""
return H5Tget_size(self.id)
@with_phil
def get_super(self):
"""() => TypeID
Determine the parent type of an array, enumeration or vlen datatype.
"""
return typewrap(H5Tget_super(self.id))
@with_phil
def detect_class(self, int classtype):
"""(INT classtype) => BOOL class_is_present
Determine if a member of the given class exists in a compound
datatype. The search is recursive.
"""
return <bint>(H5Tdetect_class(self.id, <H5T_class_t>classtype))
@with_phil
def encode(self):
"""() => STRING
Serialize an HDF5 type. Bear in mind you can also use the
native Python pickle/unpickle machinery to do this. The
returned string may contain binary values, including NULLs.
"""
cdef size_t nalloc = 0
cdef char* buf = NULL
H5Tencode(self.id, NULL, &nalloc)
buf = <char*>emalloc(sizeof(char)*nalloc)
try:
H5Tencode(self.id, <unsigned char*>buf, &nalloc)
pystr = PyBytes_FromStringAndSize(buf, nalloc)
finally:
efree(buf)
return pystr
def __reduce__(self):
with phil:
return (type(self), (-1,), self.encode())
def __setstate__(self, char* state):
with phil:
self.id = H5Tdecode(<unsigned char*>state)
# === Top-level classes (inherit directly from TypeID) ========================
cdef class TypeArrayID(TypeID):
"""
Represents an array datatype
"""
@with_phil
def get_array_ndims(self):
"""() => INT rank
Get the rank of the given array datatype.
"""
return H5Tget_array_ndims(self.id)
@with_phil
def get_array_dims(self):
"""() => TUPLE dimensions
Get the dimensions of the given array datatype as
a tuple of integers.
"""
cdef hsize_t rank
cdef hsize_t* dims = NULL
rank = H5Tget_array_dims(self.id, NULL, NULL)
dims = <hsize_t*>emalloc(sizeof(hsize_t)*rank)
try:
H5Tget_array_dims(self.id, dims, NULL)
return convert_dims(dims, rank)
finally:
efree(dims)
cdef object py_dtype(self):
# Numpy translation function for array types
cdef TypeID tmp_type
tmp_type = self.get_super()
base_dtype = tmp_type.py_dtype()
shape = self.get_array_dims()
return dtype( (base_dtype, shape) )
cdef class TypeOpaqueID(TypeID):
"""
Represents an opaque type
"""
@with_phil
def set_tag(self, char* tag):
"""(STRING tag)
Set a string describing the contents of an opaque datatype.
Limited to 256 characters.
"""
H5Tset_tag(self.id, tag)
@with_phil
def get_tag(self):
"""() => STRING tag
Get the tag associated with an opaque datatype.
"""
cdef char* buf = NULL
try:
buf = H5Tget_tag(self.id)
assert buf != NULL
tag = buf
return tag
finally:
free(buf)
cdef object py_dtype(self):
# Numpy translation function for opaque types
return dtype("|V" + str(self.get_size()))
cdef class TypeStringID(TypeID):
"""
String datatypes, both fixed and vlen.
"""
@with_phil
def is_variable_str(self):
"""() => BOOL is_variable
Determine if the given string datatype is a variable-length string.
"""
return <bint>(H5Tis_variable_str(self.id))
@with_phil
def get_cset(self):
"""() => INT character_set
Retrieve the character set used for a string.
"""
return <int>H5Tget_cset(self.id)
@with_phil
def set_cset(self, int cset):
"""(INT character_set)
Set the character set used for a string.
"""
H5Tset_cset(self.id, <H5T_cset_t>cset)
@with_phil
def get_strpad(self):
"""() => INT padding_type
Get the padding type. Legal values are:
STR_NULLTERM
NULL termination only (C style)
STR_NULLPAD
Pad buffer with NULLs
STR_SPACEPAD
Pad buffer with spaces (FORTRAN style)
"""
return <int>H5Tget_strpad(self.id)
@with_phil
def set_strpad(self, int pad):
"""(INT pad)
Set the padding type. Legal values are:
STR_NULLTERM
NULL termination only (C style)
STR_NULLPAD
Pad buffer with NULLs
STR_SPACEPAD
Pad buffer with spaces (FORTRAN style)
"""
H5Tset_strpad(self.id, <H5T_str_t>pad)
cdef object py_dtype(self):
# Numpy translation function for string types
if self.get_cset() == H5T_CSET_ASCII:
encoding = 'ascii'
elif self.get_cset() == H5T_CSET_UTF8:
encoding = 'utf-8'
else:
raise TypeError("Unknown string encoding (value %d)" % self.get_cset())
if self.is_variable_str():
length = None
else:
length = self.get_size()
return string_dtype(encoding=encoding, length=length)
cdef class TypeVlenID(TypeID):
"""
Non-string vlen datatypes.
"""
cdef object py_dtype(self):
# get base type id
cdef TypeID base_type
base_type = self.get_super()
return vlen_dtype(base_type.dtype)
cdef class TypeTimeID(TypeID):
"""
Unix-style time_t (deprecated)
"""
pass
cdef class TypeBitfieldID(TypeID):
"""
HDF5 bitfield type
"""
pass
cdef class TypeReferenceID(TypeID):
"""
HDF5 object or region reference
"""
cdef object py_dtype(self):
if H5Tequal(self.id, H5T_STD_REF_OBJ):
return ref_dtype
elif H5Tequal(self.id, H5T_STD_REF_DSETREG):
return regionref_dtype
else:
raise TypeError("Unknown reference type")
# === Numeric classes (integers and floats) ===================================
cdef class TypeAtomicID(TypeID):
"""
Base class for atomic datatypes (float or integer)
"""
@with_phil
def get_order(self):
"""() => INT order
Obtain the byte order of the datatype; one of:
- ORDER_LE
- ORDER_BE
"""
return <int>H5Tget_order(self.id)
@with_phil
def set_order(self, int order):
"""(INT order)
Set the byte order of the datatype; one of:
- ORDER_LE
- ORDER_BE
"""
H5Tset_order(self.id, <H5T_order_t>order)
@with_phil
def get_precision(self):
"""() => UINT precision
Get the number of significant bits (excludes padding).
"""
return H5Tget_precision(self.id)
@with_phil
def set_precision(self, size_t precision):
"""(UINT precision)
Set the number of significant bits (excludes padding).
"""
H5Tset_precision(self.id, precision)
@with_phil
def get_offset(self):
"""() => INT offset
Get the offset of the first significant bit.
"""
return H5Tget_offset(self.id)
@with_phil
def set_offset(self, size_t offset):
"""(UINT offset)
Set the offset of the first significant bit.
"""
H5Tset_offset(self.id, offset)
@with_phil
def get_pad(self):
"""() => (INT lsb_pad_code, INT msb_pad_code)
Determine the padding type. Possible values are:
- PAD_ZERO
- PAD_ONE
- PAD_BACKGROUND
"""
cdef H5T_pad_t lsb
cdef H5T_pad_t msb
H5Tget_pad(self.id, &lsb, &msb)
return (<int>lsb, <int>msb)
@with_phil
def set_pad(self, int lsb, int msb):
"""(INT lsb_pad_code, INT msb_pad_code)
Set the padding type. Possible values are:
- PAD_ZERO
- PAD_ONE
- PAD_BACKGROUND
"""
H5Tset_pad(self.id, <H5T_pad_t>lsb, <H5T_pad_t>msb)
cdef class TypeIntegerID(TypeAtomicID):
"""
Integer atomic datatypes
"""
@with_phil
def get_sign(self):
"""() => INT sign
Get the "signedness" of the datatype; one of:
SGN_NONE
Unsigned
SGN_2
Signed 2's complement
"""
return <int>H5Tget_sign(self.id)
@with_phil
def set_sign(self, int sign):
"""(INT sign)
Set the "signedness" of the datatype; one of:
SGN_NONE
Unsigned
SGN_2
Signed 2's complement
"""
H5Tset_sign(self.id, <H5T_sign_t>sign)
cdef object py_dtype(self):
# Translation function for integer types
return dtype( _order_map[self.get_order()] +
_sign_map[self.get_sign()] + str(self.get_size()) )
cdef class TypeFloatID(TypeAtomicID):
"""
Floating-point atomic datatypes
"""
@with_phil
def get_fields(self):
"""() => TUPLE field_info
Get information about floating-point bit fields. See the HDF5
docs for a full description. Tuple has the following members:
0. UINT spos
1. UINT epos
2. UINT esize
3. UINT mpos
4. UINT msize
"""
cdef size_t spos, epos, esize, mpos, msize
H5Tget_fields(self.id, &spos, &epos, &esize, &mpos, &msize)
return (spos, epos, esize, mpos, msize)
@with_phil
def set_fields(self, size_t spos, size_t epos, size_t esize,
size_t mpos, size_t msize):
"""(UINT spos, UINT epos, UINT esize, UINT mpos, UINT msize)
Set floating-point bit fields. Refer to the HDF5 docs for
argument definitions.
"""
H5Tset_fields(self.id, spos, epos, esize, mpos, msize)
@with_phil
def get_ebias(self):
"""() => UINT ebias
Get the exponent bias.
"""
return H5Tget_ebias(self.id)
@with_phil
def set_ebias(self, size_t ebias):
"""(UINT ebias)
Set the exponent bias.
"""
H5Tset_ebias(self.id, ebias)
@with_phil
def get_norm(self):