
;c]c           @` s   d  Z  d d l m Z m Z m Z d d l Z d d l Z d d l Z d d l	 m
 Z
 d d l m Z e j e  Z d f  d     YZ d	 e f d
     YZ d   Z e d d d d  Z e
 j e d e d S(   s    SPE file reader
i    (   t   absolute_importt   print_functiont   divisionNi   (   t   formats(   t   Formatt   Specc           B` s&  e  Z d  Z i d d 6d d 6d d 6d d 6Z i. d d 6d e j d d d d d d g  d f d 6d d 6d d 6d d 6d d 6d d  6d d" 6d d$ 6d d& 6d d( 6d d+ 6d d- 6d d/ 6d d1 6d d3 6d d5 6d d7 6d d9 6d d; 6d d= 6d d? 6d dA 6d dC 6d dF 6d dH 6d dJ 6d dL 6d dN 6d dP 6d dR 6d dT 6d dX 6d dZ 6d d] 6d d` 6d db 6d dd 6d df 6d dh 6d dj 6d dl 6d dn 6d dp 6d dr 6d dt 6Z du Z e j d'  e j d
  e j d  e j d  e j dv  g Z dw dx dy dz d{ d| d} d~ d d d d g Z	 d d d g Z
 d` g Z RS(   sW  SPE file specification data

    Tuples of (offset, datatype, count), where offset is the offset in the SPE
    file and datatype is the datatype as used in `numpy.fromfile`()

    `data_start` is the offset of actual image data.

    `dtypes` translates SPE datatypes (0...4) to numpy ones, e. g. dtypes[0]
    is dtype("<f") (which is np.float32).

    `controllers` maps the `type` metadata to a human readable name

    `readout_modes` maps the `readoutMode` metadata to something human readable
    although this may not be accurate since there is next to no documentation
    to be found.
    il   s   <ht   datatypei*   s   <Ht   xdimi  t   ydimi  s   <it	   NumFramesi  t   NumROIi  t   startxt   endxt   groupxt   startyt   endyt   groupyi
   t   ROIsi   t   xDimDeti   t   yDimDeti   t	   VChipXdimi   t	   VChipYdimi    t   controller_versioni   t   logic_outputi   t   amp_high_cap_low_noisei   t   modes   <ft   exposure_seci   s   <10St   datei$   t   detector_tempi(   t   detector_typei,   t   st_diodei.   t
   delay_timei2   t   shutter_controli4   t   absorb_livei6   t   absorb_modei8   t   can_do_virtual_chipi:   t   threshold_min_livei<   t   threshold_min_vali@   t   threshold_max_liveiB   t   threshold_max_vali   s   <7St
   time_locali   t   time_utci   t
   adc_offseti   t   adc_ratei   t   adc_typei   t   adc_resolutioni   t   adc_bit_adjusti   t   gaini   s   <80Si   t   commentsiX  t	   geometrici  s   <16St
   sw_versioni  s   <436St   spare_4ib   t
   XPrePixelsid   t   XPostPixelsif   t
   YPrePixelsih   t   YPostPixelsi  t   readout_timei  t   typei  t   clockspeed_usi  t   readout_modei  t   window_sizei  t   file_header_veri  s   <Is   new120 (Type II)s   old120 (Type I)t   ST130t   ST121t   ST138s   DC131 (PentaMax)s   ST133 (MicroMax/Roper)s   ST135 (GPIB)t   VTCCDs   ST116 (GPIB)s   OMA3 (GPIB)t   OMA4s
   full frames   frame transfert   kinetics(   il   s   <h(   i*   s   <H(   i  s   <H(   i  s   <i(   i  s   <h(   R   s   <H(   R   s   <H(   R   s   <H(   R   s   <H(   R   s   <H(   R   s   <H(   i   s   <H(   i   s   <H(   i   s   <h(   i   s   <h(   i    s   <h(   i   s   <h(   i   s   <H(   i   s   <h(   i
   s   <f(   i   s   <10S(   i$   s   <f(   i(   s   <h(   i,   s   <h(   i.   s   <f(   i2   s   <H(   i4   s   <h(   i6   s   <H(   i8   s   <h(   i:   s   <h(   i<   s   <f(   i@   s   <h(   iB   s   <f(   i   s   <7S(   i   s   <7S(   i   s   <H(   i   s   <H(   i   s   <H(   i   s   <H(   i   s   <H(   i   s   <H(   i   s   <80Si   (   iX  s   <H(   i  s   <16S(   i  s   <436S(   ib   s   <h(   id   s   <h(   if   s   <h(   ih   s   <h(   i  s   <f(   i  s   <h(   i  s   <f(   i  s   <H(   i  s   <H(   i  s   <f(   t   __name__t
   __module__t   __doc__t   basict   npt   dtypet   metadatat
   data_startt   dtypest   controllerst   readout_modest	   no_decode(    (    (    s2   lib/python2.7/site-packages/imageio/plugins/spe.pyR      s   
	

	t	   SpeFormatc           B` s9   e  Z d  Z d   Z d   Z d e j f d     YZ RS(   s   Some CCD camera software produces images in the Princeton Instruments
    SPE file format. This plugin supports reading such files.

    Parameters for reading
    ----------------------
    char_encoding : str
        Character encoding used to decode strings in the metadata. Defaults
        to "latin1".
    check_filesize : bool
        The number of frames in the file is stored in the file header. However,
        this number may be wrong for certain software. If this is `True`
        (default), derive the number of frames also from the file size and
        raise a warning if the two values do not match.

    Metadata for reading
    --------------------
    ROIs : list of dict
        Regions of interest used for recording images. Each dict has the
        "top_left" key containing x and y coordinates of the top left corner,
        the "bottom_right" key with x and y coordinates of the bottom right
        corner, and the "bin" key with number of binned pixels in x and y
        directions.
    comments : list of str
        The SPE format allows for 5 comment strings of 80 characters each.
    controller_version : int
        Hardware version
    logic_output : int
        Definition of output BNC
    amp_hi_cap_low_noise : int
        Amp switching mode
    mode : int
        Timing mode
    exp_sec : float
        Alternative exposure in seconds
    date : str
        Date string
    detector_temp : float
        Detector temperature
    detector_type : int
        CCD / diode array type
    st_diode : int
        Trigger diode
    delay_time : float
        Used with async mode
    shutter_control : int
        Normal, disabled open, or disabled closed
    absorb_live : bool
        on / off
    absorb_mode : int
        Reference strip or file
    can_do_virtual_chip : bool
        True or False whether chip can do virtual chip
    threshold_min_live : bool
        on / off
    threshold_min_val : float
        Threshold minimum value
    threshold_max_live : bool
        on / off
    threshold_max_val : float
        Threshold maximum value
    time_local : str
        Experiment local time
    time_utc : str
        Experiment UTC time
    adc_offset : int
        ADC offset
    adc_rate : int
        ADC rate
    adc_type : int
        ADC type
    adc_resolution : int
        ADC resolution
    adc_bit_adjust : int
        ADC bit adjust
    gain : int
        gain
    sw_version : str
        Version of software which created this file
    spare_4 : bytes
        Reserved space
    readout_time : float
        Experiment readout time
    type : str
        Controller type
    clockspeed_us : float
        Vertical clock speed in microseconds
    readout_mode : {"full frame", "frame transfer", "kinetics", ""}
        Readout mode. Empty string means that this was not set by the
        Software.
    window_size : int
        Window size for Kinetics mode
    file_header_ver : float
        File header version
    chip_size : [int, int]
        x and y dimensions of the camera chip
    virt_chip_size : [int, int]
        Virtual chip x and y dimensions
    pre_pixels : [int, int]
        Pre pixels in x and y dimensions
    post_pixels : [int, int],
        Post pixels in x and y dimensions
    geometric : list of {"rotate", "reverse", "flip"}
        Geometric operations
    c         C` s*   | j  d |  j d k o) | j |  j k S(   Ni   t   ?(   R   t   modest	   extensiont
   extensions(   t   selft   request(    (    s2   lib/python2.7/site-packages/imageio/plugins/spe.pyt	   _can_read   s    c         C` s   t  S(   N(   t   False(   RU   RV   (    (    s2   lib/python2.7/site-packages/imageio/plugins/spe.pyt
   _can_write  s    t   Readerc           B` sD   e  Z d  e d  Z d   Z d   Z d   Z d   Z d   Z RS(   t   latin1c         C` s  |  j  j   |  _ | |  _ |  j t j  } t j | d |  _ | d | d f |  _	 | d |  _
 | r t j j |  j  j    } | t j } | |  j	 d |  j	 d |  j j } | |  j
 k r t j d |  j  j  t | |  j
  |  _
 q n  d  |  _ d  S(   NR   R   R   R	   i    i   sM   Number of frames according to file header does not match the size of file %s.(   RV   t   get_filet   _filet   _char_encodingt   _parse_headerR   RG   RL   t   _dtypet   _shapet   _lent   ost   patht   getsizet   get_local_filenameRK   t   itemsizet   loggert   warningt   filenamet   mint   Nonet   _meta(   RU   t   char_encodingt   check_filesizet   infot   fszt   l(    (    s2   lib/python2.7/site-packages/imageio/plugins/spe.pyt   _open  s     	&c   	      C` s  |  j  d  k r|  j t j  |  _  |  j  j d d   } | d k  rK d n | } t |  j  d |   |  j  d <|  j  j d d   |  j  j d d   g |  j  d <|  j  j d d   |  j  j d d   g |  j  d	 <|  j  j d
 d   |  j  j d d   g |  j  d <|  j  j d d   |  j  j d d   g |  j  d <g  |  j  d D] } t |  ^ qA|  j  d <g  } |  j  j d d  } | d @r| j d  n  | d @r| j d  n  | d @r| j d  n  | |  j  d <|  j  d } d | k ot	 t j
  k n r#t j
 | d |  j  d <n d |  j  d <|  j  d } d | k o]t	 t j  k n r}t j | d |  j  d <n d |  j  d <x( d  D]  } t |  j  |  |  j  | <qW|  j |  j  d <n  |  j  S(!   NR
   i   R   R   R   t	   chip_sizeR   R   t   virt_chip_sizeR4   R6   t
   pre_pixelsR5   R7   t   post_pixelsR0   R1   i    t   rotatei   t   reversei   t   flipR9   t    R;   R!   R#   R$   R&   t   frame_shape(   R!   R#   R$   R&   (   Rm   Rl   R_   R   RJ   t   popt   roi_array_to_dictt   strt   appendt   lenRM   RN   t   boolRa   (	   RU   t   indext   nrt   ct   gt   ft   tt   mt   k(    (    s2   lib/python2.7/site-packages/imageio/plugins/spe.pyt   _get_meta_data  sN    -


%%   
c         C` s   d  S(   N(    (   RU   (    (    s2   lib/python2.7/site-packages/imageio/plugins/spe.pyt   _close_  s    c         ` s5  i  } t  j   f d    } x| j   D]\ } }   j j | d  t |  d k  rc d n | d } t  j   j d | d d | } | j j d k r | t	 j
 k r y | |  } Wq t k
 r t j d	 j |   q Xn  y t  j |  } Wn  t k
 r"t  j |  } n X| | | <q+ W| S(
   Nc         ` s   |  j    j  S(   N(   t   decodeR^   (   t   x(   RU   (    s2   lib/python2.7/site-packages/imageio/plugins/spe.pyt   <lambda>f  R{   i    i   i   i   RI   t   countt   SsG   Failed to decode "{}" metadata string. Check `char_encoding` parameter.(   RH   t	   vectorizet   itemsR]   t   seekR   t   fromfileRI   t   kindR   RO   t	   ExceptionRh   Ri   t   formatt   asscalart
   ValueErrort   squeeze(   RU   t   spect   retR   t   namet   spt   cntt   v(    (   RU   s2   lib/python2.7/site-packages/imageio/plugins/spe.pyR_   c  s&    ""!c         C` s%   |  j  j d d k r d S|  j Sd  S(   Ni   t   vV(   RV   R   Rb   (   RU   (    (    s2   lib/python2.7/site-packages/imageio/plugins/spe.pyt   _get_length  s    c         C` sv  | d k  r t  d |   n  | |  j k rJ t  d | |  j f   n  |  j j d d k r | d k r{ t  d   n  |  j j t j  t j	 |  j d |  j
 d |  j d |  j d |  j } | j |  j f |  j  } n| |  j j t j | |  j d |  j d |  j
 j  t j	 |  j d |  j
 d |  j d |  j d } | j |  j  } | |  j |  f S(	   Ni    s   Image index %i < 0s   Image index %i > %ii   R   s"   Index has to be 0 in v and V modesRI   R   (   t
   IndexErrorRb   RV   R   R]   R   R   RK   RH   R   R`   Ra   t   reshapeRg   R   (   RU   R   t   data(    (    s2   lib/python2.7/site-packages/imageio/plugins/spe.pyt	   _get_data  s(    		"	(-(	   RD   RE   t   TrueRs   R   R   R_   R   R   (    (    (    s2   lib/python2.7/site-packages/imageio/plugins/spe.pyRZ     s   	A			(   RD   RE   RF   RW   RY   R   RZ   (    (    (    s2   lib/python2.7/site-packages/imageio/plugins/spe.pyRP      s   h		c   	      C` s   g  } |  d d d d d d g }  x~ |  D]v \ } } } } } } i t  |  t  |  g d 6t  |  t  |  g d 6t  |  t  |  g d	 6} | j |  q) W| S(
   s  Convert the `ROIs` structured arrays to :py:class:`dict`

    Parameters
    ----------
    a : numpy.ndarray
        Structured array containing ROI data

    Returns
    -------
    list of dict
        One dict per ROI. Keys are "top_left", "bottom_right", and "bin",
        values are tuples whose first element is the x axis value and the
        second element is the y axis value.
    R   R   R   R   R   R   t   top_leftt   bottom_rightt   bin(   t   intR   (	   t   aRr   t   sxt   syt   ext   eyt   gxt   gyt   d(    (    s2   lib/python2.7/site-packages/imageio/plugins/spe.pyR~     s    t   spes   SPE file formats   .spet   iIvVt	   overwrite(   RF   t
   __future__R    R   R   Rc   t   loggingt   numpyRH   R{   R   t   coreR   t	   getLoggerRD   Rh   R   RP   R~   t   fmtt
   add_formatR   (    (    (    s2   lib/python2.7/site-packages/imageio/plugins/spe.pyt   <module>   s   ~ 	