ó
y™xTc           @   s.  d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l	 Z	 d  d l
 m Z m Z d  d l m Z d d d „  ƒ  YZ d „  Z d „  Z d	 d d
 „  ƒ  YZ d „  Z d  d l m Z d e f d „  ƒ  YZ d „  Z e d k r*e j d Z x e D] Z e e ƒ qWd GHn  d S(   iÿÿÿÿ(   t   divisionN(   t
   uncompresst   assert_equal(   t   DetectorImageBaset   cif_binary_sectionc           B   sƒ   e  Z d  Z d Z e e e Z e e d e Z e e Z e d ƒ e d ƒ e d ƒ e d ƒ Z d „  Z	 d „  Z
 d	 „  Z RS(
   s   
s   --CIF-BINARY-FORMAT-SECTION--s   --i   i   i   iÕ   c         C   sÆ  | t  |  j ƒ } | } | |  _ | |  _ | j |  j | | ƒ } | d k sU t ‚ | | | !} t j j	 ƒ  j
 | ƒ |  _ |  j d j d ƒ d k sŸ t ‚ | t  |  j ƒ } | | | d !|  j k sÒ t ‚ | | d | !|  _ d |  _ t j ƒ  }	 |	 j |  j ƒ t j |	 j ƒ  ƒ }
 |  j d |
 k s9t ‚ t |  j d ƒ |  _ t |  j d ƒ |  _ t |  j d	 ƒ } t |  j d
 ƒ } | |  j |  j k s§t ‚ | t  |  j ƒ k sÂt ‚ |  S(   Ni    s   Content-Types   x-CBF_BYTE_OFFSETi   t
   compresseds   Content-MD5s   X-Binary-Size-Fastest-Dimensions   X-Binary-Size-Second-Dimensions   X-Binary-Number-of-Elementss   X-Binary-Size(   t   lent   init_boundaryt   initt   finalt   findt   binary_string_separatort   AssertionErrort   emailt   parsert   Parsert   parsestrt
   header_dict   cbf_signaturet   datat	   data_typet   hashlibt   md5t   updatet   base64t	   b64encodet   digestt   intt	   size_fastt	   size_slow(   t   selfR   R   R	   t   section_startt   section_stopt   dividet   headert	   bin_startt   mt   derived_digestt   total_elementst   compressed_size(    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/tst_py_cbf.pyt   from_compressed_string   s0    		" 	c         C   sd   |  j  d k rH t d |  j d |  j d |  j ƒ } | |  _ d |  _  n  |  j  d k s] t ‚ |  j S(   NR   t   packedt   fastt   slowt   uncompressed(   R   R   R   R   R   R   (   R   t   decompressed_data(    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/tst_py_cbf.pyt   uncompress_in_place0   s    $	c         C   s   |  j  GHd  S(   N(   R   (   R   (    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/tst_py_cbf.pyt   show8   s    (   t   __name__t
   __module__t   endlinet   patternR   t   final_boundaryR   t   chrR   R(   R.   R/   (    (    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/tst_py_cbf.pyR      s   
*	!	c         C   sé   g  } d } t  |  ƒ } d } d } x¾ | | k  rä |  j t j | d ƒ } | d k r\ Pn  | | k sn t ‚ |  j t j | ƒ } | | k s• t ‚ | j t ƒ  j |  | | ƒ ƒ t j | ƒ } t j | ƒ } t j | ƒ } q' W| S(   Ni    i   iÿÿÿÿ(	   R   R
   R   R   R   R4   t   appendR(   t   copy(   t   rawt   return_sectionst   ptrt   endt   oldinitt   oldfinalR   R	   (    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/tst_py_cbf.pyt   get_binary_sections;   s"     c         C   s¶   d t  |  ƒ f g } xl t | ƒ D]^ \ } } | | d | j f } | j t  t j ƒ | | d f } | | | <| j | ƒ q" Wt j j g  | D] } |  | d | d !^ q” ƒ S(   Ni    i   (	   R   t	   enumerateR   R	   R   R4   R6   R2   t   join(   R8   t   binary_sectionst   provisional_slicest   it   sectiont   firstt   lastt   s(    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/tst_py_cbf.pyt   get_header_sectionsQ   s    $
t
   Goniometerc           B   s   e  Z d  „  Z RS(   c         C   s”   | d } g  | d D] } t  | ƒ ^ q j | ƒ } t | d | ƒ |  _ g  | d D] } t  | ƒ ^ qX j | ƒ } t | d | ƒ |  _ d  S(   Ns    _diffrn_measurement_axis.axis_ids   _diffrn_scan_frame_axis.axis_ids   _diffrn_scan_frame_axis.angles   _diffrn_scan_axis.axis_ids!   _diffrn_scan_axis.angle_increment(   t   strt   indext   floatt	   osc_startt	   osc_range(   R   t   modelt   axis_idt   at   row_idx(    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/tst_py_cbf.pyt   __init__[   s
    
,,(   R0   R1   RS   (    (    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/tst_py_cbf.pyRI   Z   s   c         C   s_   g  |  d D] } t  | ƒ ^ q j d ƒ } t |  d | ƒ } t |  d | ƒ } | | f S(   Ns   _axis.idt	   ELEMENT_Xs   _axis.offset[2]s   _axis.offset[1](   RJ   RK   RL   (   RO   RQ   RR   t   beamxt   beamy(    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/tst_py_cbf.pyt   get_ad_hoc_beamf   s    ,(   t   CBFImaget
   pyCBFImagec           B   s5   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   s¯   t  j |  | ƒ t | d ƒ j ƒ  } t | ƒ |  _ t | |  j ƒ |  _ t |  j ƒ d k sd t	 ‚ t
 j j d |  j ƒ } | j ƒ  |  _ |  j d } d |  _ |  j ƒ  d  S(   Nt   rbi   t   input_stringt   image_1t   CBF(   R   RS   t   opent   readR>   RA   RH   t   header_sectionsR   R   t   iotbxt   cift   fast_readerRO   t	   cif_modelt
   vendortypet
   readHeader(   R   t	   file_nameR8   Rb   t   im1(    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/tst_py_cbf.pyRS   n   s    	c         C   sÚ   |  j  d } |  j } t | ƒ } t | ƒ } i | d j d 6| d j d 6t | d ƒ d 6|  j | ƒ d 6| j d 6t	 | d	 ƒ d
 6|  j
 | ƒ d 6| d d 6| d d 6| j d 6d d 6d d 6|  _ | |  _ d  S(   NR\   i    t   SIZE1t   SIZE2s   _array_intensities.overloadt   CCD_IMAGE_SATURATIONt
   PIXEL_SIZEt	   OSC_STARTs,   _diffrn_measurement.sample_detector_distancet   DISTANCEt
   WAVELENGTHt   BEAM_CENTER_Xi   t   BEAM_CENTER_Yt	   OSC_RANGEg        t   TWOTHETAt   DETECTOR_SN(   Rd   RA   RI   RW   R   R   R   t   cbf_simple_py_get_pixel_sizeRM   RL   t   cbf_simple_py_get_wavelengthRN   t
   parameterst   binaries(   R   RO   Rx   t
   goniometert   ad_hoc_beam(    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/tst_py_cbf.pyRf   }   s"    	

c         C   s5   t  |  j ƒ d k s t ‚ |  j d j ƒ  |  _ d  S(   Ni   i    (   R   Rx   R   R.   t   linearintdata(   R   (    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/tst_py_cbf.pyR_   •   s    c         C   sD  d } d } | d } | d | k } g  | d D] } t  | ƒ ^ q1 } x1 t t | ƒ ƒ D] } | | s\ d | | <q\ q\ W| j | ƒ }	 t  | d |	 ƒ }
 |
 d k s² t ‚ | d | k } g  | d D] } t  | ƒ ^ qÍ } x1 t t | ƒ ƒ D] } | | sø d | | <qø qø W| j |
 ƒ } d	 t | d
 | ƒ } | S(   Ni    i   s   _diffrn_data_frame.array_ids   _array_structure_list.array_ids    _array_structure_list.precedences   _array_structure_list.indexs   _array_element_size.array_ids   _array_element_size.indexg     @�@s   _array_element_size.size(   R   t   xrangeR   RK   R   RL   (   R   RO   t   element_numbert   axis_numbert   array_idt
   array_maskRQ   t
   precedenceRC   t   idxt
   axis_indext   index_arrayt
   size_indext
   pixel_size(    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/tst_py_cbf.pyRu   ™   s&    
#
 #
 c         C   s   t  | d ƒ S(   Ns'   _diffrn_radiation_wavelength.wavelength(   RL   (   R   RO   (    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/tst_py_cbf.pyRv   ³   s    (   R0   R1   RS   Rf   R_   Ru   Rv   (    (    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/tst_py_cbf.pyRY   m   s
   				c         C   s[  d d l  m } t |  ƒ } | j ƒ  t |  ƒ } | j ƒ  t | j | j ƒ | j | j k sg t ‚ | j	 | j	 k s t ‚ | | j
 | j
 ƒ sš t ‚ | | j | j ƒ sµ t ‚ | | j | j ƒ sÐ t ‚ | | j | j ƒ së t ‚ | | j | j ƒ st ‚ | | j | j ƒ s!t ‚ | | j | j ƒ s<t ‚ | | j | j ƒ sWt ‚ d  S(   Niÿÿÿÿ(   t   approx_equal(   t   libtbx.test_utilsR‡   RY   R_   RX   R   R{   t   size1R   t   size2t
   saturationR†   RM   t   deltaphit
   wavelengtht   distanceRU   RV   (   Rg   R‡   t   py_image_objt   c_image_obj(    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/tst_py_cbf.pyt   run¶   s     

t   __main__i   t   OK(    (    (   t
   __future__R    t	   iotbx.cifRa   t   syst   email.parserR   R7   R   R   t   cbflib_adaptbxR   R   t   iotbx.detectors.detectorbaseR   R   R>   RH   RI   RW   t   iotbx.detectors.cbfRX   RY   R‘   R0   t   argvt   argsRg   (    (    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/tst_py_cbf.pyt   <module>   s"   <4				I	