ó
™xTc           @   s\   d  d l  m Z d  d l Z d  d l m Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   divisionN(   t   adopt_init_argst   readerc           B   sV   e  Z d  Z d „  Z e d „  ƒ Z e e d „ ƒ Z e d „ Z d „  Z	 d „  Z
 RS(   s9   A class to read the XPARM.XDS/GXPARM.XDS file used in XDSc         C   s   d  S(   N(    (   t   self(    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/xds/xparm.pyt   __init__   s    c         C   s  t  |  d ƒ �þ } g  } d } x³ t | ƒ D]¥ \ } } | j ƒ  } | d k r} t | ƒ d k r} | d d k r} d } q} n  | d k r  | d d k rÃ d	 Sn# | d k rÃ | d d k rÃ d	 Sn  | j | ƒ q+ W| d k r	| d d k st | ƒ d k r	d	 Sn  Wd	 QX| S(
   si  Check the version if the given file is a (G)XPARM.XDS file.

    If the file contains exactly 11 lines and 42 tokens, it is the old style
    version 1 file. If the file starts with XPARM.XDS it is the new style
    version 2 file.

    Params:
      filename The (G)XPARM.XDS filename

    Returns:
      The version or None if the file is not recognised

    t   ri   i    s	   XPARM.XDSi   i   i   i*   N(   t   opent	   enumeratet   splitt   lent   Nonet   extend(   t   filenamet   file_handlet   tokenst   versiont   countt   linet   line_tokens(    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/xds/xparm.pyt   find_version   s&    ""c         C   s   t  j |  ƒ d k S(   s  Check if the given file is a (G)XPARM.XDS file.

    Ensure it is named correctly and contains exactly 11 lines and 42
    tokens, otherwise return False.

    Params:
      filename The (G)XPARM.XDS filename

    Returns:
      True/False the file is a (G)XPARM.XDS file

    N(   R   R   R
   (   R   t   check_filename(    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/xds/xparm.pyt   is_xparm_file;   s    c         C   sŽ   t  j | ƒ } | d k rL g  t | d ƒ j ƒ  D] } | j ƒ  ^ q1 } n t d j | ƒ ƒ ‚ | d k r} |  j | ƒ n |  j	 | ƒ d S(   sÎ   Read the XPARM.XDS/GXPARAM.XDS file.

    See http://xds.mpimf-heidelberg.mpg.de/html_doc/xds_files.html for more
    information about the file format.

    Param:
      filename The path to the file

    R   s   {0} is not a (G)XPARM.XDS filei   N(
   R   R   R
   R   t	   readlinesR   t   IOErrort   formatt   parse_version_1_tokenst   parse_version_2_tokens(   R   R   R   R   t   lR   (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/xds/xparm.pyt	   read_fileK   s    1c         C   s  t  | d d ƒ |  _ t | d d ƒ |  _ t | d d ƒ |  _ t t t | d d d !ƒ ƒ |  _ t | d d ƒ |  _ t t t | d d d !ƒ ƒ |  _	 d |  _ t t t  | d d d !ƒ ƒ |  _ t t t | d d d !ƒ ƒ |  _ t | d d ƒ |  _ t t t | d d d !ƒ ƒ |  _ t t t | d ƒ ƒ |  _ t t t | d ƒ ƒ |  _ t t t | d ƒ ƒ |  _ t  | d d ƒ |  _ t t t | d d d !ƒ ƒ |  _ t t t | d	 ƒ ƒ |  _ t t t | d
 ƒ ƒ |  _ t t t | d ƒ ƒ |  _ d S(   sL   Parse the version 1 tokens

    Param:
      tokens The list of tokens

    i    i   i   i   i   i   i   i   i   i	   i
   N(   t   intt   starting_framet   floatt   starting_anglet   oscillation_ranget   tuplet   mapt   rotation_axist
   wavelengtht   beam_vectorR
   t   num_segmentst   detector_sizet
   pixel_sizet   detector_distancet   detector_origint   detector_x_axist   detector_y_axist   detector_normalt   space_groupt	   unit_cellt   unit_cell_a_axist   unit_cell_b_axist   unit_cell_c_axis(   R   R   (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/xds/xparm.pyR   c   s&    ##	####c      	   C   s¥  t  | d d ƒ |  _ t | d d ƒ |  _ t | d d ƒ |  _ t t t | d d d !ƒ ƒ |  _ t | d d ƒ |  _ t t t | d d d !ƒ ƒ |  _	 t  | d d ƒ |  _
 t t t | d d d !ƒ ƒ |  _ t t t | d ƒ ƒ |  _ t t t | d ƒ ƒ |  _ t t t | d ƒ ƒ |  _ t  | d d ƒ |  _ t t t  | d d d !ƒ ƒ |  _ t t t | d d d !ƒ ƒ |  _ t t t | d	 d d !ƒ ƒ |  _ t | d	 d ƒ |  _ t t t | d
 ƒ ƒ |  _ t t t | d ƒ ƒ |  _ t t t | d ƒ ƒ |  _ g  |  _ g  |  _ xq t |  j ƒ D]` } |  j j t t t  | d | d ƒ ƒ ƒ |  j j t t t | d | d d ƒ ƒ ƒ q=Wd S(   sL   Parse the version 2 tokens

    Param:
      tokens The list of tokens

    i   i    i   i   i   i   i   i   i   i	   i
   i   i   N(   R   R   R   R    R!   R"   R#   R$   R%   R&   R/   R0   R1   R2   R3   R'   R(   R)   R+   R*   R,   R-   R.   t   segmentst   orientationt   ranget   append(   R   R   t   i(    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/xds/xparm.pyR   …   s0    ######		+(   t   __name__t
   __module__t   __doc__R   t   staticmethodR   t   TrueR   R   R   R   (    (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/xds/xparm.pyR      s   	(	"t   writerc           B   s,   e  Z d d d  „ Z d d „ Z d „  Z RS(   c         C   s«   t  |  t ƒ  ƒ | | | g j d  ƒ d k r§ d |  _ g  |  _ g  |  _ x[ t |  j ƒ D]G } |  j j | d d |  j	 d d |  j	 d f ƒ |  j j d ƒ qY Wn  d  S(   Ni   i   i    (	   i    i    i    i   i    i    i    i   i    (
   R   t   localsR   R
   R'   R4   R5   R6   R7   R(   (   R   R   R    R!   R$   R%   R&   R/   R0   R1   R2   R3   R'   R(   R)   R+   R*   R,   R-   R.   R4   R5   R8   (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/xds/xparm.pyR   ±   s    				(c      	   C   sÆ  | d k r t j } n  | d IJ| d |  j |  j |  j f I| d |  j IJ| d |  j I| d |  j IJ| d |  j	 I| d |  j
 IJ| d |  j IJ| d |  j IJ| d |  j IJ| d	 |  j |  j d
 |  j d |  j d
 |  j d f IJ| d |  j I| d |  j IJ| d |  j IJ| d |  j IJ| d |  j IJxd t |  j ƒ D]S } | d t |  j | ƒ IJ| d j d g d g d ƒ t |  j | ƒ IJqkWd S(   sN   
    http://xds.mpimf-heidelberg.mpg.de/html_doc/xds_files.html#XPARM.XDS
    s	   XPARM.XDSs   %6i %13.4f %9.4fs   %9.6f %9.6f %9.6fs    %14.6fs   %14.6f %14.6f %14.6fs      %3is&   %11.4f %11.4f %11.4f %7.3f %7.3f %7.3fs    %14.6f  %14.6f  %14.6fs    %8i %9i %9i %11.6f %11.6fi    i   s    %14.6f %14.6fs    %14.6f %14.6f %14.6fs    %9i %9i %9i %9i %9it    s    %7.2fi   s    %8.5fi   Ns    %7.2f %7.2f %7.2f(   R
   t   syst   stdoutR   R    R!   R$   R%   R&   R/   R0   R1   R2   R3   R'   R(   R)   R+   R*   R,   R-   R.   R6   R"   R4   t   joinR5   (   R   t   outR8   (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/xds/xparm.pyt   showÑ   s0    
c         C   s,   t  | d ƒ � } |  j d | ƒ Wd  QXd  S(   Nt   wbRD   (   R   RE   (   R   R   t   f(    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/xds/xparm.pyt
   write_fileî   s    N(   R9   R:   R
   R   RE   RH   (    (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/xds/xparm.pyR>   ¯   s   (   t
   __future__R    RA   t   libtbxR   t   objectR   R>   (    (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/xds/xparm.pyt   <module>	   s   ¢