ó
v™xTc           @   s�  d  Z  d d l m Z d d# d „  ƒ  YZ d d$ d „  ƒ  YZ e d „ Z d „  Z e d	 k r™d d
 l	 m
 Z
 d d l m Z d d l Z d d% d „  ƒ  YZ d d& d „  ƒ  YZ i d d 6d d 6d d 6d d 6d d 6Z d d l m Z e d ƒ Z e e _ e ƒ  Z e j ƒ  Z x` e d ƒ D]R Z xE e d ƒ D]7 Z e e e e e ƒ Z e d e d e d  f Iq2We JqWe e j ƒ  d! ƒ s‘t ‚ d" GHn  d S('   sI  
The position of the direct beam center is assumed to be stated in mm throughout.
The beam center is defined as the position of the beam when the twotheta setting is 0.0 degrees.

We consider two reference frames here:
  1) Instrument reference frame: Coordinate system tied to the face of the detector instrument.
    It is assumed that a) the origin of this coordinate system is located at one of the four corners of
                          the image block.
                       b) the origin is in the center of the corner pixel.
  2) Imageblock reference frame: Coordinate system tied to the slow (x) and fast (y) directions in the
     pixel data.

There are eight possible relationships between the instrument and imageblock frames.  The instrument origin
  can be in any of the four corners of the image; and for each of these, the x and y axes can be either the
  fast or slow dimensions of the image.

It is assumed that the image header gives the beam position in the instrument reference frame.
Diffraction is obviously observed and recorded in the image reference frame.
This module provides a function, convert_beam_instrument_to_imageblock() to give the corresponding
  beam position in the imageblock reference frame.  The Image Object attribute "beam_center_reference_frame"
  defines in which reference frame the beam is expressed.
iÿÿÿÿ(   t   divisiont"   beam_center_convention_definitionsc           B   s   e  Z d  „  Z d „  Z RS(   c         C   s=   t  | d @ƒ |  _ t  | d @ƒ |  _ t  | d @ƒ |  _ d  S(   Ni   i   i   (   t   boolt   ar_flagt   yr_flagt   xr_flag(   t   selft   beam_center_convention(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/beam_center_convention.pyt   __init__   s    c         C   sü   t  d k r t t f } n  t  d k r6 t t f } n  t  d k rU t t t f } n  t  d k rt t t t f } n  t  d k r“ t t t f } n  t  d k r² t t t f } n  t  d k rÕ t t t t f } n  t  d k rø t t t t f } n  d  S(	   Ni    i   i   i   i   i   i   i   (   R   t   slowt   fastt	   fastwidtht	   slowwidth(   R   t   beam_center_in_pixels(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/beam_center_convention.pyt   explicit_formulae*   s             (   t   __name__t
   __module__R   R   (    (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/beam_center_convention.pyR      s   	t!   instrument_to_imageblock_relationc           B   s   e  Z d  „  Z d „  Z RS(   c         C   s\   | j  |  _ | j | j f |  _ | j | j f } | d |  j | d |  j f |  _ d  S(   Ni    i   (   t
   pixel_sizet   pt   beamxt   beamyt   input_beam_mmt   size1t   size2t   width_in_mm(   R   t   imageobjectt   width_in_pixels(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/beam_center_convention.pyR   7   s    c         C   sš   t  | ƒ } g  } x{ d d g D]m } t t | ƒ | j k ƒ } | j | j g | } |  j | } | r |  j | | } n  | j | ƒ q Wt	 | ƒ S(   Ni    i   (
   R   t   intR   R   R   R   R   R   t   appendt   tuple(   R   R   t   Ct   output_beam_mmt   outidxt   srcidxt   direction_reverset   imageblock_mm(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/beam_center_convention.pyt   select=   s    (   R   R   R   R%   (    (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/beam_center_convention.pyR   4   s   	c         C   s_   | r |  j  d k r d  St |  ƒ } | j | ƒ \ |  j d <|  j d <d |  _  | |  _ d  S(   Nt
   instrumentt   BEAM_CENTER_Xt   BEAM_CENTER_Yt
   imageblock(   t   beam_center_reference_frameR   R%   t
   parametersR   (   R   R   t   forcet	   converter(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/beam_center_convention.pyt%   convert_beam_instrument_to_imageblockI   s     #	c         C   sÄ   |  j  } t | ƒ } | d | d f } t |  ƒ } | j | ƒ } g  | D] }	 |	 | d ^ qK }
 |
 d | j | ƒ j |
 d | j | ƒ j g } g  | D] }	 |	 | d ^ q  } t | ƒ S(   NR'   R(   t
   PIXEL_SIZEi    i   (   R+   R   R   R%   t   tile_slow_intervalt   firstt   tile_fast_intervalR   (   t   input_imaget   image_dividert   moduleindexR   t   input_parametersR   R   R-   t   input_imageblock_beam_mmt   at   input_imageblock_beam_pixelst   module_beam_pixelst   module_beam_mm(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/beam_center_convention.pyt!   convert_beam_instrument_to_moduleQ   s    	!!t   __main__(   t   adopt_init_args(   t	   show_diffNt	   test_tilec           B   s   e  Z d  „  Z d „  Z RS(   c         C   s   t  |  t ƒ  ƒ d  S(   N(   R>   t   locals(   R   R1   t   last(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/beam_center_convention.pyR   l   s    c         C   s   |  j  |  j d S(   Ni   (   RB   R1   (   R   (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/beam_center_convention.pyt   sizen   s    (   R   R   R   RC   (    (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/beam_center_convention.pyR@   k   s   	t   test_dividerc           B   s   e  Z d  „  Z d „  Z RS(   c         C   sO   | d k r t  d d ƒ S| d k r2 t  d d	 ƒ S| d k rK t  d d ƒ Sd  S(   Ni    i   i   i   iû  i   i   i  iû  i   i   i   i  iû  (   i    i   i   (   i   i   i   (   i   i   i   (   R@   (   R   t   idx(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/beam_center_convention.pyR0   p   s       c         C   sO   | d k r t  d d ƒ S| d k r2 t  d d	 ƒ S| d k rK t  d d ƒ Sd  S(   Ni    i   i   i   iû  i   i   i  iû  i   i   i   i  iû  (   i    i   i   (   i   i   i   (   i   i   i   (   R@   (   R   RE   (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/beam_center_convention.pyR2   t   s       (   R   R   R0   R2   (    (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/beam_center_convention.pyRD   o   s   	gÍÌÌÌÌ\c@R'   gfffff–b@R(   i   t   SIZE1t   SIZE2gÇHö5Cª?R/   (   t   DetectorImageBases   no filei   i	   s   (%.1f,%.1f)i    i   sÀ  (154.7,148.5) (154.7,43.4) (154.7,-61.6) (49.6,148.5) (49.6,43.4) (49.6,-61.6) (-55.4,148.5) (-55.4,43.4) (-55.4,-61.6)
(148.5,154.7) (148.5,49.6) (148.5,-55.4) (43.4,154.7) (43.4,49.6) (43.4,-55.4) (-61.6,154.7) (-61.6,49.6) (-61.6,-55.4)
(154.7,166.2) (154.7,61.2) (154.7,-43.9) (49.6,166.2) (49.6,61.2) (49.6,-43.9) (-55.4,166.2) (-55.4,61.2) (-55.4,-43.9)
(148.5,160.0) (148.5,55.0) (148.5,-50.1) (43.4,160.0) (43.4,55.0) (43.4,-50.1) (-61.6,160.0) (-61.6,55.0) (-61.6,-50.1)
(160.0,148.5) (160.0,43.4) (160.0,-61.6) (55.0,148.5) (55.0,43.4) (55.0,-61.6) (-50.1,148.5) (-50.1,43.4) (-50.1,-61.6)
(166.2,154.7) (166.2,49.6) (166.2,-55.4) (61.2,154.7) (61.2,49.6) (61.2,-55.4) (-43.9,154.7) (-43.9,49.6) (-43.9,-55.4)
(160.0,166.2) (160.0,61.2) (160.0,-43.9) (55.0,166.2) (55.0,61.2) (55.0,-43.9) (-50.1,166.2) (-50.1,61.2) (-50.1,-43.9)
(166.2,160.0) (166.2,55.0) (166.2,-50.1) (61.2,160.0) (61.2,55.0) (61.2,-50.1) (-43.9,160.0) (-43.9,55.0) (-43.9,-50.1)
t   OK(    (    (    (    (   t   __doc__t
   __future__R    R   R   t   FalseR.   R<   R   t   libtbxR>   t   libtbx.test_utilsR?   t   StringIOR@   RD   R6   t   iotbx.detectors.detectorbaseRH   t   input_objectR+   t   IDt   St   xranget
   conventiont	   moduleidxt   Bt   getvaluet   AssertionError(    (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/beam_center_convention.pyt   <module>   s4   				 	