You must log in to use subscribtions.

Clear message

Reading SPE file from CCD camera

Some charge-coupled device (CCD) cameras (Princeton and like) produce SPE files. This page suggests how to read such binary files with Numpy but the code is not robust. The following code is only able to read files having the same format as the example, 'lampe_dt.spe' (unfortuanetly the only SPE file on the wiki).

Loading SPE file with numpy

Only Numpy is required for loading SPE file, the result will be an array made of colors. The image size is at position 42 and 656 and the data at 4100. There are then many other data in a SPE file header, one must be the data type (you are welcome to edit this page if you know where). Finally note that the image is always made of colors coded on unsigned integer of 16 bits but it might not be the case in your input file.

   1 # read_spe.py
   2 import numpy as N
   3 
   4 class File(object):
   5 
   6     def __init__(self, fname):
   7         self._fid = open(fname, 'rb')
   8         self._load_size()
   9 
  10     def _load_size(self):
  11         self._xdim = N.int64(self.read_at(42, 1, N.int16)[0])
  12         self._ydim = N.int64(self.read_at(656, 1, N.int16)[0])
  13 
  14     def _load_date_time(self):
  15         rawdate = self.read_at(20, 9, N.int8)
  16         rawtime = self.read_at(172, 6, N.int8)
  17         strdate = ''
  18         for ch in rawdate :
  19             strdate += chr(ch)
  20         for ch in rawtime:
  21             strdate += chr(ch)
  22         self._date_time = time.strptime(strdate,"%d%b%Y%H%M%S")
  23 
  24     def get_size(self):
  25         return (self._xdim, self._ydim)
  26 
  27     def read_at(self, pos, size, ntype):
  28         self._fid.seek(pos)
  29         return N.fromfile(self._fid, ntype, size)
  30 
  31     def load_img(self):
  32         img = self.read_at(4100, self._xdim * self._ydim, N.uint16)
  33         return img.reshape((self._ydim, self._xdim))
  34 
  35     def close(self):
  36         self._fid.close()
  37 
  38 def load(fname):
  39     fid = File(fname)
  40     img = fid.load_img()
  41     fid.close()
  42     return img
  43 
  44 if __name__ == "__main__":
  45     import sys
  46     img = load(sys.argv[-1])

Viewing the image with matplotlib and ipython

The 'read_spe.py' script from above and the 'lampe_dt.spe' example are provided in the archive read_spe.zip. Once decompresesed, you can then start ipython in the directory where the script lives:

ipython -pylab read_spe.py lampe_dt.spe

The following first line will show the image in a new window. The second line will change the colormap (try 'help(pylab.colormaps)' for listing them).

   1 >>> pylab.imshow(img)
   2 >>> pylab.hot()

Cookbook/Reading SPE files (last edited 2010-12-03 12:25:06 by MattClarke)