|
@@ -0,0 +1,53 @@
|
|
|
+import numpy as np
|
|
|
+
|
|
|
+import logging
|
|
|
+
|
|
|
+logger = logging.getLogger(__name__)
|
|
|
+
|
|
|
+
|
|
|
+def process_binary(path, slice_size, slice_from=None, slice_to=None, little_endian=False):
|
|
|
+ logger.debug('little_endian: ' + str(little_endian))
|
|
|
+
|
|
|
+ raw_file = open(path, 'rb')
|
|
|
+ raw_file.seek(0, 2)
|
|
|
+ raw_fileSize = raw_file.tell()
|
|
|
+ raw_file.seek(0, 0)
|
|
|
+
|
|
|
+ numx, numy = slice_size
|
|
|
+
|
|
|
+ number_of_bytes = 4
|
|
|
+ floats_per_slice = numx * numy
|
|
|
+ bytes_per_slice = floats_per_slice * number_of_bytes
|
|
|
+ number_of_slices = int(raw_fileSize / bytes_per_slice)
|
|
|
+
|
|
|
+ '''
|
|
|
+ here we can get a subset of the binary for faster testing
|
|
|
+
|
|
|
+ tmpOutputFile = open('/tmp/binary_61_100_%d.raw' % time.time(), 'wb')
|
|
|
+ raw_file.seek(bytes_per_slice * 61, 0)
|
|
|
+ tmpOutputFile.write(raw_file.read(bytes_per_slice * 40))
|
|
|
+ tmpOutputFile.close()
|
|
|
+ raw_file.seek(0, 0)
|
|
|
+
|
|
|
+ raise ValueError('outputfile written')
|
|
|
+ '''
|
|
|
+
|
|
|
+ if (slice_from is not None and
|
|
|
+ slice_to is not None):
|
|
|
+
|
|
|
+ number_of_slices = slice_to - slice_from + 1
|
|
|
+ raw_file.seek(slice_from * bytes_per_slice, 0)
|
|
|
+
|
|
|
+ floats = np.empty((1, number_of_slices * floats_per_slice), dtype=np.float32)
|
|
|
+
|
|
|
+ endian_string = '>'
|
|
|
+ if little_endian is True:
|
|
|
+ endian_string = '<'
|
|
|
+
|
|
|
+ dt = np.dtype('%s%s' % (endian_string, 'f'))
|
|
|
+ floats[0] = np.fromfile(raw_file, dtype=dt)
|
|
|
+
|
|
|
+ floats = (floats - floats.min()) / (floats.max() - floats.min())
|
|
|
+ floats = floats.reshape((1, number_of_slices, numy, numx))
|
|
|
+
|
|
|
+ return floats
|