ó
 šxTc           @   sð   d  d l  m Z d  d l Z y d  d l Z Wn d Z n Xy d  d l Z Wn d Z n Xd  d l Z d  d l m Z m	 Z	 d  d l
 m Z m Z d  d l m Z m Z d  d l m Z m Z d e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d S(   iÿÿÿÿ(   t   divisionN(   t
   Goniometert   goniometer_factory(   t   Detectort   detector_factory(   t   Beamt   beam_factory(   t   Scant   scan_factoryt   _MetaFormatc           B   s   e  Z d  Z d „  Z RS(   s~   A metaclass for the Format base class (and hence all format classes)
  to allow autoregistration of the class implementations.c         C   sž   t  t |  ƒ j | | | ƒ y t j t j Wn t k
 rB d  SXg  |  _ t | k ry d d l	 m
 } | j |  ƒ d  Sx | D] } | j j |  ƒ q€ Wd  S(   Niÿÿÿÿ(   t   Registry(   t   superR	   t   __init__t   syst   modulest   Formatt
   __module__t	   NameErrort	   _childrent   dxtbx.format.RegistryR
   t   addt   append(   t   selft   namet   basest
   attributesR
   t   base(    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/Format.pyR   *   s    	(   t   __name__R   t   __doc__R   (    (    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/Format.pyR	   &   s   R   c           B   sì   e  Z d  Z e Z e 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 e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d e d „ ƒ Z RS(   sW  A base class for the representation and interrogation of diffraction
  image formats, from which all classes for reading the header should be
  inherited. This includes: autoregistration of implementation classes,
  stubs which need to be overridden and links to static factory methods
  which will prove to be useful in other implementations.c         C   s   t  S(   s<  Overload this to publish whether this class instance
    understands a given file.  N.B. to say that we understand it,
    return True.  If a subclass also understands the image
    (because, for example, its detector serial number takes a
    certain value), it will by definition understand it better
    than its superclass.  Thus, the preferred class will be the
    deepest subclass in the inheritance hierarchy.  Finally, if
    you are writing this subclass for a given instrument and you
    are given a different example return False.

    Implementing understand() in a subclass, one can safely assume
    that the superclasss understand() function returned True.
    The understand() function of two different classes directly
    derived from the same base should never both return True for
    the same input image.(   t   False(   t
   image_file(    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/Format.pyt
   understandJ   s    c         C   s_   | |  _  d |  _ d |  _ d |  _ d |  _ t |  _ t |  _	 t
 |  _ t |  _ |  j ƒ  d S(   s/   Initialize a class instance from an image file.N(   t   _image_filet   Nonet   _goniometer_instancet   _detector_instancet   _beam_instancet   _scan_instanceR   t   _goniometer_factoryR   t   _detector_factoryR   t   _beam_factoryR   t   _scan_factoryt   setup(   R   R   (    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/Format.pyR   ^   s    									
c         C   så   |  j  ƒ  zÉ y¬ |  j ƒ  } t | t ƒ s1 t ‚ | |  _ |  j ƒ  } t | t ƒ s[ t ‚ | |  _ |  j	 ƒ  } t | t
 ƒ s… t ‚ | |  _ |  j ƒ  } t | t ƒ s¯ t ‚ | |  _ Wn t j k
 rÑ } n XWd |  j ƒ  Xd S(   sj  Read the image file, construct the information which we will be
    wanting about the experiment from this. N.B. in your implementation
    of this you will probably want to make use of the static methods
    below and probably add some format parsing code too. Please also keep
    in mind that your implementation may be further subclassed by
    someone else.N(   t   _startt   _goniometert
   isinstanceR   t   AssertionErrorR"   t	   _detectorR   R#   t   _beamR   R$   t   _scanR   R%   t
   exceptionst	   Exceptiont   _end(   R   t   goniometer_instancet   detector_instancet   beam_instancet   scan_instancet   e(    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/Format.pyR*   p   s&    
 			c         C   s   |  j  S(   sR   Get the standard goniometer instance which was derived from the
    image headers.(   R"   (   R   (    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/Format.pyt   get_goniometer“   s    c         C   s   |  j  S(   sP   Get the standard detector instance which was derived from the
    image headers.(   R#   (   R   (    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/Format.pyt   get_detector™   s    c         C   s   |  j  S(   sL   Get the standard beam instance which was derived from the image
    headers.(   R$   (   R   (    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/Format.pyt   get_beamŸ   s    c         C   s   |  j  S(   sL   Get the standard scan instance which was derived from the image
    headers.(   R%   (   R   (    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/Format.pyt   get_scan¥   s    c         C   s#   |  j  } | j ƒ  | j ƒ  } | S(   sN   Get the pixel intensities (i.e. read the image and return as a
    flex array.(   t   detectorbaset   readt   get_raw_data(   R   t   imaget   raw_data(    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/Format.pyR@   «   s    	
c         C   s    d t  f d „  ƒ  Y} | |  ƒ S(   s%   Return the instance of detector base.t   _detectorbase_proxyc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         S   s   | |  _  d  S(   N(   t   _fi(   R   t   format_instance(    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/Format.pyR   »   s    c         S   s&   | d k r |  j  j St j |  | ƒ S(   Nt	   __class__(   RD   RF   t   objectt   __getattribute__(   R   R   (    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/Format.pyRH   ¾   s    
c         S   sg   y |  j  j | ƒ SWnL t k
 rb y |  j  j j | ƒ SWqc t k
 r^ |  j  j j | ƒ SXn Xd  S(   N(   RD   RH   t   AttributeErrorR>   t   __getattr__(   R   R   (    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/Format.pyRJ   Ã   s    (   R   R   R   RH   RJ   (    (    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/Format.pyRC   º   s   		(   RG   (   R   RC   (    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/Format.pyt   get_detectorbaseµ   s    c         C   s   |  j  S(   s/   Get the image file provided to the constructor.(   R    (   R   (    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/Format.pyt   get_image_fileÐ   s    c         C   s   d S(   sÕ   Start code for handling this image file, which may open a link
    to it once, say, and pass this around within the implementation.
    How you use this is up to you, though you probably want to overload
    it...N(    (   R   (    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/Format.pyR+   Ø   s    c         C   s   d S(   si   Clean up things - keeping in mind that this should run even in the
    case of an exception being raised.N(    (   R   (    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/Format.pyR4   à   s    c         C   s   t  d ‚ d S(   sh   Overload this method to read the image file however you like so
    long as the result is an goniometer.s   overload meN(   t   RuntimeError(   R   (    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/Format.pyR,   æ   s    c         C   s   t  d ‚ d S(   sf   Overload this method to read the image file however you like so
    long as the result is an detector.s   overload meN(   RM   (   R   (    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/Format.pyR/   ì   s    c         C   s   t  d ‚ d S(   sb   Overload this method to read the image file however you like so
    long as the result is an beam.s   overload meN(   RM   (   R   (    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/Format.pyR0   ò   s    c         C   s   t  d ‚ d S(   sb   Overload this method to read the image file however you like so
    long as the result is an scan.s   overload meN(   RM   (   R   (    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/Format.pyR1   ø   s    c         C   s?   d d l  m  } | |  ƒ j } | r; t | ƒ d k r; t St S(   s   See if the file is a URL.iÿÿÿÿ(   t   urlparsei   (   RN   t   schemet   lent   TrueR   (   t   pathRN   RO   (    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/Format.pyt   is_url  s
    c         C   s0   d |  d k r t  Sd t |  d ƒ j d ƒ k S(   s7   Check if a file pointed at by filename is bzip2 format.s   .bz2iüÿÿÿt   BZht   rbi   (   R   t   openR?   (   t   filename(    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/Format.pyt   is_bz2  s    c         C   sV   d |  d k r t  St |  d ƒ j d ƒ } t | d ƒ d k oU t | d ƒ d k S(	   s:   Check if a file pointed at by filename is gzip compressed.s   .gziýÿÿÿRU   i   i    i   i   i‹   (   R   RV   R?   t   ord(   RW   t   magic(    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/Format.pyt   is_gzip  s    RU   c         C   s¬   | r1 t  j |  ƒ r1 d d l } | j |  ƒ Sn  t  j |  ƒ rh t d k rX t d ‚ n  t j |  | ƒ St  j	 |  ƒ rŸ t
 d k r� t d ‚ n  t
 j |  | ƒ St |  | ƒ S(   s;   Open file for reading, decompressing silently if necessary.iÿÿÿÿNs$   bz2 file provided without bz2 modules$   gz file provided without gzip module(   R   RS   t   urllib2t   urlopenRX   t   bz2R!   RM   t   BZ2FileR[   t   gzipt   GzipFileRV   (   RW   t   modet   urlR\   (    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/Format.pyt	   open_file(  s    (   R   R   R   R	   t   __metaclass__t   staticmethodR   R   R*   R:   R;   R<   R=   R@   RK   RL   R+   R4   R,   R/   R0   R1   RS   RX   R[   R   Rd   (    (    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/Format.pyR   A   s.   		#					
									(   t
   __future__R    R   R^   R!   R`   R2   t   dxtbx.model.goniometerR   R   t   dxtbx.model.detectorR   R   t   dxtbx.model.beamR   R   t   dxtbx.model.scanR   R   t   typeR	   RG   R   (    (    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/format/Format.pyt   <module>   s    

