ó
y™xTc           @   sx   d  d l  m Z d  d l Z d  d l m Z d  d l m Z d  d l Z d e f d „  ƒ  YZ	 d e f d „  ƒ  YZ
 d S(	   iÿÿÿÿ(   t   divisionN(   t   ReadADSC(   t   flext   DetectorImageBasec           B   sI  e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d d d „ Z e d e d e d e d e d e d e d e d e d e d e d e d e d e ƒ Z d „  Z d' d „ Z d „  Z d' d „ Z e d „ Z d  „  Z d! „  Z d' d" „ Z d# „  Z d$ „  Z d% „  Z d& „  Z  RS((   c         C   sL   | |  _  d  |  _ d  |  _ d |  _ d |  _ d |  _ d  |  _ d |  _ d  S(   Ni   t	   baseclasst
   instrumenti    (	   t   filenamet   Nonet
   parameterst   linearintdatat   bint
   vendortypet   beam_center_reference_framet   beam_center_conventiont   vendor_specific_null_value(   t   selfR   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/detectorbase.pyt   __init__   s    							c         C   s—   t  j  | j ƒ |  _ t  j  | j ƒ |  _ t  j  | j ƒ |  _ t  j  | j ƒ |  _ t  j  | j ƒ |  _ t  j  | j ƒ |  _ t  j  | j ƒ |  _ d  S(   N(   t   copyR   R
   R   R   R   t   headert   headerlines(   R   t   parentobject(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/detectorbase.pyt+   copy_common_attributes_from_parent_instance   s    c         C   sÌ   |  j  d k s | d k r d  S|  j | d k r6 d  S|  j d | |  j d <|  j d | |  j d <|  j j d ƒ r— |  j d | | |  j d <n  |  j d | |  j d <| |  _  |  j |  j ƒ d  S(   Ni   i   i    t   SIZE1t   SIZE2t   CCD_IMAGE_SATURATIONt
   PIXEL_SIZE(   R
   t   size1R   t   has_keyt   bin_safe_set_dataR	   (   R   R
   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/detectorbase.pyt   setBin   s      	c         C   s!   d d l  m } | |  | ƒ d  S(   Niÿÿÿÿ(   t%   convert_beam_instrument_to_imageblock(   t&   iotbx.detectors.beam_center_conventionR   (   R   R   R   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/detectorbase.pyt   set_beam_center_convention'   s    c         C   s,   |  j  ƒ  |  j ƒ  |  j |  j |  j ƒ  S(   N(   t
   readHeadert
   dataoffsetR   t   size2t   integerdepth(   R   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/detectorbase.pyt
   fileLength+   s    
c         C   s
   t  ‚ d  S(   N(   t   NotImplementedError(   R   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/detectorbase.pyt	   getEndian3   s    c         C   sJ   |  j  ƒ  } d d  l } | j d | j d d ƒ ƒ d d k } | | k S(   Niÿÿÿÿt   is   >ii¸  i    (   R'   t   structt   unpackt   pack(   R   t   data_is_big_endianR)   t   platform_is_big_endian(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/detectorbase.pyt   endian_swap_required6   s    (c         C   sP   |  j  ƒ  |  j t |  j |  j ƒ  |  j |  j |  j |  j |  j ƒ  ƒ ƒ d  S(   N(	   R%   R   R   R   R"   R   R
   R#   R'   (   R   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/detectorbase.pyt   read>   s    
c         C   sn   |  j  d k ra | d  k ra | j j d ƒ t k ra d d l m } | | ƒ |  _ t |  j _ n	 | |  _ d  S(   Ni   t   bin2by2iÿÿÿÿ(   t	   Bin2_by_2(	   R
   R   t   __dict__t   gett   Truet   iotbx.detectorsR1   R	   R0   (   R   t   new_data_arrayR1   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/detectorbase.pyR   E   s    c         C   sH   t  |  j j ƒ } | j d ƒ d k r+ d S| j d ƒ d k rD d Sd  S(   Nt   inti    t   double(   t   strR	   t	   __class__t   find(   R   t   typehash(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/detectorbase.pyt   get_data_typeS   s
      c         C   s   |  j  S(   N(   R	   (   R   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/detectorbase.pyt   get_raw_dataX   s    i   g      ð?c         C   s‡   |  j  ƒ  } | d k r+ d d l m } n | d k rJ d d l m } n  | d |  j d | d |  j d	 | d
 t t |  d
 d ƒ ƒ ƒ S(   NR7   iÿÿÿÿ(   t	   FlexImageR8   (   t   FlexImage_dt   rawdatat   binningR   t
   brightnesst
   saturationiÿÿ  (   R=   R5   R?   R@   R	   R   R7   t   getattr(   R   RB   RC   t   datatypeR?   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/detectorbase.pyt   get_flex_image[   s    		R   R   R   t   DISTANCEt   TWOTHETAt	   OSC_RANGEt	   OSC_STARTt   PHIt
   WAVELENGTHt   BEAM_CENTER_Xt   BEAM_CENTER_YR   t   DETECTOR_SNc         C   s  | j  j j d  k r* | j  j j | _ n  | j  j j d  k rf | j  j j | _ | j  j j | _ n  t | _	 t | _
 d | _ t | _ d d l m } | |  | ƒ d d l m } d d l m } d d d „  ƒ  Y} | ƒ  } d |  j g | _ | | ƒ } | j ƒ  d	 } t d
 d |  j d d |  j d d |  j d d |  j d d |  j d g  | j D] }	 |	 j | k rb|	 j ^ qbd	 ƒ }
 | |
 | ƒ } | j | | j  |  ƒ | j! | <| j" | | j  ƒ |  j | j! | d d <d d l# m$ } | | | ƒ | | f S(   Ni   iÿÿÿÿ(   t(   beam_center_convention_from_image_object(   t   heuristics_base(   t
   file_namest   emptyc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/detectorbase.pyRT   ƒ   s    t   Emptyi    t   twothetas   %ft   ybeamt   xbeamt   distancet
   wavelengtht   templatet
   spotoutputt   relpath(   t   pretty_image_stats(    (%   t   distlt   rest   innerR   t   distl_lowres_limitt   outert   force_method2_resolution_limitt   distl_highres_limitt   Falset   distl_force_binningt   distl_permit_binningt
   wedgelimitt   spotfinder_header_testst'   iotbx.detectors.context.config_detectorRQ   t,   spotfinder.applications.practical_heuristicsRR   t!   spotfinder.diffraction.imagefilesRS   R   t   argvt   framest   dictRX   t   beamyt   beamxR[   R\   t   FNt   numberR]   t   oneImaget   pdt   imagest   determine_maxcellt#   spotfinder.applications.stats_distlR`   (   R   t   distl_paramsRQ   RR   RS   RT   t   Et   namest
   this_framet   itemt   process_dictionaryt
   SpotfinderR`   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/detectorbase.pyt   get_spotfindern   s@    					5	c   
      C   s   |  j  j d ƒ s" d |  j  d <n  d d l m } y9 |  j ƒ  } | d k r] d |  j  d <n d |  j  d <Wn$ | k
 r‘ d	 } d |  j  d <n X|  j  j d
 ƒ ræ y t |  j  d
 ƒ |  j  d
 <Wqó t k
 râ d	 |  j  d
 <qó Xn d	 |  j  d
 <|  j  d d k r6|  j  d d k r6d |  j  d <d |  j  d <n  d |  j  } t | d ƒ } | j | ƒ d t	 | ƒ } | j d | ƒ | j
 ƒ  d d l m } | d  k r²|  j } n  | j d	 ƒ sàd d l m }	 |	 d ƒ ‚ n  | | | |  j |  j | ƒ d  S(   NRI   g        iÿÿÿÿ(   t   ImageExceptioni   t
   big_endiant
   BYTE_ORDERt   little_endiani    RP   R   iß	  R   iŸ	  s¹  {
HEADER_BYTES= 1024;
DIM=2;
BYTE_ORDER=%(BYTE_ORDER)s;
TYPE=unsigned_short;
SIZE1=%(SIZE1)4d;
SIZE2=%(SIZE2)4d;
PIXEL_SIZE=%(PIXEL_SIZE)8.6f;
TIME=0.000000;
DISTANCE=%(DISTANCE).2f;
TWOTHETA=%(TWOTHETA).2f;
PHI=%(OSC_START).3f;
OSC_START=%(OSC_START).3f;
OSC_RANGE=%(OSC_RANGE).3f;
WAVELENGTH=%(WAVELENGTH).6f;
BEAM_CENTER_X=%(BEAM_CENTER_X).2f;
BEAM_CENTER_Y=%(BEAM_CENTER_Y).2f;
CCD_IMAGE_SATURATION=65535;
DETECTOR_SN=%(DETECTOR_SN)d;
}t   wbi   t    (   t	   WriteADSC(   t   Sorrys,   Negative values not allowed when writing SMV(   R   R   R5   R„   R'   R7   t
   ValueErrort   opent   writet   lent   closeRŠ   R   R	   t   all_get   libtbx.utilsR‹   R   R#   (
   R   t   fileoutt   mod_dataR„   t   endiant   infot   Ft   len_nullRŠ   R‹   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/detectorbase.pyt   debug_write˜   sB    &
 c         C   s{  | d k r |  j  d S| d k r. |  j  d S| d k rP |  j  d |  j  d S| d k ro |  j  j d d ƒ S| d	 k r‚ |  j S| d
 k r™ |  j  d S| d k r¸ |  j  j d d ƒ S| d k rÏ |  j  d S| d k rî |  j  j d d ƒ S| d k r|  j  d S| d k r|  j  d S| d k r;|  j  j d d ƒ S| d k rZ|  j  j d d ƒ S| d k rq|  j  d St ‚ d  S(   NR   R   R#   R   t   npixelsRD   R   iÿÿ  RA   t
   pixel_sizeR   t	   osc_startRK   g        R[   RH   R\   RM   Rt   RN   Rs   RO   t   deltaphiRJ   RX   RI   t   serial_numberRP   (   R   R3   R	   t   AttributeError(   R   t   attr(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/detectorbase.pyt   __getattr__Ó   s:                  c         C   sÚ   | d  k r t j } n  | d I|  j IJ| d |  j |  j f IJ| d |  j IJ| d |  j IJ| d |  j IJ| d |  j	 IJ| d |  j
 IJ| d |  j IJ| d	 |  j |  j f IJ| d
 |  j IJd  S(   Ns   File:s!   Number of pixels: slow=%d fast=%ds   Pixel size: %f mms   Saturation: %.0fs   Detector distance: %.2f mms    Detector 2theta swing: %.2f deg.s   Rotation start: %.2f deg.s   Rotation width: %.2f deg.s!   Beam center: x=%.2f mm  y=%.2f mms   Wavelength: %f Ang.(   R   t   syst   stdoutR   R   R#   R›   RD   R[   RX   Rœ   R�   Rt   Rs   R\   (   R   t   out(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/detectorbase.pyt   show_headerç   s    c         C   s³   t  |  _ d d l m } | |  | ƒ } | r: d | GHn  | d k r[ t |  _ t |  _ n0 | d k rs t  |  _ n | d k r‹ t |  _ n  |  j |  _ |  j |  _	 |  j
 |  _ d  S(   Niÿÿÿÿ(   RQ   s   beam center convention: %di    i   i   (   Rh   t   _invert_beam_centerRm   RQ   R4   t	   _invert_yR#   t   image_size_fastR   t   image_size_slowR›   t   pixel_resolution(   R   t   phil_parameterst   verboseRQ   t   bc(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/detectorbase.pyt   initialize_viewer_propertiesö   s    		c         C   sg   |  j  |  j } |  j |  j } | | } |  j rE | | d } n
 | | } | |  j  | |  j f S(   s_   
    Convert absolute detector position (in mm) to floating-value image pixel coordinates.
    g      ð?(   R¨   Rª   R©   R§   (   R   t   xt   yt   dwt   dht   x_fract   y_frac(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/detectorbase.pyt%   detector_coords_as_image_coords_float  s    
	

c         C   s.   |  j  | | ƒ \ } } t | ƒ t | ƒ f S(   s^   
    Convert absolute detector position (in mm) to integer-value image pixel coordinates.
    (   Rµ   R7   (   R   R¯   R°   t   x_pointt   y_point(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/detectorbase.pyt   detector_coords_as_image_coords  s    c   
      C   sr   |  j  |  j } |  j |  j } | |  j  } | |  j } | | } |  j r^ d | | }	 n
 | | }	 | |	 f S(   s[   
    Convert image pixel coordinates to absolute position on the detector
    (in mm).
    g      ð?(   R¨   Rª   R©   R§   (
   R   R¯   R°   t   readoutR±   R²   R³   R´   t
   x_detectort
   y_detector(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/detectorbase.pyt   image_coords_as_detector_coords   s    
	
c         C   s:   |  j  r |  j } |  j } n |  j } |  j } | | f S(   N(   R¦   Rs   Rt   (   R   t   center_xt   center_y(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/detectorbase.pyt   get_beam_center_mm0  s    				c         C   s"   |  j  ƒ  \ } } |  j | | ƒ S(   N(   R¿   Rµ   (   R   R½   R¾   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/detectorbase.pyt    get_beam_center_pixels_fast_slow:  s    c         C   sB   y) |  j  t | d ƒ t | d ƒ f SWn t k
 r= d  SXd  S(   Ni    i   (   R	   R7   t
   IndexErrorR   (   R   t   coords(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/detectorbase.pyt   get_pixel_intensity>  s    )c      
   C   sN   t  | d t |  j |  j ƒ t |  j |  j ƒ f d |  d |  j d |  j ƒS(   Nt   beamt   reference_imageR   R#   (   t   tile_manager_baseR7   Rt   R›   Rs   R   R#   (   R   t   phil(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/detectorbase.pyt   get_tile_managerD  s    	N(!   RU   RV   R   R   R   R    R%   R'   R.   R/   R   R=   R>   RG   Rr   R7   t   floatR9   t
   data_typesRƒ   R   R™   R¡   R¥   R4   R®   Rµ   R¸   R¼   R¿   RÀ   RÃ   RÈ   (    (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/detectorbase.pyR      s:   	
												*;				
		RÆ   c           B   s8   e  Z d d d d d  „ Z e d e d „ Z d „  Z RS(   c         C   s1   | |  _  | |  _ | |  _ | |  _ | |  _ d  S(   N(   t   working_paramsRÄ   R   R#   RÅ   (   R   RË   RÄ   R   R#   RÅ   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/detectorbase.pyR   O  s
    				c         K   s÷  | d k	 r | |  _ n  |  j | �  } | rÞ y |  j j j } Wn t k
 rY d } n Xx� t t | ƒ d ƒ D]f } | d | d c | 7<| d | d c | 7<| d | d c | 8<| d | d c | 8<qq Wn  |  j j j	 d k	 r[| t
 k r[g  } x( |  j j j	 D] }	 | |	 g d } qWt j t j | ƒ d k ƒ }
 | j |
 ƒ S|  j j j	 d k	 ró| t k róg  } x( |  j j j	 D] }	 | |	 g d } q’Wt j ƒ  } x3 t t | ƒ ƒ D] } | j | | | | ƒ qÌW| S| S(   s   Some documentation goes herei   i   i    i   i   N(   R   RÅ   t!   effective_tiling_as_flex_int_implRË   Ra   t   peripheral_margint	   Exceptiont   xrangeR�   t
   tile_flagsRh   R   t   boolR7   t   selectR4   t   append(   R   t   reapply_peripheral_marginRÅ   t   encode_inactive_as_zeroest   kwargst   ITRÍ   R(   t   expand_flagst   flagt
   bool_flagst   Zero_ITt   idx(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/detectorbase.pyt   effective_tiling_as_flex_intX  s8      
!!c   	      K   s  |  j  d  k	 s t ‚ t j ƒ  } d d l m } |  j  j d  k r` |  j  j ƒ  |  j  j	 ƒ  n  |  j  j
 } | |  j  j | ƒ } x€ t | j ƒ  ƒ D]l } | j | ƒ } | j | ƒ } | j | j ƒ | j | j ƒ | j | j d ƒ | j | j d ƒ q” W| S(   Niÿÿÿÿ(   t   image_divideri   (   RÅ   R   t   AssertionErrorR   R7   R5   RÞ   R	   R!   R/   R   t   ranget   module_countt   tile_slow_intervalt   tile_fast_intervalRÓ   t   firstt   last(	   R   RÖ   R×   RÞ   t
   null_valuet   dividerR(   t   slowt   fast(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/detectorbase.pyRÌ   ‚  s$    		N(   RU   RV   R   R   Rh   RÝ   RÌ   (    (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/detectorbase.pyRÆ   M  s   	((   t
   __future__R    R   R5   R   t   scitbx.array_familyR   R¢   t   objectR   RÆ   (    (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/detectors/detectorbase.pyt   <module>   s   ÿ G