ó
ųxTc           @   sé   d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l Z d d d g a d d d g Z g  t D] Z e j	 e  ^ qe a
 d	   Z d
   Z d   Z d   Z d   Z d   Z d   Z d d d     YZ d d d     YZ d S(   i’’’’(   t   divisionNs   ([0-9]{2,12})\.(.*)s   (.*)\.([0-9]{2,12})_(.*)s   (.*)\.([0-9]{2,12})(.*)t   .t   _t    c         C   s4  |  d d d  } xt  t  D] \ } } | j |  } | sG q  n  | j   } t |  d k r¹ d | d d d d  } | d d d d  } | d d d d  t | } n< d } | d d d d  } | d d d d  t | } | d j g  | D] }	 d	 ^ q | }
 Pq  W|
 t |  f S(
   s   Try a bunch of templates to work out the most sensible. N.B. assumes
  that the image index will be the last digits found in the file name.Ni’’’’i   R   i    i   i   R   t   #(   t	   enumeratet   compiled_patternst   matcht   groupst   lent   joinerst   joint   int(   t   filenamet	   rfilenamet   jt   cpR   R   t   extent   digitst   prefixt   dt   template(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/scan_helpers.pyt   template_regex   s     "*c         C   s   t  |   d S(   Ni    (   R   (   R   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/scan_helpers.pyt   image2template7   s    c         C   s   t  |   d S(   Ni   (   R   (   R   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/scan_helpers.pyt   image2image:   s    c         C   sS   t  j j |   } | s' t  j   } n  t  j j |   d } t |  } | | f S(   s;   Separate out the template and directory from an image name.i’’’’(   t   ost   patht   dirnamet   getcwdt   splitR   (   R   t	   directoryt   imageR   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/scan_helpers.pyt   image2template_directory=   s    c   	      C   s©   t  j |  } |  j d  } t j |   j d | d |  } t j |  } g  } xB | D]: } | j |  } | r] | j t	 | j
 d    q] q] W| j   | S(   sG   Find images which match the input template in the directory
  provided.R   s   \#s   ([0-9]{%d})i   (   R   t   listdirt   countt   ret   escapet   replacet   compileR   t   appendR   t   groupt   sort(	   R   R   t   filest   lengtht   regexp_textt   regexpt   imagest   fR   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/scan_helpers.pyt   find_matching_imagesL   s    
#
c         C   sn   |  j  d  } t j d |  d | k  r7 t d  n  d | } t j j | |  j d | | |   } | S(   sT   Construct the full path to an image from the template, directory
  and image number.R   i
   i   s   number too big for templates   %%0%dd(   R"   t   matht   powt   RuntimeErrorR   R   R   R%   (   R   R   t   numberR+   t   formatR   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/scan_helpers.pyt   template_directory_number2imagek   s    
c         C   s_   |  j  d  } t j d |  d | k  r7 t d  n  d | } |  j d | | |  } | S(   s:   Construct the an image from the template and image number.R   i
   i   s   number too big for templates   %%0%dd(   R"   R1   R2   R3   R%   (   R   R4   R+   R5   R   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/scan_helpers.pyt   template_number2image   s    
t   scan_helper_image_filesc           B   sh   e  Z d  Z e d    Z e d    Z e d    Z e d    Z e d    Z e d    Z	 RS(   sü   A helper class which handles things like image names, making templates,
  finding matching images and so on. Currently this just provides aliases
  to existing functions elsewhere, but ultimately it would be good if they
  were all encapsulated herein.c         C   s
   t  |   S(   s>   From an image name, return a file template which should match.(   R   (   R   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/scan_helpers.pyt   image_to_template   s    c         C   s
   t  |   S(   s   From an image name, determine the index within the scan for this
    image, complementary to the image_to_template method above.(   R   (   R   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/scan_helpers.pyt   image_to_index   s    c         C   s
   t  |   S(   sM   From a full path to an image, return the filename template and
    directory.(   R    (   R   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/scan_helpers.pyt   image_to_template_directory¤   s    c         C   s   t  |  |  S(   s   For a given template and directory, return a list of image indices
    which match. Also complementary with image_to_template_directory.(   R0   (   R   R   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/scan_helpers.pyt   template_directory_to_indicesŖ   s    c         C   s   t  |  | |  S(   sN   Construct the full image name from the template, directory and
    file index.(   R6   (   R   R   t   index(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/scan_helpers.pyt!   template_directory_index_to_image°   s    c         C   s   t  |  |  S(   s?   Construct the image file name from the template and file index.(   R7   (   R   R=   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/scan_helpers.pyt   template_index_to_image¶   s    (
   t   __name__t
   __module__t   __doc__t   staticmethodR9   R:   R;   R<   R>   R?   (    (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/scan_helpers.pyR8      s   t   scan_helper_image_formatsc           B   s;   e  Z d  Z d Z d Z d Z d Z d Z e d    Z	 RS(   sÄ   A helper class which enxapsulates the allowed and supported image
  formats namely CBF, TIFF, SMV, RAXIS, MAR. N.B. there will be some
  crosstalk between this class and the _image_format classes.t
   FORMAT_CBFt   FORMAT_TIFFt
   FORMAT_SMVt   FORMAT_RAXISt
   FORMAT_MARc         C   s;   |  t  j t  j t  j t  j t  j g k r. t Sd |  GHt S(   Ns3   Format %s may not be supported--contact Nick Sauter(   RD   RE   RF   RG   RH   RI   t   True(   R5   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/scan_helpers.pyt   check_formatĘ   s    		(
   R@   RA   RB   RE   RF   RG   RH   RI   RC   RK   (    (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/scan_helpers.pyRD   »   s   (    (    (   t
   __future__R    R   R#   R1   t   copyt   patternsR
   t   patternR&   R   R   R   R   R    R0   R6   R7   R8   RD   (    (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/model/scan_helpers.pyt   <module>   s$   	"							(