WolfArray - Data type

A WolfArray can store any type of data, including:

  • int (integer8, integer16, integer32)

  • float (floating point number, 32-bit or 64-bit)

  • bool (boolean/logical)

WOLF types are defined as follows (for compatibility with Fortran and VB6):

  • WOLF_ARRAY_HILLSHAPE = -1

  • WOLF_ARRAY_FULL_SINGLE = 1

  • WOLF_ARRAY_FULL_DOUBLE = 2

  • WOLF_ARRAY_SYM_DOUBLE = 12

  • WOLF_ARRAY_FULL_LOGICAL = 4

  • WOLF_ARRAY_CSR_DOUBLE = 5

  • WOLF_ARRAY_FULL_INTEGER = 6

  • WOLF_ARRAY_FULL_SINGLE_3D = 7

  • WOLF_ARRAY_FULL_INTEGER8 = 8

  • WOLF_ARRAY_FULL_UINTEGER8 = 88

Equivalent Numpy types can be obtained using the dtype attribute.

[1]:
import _add_path
from wolfhece.wolf_array import WolfArray, WOLF_ARRAY_FULL_SINGLE, WOLF_ARRAY_FULL_INTEGER8, WOLF_ARRAY_FULL_DOUBLE, header_wolf

[2]:
h = header_wolf()
h.shape = (10,10)

w_single = WolfArray(srcheader=h, whichtype=WOLF_ARRAY_FULL_SINGLE)
w_double = WolfArray(srcheader=h, whichtype=WOLF_ARRAY_FULL_DOUBLE)
w_integer8 = WolfArray(srcheader=h, whichtype=WOLF_ARRAY_FULL_INTEGER8)

[3]:
# check the dtype of the array and the WolfArray object
print(w_single.dtype, w_single.array.dtype)
print(w_double.dtype, w_double.array.dtype)
print(w_integer8.dtype, w_integer8.array.dtype)
<class 'numpy.float32'> float32
<class 'numpy.float64'> float64
<class 'numpy.int8'> int8
[4]:
# Modify some values in the array
w_integer8.array [0,0] = 10
w_integer8.array [-1,-1] = 11
[10]:
# convert the array to a different type (all wolfhece versions)
w3_single = WolfArray(srcheader=w_integer8.get_header(), whichtype=WOLF_ARRAY_FULL_SINGLE)

w3_single.set_array_from_numpy(w_integer8.array.astype(w3_single.dtype))

print(w3_single.dtype, w3_single.array.dtype)
print(w3_single.array [0,0], type(w3_single.array [0,0]))
print(w3_single.array [-1,-1], type(w3_single.array [-1,-1]))
print(w3_single.array [0,1], type(w3_single.array [0,1]))
<class 'numpy.float32'> float32
10.0 <class 'numpy.float32'>
11.0 <class 'numpy.float32'>
1.0 <class 'numpy.float32'>
[14]:
# convert the array to a different type (wolfhece 2.2.6 and above)
from wolfhece.apps.version import WolfVersion
print(WolfVersion().get_version())

if WolfVersion().get_version() >= '2.2.6':
    w4_single = WolfArray(mold=w_integer8, whichtype=WOLF_ARRAY_FULL_SINGLE)
    print(w4_single.dtype, w4_single.array.dtype)
    print(w4_single.array [0,0], type(w4_single.array [0,0]))
    print(w4_single.array [-1,-1], type(w4_single.array [-1,-1]))
    print(w4_single.array [0,1], type(w4_single.array [0,1]))
2.2.5
[ ]:
w3_single.array [2,2] = 1000.0 # set a value in the new array
w4_int8 = WolfArray(mold=w3_single, whichtype=WOLF_ARRAY_FULL_INTEGER8) # Try to convert to int8

print(w4_int8.array [2,2], type(w4_int8.array [2,2])) # 1000.0 should be converted to 1000 but the type is int8 so 1000 is not possible
-24 <class 'numpy.int8'>
[ ]:
# Check the conversion of a "BAD" float to int8
import numpy as np

np.float32(1000.0).astype(np.int8) # 1000.0 should be converted to 1000 but the type is int8 so 1000 is not possible
np.int8(-24)