Skip to content

Instantly share code, notes, and snippets.

@takapiro99
Last active September 29, 2021 05:59
Show Gist options
  • Save takapiro99/a44c775045542a7b4f0b323990e847c1 to your computer and use it in GitHub Desktop.
Save takapiro99/a44c775045542a7b4f0b323990e847c1 to your computer and use it in GitHub Desktop.
import struct
import numpy as np
import matplotlib.pyplot as plt
def from_bytes(b, fmt, offset):
size = struct.calcsize(fmt)
return struct.unpack(fmt, b[offset:offset+size])[0]
def read_spe(b):
datatype = from_bytes(b, "h", 108)
version = from_bytes(b, "<f", 1992)
frame_width = from_bytes(b, "H", 42)
frame_height = from_bytes(b, "H", 656)
num_frames = from_bytes(b, "i", 1446)
exposure = from_bytes(b, "<f", 10)
delay_time = from_bytes(b, "<f", 134)
pulse_rep_width = from_bytes(b, "<f", 118)
date = b[20:30]
time = b[172:179]
pulse_type = [None, "Fixed", "Exponential"][from_bytes(b, "h", 142)]
# print("datatype:", datatype)
to_np_type = [np.float32, np.int32, np.int16, np.uint16,
None, np.float64, np.uint8, None, np.uint32]
np_type = to_np_type[datatype]
itemsize = np.dtype(np_type).itemsize
# print("numpy type:", np_type)
# print("SPE version:", version)
# print("Frame width:", frame_width)
# print("Frame height:", frame_height)
# print("Number of frames:", num_frames)
# print("Exposure (s):", exposure)
# print("Delay time (us)", delay_time)
# print("Pulse repetition width (us)", pulse_rep_width)
# print("Pulse type increments: ", pulse_type)
# print("Date collected: ", date)
# print("Time collected (hhmmss):", time)
count = frame_width * frame_height
all_data = []
for i in range(0, num_frames):
# print("Showing frame number ", i+1)
data = np.frombuffer(b, dtype=np_type, count=count,
offset=4100 + i*count*itemsize)
image = np.reshape(data, (frame_height, frame_width))
all_data.append(image)
return all_data
def read_spe_from_file(filename):
print(filename)
f = open(filename, "rb")
return read_spe(f.read())
def show_spe_image_from_file(filename):
files = read_spe_from_file(filename)
for f in files:
plt.figure()
plt.imshow(f)
plt.show()
def show_spe_image(lists):
for f in lists:
plt.figure()
plt.imshow(f)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment