ó
ü™xTc           @   s½   d  d l  m Z d  d l Z d  d l m Z d  d l m Z d  d l m Z m 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   FormatCBFMultiTile(   t   HierarchicalDetector(   t   sqrt   col(   t   Sorryt   FormatCBFMultiTileHierarchyc           B   sh   e  Z d  Z e d „  ƒ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d
 d	 „ Z RS(   s+   An image reading class multi-tile CBF filesc         C   s‰   t  j ƒ  } | j |  t  j ƒ | j ƒ  d k r5 t Sy | j d ƒ | j d ƒ Wn/ t k
 r„ } d t	 | ƒ k r{ t S| ‚ n Xt
 S(   sV   Check to see if this looks like an CBF format image, i.e. we can
    make sense of it.i   t   axist   equipment_componentt   CBF_NOTFOUND(   t   pycbft   cbf_handle_structt   read_widefilet
   MSG_DIGESTt   count_elementst   Falset   find_categoryt   find_columnt	   Exceptiont   strt   True(   t
   image_filet
   cbf_handlet   e(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/FormatCBFMultiTileHierarchy.pyt
   understand   s    
c         C   s)   |  j  | ƒ s t ‚ t j |  | ƒ d S(   s3   Initialise the image structure from the given file.N(   R   t   AssertionErrorR   t   __init__(   t   selfR   (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/FormatCBFMultiTileHierarchy.pyR   -   s    c         C   s   t  j |  ƒ d S(   sc   Parent class will open the image file as a cbf file handle, and keep
    the handle somewhere safe.N(   R   t   _start(   R   (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/FormatCBFMultiTileHierarchy.pyR   6   s    c         C   st  |  j  } | j | ƒ } t | j | ƒ ƒ } t | j | ƒ ƒ j ƒ  } | j | ƒ \ } } d } | d k r÷ | j | | d t	 ƒ}	 t
 |	 d |	 d |	 d | d |	 d |	 d |	 d | d |	 d	 |	 d
 |	 d | d d d d d f ƒ } ny | d k r`| | | | }
 t
 d d d |
 d d d d |
 d d d d |
 d d d d d f ƒ } n t d | ƒ ‚ | S(   s™    Get the 4x4 homogenous coordinate matrix for a given axis.  Assumes
    the cbf handle has been intialized
    @param axis_id axis name of basis to get t   rotationt   degi    i   i   i   i   i   i   i   i   t   translations   Unrecognized vector type: %dN(   t   _cbf_handlet   get_axis_typeR   t   get_axis_offsett   get_axis_vectort	   normalizet   get_axis_settingt   Nonet$   axis_and_angle_as_r3_rotation_matrixR   R   R   (   R   t   axis_idt   cbft	   axis_typet   offsett   vectort   settingt	   incrementt   cobt   r3R   (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/FormatCBFMultiTileHierarchy.pyt   _get_change_of_basis;   s(    	c   	      C   s”   |  j  } |  j | ƒ } | j | ƒ } | d k r= d | f S| j | ƒ } | j | ƒ } | | k rŠ |  j | ƒ \ } } | | | f S| | f S(   s×   Get the 4x4 homogenous coordinate matrix for a given axis, combining it with the change of
    basis matrices of parent axes with the same equipment component as the given axis. Assumes
    the cbf handle has been intialized
    @param axis_id axis name of basis to get
    @return (parent, change of basis matrix), where parent is None if the parent in the cbf file
    is ".".  Parent is the axis that the top level axis in this chain of dependent axis depends on
    t   .N(   R    R1   t   get_axis_depends_onR&   t   get_axis_equipment_componentt    _get_cummulative_change_of_basis(	   R   R(   R)   R/   t	   parent_idt   eq_compt   parent_eq_compt   non_matching_parentt
   parent_cob(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/FormatCBFMultiTileHierarchy.pyR5   Z   s    		
c         C   sm  | d k s t  ‚ | } x0 | j ƒ  j ƒ  D] } | j ƒ  | k r+ | Sq+ W|  j | ƒ \ } } | d k rÃ | j ƒ  } y | j ƒ  Wn+ t k
 r³ } d t | ƒ k sÀ t  ‚ qá Xt	 sá t  ‚ n |  j
 | | ƒ }	 |	 j ƒ  } t | t d ƒ d d !ƒ }
 t | t d ƒ d d !ƒ |
 } t | t d	 ƒ d d !ƒ |
 } | j | j | j |
 j ƒ | j | ƒ | S(
   só    Adds a panel group to the detector d.  If the group's parent hasn't been
    added yet, recursively add parents to the detector until the detector itself
    is reached.
    @param group_id name of a cbf axis
    @param d detector object
    R2   s   DXTBX_ASSERT(D_)i    i   i   N(   i    i    i    i   (   i   i    i    i   (   i    i   i    i   (   R   t	   hierarchyt   iter_preordert   get_nameR5   R&   t   get_D_matrixt   RuntimeErrorR   R   t   _add_panel_groupt	   add_groupR   t   set_local_framet   elemst   set_name(   R   t   group_idt   dt   namet   subobjt   parentR/   t   pgR   t	   parent_pgt   origint   fastt   slow(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/FormatCBFMultiTileHierarchy.pyR@   s   s0    
!!
c         C   s‹  |  j  } t ƒ  } | j d ƒ y | j d ƒ Wn; t k
 rm } d t | ƒ k r] | ‚ n  | j d ƒ n Xg  } xg t | j ƒ  ƒ D]S } | j | ƒ | j	 ƒ  d k r² q‡ n  | j
 ƒ  } | | k r‡ | j | ƒ q‡ q‡ Wg  } xE t t | ƒ ƒ D]1 } | j | ƒ } | j d ƒ }	 | j |	 ƒ q÷ Wg  }
 | j d ƒ xŠ | D]‚ } | j d ƒ | j | ƒ y | j d ƒ Wn; t k
 r´} d t | ƒ k r¤| ‚ n  | j d ƒ n X|
 j | j
 ƒ  ƒ qFWx¸| D]°} | j |
 j | ƒ ƒ } | j d ƒ | j d	 ƒ f } | j d ƒ }	 | j d ƒ } | j |	 ƒ | k sHt ‚ y t | j | ƒ ƒ } Wnä t k
 rG} d t | ƒ k r>d d g } | j d ƒ xt |	 | g D]f } | j d ƒ | j | ƒ | j d
 ƒ t | j
 ƒ  ƒ d } | j d ƒ t | j
 ƒ  ƒ | | <q«W| d d k r5| d d k sDt ‚ qH| ‚ n X|  j |	 ƒ \ } } |  j | | ƒ } | j | j ƒ  ƒ } | j |	 ƒ } | j | ƒ } | t d ƒ d d !} | j | | | ƒ yE | j d ƒ | j d ƒ | j ƒ  } | j d ƒ } | | f } Wn d } n X| j t t  t! | ƒ ƒ ƒ | j" | ƒ | j# t t  t! | ƒ ƒ ƒ | j$ | ƒ | j% | ƒ ~ qÓW| S(   s#   Return a working detector instance.t   array_structure_listt   array_section_idR	   t   array_idt   nulli    t   axis_set_idi   i   t
   precedencet	   dimensioni   t   array_intensitiest   undefined_valueg        (   i    i    i    i   (   g        g        (&   R    R   R   R   R   R   t   xranget
   count_rowst
   select_rowt   get_typeofvaluet	   get_valuet   appendt   lent   construct_detectort   get_detector_surface_axest   find_rowt   indext   get_inferred_pixel_sizeR3   R   t   tuplet   get_image_size_fst   intR5   R@   t	   add_panelR#   R   RB   t   get_doublevaluet   get_overloadt   set_pixel_sizet   mapt   floatt   set_image_sizet   set_trusted_rangeRD   t   __swig_destroy__(   R   R)   RF   R   t   panel_namest   it   valt   detector_axest   cbf_detectort   axis0t   panel_names_detectorordert   detector_axist
   panel_namet   pixelt   axis1t   sizeR   t   idxRI   R/   RJ   t   pRM   RN   RL   t	   underloadt   overloadt   trusted_range(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/FormatCBFMultiTileHierarchy.pyt	   _detector    sœ    		 	 	)

c         C   s   |  j  j |  j ƒ S(   s   Return a working beam instance.(   t   _beam_factoryt   imgCIF_HR    (   R   (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/FormatCBFMultiTileHierarchy.pyt   _beam  s    c         C   s¨  |  j  d  k r†d d  l } d d l m } g  |  _  |  j } | j d ƒ | j d ƒ | j d ƒ g  } x7 t	 | j
 ƒ  ƒ D]# } | j | j ƒ  ƒ | j ƒ  q} Wt | ƒ | j
 ƒ  k sÂ t ‚ i  } | j d ƒ xWt	 | j
 ƒ  ƒ D]C} | j d ƒ | j ƒ  } | j d ƒ | j ƒ  j d	 ƒ d k s5t ‚ | | d
 k r™| j ƒ  }	 | j | j |	 | j ƒ ƒ }
 | j ƒ  } | d | d | d f } nh | | d k rý| j ƒ  }	 | j | j |	 | j ƒ ƒ }
 | j ƒ  } | d | d | d f } n d  S|
 j | j | Œ  ƒ |
 | | <| j ƒ  qè W| j ƒ  ròi  } x&t	 | j
 ƒ  ƒ D]} | j d ƒ | j ƒ  } | | k rŒi  | | <n  | j d ƒ d | | k rÀ| j ƒ  | | d <n  | | d | j ƒ  k sàt ‚ | j d ƒ t | j ƒ  ƒ d } | j d ƒ t | j ƒ  ƒ d } | j d ƒ t | j ƒ  ƒ } t | | ƒ | | | <| j ƒ  qTWxé t | ƒ D]t } | | } | | d | d | d | d f } | j | j | j  ƒ  d | j  ƒ  d ƒ ƒ |  j  j | ƒ qwWnd xa t | ƒ D]S } | | j | j | | j  ƒ  d | | j  ƒ  d ƒ ƒ |  j  j | | ƒ qÿW|  j! ƒ  } t | ƒ t |  j  ƒ k s†t ‚ n  | d  k	 r�|  j  | S|  j  d S(   Niÿÿÿÿ(   t   flext   array_structuret   encoding_typei    t
   array_dataRQ   t   datat   bnrys   signed 32-bit integeri   i
   i	   s   signed 64-bit real IEEEi   i   i   t   idRb   i   t   startt   endi   iþÿÿÿ("   t	   _raw_dataR&   t   numpyt   scitbx.array_familyR…   R    R   R   RZ   RX   RY   R]   R\   t   next_rowR^   R   R[   t   findt   get_integerarray_as_stringRf   t
   fromstringt   int32t#   get_integerarrayparameters_wdims_fst   get_realarray_as_stringt   doubleRl   t    get_realarrayparameters_wdims_fst   reshapet   gridt   has_sectionst   slicet   sortedt   focust   get_detector(   R   Rb   R�   R…   R)   t   typesRq   R‰   RG   t   array_stringt   arrayt
   parameterst
   array_sizet   section_shapest   section_namet
   axis_indext
   axis_startt   axis_endt   section_shapet   sectiont   keyRF   (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/FormatCBFMultiTileHierarchy.pyt   get_raw_data  s†    		!
    
-9$N(   t   __name__t
   __module__t   __doc__t   staticmethodR   R   R   R1   R5   R@   R�   R„   R&   R®   (    (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/FormatCBFMultiTileHierarchy.pyR      s   						-	l	t   __main__i   (   t
   __future__R    R
   R   t   dxtbx.model.detectorR   t   scitbx.matrixR   R   t   libtbx.utilsR   R   R¯   t   syst   argvt   argR   (    (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/FormatCBFMultiTileHierarchy.pyt   <module>
   s   ÿ Q