Skip to content

Commit f1a5c12

Browse files
committed
Additional datatype conversions
1 parent 2bbd047 commit f1a5c12

1 file changed

Lines changed: 100 additions & 8 deletions

File tree

sciopy/com_util.py

Lines changed: 100 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def length_correction(array: list) -> list:
127127
# split in burst count messages
128128
split_length = lst.shape[0] // ssms.burst_count
129129
for split in range(ssms.burst_count):
130-
split_list.append(lst[split * split_length : (split + 1) * split_length])
130+
split_list.append(lst[split * split_length: (split + 1) * split_length])
131131
return np.array(split_list)
132132

133133

@@ -164,13 +164,50 @@ def bytesarray_to_float(bytes_array: np.ndarray) -> float:
164164
Returns
165165
-------
166166
float
167-
double precision float
167+
single precision float
168168
"""
169169
bytes_array = [int(b, 16) for b in bytes_array]
170170
bytes_array = bytes(bytes_array)
171171
return struct.unpack("!f", bytes(bytes_array))[0]
172172

173173

174+
175+
def byteintarray_to_float(bytes_array: np.ndarray) -> float:
176+
"""
177+
Converts a bytes array to a float number. Array is array of integers representing bytes.
178+
179+
Parameters
180+
----------
181+
bytes_array : np.ndarray
182+
array of integers former being bytes
183+
184+
Returns
185+
-------
186+
float
187+
single precision float
188+
"""
189+
return struct.unpack("!f", bytes(bytes_array))[0]
190+
191+
192+
def bytesarray_to_double(bytes_array: np.ndarray) -> float:
193+
"""
194+
Converts a bytes array to a float number.
195+
196+
Parameters
197+
----------
198+
bytes_array : np.ndarray
199+
array of bytes
200+
201+
Returns
202+
-------
203+
float
204+
double precision float
205+
"""
206+
bytes_array = [int(b, 16) for b in bytes_array]
207+
bytes_array = bytes(bytes_array)
208+
return struct.unpack("!d", bytes(bytes_array))[0]
209+
210+
174211
def bytesarray_to_byteslist(bytes_array: np.ndarray) -> list:
175212
"""
176213
Converts a bytes array to a list of bytes.
@@ -207,6 +244,61 @@ def bytesarray_to_int(bytes_array: np.ndarray) -> int:
207244
return int.from_bytes(bytes_array, "big")
208245

209246

247+
TWOPOWER24 = 16777216
248+
TWOPOWER16 = 65536
249+
TWOPOWER8 = 256
250+
251+
252+
def four_byte_to_int(bytelist):
253+
"""
254+
Converts a list of 4 integers representing bytes to int.
255+
256+
Parameters
257+
----------
258+
bytelist : np.ndarray/list of integers representing bytes MSB first
259+
260+
Returns
261+
-------
262+
int
263+
integer number
264+
"""
265+
return TWOPOWER24 * bytelist[0] + TWOPOWER16 * bytelist[1] + TWOPOWER8 * bytelist[2] + bytelist[3]
266+
267+
268+
def two_byte_to_int(bytelist):
269+
"""
270+
Converts a list of 2 integers representing bytes to int.
271+
272+
Parameters
273+
----------
274+
bytelist : np.ndarray/list of integers representing bytes MSB first
275+
276+
Returns
277+
-------
278+
int
279+
integer number
280+
"""
281+
return TWOPOWER8 * bytelist[0] + bytelist[1]
282+
283+
284+
def bytelist_to_int(bytelist):
285+
"""
286+
Converts a list of integers representing bytes MSB first to int.
287+
Parameters
288+
----------
289+
bytelist : np.ndarray/list of integers representing bytes MSB first
290+
291+
Returns
292+
-------
293+
int
294+
integer number
295+
"""
296+
r = bytelist[-1]
297+
for j in range(2, len(bytelist)):
298+
r += bytelist[-j] * 2 ** ((j - 1) * 8)
299+
return r
300+
301+
210302
def parse_single_frame(lst_ele: np.ndarray) -> SingleFrame:
211303
"""
212304
Parse single data to the class SingleFrame.
@@ -226,8 +318,8 @@ def parse_single_frame(lst_ele: np.ndarray) -> SingleFrame:
226318
for i in range(11, 135, 8):
227319
enum += 1
228320
channels[f"ch_{enum}"] = complex(
229-
bytesarray_to_float(lst_ele[i : i + 4]),
230-
bytesarray_to_float(lst_ele[i + 4 : i + 8]),
321+
bytesarray_to_float(lst_ele[i: i + 4]),
322+
bytesarray_to_float(lst_ele[i + 4: i + 8]),
231323
)
232324

233325
excitation_stgs = np.array([single_hex_to_int(ele) for ele in lst_ele[3:5]])
@@ -245,7 +337,7 @@ def parse_single_frame(lst_ele: np.ndarray) -> SingleFrame:
245337

246338

247339
def split_bursts_in_frames(
248-
split_list: np.ndarray, burst_count: int, channel_group: list
340+
split_list: np.ndarray, burst_count: int, channel_group: list
249341
) -> np.ndarray:
250342
"""
251343
Takes the splitted list from `reshape_full_message_in_bursts()` and parses the single frames.
@@ -256,7 +348,7 @@ def split_bursts_in_frames(
256348
channel depending burst frames
257349
"""
258350
msg_len = 140 # Constant
259-
iC= 0
351+
iC = 0
260352
frame = [] # Channel group depending frame
261353
burst_frame = [] # single burst count frame with channel depending frame
262354
subframe_length = split_list.shape[1] // msg_len
@@ -269,8 +361,8 @@ def split_bursts_in_frames(
269361
frame.append(parsed_sgl_frame)
270362

271363
else:
272-
iC+=1
364+
iC += 1
273365
burst_frame.append(frame)
274366
frame = [] # Reset channel depending single burst frame
275-
print("UNUSED CG "+ str(iC))
367+
print("UNUSED CG " + str(iC))
276368
return np.array(burst_frame)

0 commit comments

Comments
 (0)