ó
—™xTc           @  sƒ  d  d l  m Z d  d l  m Z d  d l m Z d d d d „ Z d d	 d	 d	 e j d
 ƒ j ƒ  d d „ Z g  d „ Z	 d d	 d	 d	 e j d
 ƒ j ƒ  d d d „ Z e d k rd  d l m Z d  d l m Z d  d l m Z e j d e j d d ƒ d d g d d d ƒ Z e j d e j d d ƒ d d g d d d ƒ Z e ƒ  Z e e e e f d d ƒe e j ƒ  ƒ e j n  d S(   iÿÿÿÿ(   t   division(   t   print_function(   t   wavelengthsi   t    g      ð?c           s�  |  j  ƒ  | d k r( d j ˆ  ƒ } n  |  j ƒ  } |  j ƒ  } d j d ˆ  d | d t | ƒ d d d	 | j ƒ  ƒ } xo | D]g } | d
 j d | j d | j ƒ  d | j d d | j d d | j d d | j	 ƒ  d | j
 ƒ 7} q} W| j ƒ  j ƒ  \ } }	 }
 } } } | d j d d | d | d |	 d |
 d | d | d | d ˆ  ƒ 7} ‡  f d †  } | | | j ƒ  j ƒ  ƒ d  7} | d! 7} | S("   s  Create a pcr phase block skelleton with placeholder strings for different
  refinement options.

  (for internal use)

  :param phase: a crystal structucture
  :type phase: cctbx.xray_structure
  :param number: the number of the phase
  :type number: integer
  :param name: a title to be used for the phase in the pcr file
  :type name: string
  :param scale_down: factor to divide intensities by (to avoid overflows)
  :type scale_down: float

  :returns: the pcr phase block skelleton as a string
  :rtype: string
  R   s	   Phase_{0}s  !-------------------------------------------------------------------------------
!  Data for PHASE number:  {number}  ==> Current R_Bragg for Pattern#  {number}:
!-------------------------------------------------------------------------------
 {name:s}
!
!Nat Dis Ang Pr1 Pr2 Pr3 Jbt Irf Isy Str Furth       ATZ    Nvk Npr More
{nat:>4d}   0  0  0.0 0.0 0.0   0   0   0   0   0        {atz}    0   0   0
!
{space_group}               <--Space group symbol
!Atom   Typ       X        Y        Z     Biso       Occ     In Fin N_t Spc /Codes
t   numbert   namet   natt   atzg        t   space_groups—   {lbl:s}     {stype:s}      {x:f}  {y:f}  {z:f}  {biso:7.5f}  {occ:7.5f}   0   0   0    0
             ##_ap*_## ##_ap*_## ##_ap*_##     0.00      0.00
t   lblt   stypet   xi    t   yi   t   zi   t   bisot   occs˜  !-------> Profile Parameters for Pattern #  1
!  Scale        Shape1      Bov      Str1      Str2      Str3   Strain-Model
  {scale:f}       0.10033   0.03837   0.00000   0.00000   0.00000      0
##_scf_##    ##_shp1.{n}_## ##_shp2.{n}_##   0.000    0.000     0.000
!       U         V          W           X          Y        GauSiz   LorSiz Size-Model
   1.706310  -1.179299   0.405540   0.000000   0.000000   0.000000   0.000000    0
 ##_prfu.{n}_## ##_prfv.{n}_##  ##_prfw.{n}_##      0.000      0.000      0.000      0.000
!     a          b         c        alpha      beta       gamma      #Cell Info
   {a:f}  {b:f}  {c:f}  {alpha:f}  {beta:f}   {gamma:f}
t   scaleg{®Gáz„?t   at   bt   ct   alphat   betat   gammat   nc           s‘   |  j  ƒ  }  |  d k r% d j ˆ  ƒ S|  d k r> d j ˆ  ƒ S|  d k rW d j ˆ  ƒ S|  d k rp d
 j ˆ  ƒ S|  d k r‰ d j ˆ  ƒ Sd Sd  S(   Nt	   triclinicsU     ##_lp1.{0}_## ##_lp2.{0}_## ##_lp3.{0}_## ##_lp4.{0}_## ##_lp5.{0}_## ##_lp6.{0}_##t
   monoclinicsP     ##_lp1.{0}_## ##_lp2.{0}_## ##_lp3.{0}_## ##_lp4.{0}_##     0.00 ##_lp5.{0}_##t   orthorhombicsF     ##_lp1.{0}_## ##_lp2.{0}_## ##_lp3.{0}_##     0.00     0.00     0.00t   trigonalt	   hexagonalt
   tetragonalsF     ##_lp1.{0}_## ##_lp1.{0}_## ##_lp2.{0}_##     0.00     0.00     0.00t   cubicsF     ##_lp1.{0}_## ##_lp1.{0}_## ##_lp1.{0}_##     0.00     0.00     0.00s6        0.00     0.00     0.00     0.00     0.00     0.00(   R   R   R   (   t   lowert   format(   t   xtal_system(   R   (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/fullprof/write_pcr.pyt   __constraints_from_symmF   s    s   
s¥   !  Pref1    Pref2      Asy1     Asy2     Asy3     Asy4
  0.00000  0.00000  0.00000  0.00000  0.00000  0.00000
     0.00     0.00     0.00     0.00     0.00     0.00
(   t/   make_scatterer_labels_shelx_compatible_in_placeR    t
   scattererst   crystal_symmetryt   lent   space_group_infot   labelt   element_symbolt   sitet   b_isot	   occupancyt	   unit_cellt
   parametersR   t   crystal_system(   t   phaseR   R   t
   scale_downt   scattt   symmt   tmpt   atomR   R   R   R   R   R   R"   (    (   R   sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/fullprof/write_pcr.pyt   _make_phase_block   s>    
		$		
 t   unnamedi    t   CUc   
   
   C  s¹   t  |  ƒ } d | d } | d j | | | | d | d k rD d n d ƒ7} | d j d	 d
 d | ƒ 7} | d 7} x> t | ƒ D]0 }	 | t |  |	 d |	 d d d d | ƒ7} q� W| S(   sß  Create a pcr skelleton with placeholder strings for different refinement
  options.

  (for internal use)

  :param phases: a list/tuple/set of structures
  :type phases: list(cctbx.xray_structure)
  :param title: a title to be used in the pcr file
  :type title: string
  :param jobtype: the jobtype to be used
  :type jobtype: integer
  :param nprof: the default peak profile to be used
  :type nprof: integer
  :param nbckgd: the type of background to be used
  :type nbckgd: integer
  :param wavelength: the wavelength to be used in Angstroms
  :type wavelength: float
  :param scale_down: factor to divide intensities by (to avoid overflows)
  :type scale_down: float

  :returns: the pcr skelleton as a string
  :rtype: string
  s   COMM s   
s¥   !Job Npr Nph Nba Nex Nsc Nor Dum Iwg Ilo Ias Res Ste Nre Cry Uni Cor Opt Aut
  {0}   {1}   {2}   {3}   0   0   0   {dum}   0   0   0   0   0   0   0   0   0   0   1
t   dumi   i   i    s  !
!Ipr Ppl Ioc Mat Pcr Ls1 Ls2 Ls3 Syo Prf Ins Rpa Sym Hkl Fou Sho Ana
  2   0   1   1   1   0   0   0   1   1  {filetype}   0   1   4   2   0   0
!
! lambda1 Lambda2    Ratio    Bkpos    Wdt    Cthm     muR   AsyLim   Rpolarz ->Patt# 1
 {xlambda} {xlambda}  1.0000   60.000  5.0000  0.0000  0.0000   50.00    0.0000
!
!NCY  Eps  R_at  R_an  R_pr  R_gl     Thmin       Step       Thmax    PSD    Sent0
 20  0.01  1.00  1.00  1.00  1.00     10.0000   0.100000   100.0000   0.000   0.000
!
!
  #__npar__#    !Number of refined parameters
t   filetypet   0t   xlambdasz  !
!  Zero    Code    SyCos    Code   SySin    Code  Lambda     Code MORE ->Patt# 1
  0.00000    0.0  0.00000    0.0  0.00000    0.0 0.000000    0.00   0
!   Background coefficients/codes  for Pattern#  1  (Polynomial of 6th degree)
       0.000       0.000       0.000       0.000       0.000       0.000
        0.00        0.00        0.00        0.00        0.00        0.00
R   R   R   R1   (   R&   R    t   rangeR6   (
   t   phasest   titlet   jobtypet   nproft   nbckgdt
   wavelengthR1   t   nphaset   rett   i(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/fullprof/write_pcr.pyt   _pcr_skelleton]   s    1.c   	      C  s  d d l  } d } |  } t ƒ  } d „  } | g  k	 r§xn| D]c} | d k r‹ xN| j d | ƒ D]" } | | | | ƒ } | d 7} qb Wq= | d k rx| j d | ƒ D]Z } | j d	 ƒ | k rå | | | j d	 ƒ <| d 7} n  | | | | | j d	 ƒ ƒ } qª Wq= | d
 k r‹x† | j d | ƒ D]Z } | j d	 ƒ | k re| | | j d	 ƒ <| d 7} n  | | | | | j d	 ƒ ƒ } q*Wq= t d j | ƒ ƒ ‚ q= Wn  xT | j d | ƒ D]@ } | | j ƒ   d j t | j d	 ƒ ƒ ƒ | | j	 ƒ  } qºW| j
 d t | d ƒ ƒ } | S(   së  Parse a pcr skelleton to enable or disable the different refinement
  parameters. The placeholder string for a parameter starts with '##_' and ends
  with '_##'.

  (for internal use)

  Allowed values in the freeparams list are:
    * 'scale'   --> free all scale factors
    * 'lattice' --> free all lattice parameters
    * 'profile' --> free all profile parameters
  The parameters are freed according to their order inside the list.
  If an empty list is passed all parameters will be fixed.

  :param inputstring: a string containing placeholders for different refinement   variables
  :type inputstring: string
  :param freeparams: a list of parameter sets to free
  :type freeparams: list

  :returns: the parsed string
  :rtype: string
  iÿÿÿÿNi   c         S  sM   t  | j d ƒ ƒ } d j | ƒ j | ƒ } |  | j ƒ   | |  | j ƒ  S(   Ni    s
   {0:>d}1.00(   R&   t   groupR    t   rjustt   startt   end(   t   stringt   matcht   vart   mlent   replace(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/fullprof/write_pcr.pyt   __replace_match¹   s    R   s	   ##_scf_##t   lattices   ##_lp.*?_##i    t   profiles   ##_prf*?_##s   unknown parameter type: '{0}'s	   ##_.*?_##s   0.00s
   #__npar__#(   t   ret   dictt   finditerRH   t
   ValueErrorR    RJ   RI   R&   RK   RP   t   str(	   t   inputstringt
   freeparamsRT   t   varcountRE   t   param_to_varRQ   t   paramt   m(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/fullprof/write_pcr.pyt   _set_ref_flagsž   s8    		&&>c	   
      C  s›   t  | t t t f ƒ s$ | f } n  t d | d | d | d | d | d | ƒ }	 | d k rl t |	 ƒ }	 n t |	 d d	 d
 d g ƒ}	 t |	 d |  ƒd S(   sF  Write a pcr file to a file or IO buffer.

  :param s: a buffer/file to write to
  :type s: StringIO or filestream
  :param phases: a list/tuple/set of structures or a single crystal structucture
  :type phases: list(cctbx.xray_structure) or cctbx.xray_structure
  :param title: a title to be used in the pcr file
  :type title: string
  :param jobtype: the jobtype to be used (0=xray refine, 2=xray powdersim)
  :type jobtype: integer
  :param nprof: the default peak profile to be used
  :type nprof: integer
  :param nbckgd: the type of background to be used
  :type nbckgd: integer
  :param wavelength: the wavelength to be used in Angstroms
  :type wavelength: float
  :param I_obs: observed intensities
  :type I_obs: cctbx.miller
  :param scale_down: factor to divide intensities by (to avoid overflows)
  :type scale_down: float
  R>   R?   R@   RA   RB   R1   i   RZ   R   RR   RS   t   fileN(   t
   isinstancet   tuplet   listt   setRG   R_   t   print(
   t   sR>   R?   R@   RA   RB   RC   t   I_obsR1   t   pcrfile(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/fullprof/write_pcr.pyt	   write_pcrÚ   s    	t   __main__(   t   StringIO(   t   sgtbx(   t   random_structureR'   R   t   elementst   Ci
   t   u_isog{®Gázt?i{   i   R@   i   N(   t
   __future__R    R   t   cctbx.eltbxR   R6   t   characteristict   as_angstromRG   R_   t   NoneRi   t   __name__Rk   t   cctbxRl   t   cctbx.developmentRm   t   xray_structureR'   t   xrs1t   xrs2t   bufferRe   t   getvaluet   close(    (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/fullprof/write_pcr.pyt   <module>   s>   Y;>(			