ó
(™xTc           @   së   d  d l  m Z d  d l m Z d  d l m Z d  d l Z d  d l Z d Z e j	 j
 d d ƒ Z d „  Z d	 „  Z d
 e f d „  ƒ  YZ d „  Z d e f d „  ƒ  YZ d „  Z d „  Z d „  Z d „  Z e d k rç e d ƒ n  d S(   iÿÿÿÿ(   t   division(   t	   join_open(   t   write_this_is_auto_generatedNs1   cctbx.source_generators.eltbx.generate_sasaki_cppt   cctbxs   reference/sasakic         C   s   t  |  t ƒ |  d IJd  S(   Ns§   #include <cctbx/eltbx/sasaki.h>
#include <scitbx/constants.h>

namespace cctbx { namespace eltbx { namespace sasaki {

namespace table_data {

using namespace detail;
(   R   t   this(   t   f(    (    s_   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/source_generators/eltbx/generate_sasaki_cpp.pyt   print_header   s    	c         C   s   |  d IJd  S(   NsF  /*
  Sasaki Tables

  Scattering factors based on the Cromer and Liberman method.
  Original data can be downloaded from:
  ftp://pfweis.kek.jp/pub/Sasaki-table/
  Any reports or publications of these data will acknowledge
  its use by the citation:
    Anomalous scattering factors
        S.Sasaki (1989) Numerical Tables of Anomalous Scattering Factors
        Calculated by the Cromer and Liberman Method,
        KEK Report, 88-14, 1-136
  Questions about these data should be addressed to Dr.Satoshi Sasaki,
  Tokyo Institute of Technology.  Email: sasaki@nc.titech.ac.jp
 */
(    (   R   (    (    s_   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/source_generators/eltbx/generate_sasaki_cpp.pyt   print_ftp_info   s    t   sasaki_tablec           B   s)   e  Z d  d  d „ Z d „  Z d „  Z RS(   i    c         C   sX   | d k s t  ‚ | j ƒ  j ƒ  |  _ | |  _ | |  _ | |  _ g  |  _ g  |  _ d  S(   Ni    t   Kt   L1t   L2t   L3(   i    R	   R
   R   R   (	   t   AssertionErrort   lowert
   capitalizet   atomic_symbolt   atomic_numbert
   edge_labelt   edge_wave_lengtht   fpt   fdp(   t   selfR   R   R   R   (    (    s_   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/source_generators/eltbx/generate_sasaki_cpp.pyt   __init__.   s    				c         C   s]   t  |  j | |  j d | ƒ d k  s. t ‚ t  | d |  j d | ƒ d k  sY t ‚ d  S(   Ng     @�@g:Œ0âŽyE>i	   g     ˆÃ@(   t   abst   firstt   incr1000R   (   R   t   i_blockR   t   last(    (    s_   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/source_generators/eltbx/generate_sasaki_cpp.pyt   check_first_last8   s    +c         C   sC   t  |  j ƒ t  |  j ƒ k s$ t ‚ t  |  j ƒ d k s? t ‚ d  S(   Ni  (   t   lenR   R   R   (   R   (    (    s_   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/source_generators/eltbx/generate_sasaki_cpp.pyt   validate=   s    $(   t   __name__t
   __module__R   R   R   (    (    (    s_   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/source_generators/eltbx/generate_sasaki_cpp.pyR   ,   s   		c         C   sÜ  d d  l  } g  } d } x—|  j ƒ  D]‰} | sF | j d | ƒ } n | j d d | ƒ } | r÷ | d k rˆ | j ƒ  | j | ƒ n  | s² t | j d ƒ | j d ƒ ƒ } n9 t | j d ƒ | j d ƒ | j d ƒ | j d ƒ ƒ } d } q% n  | j ƒ  } | d d	 k r¬| d d
 k s)t ‚ g  d D] }	 t	 | |	 ƒ ^ q0\ }
 } | d } | d k r}d | _
 d | _ n  | j | |
 | ƒ | d 7} | j | 7_ nê | d j d ƒ d k rg| d d
 k sÛt ‚ g  | d j d ƒ D] } t	 | ƒ ^ qï\ }
 } | d } | d k r8|
 | _
 d | _ n  | j | |
 | ƒ | d 7} | j | 7_ n/ | d d k s}t ‚ | d } | j | 7_ t | ƒ d k s% t ‚ q% W| d k rØ| j ƒ  | j | ƒ n  | S(   Niÿÿÿÿi    s5   ATOMIC SYMBOL\s+=\s+(\S+)\s+ATOMIC NUMBER\s+=\s+(\d+)s+   \s+(\S+)\s+ABSORPTION EDGE\s+\(\s*(\S+)\s+Ai   i   i   i   t   TOs   F'gš™™™™™¹?id   t   ,s   F"i
   (   i    i   (   t   ret
   xreadlinest   matchR   t   appendR   t   groupt   splitR   t   floatR   R   R   R   t   findR   R   (   t   file_objectt   edgeR$   t   tablest   tablet   linet   mR   t   fldst   iR   R   t   x(    (    s_   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/source_generators/eltbx/generate_sasaki_cpp.pyt   collect_tablesA   s`    
$9)
	
2
	


t   table_referencesc           B   s   e  Z d  „  Z RS(   c         C   s1   | |  _  d |  _ d |  _ d |  _ d |  _ d  S(   Ni    (   t   widet   kt   l1t   l2t   l3(   R   R7   (    (    s_   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/source_generators/eltbx/generate_sasaki_cpp.pyR   x   s
    				(   R    R!   R   (    (    (    s_   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/source_generators/eltbx/generate_sasaki_cpp.pyR6   v   s   c         C   sô   i  } x! |  D] } t  | ƒ | | j <q Wx | D] } | | | j _ q1 Wxw | D]o } | j d k rz | | | j _ qR | j d k rœ | | | j _ qR | j d k s± t ‚ | | | j _ qR Wg  } x" |  D] } | j | | j ƒ qÒ W| S(   NR
   R   R   (	   R6   R   R8   R   R9   R:   R   R;   R'   (   t   tables_widet   tables_kt   tables_lt	   ctab_dictt   wR8   t   lt	   ctab_list(    (    s_   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/source_generators/eltbx/generate_sasaki_cpp.pyt   combine_tables   s     c   
   	   C   s>  xt  | | ƒ D]
} | | } x÷ | j | j | j | j | j f D]Ñ } | sW qE n  | j j ƒ  } d t | j j	 ƒ } | j
 r´ | d | j
 j ƒ  7} | d | j d 7} n  |  d | d | IJx? t  t | j ƒ ƒ D]( }	 |  d | j |	 | j |	 f IJqà W|  d IJqE Wq W|  J|  d	 IJ|  J|  d
 IJd  S(   Ns   Z = t   _s
   ; edge at s    As   raw s
   [] = { // s   {%s,%s},s   };s   } // namespace table_datas%   }}} // namespace cctbx::eltbx::sasaki(   t   xrangeR7   R8   R9   R:   R;   R   R   t   strR   R   R   R   R   R   (
   R   t   tables_combinedt   i_begint   i_endt   i_tablet   ctabR-   t   lblt   annR3   (    (    s_   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/source_generators/eltbx/generate_sasaki_cpp.pyt   print_table_block’   s$    
+ 	&
c         C   sÏ  x« | D]£ } |  d | j  j j ƒ  d IJx| | j | j | j | j f D]\ } | rJ | j | j  j k sq t ‚ |  d | j j ƒ  d | j j ƒ  d j ƒ  IJqJ qJ Wq W|  J|  d IJd } xè | D]à } | d 7} d | j  j d t	 | j  j
 ƒ } | d	 | j  j j ƒ  7} x{ | j | j | j | j f D][ } | r‡| d
 | j f 7} | d	 | j j ƒ  7} | d | j j ƒ  7} q6| d 7} q6W| d 7} |  | IJqÉ W|  d IJ|  d IJ|  d IJd  S(   Ns   extern raw s   [];RD   s#   static const detail::info all[] = {i    i   s   {"s   ", s   , s   , %.4fs   , 0., 0s   },s%   {0, 0, 0, 0., 0, 0., 0, 0., 0, 0., 0}s   };sæ  
  } // namespace table_data

  table::table(
    std::string const& label,
    bool exact,
    bool exception_if_no_match)
  {
    std::string work_label = basic::strip_label(label, exact);
    info_ = anomalous::find_entry(
      table_data::all, work_label, exact, exception_if_no_match);
  }

  namespace detail {
  namespace {

    long
    find_table_interval(double given, double first, double incr,
                        double tolerance = 1.e-8)
    {
      double span = (n_raw - 1) * incr;
      double f = (given - first) / span;
      if (f < -tolerance || f > (1.+tolerance)) return -1;
      long i = static_cast<long>(f * (n_raw - 1));
      if (i == n_raw - 1) i--;
      return i;
    }

    bool
    interpolate(double given, double first, const raw* table, bool edge,
                fp_fdp& result)
    {
      if (!table) return false;
      double incr;
      if (!edge) incr = wide_incr;
      else       incr = edge_incr;
      long i = find_table_interval(given, first, incr);
      if (i < 0) return false;
      double x = (given - first) / incr - double(i);
      double fp  = table[i].fp  + x * (table[i+1].fp  - table[i].fp);
      double fdp = table[i].fdp + x * (table[i+1].fdp - table[i].fdp);
      result = fp_fdp(fp, fdp);
      return true;
    }

  } // namespace <anonymous>
  } // namespace detail

  fp_fdp
  table::at_ev(double energy) const
  {
    using detail::interpolate;
    fp_fdp result(fp_fdp_undefined, fp_fdp_undefined);
    double given = scitbx::constants::factor_ev_angstrom / energy;
    if (interpolate(given, info_->first_k, info_->k, true, result)) {
      return result;
    }
    if (interpolate(given, info_->first_l1, info_->l1, true, result)) {
      return result;
    }
    if (interpolate(given, info_->first_l2, info_->l2, true, result)) {
      return result;
    }
    if (interpolate(given, info_->first_l3, info_->l3, true, result)) {
      return result;
    }
    interpolate(given, detail::first_wide, info_->wide, false, result);
    return result;
  }

  table_iterator::table_iterator()
  :
    current_("Li", true)
  {}

  table
  table_iterator::next()
  {
    table result = current_;
    if (current_.is_valid()) current_.info_++;
    return result;
  }

}}} // namespace cctbx::eltbx::sasaki(   R7   R   R   R8   R9   R:   R;   R   R   RF   R   R   (   R   RG   RK   R-   R3   t   out(    (    s_   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/source_generators/eltbx/generate_sasaki_cpp.pyt   print_sasaki_cpp¥   s4    %9

$%


Sc   	      C   s]  t  t d d ƒ } t | d ƒ } | j ƒ  t  t d d ƒ } t | d ƒ } | j ƒ  t  t d d ƒ } t | d ƒ } | j ƒ  t | | | ƒ } t  |  d d ƒ } t | ƒ t | ƒ t | | ƒ | j ƒ  d	 } x€ t d t	 | ƒ | ƒ D]f } t
 t	 | ƒ | | ƒ } t  |  d
 | d | f d ƒ } t | ƒ t | | | | ƒ | j ƒ  qï Wd  S(   Ns
   fpwide.tblt   ri    s   fpk.tbli   s   fpl.tbls
   sasaki.cppR@   i   s   sasaki_tables_%02d_%02d.cpp(   R   t   reference_tables_directoryR5   t   closeRC   R   R   RP   RE   R   t   minRN   (	   t
   target_dirR   R<   R=   R>   RG   t
   block_sizeRH   RI   (    (    s_   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/source_generators/eltbx/generate_sasaki_cpp.pyt   run  s.    






t   __main__t   .(   t
   __future__R    t   scitbx.source_generators.utilsR   R   t   libtbx.load_envt   libtbxt   stringR   t   envt
   under_distRR   R   R   t   objectR   R5   R6   RC   RN   RP   RW   R    (    (    (    s_   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/source_generators/eltbx/generate_sasaki_cpp.pyt   <module>   s$   				5				p	