ó
L]Qc           @   sÿ  d  Z  d d l Td d l Z d e j d <d d l Td d l Td d l m Z m Z m	 Z	 m
 Z
 d d l m Z m Z d d l m Z d d	 l m Z m Z m Z m Z d d
 l m Z m Z m Z m Z d d l m Z d d l Z d d l m Z d d l m Z d d l  Td d l! m" Z" m# Z# m$ Z$ d d l% m& Z& m' Z' m( Z( d d l) m* Z* m+ Z+ d d l  m, Z, d d l- m. Z. d8 Z/ d9 Z0 d+ „  Z1 d, d- d. „ Z2 d d d/ d/ d0 „ Z4 e5 d1 d2 „ Z6 e5 d1 d3 „ Z7 d4 „  Z8 d d5 „ Z9 d6 „  Z: d7 „  Z; d S(:   s%  
           calculate nonegative projections of the spectral basis to the 
           measured spectra. Projection basis was found as nonnegative sparse 
           decomposition of about 3500 spectra to 11 components. 

           In the first part of the algoritm is remove stray light and  made a corrections of the instrumental function
           It is very important because highly subpixel precision is necassary. 
           First correction is made as a blind deconvolution  of shape and then a zero and first 
           order of the wavelength polynome are made. Than a projections even with realible estimate of the 
           statistical errors are made. 

           In the second part of the script is Integrated data analyzis of the radiation. 
           Data from spectrometer with low time but very high wavelength resolution van be combinated
           with higt time, but low wavelength resoltion from othe diagnostics as photodiodes, cameras etc..


           Advantages: extremly high sensitivity, can be identified even weak lines lost between 
           strong surrounding lines of other ions. Immune agains line overburning. 
           (Relatively) fast caculaton. It can never missidentified one of the strong lines. 
           Disadvantage: In  rare cases there can happend something interesting which 
           can not be described by these limited number of components, 
           one kind of event is here:7307,7960;9298,9156;10097
           and also probably a new iont from unknow element can occure 
            9664,9665,9670,9839,9906,10131,10136. And finally a molecular spectrum 9504
            On the other hand, without this algoritm it would by imposible to find that 
            there is something interesting in this discharges. 



	Autor: Tomas Odstrcil
	date:12.12.2012

iÿÿÿÿ(   t   *Nt   Aggt   backend(   t   normt   invt   qrt   pinv(   t   erfinvt   erf(   t
   mquantiles(   t   choleskyt   analyzet   cholesky_AAtt   CholmodError(   t   fftconvolvet   convolvet   gaussiant   order_filter(   t   linalg(   t   sparse(   t   repmat(   t   minimize_scalart   nnlst   fmin_powell(   t   fftt   ifftt   fftshift(   t   save_advt   load_adv(   t
   block_diag(   t	   saveconstt   HIt   OIt   OIIt   OIIIt   HeIt   CIIt   CIIIt   NIt   NIIt   NIIIt   Mystery1s   CIV+NIV+OIVs   b-s   r-s   r--s   r-.s   g-s   k--s   k-.s   y-s   y--s   y-.s   c-s   m:c   
      C   s  | d 8} | d 9} | d d } t  d | f ƒ } | d d  d  … f c | t | ƒ 9<| d d  d  … f c t | ƒ d | 9<t | t | ƒ t | ƒ d f | | d d ƒ} t | ƒ } d | | d <t | j d | ƒ} t | | j | ƒ ƒ } t |  | d	 d
 ƒ}	 |	 S(   Ng      à?iÿÿÿÿi   i   i    t   formatt   csrt   betat   modet   same(   t   onest   floort   spdiagst   zerosR   t   Tt   squeezeR   (
   t   xt   st   wint   lamt	   diag_datat   St   f0t   factort   gt   Sx(    (    sI   ../includes/analysis/Radiation/1212Impurities_TO.ON/CalcIonProjections.pyt   ShiftC   s    

&*3id   gš™™™™™¹?c         C   sß  d d l  m } m } m } d d l m } m } t |  ƒ } | | d | }	 t |	 ƒ t	 }
 |	 | d } |  |
 | | | d +t
 |  t |  ƒ | d  ƒ |
 | d *t
 |  t |  ƒ | d ƒ |
 | d )t d |	 | d ƒ } t |
 ƒ } d |
 | <t |
 ƒ } t d | f ƒ } | d d d … f c d 9<| | d | | d	 d
 ƒ} | j | } | | | d	 d ƒ} | | d | ƒ } xt t | ƒ d ƒ D]õ } | t | | | | | d !ƒ d | | d	 d ƒ} | j | t | ƒ d t ƒ| |
 | | | | d !| ƒ } | j | t | ƒ d t ƒ| t | ƒ d t | ƒ d … d f | | | | | d d | | d | | d d +q¡Wt |
 | | d ƒ | } | |	 | d |	 | d !} | | f S(   sÉ  
    ===============================  Gaps Filling Filter 0.1 =====================
     reconstruct the corrupted data (data with nans)  by tikhonov-philips regularization with regulariting by laplace operator. And return smoothed retrofit
    Reconstruction is based on the invertation of the identical operator with zeros at the lines corresponding to the mising signal.
     due to memory and speed limitation the reconstruction is done on the overalaping intervals with width "win"
     signal - long data vector
     win - width of the recosntruction interval - it mas be much bigger than the gaps width
     lam - regularization parameter, dependes on the noise in data

    Autor: Tomas Odstrcil 2012
    iÿÿÿÿ(   R
   R   R   (   R1   t   eyei   i   i   i    NR*   R+   t   cscg      ð?t   subtracti   (   i    i   (   t   scikits.sparse.cholmodR
   R   R   t   scipy.sparseR1   R@   t   lenR/   t   nant   mediant   isnant   aranget   copyR3   t   ranget   int_t   update_inplacet   sqrtt   Truet   Falset   sum(   t   signalR7   R8   R
   R   R   R1   R@   t   nt   n_extt
   ext_signalt   sidet	   intervalst   ind_nant   reconR9   t   Dt   DDt   It   Factort   it   gapsR=   t   chi2(    (    sI   ../includes/analysis/Radiation/1212Impurities_TO.ON/CalcIonProjections.pyt   GapsFillingZ   s8    ')
4#bi    c         C   sÙ   | d  k r t |  ƒ } n  | d  k r6 t |  ƒ } n  | | |  d |  d | d } t | d ƒ } t | d | ƒ } | t | ƒ :} t | | d d ƒ}	 t | | | ƒ }
 t |
 |  |	 d | d | ƒ} |
 | f S(	   Ni   i    i   i   R-   R.   t   leftt   right(	   t   Nonet   amint   amaxt   maxR   RQ   R   t   linspacet   interp(   t   tvect   vecRS   t   t_mint   t_maxRb   Rc   t   stdt	   gauss_wint
   vec_smootht   tvec_newt   vec_new(    (    sI   ../includes/analysis/Radiation/1212Impurities_TO.ON/CalcIonProjections.pyt   Resample˜   s    "i   c         C   sT   t  |  d | f d d ƒ} | r: t | d d ƒ| } n | d  d  … d f } | S(   Niÿÿÿÿt   ordert   Ct   axisi   i    (   t   reshapeRQ   (   t   datat   sampleIntegt
   downsamplet   downsampled(    (    sI   ../includes/analysis/Radiation/1212Impurities_TO.ON/CalcIonProjections.pyRz   ­   s
    c         C   sà  d | d } t  |  ƒ } | | } | | d | } t | ƒ } |  | | | d | | d +d | | | d d *d | | | d d )t | | ƒ }	 t d | f ƒ }
 |
 d d  d  … f c d 9<t |
 d | | d d ƒ} | j | } | j | } | r7t t | | ƒ t d | f ƒ | d d ƒ} n' t t | | ƒ t d | ƒ d d ƒ} | } | } t | ƒ } d | | d <t | j | | | ƒ } t	 | | j | ƒ ƒ } t | | f ƒ } | | d  d  … d f <t	 t
 | d d d ƒƒ } | | d	 k r#t | | d
 d ƒ} n t | | d
 d ƒ} | d |	 d *t
 |	 d | f d d ƒ}	 | r|t |	 d d ƒ}	 n |	 d  d  … d f }	 | | | d | | | d | !} |	 | | d | | d !}	 | |	 f S(   Ni   i   i    iÿÿÿÿR*   R+   Rt   Ru   id   R-   R.   Rv   (   i    i   (   iÿÿÿÿi   (   RE   t   emptyR2   R/   R1   R3   t   kronR@   R
   R4   Rw   R   R   t   mean(   Rx   R7   R8   Ry   t   upsampleRS   t   npixRT   RU   t   retrofitR9   RZ   R[   t   ReductMatrixR3   t   fR;   R<   R=   t   T_Tft	   upsampled(    (    sI   ../includes/analysis/Radiation/1212Impurities_TO.ON/CalcIonProjections.pyt   upsample_smooth·   sJ    
1'&c   	      C   sÊ   t  |  ƒ } d } d } d d t | d d ƒ } t | ƒ } t | | ƒ } t |  | | | ƒ } t t d d | ƒ d ƒ } | t | ƒ 8} | t | ƒ :} t	 | | d	 d
 ƒ} |  | d 8}  |  S(   Nie   gš™™™™™¹?gffffffö?i
   g      à?i   iÿÿÿÿi   R-   R.   gš™™™™™é?(
   RE   R   R/   t   intR   t   expRh   t   minRQ   R   (	   Rx   t   mR7   t   quantilet   shiftt   domaint   rankt
   backgroundt   gauss(    (    sI   ../includes/analysis/Radiation/1212Impurities_TO.ON/CalcIonProjections.pyt   removeStrayLight  s    c   
         sC  t  |  ƒ ‰ t | t d ƒ d d ƒd d k  ‰ t | ˆ ˆ ƒ t ˆ ƒ ‰ t |  ˆ ˆ ƒ t ˆ ƒ ‰ ‡  ‡ ‡ ‡ ‡ f d †  } | d  k r¹ d } t | ƒ } d | | d d <n t  | ƒ } t j ƒ  } t | | d	 d
 d d
 d d d t	 ƒ} t ˆ ƒ }	 t
 | ƒ |	 ˆ d | d ˆ d | d d +t
 | ƒ |	 f S(   Ni   R-   R.   g      ð?c            s   t  ˆ ƒ } |  | ˆ d t |  ƒ d d ˆ d t |  ƒ d d +t | ƒ } t ˆ ˆ t | ƒ ƒ d t ˆ ƒ d } | t |  ƒ ˆ  S(   Ni   i   i   (   R2   RE   R   R   R   RQ   (   t   kert   ker_fullR`   (   R8   t   frt   fst   normalRS   (    sI   ../includes/analysis/Radiation/1212Impurities_TO.ON/CalcIonProjections.pyRƒ     s
    9,i
   i   i   t   xtolgñhãˆµøä>t   ftolt   maxfung    €„.At   disp(   RE   R   R/   R   RN   Rd   R2   t   timeR   RP   t   real(
   t   spectraR   t
   overburnedR8   t   ker0Rƒ   t   Lt   tt   yR“   (    (   R8   R”   R•   R–   RS   sI   ../includes/analysis/Radiation/1212Impurities_TO.ON/CalcIonProjections.pyt
   deconv_opt  s    %'/c         C   s_   t  |  | | | | ƒ } t | t | | d d ƒƒ d t | ƒ d } t t t | ƒ ƒ ƒ S(   NR-   R.   i   i   (   t   deconvR   R   t   sizeRˆ   t   abst   log(   t   noiseR   R   Rž   t   limt   decR`   (    (    sI   ../includes/analysis/Radiation/1212Impurities_TO.ON/CalcIonProjections.pyt	   optim_fun9  s    1c   3         sb	  | j  d } | j ƒ  \ } ‰ d } ˆ | k ‰ t ˆ |  j d  d  … f <|  j g  k ru t ˆ |  j d  d  … f <n  t d ƒ } | d } ˆ t ˆ |  j d  d  … f ƒ 8‰ t | d ƒ }	 t | d ƒ ‰ t ˆ d ƒ ‰ d }
 t	 ˆ ˆ f ƒ } t	 ˆ ˆ f ƒ } | j
 } t ˆ d d ƒd | k ‰  t d	 | ˆ  ƒ t ˆ  ƒ sJt St t ˆ  ƒ d ƒ d } t t ˆ  ƒ d ƒ } ˆ  d ˆ  d
  Bˆ  d )ˆ  d ˆ  d
  Bˆ  d
 *xD t ˆ  ƒ d D]2 } t ˆ d  d  … | f ƒ ˆ d  d  … | f <q·Wˆ | :‰ t | j d1 d d ƒd d  d  … f } t | d d d t d |
 ƒ\ } } t | |	 |
 d
 f d d ƒj } t | d d ƒd k } ‡  ‡ ‡ ‡ f d †  ‰ ‡ f d †  ‰ ‡ ‡ f d †  } t ˆ d  d  … ˆ  f d d ƒ} d } t	 | ƒ } d | | d d <t	 |	 ƒ } d | |	 d <t ˆ d ƒ d k } t | ƒ } d GHxÒ t d ƒ D]Ä } xI t ˆ ƒ D]; } t | | d  d  … f | d d ƒ| | d  d  … f <q\Wt	 ˆ ˆ f ƒ } t | d  d  … | f j | | ƒ \ } } t | j | ƒ j } t | | | d | ƒ \ } } qIWd2 }  t | |  d | g d d d d d d d t ƒ}! ˆ | |! ƒ } t | d  d  … | f j | | ƒ \ } } | d |	 t ˆ  ƒ GHd  GH| } d | | d k  <t	 |	 ƒ }" t	 ˆ ƒ }# t ˆ ƒ t |	 ƒ } t | j d d! ƒ\ }$ }% t  t t! |% ƒ |$ j ƒ ƒ }& xxt ˆ  ƒ d D]f} ˆ d  d  … | f } t | d  d  … | f j ˆ | | f ƒ \ | | d  d  … f <| | <ˆ | | f t | j | | d  d  … f ƒ | |" | <t |" | ƒ |" | <t |" d d ƒ t d ƒ d }' t t" |& t" |' d ƒ |& j ƒ ƒ | | d  d  … f <xZ t ˆ ƒ D]L } |# | c t# |" | | d  d  … f d t | d d ƒƒ d |	 7<qBWq,Wt ˆ d  d  … ˆ  f d d ƒ}( t t | | ƒ j d d ƒ} t# |( | ƒ d |	 t ˆ  ƒ }) d" G|) GHt ˆ d ƒ d k } t | d ƒ ˆ |	 }) |) G| d |	 GH|) G|# GHt$ d# d$ |# ƒ t$ d% d$ | d |	 ƒ t% d& |) ƒ t	 ˆ ƒ }* xU t ˆ ƒ D]G } |  j& | j' t( | | d  d  … f ƒ | j) ƒ }+ t |+ ƒ |* | <q˜Wt* d' ƒ }, |* |, 9}* t( | ƒ }- t( | ƒ }. | |* 9} | |* 9} t | d ƒ }/ t t | d d ƒ ƒ }0 t+ | ƒ r¹t+ | ƒ r¹t |/ | ƒ t |/ ƒ }1 |1 GH| | d GH| |1 | | d | j) d d } n  t d( | |/ d) |0 d* d ƒ| | | j) d+ }2 t, d, t |/ ˆ  ƒ |2 ƒ t d- | |- d) |. d* d ƒt d. | ˆ ƒ t d/ | | ƒ t d	 | ˆ  ƒ t% d0 |* ƒ d  S(3   Ng     @@i }  s   SpectraComponents2.npzt
   componentsi   i    i
   Rv   s   ./data/plasmaiÿÿÿÿRt   t   Fi   Ry   R   c            sŸ   t  ˆ ƒ } xe t ˆ  ƒ d D]S } ˆ d  d  … | f } t |  d  d  … | f j ˆ | | f ƒ \ } | | <q Wt | ƒ d t ˆ  ƒ t ˆ d ƒ } | S(   Ni    i   (   R2   t   whereR   R3   R   RQ   R¥   (   R¬   t   residt   jt   normalPxt   _R`   (   t   plasmat   n_measurementRž   t   intensities(    sI   ../includes/analysis/Radiation/1212Impurities_TO.ON/CalcIonProjections.pyt   estimateResid–  s    :'c            s’   | \ } } t  |  d ƒ } t |  ƒ } xd t ˆ  ƒ D]V } t | t | ƒ t | | | d | ƒ |  | d  d  … f ƒ | | d  d  … f <q4 W| S(   Ni   (   R¥   t
   zeros_liket   xrangeRi   RI   Rh   (   R¬   t   .1t   s1t   s2RS   t   shift_componentsR^   (   t   n_components(    sI   ../includes/analysis/Radiation/1212Impurities_TO.ON/CalcIonProjections.pyt   shiftSpectra¡  s    	Tc            s   ˆ  | |  ƒ } ˆ | ƒ } | S(   N(    (   t   paramst   components_corrR¼   R`   (   R¾   R¶   (    sI   ../includes/analysis/Radiation/1212Impurities_TO.ON/CalcIonProjections.pyt   fitfunª  s    i   s   calc inst funi   R-   R.   t   argsR—   g-Cëâ6?R˜   gü©ñÒMbP?R™   g    €„.ARš   s   calc inst fun finishedt   economics   standart chi2s   ./results/IonChi2s   .txts   ./results/TimeChi2s   ./results/TotalChi2s   absolute_calibration.txts   ./data/TotalPowert   data_errt   tvec_erriè  s   ./HistoricalAnalysis/meanPowers   ./data/projections   ./data/intensitiess   ./data/componentss   ./data/energy_constants(   i   iÿÿÿÿ(   i    i    (-   t   time_stampst   getDataRP   t   black_pixelst	   hotPixelsRO   t   loadR~   R¥   R2   t   readoutNoiseRMSt   nanmaxR   t   anyRe   R®   Rf   R‘   Rw   R3   R†   RQ   R·   RK   R   R   t   dotR£   R   R/   RN   R   t   matrixR   t   diagR   t   savetxtt   savet   convertCountToPhotonst
   wavelengthRJ   t
   integ_timet   loadtxtt   isfiniteR   (3   t   spectrometrt   shott   plasma_startt
   plasma_endRj   RÔ   t   MaxIntensityRx   R¬   t
   n_featuresR   t
   projectiont   projectionErrort   dataSTDt   imint   imaxR^   t   components_reshapedR…   R   t   upsampled_componentst   spect_linesRÁ   t   total_intensR    R’   R“   R±   RÀ   R°   t   projR¯   t   par0R¢   t
   differencet   chi2iont   qt   rR­   t	   DataErrorR   R`   t   energy_constantst	   norm_compt   abs_calibrationt   rel_projt   rel_proj_errt   P_totalt   P_total_errort	   mean_tvect   n_frames(    (   R³   R½   Rž   R´   Rµ   R¾   R¶   sI   ../includes/analysis/Radiation/1212Impurities_TO.ON/CalcIonProjections.pyt   CalcProjectionsC  sÜ    
#		0
($"		"9,#0,F8"7N""		%


((   R   R    R!   R"   R#   R$   R%   R&   R'   R(   R)   s   CIV+NIV+OIV(   s   b-s   r-s   r--s   r-.s   g-s   k--s   k-.s   y-s   y--s   y-.s   c-s   m:(<   t   __doc__t   numpyt
   matplotlibt   rcParamst   matplotlib.pyplott   SpectrometerControlt   scipy.linalgR   R   R   R   t   scipy.specialR   R   t   scipy.stats.mstatsR	   RC   R
   R   R   R   t   scipy.signalR   R   R   R   R   R›   t   scipyR   t   numpy.matlibR   RD   t   scipy.optimizeR   R   R   t   scipy.fftpackR   R   R   t   pygolem_lite.modulesR   R   R   t   pygolem_liteR   t   namest   stylesR?   Ra   Rd   Rs   RP   Rz   R†   R‘   R£   R«   R÷   (    (    (    sI   ../includes/analysis/Radiation/1212Impurities_TO.ON/CalcIonProjections.pyt   <module>$   s>   


"""
	>
J	"	
