Skip to content

Commit 6af9b98

Browse files
committed
Refactoring of all datatype conversion into new file
1 parent f1a5c12 commit 6af9b98

2 files changed

Lines changed: 195 additions & 192 deletions

File tree

sciopy/com_util.py

Lines changed: 1 addition & 189 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import struct
1414
import sys
1515
from glob import glob
16-
16+
from .datatype_conversion import *
1717

1818
def available_serial_ports() -> list:
1919
"""
@@ -66,27 +66,6 @@ def clTbt_dp(val: float) -> list:
6666
return [int(ele) for ele in struct.pack(">d", val)]
6767

6868

69-
def del_hex_in_list(lst: list) -> np.ndarray:
70-
"""
71-
Delete the hexadecimal 0x python notation.
72-
73-
Parameters
74-
----------
75-
lst : list
76-
list of hexadecimals
77-
78-
Returns
79-
-------
80-
np.ndarray
81-
cleared message
82-
"""
83-
return np.array(
84-
[
85-
"0" + ele.replace("0x", "") if len(ele) == 1 else ele.replace("0x", "")
86-
for ele in lst
87-
]
88-
)
89-
9069

9170
def reshape_full_message_in_bursts(lst: list, ssms: EitMeasurementSetup) -> np.ndarray:
9271
"""
@@ -131,173 +110,6 @@ def length_correction(array: list) -> list:
131110
return np.array(split_list)
132111

133112

134-
def single_hex_to_int(str_num: str) -> int:
135-
"""
136-
Delete the hexadecimal 0x python notation.
137-
138-
Parameters
139-
----------
140-
str_num : str
141-
single hexadecimal string
142-
143-
Returns
144-
-------
145-
int
146-
integer number
147-
"""
148-
if len(str_num) == 1:
149-
str_num = f"0x0{str_num}"
150-
else:
151-
str_num = f"0x{str_num}"
152-
return int(str_num, 16)
153-
154-
155-
def bytesarray_to_float(bytes_array: np.ndarray) -> float:
156-
"""
157-
Converts a bytes array to a float number.
158-
159-
Parameters
160-
----------
161-
bytes_array : np.ndarray
162-
array of bytes
163-
164-
Returns
165-
-------
166-
float
167-
single precision float
168-
"""
169-
bytes_array = [int(b, 16) for b in bytes_array]
170-
bytes_array = bytes(bytes_array)
171-
return struct.unpack("!f", bytes(bytes_array))[0]
172-
173-
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-
211-
def bytesarray_to_byteslist(bytes_array: np.ndarray) -> list:
212-
"""
213-
Converts a bytes array to a list of bytes.
214-
215-
Parameters
216-
----------
217-
bytes_array : np.ndarray
218-
array of bytes
219-
220-
Returns
221-
-------
222-
list
223-
list of bytes
224-
"""
225-
bytes_array = [int(b, 16) for b in bytes_array]
226-
return bytes(bytes_array)
227-
228-
229-
def bytesarray_to_int(bytes_array: np.ndarray) -> int:
230-
"""
231-
Converts a bytes array to int number.
232-
233-
Parameters
234-
----------
235-
bytes_array : np.ndarray
236-
array of bytes
237-
238-
Returns
239-
-------
240-
int
241-
integer number
242-
"""
243-
bytes_array = bytesarray_to_byteslist(bytes_array)
244-
return int.from_bytes(bytes_array, "big")
245-
246-
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-
301113

302114
def parse_single_frame(lst_ele: np.ndarray) -> SingleFrame:
303115
"""

0 commit comments

Comments
 (0)