ó
þ™xTc           @   sŸ   d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l m Z d e f d „  ƒ  YZ e d k r› d  d l	 Z	 x# e	 j
 d D] Z e j e ƒ GHq€ Wn  d S(   iÿÿÿÿ(   t   divisionN(   t
   FormatTIFFt   FormatTIFFRayonixc           B   s›   e  Z d  Z e d „  ƒ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sr   A class for reading TIFF format Rayonix images, and correctly
  constructing a model for the experiment from this.c         C   s“  t  j |  ƒ \ } } } } } t | ƒ d k s6 t ‚ | t  j k rN d } n d } | d } | d } t j | | d d !ƒ d	 }	 t j | | d d !ƒ d	 }
 t j | | d d !ƒ d	 } | |	 k sã | |
 k sã | | k rç t St j | | d d !ƒ d	 } t j | | d d !ƒ d	 } t j | | d d !ƒ d	 } t j | | d d !ƒ d	 } | d k s‹| d	 k s‹| d	 k s‹| d	 k r�t St S(    sÛ   Check to see if this looks like an Rayonix TIFF format image,
    i.e. we can make sense of it. This simply checks that records which
    describe the size of the image match with the TIFF records which do
    the same.i   t   <t   >t   It   ii   iP   iT   i    iX   i\   ip   it   ix   i|   i€   i   iP  iT  iT  iX  iX  i\  ip  it  it  ix  ix  i|  i|  i€  (	   R   t   get_tiff_headert   lent   AssertionErrort   LITTLE_ENDIANt   structt   unpackt   Falset   True(   t
   image_filet   widtht   heightt   deptht   ordert   bytest   endiant   _It   _it   _widtht   _heightt   _deptht   nimagest   origint   orientationt   view(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/FormatTIFFRayonix.pyt
   understand   s(    	

$0c         C   s˜   |  j  | ƒ s t ‚ t j | ƒ \ } } } } } d |  _ | t j k ri d |  _ d |  _ d |  _ n d |  _ d |  _ d |  _ t j	 |  | ƒ d S(	   sc   Initialise the image structure from the given file, including a
    proper model of the experiment.i   s   <Is   <is   <iis   >Is   >is   >iiN(
   R   R	   R   R   t   _header_sizeR
   R   R   t   _iit   __init__(   t   selfR   R   R   R   R   R   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/FormatTIFFRayonix.pyR"   <   s    						c         C   s@   t  j |  ƒ d d l m } | |  j ƒ |  _ |  j j ƒ  d  S(   Niÿÿÿÿ(   t   MARImage(   R   t   _startt   iotbx.detectors.marR$   t   _image_filet   detectorbaset
   readHeader(   R#   R$   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/FormatTIFFRayonix.pyR%   W   s    c         C   sk   |  j  ƒ  \ } } } } xC t t | ƒ ƒ D]/ } | | k r+ | | | | k sZ t ‚ q+ q+ W|  j j ƒ  S(   s°   Return a model for goniometer corresponding to the values stored
    in the image header. In the first instance assume this is a single
    axis annd raise exception otherwise.(   t   _get_rayonix_scan_anglest   rangeR   R	   t   _goniometer_factoryt   single_axis(   R#   t   startst   endst   offsetR   t   j(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/FormatTIFFRayonix.pyt   _goniometer`   s
    !c      
   C   s/  |  j  ƒ  \ } } } } |  j ƒ  } | d | d k s> t ‚ | d d k sT t ‚ | d d k sj t ‚ | d d k s€ t ‚ |  j ƒ  } |  j ƒ  \ } } |  j ƒ  }	 |  j |  j f }
 t j	 |  j
 |  j d d !ƒ d } d } | |	 d | |	 d f } |  j j d | | d d	 |	 |
 | | f g  ƒ	 S(
   s   Return a model for a simple detector, which at the moment [NKS does not] insist
    that the offsets and rotations are all 0.0.i    g        i   i   ih  il  t   CCDs   +xs   -y(   R*   t   _get_rayonix_detector_rotationsR	   t   _get_rayonix_distancet   _get_rayonix_beam_xyt   _get_rayonix_pixel_sizet   _tiff_widtht   _tiff_heightR   R   R   t   _tiff_header_bytest   _detector_factoryt   simple(   R#   R.   R/   R0   R   t	   rotationst   distancet   beam_xt   beam_yt
   pixel_sizet
   image_sizet   overloadt	   underloadt   beam(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/FormatTIFFRayonix.pyt	   _detectorm   s"    	c         C   s7   t  j |  j |  j d d !ƒ d d } |  j j | ƒ S(   s#   Return a simple model for the beam.iŒ  i�  i    gñhãˆµøä>(   R   R   R   R:   t   _beam_factoryR<   (   R#   t
   wavelength(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/FormatTIFFRayonix.pyt   _beam‹   s    !c   
      C   s�   |  j  j d ƒ } |  j ƒ  d } t j |  j ƒ  ƒ } |  j ƒ  \ } } } } | | } | }	 |  j  j |  j | | | |	 | ƒ S(   s+   Return the scan information for this image.t   TIFFi   (	   t   _scan_factoryt   formatt   _get_rayonix_timest   timet   mktimet   _get_rayonix_timestampR*   t   singleR'   (
   R#   RL   t   exposure_timet   epochR.   R/   R0   R   t	   osc_startt	   osc_range(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/FormatTIFFRayonix.pyt   _scan“   s    
	c         C   s{   t  j |  j |  j d d !ƒ d } | d k r7 | d St  j |  j |  j d d !ƒ d } | d k rn | d St d ‚ d S(	   sU   Look in the usual places for the detector distance, return this
    as a float in mm.i€  i„  i    gü©ñÒMbP?i¸  i¼  s   cannot find distance in headerN(   R   R   R   R:   t   RuntimeError(   R#   R>   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/FormatTIFFRayonix.pyR5   ª   s    c         C   s;   t  j |  j |  j d d !ƒ d  \ } } | d | d f S(   s
  Get the beam x, y positions which are defined in the standard
    to be in pixels. X and Y are not defined by the documentation, so
    taking as a given that these are horizontal and vertical. N.B.
    the documentation states that the horizontal direction is fast.i„  iŒ  i   gü©ñÒMbP?(   R   R   R!   R:   (   R#   R?   R@   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/FormatTIFFRayonix.pyR6   ¼   s    #c         C   s;   t  j |  j |  j d d !ƒ d  \ } } | d | d f S(   s   Get the pixel sizes in mm.i  i  i   g�íµ ÷Æ°>(   R   R   R!   R:   (   R#   t   pixel_xt   pixel_y(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/FormatTIFFRayonix.pyR7   Ç   s    #c         C   s;   t  j |  j |  j d d !ƒ d  \ } } | d | d f S(   s/   Get the integration, exposure times in seconds.iŒ  i”  i   gü©ñÒMbP?(   R   R   R!   R:   (   R#   t   integrationt   exposure(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/FormatTIFFRayonix.pyRM   Ï   s    #c         C   s¡   |  j  d d !} t | d  ƒ } t | d d !ƒ } t | d d !ƒ } t | d d !ƒ } t | d d !ƒ } t | d d	 !ƒ } t j | | | | | | ƒ j ƒ  S(
   s$   Get the image acquisition timestamp.i@	  i`	  i   i   i   i   i   i   i   (   R:   t   intt   datetimet	   timetuple(   R#   t	   timestampt   montht   dayt   hourt   minutet   yeart   second(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/FormatTIFFRayonix.pyRP   ×   s    c   
      C   sõ   |  j  t j k r d } n d } t j | |  j d d !ƒ d  } t j | |  j d d !ƒ d  } t j |  j |  j d d	 !ƒ d
 } t j |  j |  j d	 d !ƒ d
 } g  | D] } | d ^ q® } g  | D] } | d ^ qË }	 | |	 | | d f S(   sp  Get the scan angles for: twotheta, omega, chi, kappa, phi, delta,
    gamma. The exact definitions for these are somewhat poorly defined,
    though I presume that they will come from some kind of standard
    goniometer definition... Also returns the scan axis offset and
    the apparent oscillation width, as these are sometimes not properly
    recorded elsewhere.s   <iiiiiiis   >iiiiiiiiœ  i¸  i   i¼  iØ  iÜ  ià  i    iä  gü©ñÒMbP?(   t   _tiff_byte_orderR   R
   R   R   R:   R   (
   R#   t   iiiiiiit   start_anglest
   end_anglest   axis_offsett
   axis_ranget   st   starts_degreest   et   ends_degrees(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/FormatTIFFRayonix.pyR*   å   s    	c         C   sb   |  j  t j k r d } n d } t j | |  j d d !ƒ d  } g  | D] } | d ^ qH } | S(   se   Get the recorded rotx, roty, rotz of the detector - which in most
    cases will probably all be 0.0.s   <iiis   >iiiiä  ið  i   gü©ñÒMbP?(   Rf   R   R
   R   R   R:   (   R#   t   iiit
   rot_anglest   rt   rot_degrees(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/FormatTIFFRayonix.pyR4     s    	c         C   s   |  j  t j k s t ‚ d d l m } d d l m } d d l m	 } t
 |  j ƒ  ƒ d k sf t ‚ |  j ƒ  d j ƒ  } t j |  j ƒ } | j |  j ƒ | | | ƒ t | d | d ƒ ƒ } |  j ƒ  d j ƒ  } | j | j | d | d ƒ ƒ | S(   s^   Get the pixel intensities (i.e. read the image and return as a
       flex array of integers.)iÿÿÿÿ(   t	   streambuf(   t   read_uint16(   t   flexi   i    (   Rf   R   R
   R	   t   boost.pythonRt   t   dxtbxRu   t   scitbx.array_familyRv   R   t   get_detectort   get_image_sizet	   open_fileR'   t   readR    R\   t   reshapet   grid(   R#   Rt   Ru   Rv   t   sizet   ft   raw_dataRB   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/FormatTIFFRayonix.pyt   get_raw_data  s    '!(   t   __name__t
   __module__t   __doc__t   staticmethodR   R"   R%   R2   RF   RI   RV   R5   R6   R7   RM   RP   R*   R4   Rƒ   (    (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/FormatTIFFRayonix.pyR      s    %														t   __main__i   (   t
   __future__R    RN   R]   R   t   dxtbx.format.FormatTIFFR   R   R„   t   syst   argvt   argR   (    (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/FormatTIFFRayonix.pyt   <module>   s   ÿ 