ó
÷™xTc           @   s&  d  d l  m Z d  d l Z d  d l Z d  d l m Z d  d l m Z m Z m	 Z	 d  d l m
 Z
 m Z d  d l m Z d  d l m Z d e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d „  Z d „  Z e e	 _ e e	 _ d e	 _ d e	 f d „  ƒ  YZ d d d „  ƒ  YZ d S(   iÿÿÿÿ(   t   divisionN(   t   matrix(   t   Panelt   VirtualPanelt   Detector(   t   SimplePxMmStrategyt   ParallaxCorrectedPxMmStrategy(   t   detector_helper_sensors(   t   find_undefined_valuet   PanelTreeNodec           B   s‰   e  Z d  Z d d 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(   s’    A class to wrap a virtual panel object and project methods for setting
  parent and local coordinate frames for the hierarchical detector model. c         C   s.   | d k r t ƒ  } n  | |  _ | |  _ d S(   s6    If no node is set, then create a virtual panel node. N(   t   NoneR   t   _nodet   _parent(   t   selft   nodet   parent(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyt   __init__   s    	c         C   s   t  |  j | ƒ S(   s&    Inherit the interface from the node. (   t   getattrR   (   R   t   attr(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyt   __getattr__"   s    c         C   s   |  j  S(   s    Return the parent. (   R   (   R   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyR   &   s    c         C   s   |  j  r |  j  j ƒ  S|  S(   s    Return the root. (   R   t   root(   R   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyR   *   s    	c         C   sa   d d l  m } | j |  j ƒ  ƒ } | | j ƒ  } |  j | d d !| d d !| d d !ƒ d	 S(
   s/    Apply a transformation to the current matrix. iÿÿÿÿ(   R   i    i   i   i   i   i   N(   t   scitbxR   t   sqrt   get_transformation_matrixt	   transposet	   set_frame(   R   t   tR   t   tgt   tgt(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyt   apply_transformation0   s    c   	      C   s  d d l  m } |  j ƒ  d
 k r8 |  j | | | ƒ n  | j | ƒ j ƒ  } | j | ƒ j ƒ  } | j | ƒ } | j |  j ƒ  j	 ƒ  ƒ } | j | j
 d | j
 d | j
 d t | ƒ d ƒ j ƒ  } | | j ƒ  } |  j | d d !| d d !| d d	 !ƒ d
 S(   s    Set the local frame. iÿÿÿÿ(   R   i    i   i   i   i   i   i   N(   i    (   i    (   i    (   i   (   R   R   R   R
   R   t   colt	   normalizet   crossR   R   t   elemst   tupleR   (	   R   t	   fast_axist	   slow_axist   originR   t   normalt   tpt   tlR   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyt   set_local_frame7   s    :c         C   st   d d l  m } |  j ƒ  d	 k r, |  j ƒ  S| j |  j ƒ  ƒ j ƒ  } | j | d d !| d d !| d d !ƒ j S(
   s    Get the local d matrix. iÿÿÿÿ(   R   i    i   i   i   i   i   N(	   R   R   R   R
   t   get_d_matrixR   t   get_local_transformation_matrixR   R!   (   R   R   R(   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyt   get_local_d_matrixR   s
    
c         C   sZ   d d l  m } | j |  j ƒ  d |  j ƒ  d |  j ƒ  d |  j ƒ  d ƒ j ƒ  j S(	   s     Get the transformation matrix. iÿÿÿÿ(   R   i    i   (   i    (   i    (   i    (   i   (	   R   R   R   t   get_fast_axist   get_slow_axist
   get_normalt
   get_originR   R!   (   R   R   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyR   Z   s    c         C   sm   d d l  m } |  j ƒ  d k r, |  j ƒ  S| j |  j ƒ  j ƒ  ƒ } | j |  j ƒ  ƒ } | j ƒ  | j S(   s&    Get the local transformation matrix. iÿÿÿÿ(   R   N(   R   R   R   R
   R   R   t   inverseR!   (   R   R   R'   R   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyR+   c   s    
c         C   s6   d d l  m } | j |  j ƒ  ƒ j ƒ  } | d d !S(   s!    Get the local fast axis vector. iÿÿÿÿ(   R   i    i   (   R   R   R   R+   R   (   R   R   t   dl(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyt   get_local_fast_axisl   s    c         C   s6   d d l  m } | j |  j ƒ  ƒ j ƒ  } | d d !S(   s!    Get the local slow axis vector. iÿÿÿÿ(   R   i   i   (   R   R   R   R+   R   (   R   R   R2   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyt   get_local_slow_axisr   s    c         C   s6   d d l  m } | j |  j ƒ  ƒ j ƒ  } | d d !S(   s    Get the local origin vector. iÿÿÿÿ(   R   i   i   (   R   R   R   R+   R   (   R   R   R2   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyt   get_local_originx   s    c         C   s6   d d l  m } | j |  j ƒ  ƒ j ƒ  } | d d !S(   s    Get the local normal vector. iÿÿÿÿ(   R   i   i   (   R   R   R   R+   R   (   R   R   R2   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyt   get_local_normal~   s    N(   t   __name__t
   __module__t   __doc__R
   R   R   R   R   R   R)   R,   R   R+   R3   R4   R5   R6   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyR	      s   													t
   PanelGroupc           B   s¼   e  Z d  Z d 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 d „  Z d „  Z d „  Z d „  Z RS(   sŽ   A class providing an iterface to a group of panels.

  This class is the basis for the construction of a detector hierarchy. The
  class inherits from VirtualPanel which has a C++ implementation providing
  the methods to manipulate the virtual detector plane. This class holds
  a reference to a list of children and allows for propagating the panel
  coordinate frames through the hierarchy.

  c         C   s#   t  j |  ƒ | |  _ g  |  _ d S(   s3    Initialise the list of children to an empty list. N(   R   R   R   t	   _children(   R   R   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyR   �   s    	c         C   sP   t  j |  | | | ƒ x3 |  D]+ } | j |  j ƒ  |  j ƒ  |  j ƒ  ƒ q Wd S(   s]   Set the parent frame.

    Sets its own parent plane and then, after updating its global
    frame, propagates the frame down to its children.

    :param fast_axis: The fast axis of the virtual detector plane
    :param slow_axis: The slow axis of the virtual detector plane
    :param origin: The origin vector to the virtual detector plane

    N(   R   t   set_parent_frameR-   R.   R0   (   R   R#   R$   R%   t   child(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyR<   •   s    		c         C   sP   t  j |  | | | ƒ x3 |  D]+ } | j |  j ƒ  |  j ƒ  |  j ƒ  ƒ q Wd S(   s[   Set the local frame.

    Sets its own local plane and then, after updating its global
    frame, propagates the frame down to its children.

    :param fast_axis: The fast axis of the virtual detector plane
    :param slow_axis: The slow axis of the virtual detector plane
    :param origin: The origin vector to the virtual detector plane

    N(   R   R)   R<   R-   R.   R0   (   R   R#   R$   R%   R=   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyR)   §   s    		c         C   sP   t  j |  | | | ƒ x3 |  D]+ } | j |  j ƒ  |  j ƒ  |  j ƒ  ƒ q Wd S(   s[   Set the local frame.

    Sets its own global plane and then, after updating its local
    frame, propagates the frame down to its children.

    :param fast_axis: The fast axis of the virtual detector plane
    :param slow_axis: The slow axis of the virtual detector plane
    :param origin: The origin vector to the virtual detector plane

    N(   R   R   R<   R-   R.   R0   (   R   R#   R$   R%   R=   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyR   ¹   s    		c         C   sE   t  |  ƒ } | j |  j ƒ  |  j ƒ  |  j ƒ  ƒ |  j j | ƒ | S(   s]    Add a new group to this group.

    :returns: A new panel group
    :rtype: PanelGroup

    (   R:   R<   R-   R.   R0   R;   t   append(   R   t   group(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyt	   add_groupË   s    		c         C   s—   t  | t ƒ s t ‚ t | d ƒ s: | j d k s: t ‚ | |  j ƒ  j k sU t ‚ |  | _ | j |  j	 ƒ  |  j
 ƒ  |  j ƒ  ƒ |  j j | ƒ | S(   sR    Add a new panel to this group.

    :returns: A new panel
    :rtype: Panel

    R   N(   t
   isinstanceR   t   AssertionErrort   hasattrR   R
   R   t
   _containerR<   R-   R.   R0   R;   R>   (   R   t   panel(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyt	   add_panelÚ   s    %			c         C   s   |  j  | S(   sZ    Get the child at the given index.

    :param index: The index of the child to get.

    (   R;   (   R   t   index(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyt   __getitem__ì   s    c         C   s   |  j  j | ƒ S(   s    Remove a child from the tree. (   R;   t   remove(   R   t   item(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyRI   ô   s    c         C   s   |  j  j | ƒ S(   s    Get the index of a child. (   R;   RG   (   R   RJ   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyRG   ø   s    c         C   s   |  j  S(   s    Return the parent. (   R   (   R   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyR   ü   s    c         C   s   |  j  r |  j  j ƒ  S|  S(   s    Return the root. (   R   R   (   R   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyR      s    	c         C   s   t  |  j ƒ S(   s*    Return an iterator to the list children. (   t   iterR;   (   R   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyt   children  s    c         C   s   t  |  j ƒ S(   s4    Return a reverse iterator to the list of children. (   t   reversedR;   (   R   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyt   reverse
  s    c         C   s
   |  j  ƒ  S(   s    Iterate through the children. (   RL   (   R   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyt   __iter__  s    c         C   s   t  |  j ƒ S(   s)    Get the length of the list of children. (   t   lenR;   (   R   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyt   __len__  s    c         C   sS   t  j |  | ƒ rK t |  ƒ t | ƒ k r. t St d „  t |  | ƒ Dƒ ƒ St Sd S(   s,    Check that this is equal to another group. c         s   s!   |  ] \ } } | | k Vq d  S(   N(    (   t   .0t   at   b(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pys	   <genexpr>  s    N(   R   t   __eq__RP   t   Falset   allt   zip(   R   t   other(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyRU     s
    c         C   s   |  j  | ƒ S(   s0    Check that this is not equal to another group. (   RU   (   R   RY   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyt   __ne__  s    c         C   s9   t  |  ƒ t  | ƒ k r t St d „  t |  | ƒ Dƒ ƒ S(   s$    Check is the hierarchy is similar. c         s   s$   |  ] \ } } | j  | ƒ Vq d  S(   N(   t   is_similar_to(   RR   RS   RT   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pys	   <genexpr>'  s    (   RP   RV   RW   RX   (   R   RY   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyR[   #  s    c         C   sÇ   t  j |  ƒ } g  } x¡ |  j D]– } t | t ƒ r¢ d } x< t |  j ƒ  j ƒ D]% \ } } | j	 | ƒ rP | } PqP qP W| d k	 s‹ t
 ‚ | j i | d 6ƒ q | j | j ƒ  ƒ q W| | d <| S(   s*    Convert the panel group to a dictionary. RE   RL   N(   R   t   to_dictR;   RA   R   R
   t	   enumerateR   RD   t   is_RB   R>   (   R   t   dRL   t   ct   idxt   it   p(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyR\   )  s    "
N(   R7   R8   R9   R
   R   R<   R)   R   R@   RF   RH   RI   RG   R   R   RL   RN   RO   RQ   RU   RZ   R[   R\   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyR:   …   s(   																			t   PanelGroupRootc           B   s5   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z RS(   s­    The top level Detector model.

  This class is derived from the panel group class but provides some
  additional convenience methods for navigating the panel hierarchy.

  c         C   s    t  t |  ƒ j ƒ  | |  _ d  S(   N(   t   superRd   R   RD   (   R   t	   container(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyR   C  s    c         c   s2   x+ |  j  ƒ  D] } t | t ƒ r | Vq q Wd S(   s.    Iterate through just the panels depth-first. N(   t   iter_preorderRA   R   (   R   t   obj(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyt   iter_panelsG  s    c         c   sm   |  g } x] t  | ƒ d k rh | j ƒ  } | Vt | t ƒ r x$ | j ƒ  D] } | j | ƒ qK Wq q Wd S(   s4    Iterate through the groups and panels depth-first. i    N(   RP   t   popRA   R:   RN   R>   (   R   t   stackR   R=   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyRg   M  s    	c         c   s}   d d l  m } | |  g ƒ } xW t | ƒ d k rx | j ƒ  } | Vt | t ƒ r" x | D] } | j | ƒ q[ Wq" q" Wd S(   s4    Iterate through the groups and panels depth-first. iÿÿÿÿ(   t   dequei    N(   t   collectionsRl   RP   t   popleftRA   R:   R>   (   R   Rl   t   queueR   R=   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyt   iter_levelorderW  s    N(   R7   R8   R9   R
   R   Ri   Rg   Rp   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyRd   <  s
   		
c         C   s)   d } | |  j  g  |  D] } | ^ q f S(   s    Get the state for pickling. i   (   t   __dict__(   R   t   versionRc   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyt   detector__getstate__c  s    c         C   sh   t  | ƒ d k s t ‚ | d d k s. t ‚ |  j j | d ƒ x | d D] } |  j | ƒ qM Wd S(   s    Set the state from pickling. i   i    i   i   N(   RP   RB   Rq   t   updateRF   (   R   t   stateRc   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyt   detector__setstate__h  s
    i   t   HierarchicalDetectorc           B   s}   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 e
 d „  ƒ Z e
 d „  ƒ Z d	 „  Z d
 „  Z d Z RS(   s    The detector class. c         C   sK   | d k r" t t |  ƒ j ƒ  n t t |  ƒ j | ƒ t |  ƒ |  _ d S(   s    Construct the detector. N(   R
   Re   Rw   R   Rd   t   _root(   R   RE   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyR   x  s    c         C   s   |  j  S(   s    Return the hierarchy. (   Rx   (   R   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyt	   hierarchy€  s    c         C   s>   t  | t ƒ s t S|  j j | j ƒ o= t t |  ƒ j | ƒ S(   so    Check if the detectors are similar (i.e. only differ in terms of
    things that could change per experiment. (   RA   Rw   RV   Rx   R[   Re   (   R   t   rhs(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyR[   „  s    c         C   s;   t  | t ƒ s t S|  j | j k o: t t |  ƒ j | ƒ S(   s,    Check that this is equal to another group. (   RA   Rw   RV   Rx   Re   RU   (   R   Rz   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyRU   Œ  s    c         C   s   |  j  | ƒ S(   s0    Check that this is not equal to another group. (   RU   (   R   RY   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyRZ   “  s    c         C   s&   t  j |  ƒ } |  j j ƒ  | d <| S(   s#    Return the class as a dictionary. Ry   (   R   R\   Rx   (   R   R_   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyR\   —  s    c         C   sg   t  ƒ  } |  d } x$ | D] } | j t j | ƒ ƒ q Wd |  k rc t  j |  d | j ƒ  ƒ n  | S(   s+    Convert a dictionary to a detector model. t   panelsRy   (   Rw   RF   R   t	   from_dictt   _group_or_panel_from_dictRy   (   R_   Rh   R{   Rc   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyR|   �  s    	
c         C   s¼   t  j |  ƒ } | j | j ƒ  ƒ | j | j ƒ  ƒ | j | j ƒ  | j ƒ  | j	 ƒ  ƒ x[ |  d D]O } d | k rž | d } | j
 | j ƒ  j | ƒ qe t j | | j ƒ  ƒ qe Wd S(   s<    Convert a dictionary to a group or panel and add to model. RL   RE   N(   R   R|   t   set_typet   get_typet   set_namet   get_nameR)   R3   R4   R5   RF   R   RD   Rw   R}   R@   (   R_   Rh   t   baseR=   RG   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyR}   ©  s    		
c         C   sv   d } g  } xW |  D]O } | } x3 |  j  ƒ  j ƒ  D] } | j | ƒ r2 | } Pq2 q2 W| j | ƒ q W| |  j | f S(   s    Get the state for pickling. i   (   Ry   Ri   R^   R>   Rq   (   R   Rr   t   panels_listt   p1t   p3t   p2(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyt   __getstate__º  s    c            s–   t  | ƒ d k s t ‚ | d d k s. t ‚ ˆ  j j | d ƒ x | d D] } ˆ  j | ƒ qM Wt ‡  f d †  ˆ  j ƒ  j ƒ  Dƒ ƒ s’ t ‚ d S(   s    Set the state from pickling. i   i    i   i   c         3   s   |  ] } | ˆ  k Vq d  S(   N(    (   RR   Rc   (   R   (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pys	   <genexpr>Î  s    N(   RP   RB   Rq   Rt   t   add_panel_by_pointerRW   Ry   Ri   (   R   Ru   Rc   (    (   R   sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyt   __setstate__Ç  s    i   N(   R7   R8   R9   R
   R   Ry   R[   RU   RZ   R\   t   staticmethodR|   R}   R‡   R‰   t   __getstate_manages_dict__(    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyRw   v  s   							
t   detector_factoryc           B   s¡   e  Z d  Z d „  Z e d d d d „ ƒ Z e d g  d d „ ƒ Z e d g  d d „ ƒ Z e d d d „ ƒ Z	 e d „  ƒ Z
 e d	 „  ƒ Z e d
 „  ƒ Z RS(   si  A factory class for detector objects, which will encapsulate standard
  detector designs to make it a little easier to get started with these. In
  cases where a CBF image is provided a full description can be used, in
  other cases assumptions will be made about the experiment configuration.
  In all cases information is provided in the CBF coordinate frame.c         C   s   d  S(   N(    (   R   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyR   Ú  s    g        R   c	         C   s  | d k r t ƒ  } n  yà t ƒ  }	 |	 j ƒ  }
 |
 j t |  ƒ ƒ |
 j t | ƒ ƒ |
 j t t	 t
 | ƒ ƒ t t	 t
 | ƒ ƒ t t	 t
 | ƒ ƒ ƒ |
 j t t	 t
 | ƒ ƒ ƒ |
 j t t	 t | ƒ ƒ ƒ |
 j t t	 t
 | ƒ ƒ ƒ |
 j | ƒ Wn t k
 r} | GH| ‚ n X|	 S(   s@   Ensure all types are correct before creating c++ detector class.N(   R
   R   R   RF   R~   t   strR€   R)   R"   t   mapt   floatt   set_pixel_sizet   set_image_sizet   intt   set_trusted_ranget   set_px_mm_strategyt	   Exception(   t   stypeR#   R$   R%   t
   pixel_sizet
   image_sizet   trusted_ranget   px_mmt   nameR_   Rc   t   e(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyt   make_detectorÝ  s&    	
c
      	   C   sï   | d k s t  ‚ | d k s$ t  ‚ | d | d k s> t  ‚ i d d 6d d 6d d 6d d 6}
 t j |
 | ƒ } t j |
 | ƒ } t j d ƒ | | | d	 | | d } t j t j |  ƒ | | | | | | |	 ƒ } | | d	 _ | S(   sÎ  Construct a simple detector at a given distance from the sample
    along the direct beam presumed to be aligned with -z, offset by the
    beam centre - the directions of which are given by the fast and slow
    directions, which are themselves given as +x, +y, -x, -y. The pixel
    size is given in mm in the fast and slow directions and the image size
    is given in pixels in the same order. Everything else is the same as
    for the main reference frame.s   -xs   +ys   +xs   -yi   g      ð?g        g      ð¿i    iÿÿÿÿ(   s   -xs   +ys   +xs   -y(   s   -xs   +ys   +xs   -y(   g      ð?g        g        (   g      ð¿g        g        (   g        g      ð?g        (   g        g      ð¿g        (   i    i    iÿÿÿÿ(   RB   R   R   RŒ   R�   t   sensort   mask(   Rž   t   distancet   beam_centret   fast_directiont   slow_directionR—   R˜   R™   RŸ   Rš   t   direction_mapt   fastt   slowR%   t   detector(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyt   simple÷  s     
+c      	   C   s1  | d k s t  ‚ | d k s$ t  ‚ | d k s6 t  ‚ | d | d k sP t  ‚ i d d 6d d 6d d 6d d 6} t j | | ƒ } t j | | ƒ } t j d ƒ | | | d	 | | d } t j | | ƒ } | j | d t ƒ} t j t j |  ƒ | | | | | | | | |	 | ƒ } |
 | _ | S(   sB  Construct a simple detector at a given distance from the sample
    along the direct beam presumed to be aligned with -z, offset by the
    beam centre - the directions of which are given by the fast and slow
    directions, which are themselves given as +x, +y, -x, -y. The pixel
    size is given in mm in the fast and slow directions and the image size
    is given in pixels in the same order. Everything else is the same as
    for the main reference frame. Also given are the direction of the
    two-theta axis and the angle in degrees by which the detector is
    moved.s   -xs   +ys   +xs   -yi   g      ð?g        g      ð¿i    iÿÿÿÿt   deg(   s   -xs   +ys   +xs   -y(   s   -xs   +ys   +xs   -y(   s   -xs   +ys   +xs   -y(   g      ð?g        g        (   g      ð¿g        g        (   g        g      ð?g        (   g        g      ð¿g        (   i    i    iÿÿÿÿ(	   RB   R   R   t$   axis_and_angle_as_r3_rotation_matrixt   TrueRŒ   R�   Rž   RŸ   (   Rž   R    R¡   R¢   R£   t   two_theta_directiont   two_theta_angleR—   R˜   R™   RŸ   Rš   R¤   R¥   R¦   R%   t	   two_thetat   RR§   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyR®     s*    
+		c      	   C   s£   t  | ƒ d k s t ‚ t  | ƒ d k s0 t ‚ t  | ƒ d k sH t ‚ t  | ƒ d k s` t ‚ t  | ƒ d k sx t ‚ t j t j |  ƒ | | | | | | | ƒ S(   s-  A complex detector model, where you know exactly where everything
    is. This is useful for implementation of the Rigaku Saturn header
    format, as that is exactly what is in there. Origin, fast and slow are
    vectors in the CBF reference frame, pixel is the dimensions as a tuple
    as is size.i   i   (   RP   RB   RŒ   R�   Rž   (   Rž   R%   R¥   R¦   t   pixelt   sizeR™   Rš   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyt   complexJ  s    	c      	   C   sL  t  j ƒ  } | j |  t  j ƒ | j d ƒ } | j d ƒ | j d ƒ f } t | j d d ƒ ƒ } | j d d ƒ } | j d d ƒ } g  t d ƒ D] } | | | | ^ q• }	 g  t d ƒ D] } | | | | ^ qÀ }
 t	 j
 t g  t d ƒ D] } |	 | |	 | ^ qô ƒ ƒ } t	 j
 t g  t d ƒ D] } |
 | |
 | ^ q.ƒ ƒ } t g  t d ƒ D] } |	 | | ^ qbƒ } t g  t d ƒ D] } |
 | | ^ q�ƒ } t t | j d ƒ ƒ ƒ } y+ t | ƒ } | j d ƒ } | | f } Wn d } n X| j | ƒ ~ t j | ƒ } t ƒ  } t j | | | | | | | | ƒ S(   s0   Initialize a detector model from an imgCIF file.i    i   i   i   g        (   g        g        (   t   pycbft   cbf_handle_structt	   read_filet
   MSG_DIGESTt   construct_detectort   get_inferred_pixel_sizeR"   t   get_pixel_coordinatest   ranget   matht   sqrtt   sumRM   t   get_image_sizeR   t   get_overloadt   __swig_destroy__RŒ   Rž   R   R�   (   t   cif_fileRž   t
   cbf_handlet   cbf_detectorR°   R%   R¥   R¦   t   jt   dfastt   dslowt   lfastt   lslowR±   t	   underloadt   overloadR™   t   dtypeRš   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyt   imgCIF]  s8    ++::--
	c         C   s  |  j  d ƒ } | j d ƒ | j d ƒ f } t | j d d ƒ ƒ } | j d d ƒ } | j d d ƒ } g  t d ƒ D] } | | | | ^ qv } g  t d ƒ D] } | | | | ^ q¡ }	 t j t g  t d ƒ D] } | | | | ^ qÕ ƒ ƒ }
 t j t g  t d ƒ D] } |	 | |	 | ^ qƒ ƒ } t g  t d ƒ D] } | | |
 ^ qCƒ } t g  t d ƒ D] } |	 | | ^ qpƒ } t t |  j	 d ƒ ƒ ƒ } y+ t
 |  ƒ } |  j d ƒ } | | f } Wn d } n X| j | ƒ ~ t j t j | ƒ | | | | | | ƒ S(   st   Initialize a detector model from an imgCIF file handle, where it
    is assumed that the file has already been read.i    i   i   i   g        (   g        g        (   R·   R¸   R"   R¹   Rº   R»   R¼   R½   RM   R¾   R   R¿   RÀ   RŒ   R�   Rž   (   RÂ   Rž   RÃ   R°   R%   R¥   R¦   RÄ   RÅ   RÆ   RÇ   RÈ   R±   RÉ   RÊ   R™   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyt   imgCIF_H”  s0    ++::--
c         C   sˆ   t  j |  ƒ r |  S|  j ƒ  d k r, t  j S|  j ƒ  d k rE t  j S|  j ƒ  d k r^ t  j S|  j ƒ  d k rw t  j St d |  ‚ d S(   s  Return the correct sensor token for a given name, for example:

    ccd, CCD
    image_plate, IMAGE_PLATE
    pad, PAD

    to the appropriate static token which will be used as a handle
    everywhere else in this. Also allow existing token to be passed in.t   PADt   CCDt   IMAGE_PLATEt   UNKNOWNs   name %s not knownN(   R   t   check_sensort   uppert
   SENSOR_PADt
   SENSOR_CCDt   SENSOR_IMAGE_PLATEt   SENSOR_UNKNOWNt   RuntimeError(   R›   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyRž   ¿  s    (   g        g        N(   g        g        (   g        g        (   g        g        (   R7   R8   R9   R   RŠ   R
   R�   R¨   R®   R²   RÌ   RÍ   Rž   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyRŒ   Ó  s    	!+7+(    (   t
   __future__R    R»   R³   R   R   t   dxtbx_model_extR   R   R   R   R   t   detector_helpersR   R   t   objectR	   R:   Rd   Rs   Rv   R‡   R‰   R‹   Rw   RŒ   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/detector.pyt   <module>   s"   n·'						]