
{uKPc           @   sd  d  Z  d d l Td d l Z e j d  d d l Td d l Z d d l m Z m Z m	 Z	 m
 Z
 m Z m Z m Z d d l Z d d l m Z d d l m Z d d l m Z d d l Z d d	 l m Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d
 l m Z m  Z  e! d d d d  d e" d <e# Z$ d   Z% d   Z& d   Z' d   Z( d f  d     YZ) d f  d     YZ* d e* f d     YZ+ d f  d     YZ, d f  d     YZ- d   Z. d    Z/ d!   Z0 d"   Z1 d#   Z2 d$   Z3 d%   Z4 d&   Z5 d'   Z6 d(   Z7 d)   Z8 d*   Z9 e: d+ k r`e9   n  d S(,   sN  
Ocean Optics spectrometer data analyzer 

Program for controlling of the Ocean optics spectrometers and extractin the most important information from spectra

    ## oospecanalyz is free software: you can redistribute it and/or modify
    ## it under the terms of the GNU General Public License as published by
    ## the Free Software Foundation, either version 2 of the License, or
    ## (at your option) any later version.

    ## oospecanalyz is distributed in the hope that it will be useful,
    ## but WITHOUT ANY WARRANTY; without even the implied warranty of
    ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    ## GNU General Public License for more details.

    ## You should have received a copy of the GNU General Public License
    ## along with oospecanalyz.  If not, see <http://www.gnu.org/licenses/>.
  f
i(   t   *Nt   agg(   t   mktimet   strptimet   sleept   asctimet	   localtimet   struct_timet   time(   t   leastsq(   t   interpolate(   t   stats(   t   erf(   t   NullFormattert   ScalarFormattert	   precisioni   t	   linewidthi  t   Aggt   backendc         C   s   t  t |  t  |     d S(   s#   
    Median absolute deviation
    g)Ǻ?(   t   mediant   abs(   t   x(    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   MAD>   s    c      
   C   s   | d k  r- t  |   | k  r- t d   n  t t |    } x< t |  D]. } | | d | d c !|  | | 
| !7+qL W| t |  :} t |  | d d   | | d *t |  | d d  | | d )| S(   s   
    Simple moving average
    i   s/   subset_size must be > 1 and less then len(data)i   (   t   lent
   ValueErrort   zerost   sizet   ranget   floatt   mean(   t   datat   subset_sizet   mvavgt   i(    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   MovingAverageD   s    , "c         C   su  t  j t t |   |  } | } d } t | d | d | d | d t |  |  t t |   d } |  d } |  d } t |  d  } |  d } |  d }	 t |  d t	 d t
 | d  t | | | d d  d t | | | t	 d  |  d |	 |	 d t
 | d | | d | d }
 t |
 t |
  | | f  }
 t |
 d  | }
 |
 S(	   s   
    The most general line shape. xc is position, A amplitude, w width, a skewness, n voitig profile
    Line profile is integrated over single pixels 
    i
   i    iii   i   i   i   (   R
   t   interp1dt   arangeR   t   linspaceR   R   t   diffR   t   sqrtt   pit   expR   t   reshapet   sum(   t   pR   t   x_interpt   x0t   mt   xct   At   wt   at   nt   y(    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   UniversalLineShapeV   s    H



c         C   s^  t  d  } |  d } |  d } t |  d  |  d <| t d | d  } |  d | d <|  d d | |  d | t d t  | d <|  d d | } d | |  d t d d | d t  d t d t d   } d | t d | d | d  | d <d | d t d | t d t  d d d | d t d	 | d <| | d <| S(
   s   
    Calculate real properties of the line shape from function UniversalLineShape. 
    pc[0] - surface under line, pc[1] - FWHM, pc[2] - center of mass, pc[3] - skewness, pc[4] - voitig profile
    i   i   i   i   i   i    g      ?g)\(?g      ?(   R   R   R'   R(   t   log(   R,   t   pcR3   R4   t   dt   f_Lt   f_G(    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   lineCharacteristicsp   s    

0D(H
t   Tokamakc           B   sM   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 RS(   s/   Tokamak properties and methods container class c         C   ss   | j    |  _ |  j   d |  _ d |  _ t   |  _ x5 |  j D]* } |  j j t	 d | d  | g  qA Wd  S(   Nis   0.0.0.0s   SpectralLines/s   .txt(
   t   uppert   namet
   loadConfigt
   shotNumbert   uploadServerIpt   listt   elementst   ElementsListt   appendt   loadtxt(   t   selfR?   (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   __init__   s    
		c         C   s#  t  j j d |  j d  s, t d   n  t j   } | j d |  j d  | j d d  } | |  j k r t d   n  | j	 d d  |  _
 | j	 d d	  |  _ | j d
 d  |  _ | j d
 d  |  _ | j d
 d  |  _ | j d d  } | j d d  j d  j   |  _ d S(   s,   
	Load tokamak properties from config file
	t   tokamak_s   .cfgs$   Tokamak config file does not exists.t   DEFAULTt   Namet   WrongConfigs   Basic parameterss   Plasma lengths   trigger shiftt   Databases   remote database folders   trigger ports   database logint   Elementst   ionss   ', 't    s   '[]N(   t   ost   patht   isfileR?   t	   Exceptiont   ConfigParsert   RawConfigParsert   readt   gett   getfloatt   dataAcqusitionTimet   trigger_shiftt   databaset   getintt   trigger_portt   logint   replacet   stript   splitRE   (   RH   t   configR?   t   elements_str(    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyR@      s    c         C   s  t  j   } | j d d |  j  | j d  | j d d |  j  | j d d |  j  | j d  | j d d |  j  | j d d |  j  | j d d	 |  j	  | j d
  | j d
 d |  j
  t d |  j d d   } | j |  Wd QXd S(   s*   
	Save tokamak properties to config file
	RK   RL   s   Basic parameterss   Plasma lengths   trigger shiftRN   s   remote database folders   trigger ports   database loginRO   RP   RJ   s   .cfgt   wbN(   RV   RW   t   setR?   t   add_sectionR[   R\   R]   R_   R`   RE   t   opent   write(   RH   Rd   t
   configfile(    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt
   saveConfig   s    c         C   s  |  j  d k r d GHd S|  j d k s6 |  j  d k rE t d   n  t j j | d  rv t j d | f d  n  x?t d	  D]1} yt j d
 | |  j	 d |  j |  j
 f |  j   r t d   n  t j j | d  r,t j d
 | d |  j	 d |  j |  j
 d f |  j   r,d GHq,n  t j j | d  rt j d
 | d |  j	 d |  j |  j
 d f |  j   rd GHqn  PWq t k
 r} d G| GHt d  q Xq Wd S(   s/   
	Upload measured data and graphs to database
	s   missing shot numberNs   0.0.0.0is   can not access to databases   spectra.txt_LinesEvolution_.pngs@   convert -resize 150 %sspectra.txt_LinesEvolution_.png %sicon.pngi   i
   s   scp -r %s %s:%st   @s   can not copy datas   icon.pngs   can not copy icont   statuss   can not copys   problem with database access:i   (   RA   t   NoneRB   RU   RR   RS   RT   t   systemR   R`   R]   R   (   RH   t   data_folderR!   t   inst(    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt	   storeData   s*    4<<	c         C   s   |  j  S(   s$   
	Return number of the actual shot
	(   RA   (   RH   (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   actualShotNumber   s    c         C   sj   d   } d } |  j  } | | |  \ } } | d k rJ d |  _ d GHn t |  |  _ | d |  _ d S(   su   
	Waits on the signal from the main server, that database is ready. Also shot number of actual shot  is reclieved. 
	c         S   sm   t  j  t  j t  j  } | j |  | f  | j d  | j   \ } } | j d  } | j   | | f S(   Ni   i   (   t   sockett   AF_INETt   SOCK_STREAMt   bindt   listent   acceptt   recvt   close(   t   hostnamet   portt   st   connt   addrR   (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   netcat   s    
t    s   wrong shot number reclievedi    N(   R_   Ro   RA   t   intRB   (   RH   R   t   HOSTt   PORTR   R   (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   waitOnSoftwareTrigger   s    				c         C   s   t    } t j j |  s$ d } n t j |  j } | } xZ | | k r t    | | k  r t j j |  sy d } n t j |  j } t d  q? W| | k r d Gt t    |  Gd GHt Sd GHt	 Sd S(   s;   
	Detect that spectrometer has reclieved hardware trigger
	i    g?s   hw trigger R   t   timeoutN(
   R   RR   RS   RT   t   statt   st_mtimeR   t   strt   Truet   False(   RH   t   filenamet   tt   t0t
   creat_timet   change_time(    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   waitOnHardwareTrigger  s     		"	[(
   t   __name__t
   __module__t   __doc__RI   R@   Rl   Rs   Rt   R   R   (    (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyR=      s   				 		t   Spectrometerc           B   sD   e  Z d  Z d   Z d   Z e d  Z e d  Z e d  Z	 RS(   s4   Spectrometer properties and methods container class c         C   s   | |  _  | |  _ d  S(   N(   t   SerialNumberR=   (   RH   R   t   tokamak(    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyRI     s    	c         C   s  |  j  d |  j  d |  j } t |  sV t t |  j d | d  | d  } n  t t |  j d  | d  } t   } t } d } x |  j j	 D] } t
 | d |  | k  }	 t |	  r| j | d |	 | d g  d G| d G| d |	 G| d | |	 GHn  t t
 | d |   }
 | d |
 } t
 | |  t
 | |  k r t
 | |  | k  r | } | d } q q Wt |  rd | | f GHn  | t k r| } d } n  | | | f S(	   s   
	Find the closest line to the "wavelength" from lines in tokamak database. If any line was found, amed "unknown" is returned. 
	i   i    i   i   R   s   			s*   			closest line: %4.3f , difference: %2.3ft   unknown(   t   wavelength_ranget
   resolutiont   isnant   minR'   t   wavelength_presicionRC   t   infR=   RD   R   t   anyRF   t   argmin(   RH   t
   wavelengtht   sigmat
   dispersiont	   presiciont   close_line_listt   closest_linet   element_nameR!   t   indext   close_line_it
   close_line(    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   elementIdentification  s.    + 	,6	c   
   
   C   s  |  j  d k rw|  j d k rwt j |  j d d  d f |  j d d  d f d t d d d d } t j |  j d d  d f |  j d d  d f d t d d	 d d } d
 |  j d d  d f d
 |  j	 } t j |  j d d  d f | d t d d	 d d } | | |  | |  | |  |  j
 :} | rt | |  j
 | |  | | | |    qn.|  j  rt j |  j  d d  d f |  j  d d  d f d d }	 | |	 |  :} n t j |  j d d  d f |  j d d  d f d d }	 d
 |  j d d  d f d
 |  j	 } t j |  j d d  d f | d t d d	 d d } | |	 |  | |  :} | rt | |	 |   n  | rt d  t d  t   n  | S(   s   
	Estimate the number of photons necessery to achieve number of counts in "intensity" on the "wavelength". 
	This estimation is not based on real measurement of effectivity, but only as multiplying of effectivity of CCD,
	gratting and optical fiber. 
	Ni    i   t   bounds_errort
   fill_valueg333333?t   kindt	   quadraticg?i
   t   slinears   wavelength [nm]s
   unit/count(   t   absolute_calibrationRo   t   relative_calibrationR
   R#   t   sensor_efficiencyR   t   gratting_efficiencyt   fiber_attenuationt   fiber_lengtht   photonsPerCountt   plott   xlabelt   ylabelt   show(
   RH   R   t	   intensityt
   integ_timet	   make_plott   f1t   f2t   fiber_effectivityt   f3t   f(    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   convertCountToPhotons  s,    JJ)7+4	>>)7


c             s    | } | d  t  |  } | | } x>  | k  rj |    t d  k rj    }  d 7 q- W  | } |  xF  d k r |    d t d  k r    d }  d 8 q~ W  d d k rt |  d   t d  d   n    | t d t t  j d d  d f  d  } | t  j d d  d f  | f }	      f d   }
 t |
 |	 d |   !d d \ } } } } } t | d k  rt
 d-  t } n   j |  } t | d	 | d	  }   d } d
 | d t | d.  | t | d  f GH| t j j j d |  k ra| | | 9} n   j | d t | d/   \ } } } | d d k rt | d <n  | rt   t t  d d  t  d |   } t j | | |  } | | } t |   | d d d t |   d !|  d |  d     d !d d d t | |   | d t
 t  |   d d t |   !    !d t
    d d | t t |  t |  t  |  d   } | t t |  t |  t  |  d   } t |  j | |  d  t |   ! j | |   ! d  t d d d d d d  d | d t | d0  f GHt d  | d d d! d" d# d d$  d1 } xf t  |  D]X \ } } xI t  | d  D]7 \ } } t d  | d | d d" | | t  |   qWqWt! d'  t" d(  t# d)  t$ d* d+  t% d, | d t | d2  | | | d f  t&   n  t' |  t d | d  } t t( | | d | d     t t( | | d | d     | t t) |     | | f S(3   s   
	Identify the peak around the middle in index ix0. The peak is fitted by NLLSQ and
	centre of mass of line is compared with lines from database. Option plot_fit will show 
	the plot with data, their best fit and spectral lines near of this line. 
	i   i   i    i   Nc            s       ! j  |  |  S(   N(   t   fittingFunction(   R,   R   (   t   ixlRH   R   t   ixr(    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   <lambda>  s    t   argst   full_outputt   fvecs.   		wavelength %4.3f +/- %1.3f; chi^2/doF: %4.2fg?i   t   rt   wheret   midt   bt   postt   yerrt   fmts   r.s   b.id   s   b--t   .R5   i   t   lss   --t   labelt	   thresholds   wavelength  %4.3f +/- %1.3fR   s   -.t   ct   gs   Center of massR/   t   kt   tights   $\lambda$ [nm]s   SNR[-]t   loct   bests6   wavelength %4.3f +/- %1.3f; chi^2/doF: %4.2f, A: %4.1f(   i   i   (   i   i   (   i   i   (   i   i   (   R   R   R   R/   R5   R   (   i   i   (*   R   R'   R   t   maxR(   R   t
   inst_widthR	   R   Ro   t   onest   nanR<   R+   t   scipyR   t   chi2t   isfR   t   clfR$   R
   R#   t   stept   errorbarR%   t   amint   amaxR   R   t   axhlinet   axvlinet	   enumeratet   axisR   R   t   legendt   titleR   R&   R   t   diag(    RH   t   ix0R   R   t   plot_fitt   ynR4   R.   R3   t   p0t   funt   poptt   pcovt   infot   mesgt   successt	   popt_corrt   chisqt   doFt	   line_namet   lineR   t   intervt   w_interpt   crop_wt
   crop_w_100t   crop_w_1t   colourR!   t   elementt   jR9   (    (   R   R   RH   R   s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   peakFitting  sx    


)

-9(1-))
D-1..*#3


0
%%c      
   C   s  d GHt    } t    } t |  } t |  } d }	 t j j j d | |	 d  d }
 | | :} d } xwt r| d 7} | d k r d GHPn  t | | d k d  d t	 |  } | d	 | | d	  |	 | d | d +t
 | |
 k   r Pn  t |  } t | t d | |	  t | |	 |  ! t d | |	  } | | } | rt | | d
 d t d | d d d d  t d  t t |  t |   t   n  |  j | | | |  \ } } } } } } |  j | | | d d  | d <|  j | | | d d  | d <| t j j d | | d  9} | j | | | d | d | d | d | d | | d f  t d | | | !| | | !f  t t | d d  t | |  j d  d  } t j t |   | | <ql W| S(   sZ  
	Detect line in the spectra given by field "intensity". Detection is based on chi2 statistics criterium. 
	The detection threshold is set so that false detect of line in the spectrum with purely gaussian noise 
	with sigma equal to readoutNoiseRMS is 1%. This method is more sensitive to weak lines than the naive detection of pixels higher than some threshold. 
	Lines are detected in infinite cycle until any line is detected.  The peak is fitted by function "peakFitting"  
	and at the and of the cycle the peaks are removed. 
	There are two ways how to remove peak. The peak can be substracted from the spectrum or
	the peak can be replaced by gaussian noise. Substrating can be used only of the instumental
	function is measured very precisely and therefore chi2 of the fit is close to 1. Because this is not fulfiled, tzhe second way is now used. 
	
	s   	 identifing linesi   g{Gz?i   i    i   i   s   infinitely cyclei   R   s   culumative sum of squarsR   R   R   R   s   --s    cumulative sum of squars of datag{Gz?i   s   current_peak.txt(   RC   R   R   R   R   R   R   R   t   cumsumR$   t   allt   argmaxR   R   R   R   R   t   xlimR   R   R   R	  R   R   RF   t   savetxtR   R   t   randomt   randn(   RH   R   R   t   readoutNoiseRMSt   debugt   spectraParametrest   line_fitt   lengtht
   derivationt   mean_peak_FWHMt   limR   t   cumSumSpectraR   R.   R   t   sigR   R   R   R   t
   peakInterv(    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   identifyLiness  sH    		$
	
(%>


*!!!A!0(
   R   R   R   RI   R   R   R   R	  R   R  (    (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyR     s   		#(xt   OceanOptics_Specc           B   sw   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z e	 d  Z
 d   Z d	   Z d
   Z d d  Z RS(   sD   child of class Spectrometer with the Ocean Optics specific functionsc         C   sL   t  j |  | |  t d  |  _ d |  j d <t j d  } |  j   d  S(   Ni   i   i    t   SpectralLines(   R   RI   R   t   NonLinCoeffRR   t   listdirR@   (   RH   R   R   R]   (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyRI     s
    c         C   s   d   } d } | d  r$ d GHd Sy t  j d  t  j d  Wn d GHn Xt  j d	 | d
  t j d d  |  j t |  j j d  t d |  j	  f } t  j d | d |  d S(   sA   
	It will start the java constrol program of the spectrometer. 
	c         S   sk   t  j d d t d t  j } | j j   } | j |   } | j j   | j   | d k rc t	 St Sd  S(   Ns   ps -eaft   shellt   stdouti(
   t
   subprocesst   PopenR   t   PIPER"  RX   t   findR|   t   waitR   (   t   process_namet   pst   output(    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   isThisRunning  s    
sT   /opt/OmniDriver-/OOI_HOME/HighResTiming.jar:/opt/OmniDriver-/OOI_HOME/OmniDriver.jart   HighSpeedAcquisitionSamples   spectrometer is already runningNs   ./data/nohup_java.outs;   highspeedacquisitionsample/HighSpeedAcquisitionSample.classs   file can not be removeds   javac -classpath s     HighSpeedAcquisitionSample.javas    HighSpeedAcquisitionSample.classi  s7   nohup chrt -f 99 java -Djava.library.path=. -classpath s[   :. highspeedacquisitionsample.HighSpeedAcquisitionSample %s %i %i > ./data/nohup_java.out &(
   RR   t   removeRp   t   shutilt   copyfileR   R   R=   R[   t	   integTime(   RH   R+  t   java_libraryt   run_options(    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   start  s    		/c         C   sB   t  |  } x/ t |  j  D] \ } } | | | | 7} q W| S(   s,   
	Apply the wavelength correction polynome
	(   t   copyR   t   calibrationPolynomeCorrection(   RH   t   old_wavelengtht   new_wavelegthR!   R3   (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   wavelengthCorrection5  s    c         C   s  d   } t  j j |  j d  s1 t d   n  t j   } | j |  j d  | j d d  } | |  j k r t d   n  | j d d  |  _	 | j d d  |  _
 | j d d	  |  _ | j d d
  |  _ | j d d  } t | |   |  _ | j d d  |  _ | j d d  |  _ | j d d  } | |  |  _ | j d d  } | |  |  _ y% | j d d  } | |  |  _ Wn t j k
 r} d |  _ n Xy% | j d d  } | |  |  _ Wn t j k
 r} d |  _ n X| j d d  } | |  |  _ | j d d  |  _ | j d d  } | |  d |  _ | j d d  } | |  d |  _ | j d d  } | |  d |  _ | j d d  } | |  d |  _ | j d d  } | |  |  _ t  j! |  j d d  d f |  j d d  d f d t" d  d |  _# | j d d!  } | |  |  _$ t  j! |  j$ d d  d f |  j$ d d  d f d t" d  d |  _% | j d d"  |  _& d# G| GHd S($   s3   
	Load the configuration file of the spectrometer
	c         S   s}   |  j    }  t   } x[ |  D]S } | j d  j   } | j t    x% | D] } | d j t |   qN Wq Wt |  S(   Ns   []i(   t
   splitlinesRC   Rb   Rc   RF   R   t   array(   R   t   fieldR   t   rowt   col(    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   parseStringOfFieldF  s    	s   .cfgs)   Spectrometer config file does not exists.s   Basic parameterss   Serial Numbert   WrongSerialNumbert   Detectors   Optical gratings   Wavelength presicion [nm]s   Minimum integration time [ms]s   Opticaly black pixelss   Resolution [px]s   min photons/countt   Efficienciess   Gratting efficiencys   Sensor efficiencys   Absolute calibrations   Overall effectivitys!   optical fibers attenuation (dB/m)s   fiber lengths   Other propertiess   Calibration polynome correctioni    s   Linearity polynome corrections
   Hot pixelss   wavelength ranges    Instrumental broadening functions   width(wavelength)Ni   R   R   s   skewness(wavelength)s   voitig parameters   Loaded data of ('   RR   RS   RT   R   RU   RV   RW   RX   RY   t   detectort
   GratingTypRZ   R   t   min_integ_timet   int_t   black_pixelsR^   R   R   R   R   R   t   NoOptionErrorRo   R   R   R   R5  t   linearityPolynomeCorrectiont	   hotPixelsR   R   R
   R#   R   t   F_inst_widtht   inst_skewnesst   F_inst_skewnesst   voitig_parameter(   RH   R>  Rd   R   t   tmpstrt   err(    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyR@   B  sb    	GGc         C   s;  t  j   } | j d  | j d d d  | j d d |  j  | j d d |  j  | j d d |  j  | j d d t    | j d d |  j  | j d d	 |  j	  | j d d
 |  j
  | j d d |  j  | j d d |  j  | j d  | j d d |  j  | j d d |  j  | j d d |  j  | j d d |  j  | j d  | j d d |  j  | j d d |  j  | j d d |  j  | j d d |  j  | j d  | j d d |  j  | j d d |  j  | j d d |  j  t |  j d d   } | j |  Wd QXd S(   s3   
	Save the configuration file of the spectrometer
	s   Basic parameterst   Types   Ocean Optics Spectrometers   Serial NumberR@  s   Optical gratingt   Dates   Wavelength presicion [nm]s   Opticaly black pixelss   Resolution [px]s   Minimum integration time [ms]s   min photons/countRA  s   Gratting efficiencys   Sensor efficiencys!   Optical fibers attenuation (dB/m)s   fiber lengths   Other propertiess   Calibration polynome corrections   linearity polynome corrections
   Hot pixelss   wavelength ranges    Instrumental broadening functions   width(wavelength)s   skewness(wavelength)s   voitig parameters   .cfgRf   N(   RV   RW   Rh   Rg   R   RB  RC  R   R   RF  R   RD  R   R   R   R   R   R5  RH  RI  R   R   RK  RM  Ri   Rj   (   RH   Rd   Rk   (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyRl     s8    c         C   s   d  S(   N(    (   RH   (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   checkCalibration  s    c         C   s   d  S(   N(    (   RH   t   dryRun(    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   makeCalibration  s    c         C   so   t  d  } | d  | d *t | d d |  j | d  d  | d <|  j | d  | d <|  j | d <| S(   s   
	The lines are not fitted by full set of paramathes of the general peak function UniversalShape. Only a few paramethers are used, other are calculated here. 
	i   i   i   i   i   (   t   emptyR'   RJ  RL  RM  (   RH   R,   t   p_full(    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   calcParamsForFitting  s    -c         C   s   |  j  |  } t | |  S(   N(   RW  R6   (   RH   R,   R   RV  (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyR     s    c         C   sR   |  j  |  } t |  } t | d d |  j | d  d  | d <t |  S(   s   
	The lines are not fitted by full set of paramathes of the general peak function UniversalShape. 
	Therefore the real properties of the peak are calcutelad here from the limited set of the paramethers. 
	
	i   i   (   RW  R<   R'   RJ  (   RH   R,   RV  t   p_corrected(    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyR<     s    -t   spectrac         C   s  t  |  j j |   } | d k r| | d | _ t j j | j  sa t d | j d   n  t | j d  } x(t	 |  D]\ } } | d  d k r t
 t | d d	 !d
   | _ n  | d  d k r t | d d	 ! | _ n  | d  d k rt | d d	 ! | _ n  | d  d k r=t | d d	 ! d | _ n  | d  d k rft | d d	 ! | _ n  | d  d k r| d d	 !} n  | d  d k r Pq q W| |  j k rt d | d |  j   n  t | j  | _ xa t	 |  D]S \ } } | | j k r	Pn  t j d |  }	 |	 j d  }
 t |
  | j | <qW| j d :_ | j | j d |  j j |  j 7_ t   t t | j  d d d  t d! |  j d" d# d d$  t  d dH  t" d% d&  t# | j d' d( d) t   d* Gt$ t | j   Gd+ Gt | j  GH|  j j |  j d, t% | j  d- | j | _ t& |  } | j'   | | j  | _( d | _) x t* | j  D]r } | | d. | j | d, | j !} | j+ j, t- | | j( | | j |   | j) t. | |  j/  d, 7_) qWt0 | j) | j  | _) t j j | d/  rd } t | d/ d  } xj t	 |  D]\ \ } } | d	  j1 d d0  j2   r| } Pn  | d d !d k rTt | d  d1 } qTqTW| j'   t& | d/ d2 | | _3 | j3 t4 | j3 |  j/  8_3 | j3 | j t |  9_3 n  t j j | d3  rd } t | d3 d  } x t	 |  D] \ } } | d	  j1 d d0  j2   r| } Pn  | d d !d k rt | d d	 ! d1 } n  | d d4 !d5 k rQt0 t | d   | _) d6 G| j) GHqQqQW| j'   t& | d3 d2 | | _5 t6 t$ | j5 d7   t$ | j5 d7   d8 k rs| j5 d7 c | j5 d9 | j5 d7 8)d: | _) n  | j5 | j3 | j | 8_5 qt$ | |  j/  | _5 n  | d; k r9
| | d | _ t j j | j  st d | j d   n  t | j d  } d } xt	 |  D]\ } } | d. 7} | j7 d<  } | d	 k rb| | d d	 !} n  | j7 d=  } | d	 k rt
 t | | d> d	 !  | _ n  | j7 d?  } | d	 k rt | | d@ d	 ! | _ n  | j7 dA  } | d	 k rt | | d4 d	 ! | _ n  | j7 dB  } | d	 k rHt | | d d	 ! | _ n  | j7 dC  } | d	 k rt | | d d	 ! | _ n  | j7 dD  } | d	 k r| | d d	 !}
 t8 |
 j9 dE  j:    | _ n  | j7 dF  } | d	 k r	t | | d d	 ! | _) n  | j7 dG  d	 k rPqqW| |  j k rO	t d | d |  j   n  | j'   | j) d: k rt	d: | _) n  t | j d  } t& | d2 | } | j'   | dH dH  d f | _( xT t* | j  D]C } | j+ j, t- | | j( | dH dH  | d. f | j |   q	Wt | j  | _3 t | j  | _5 n  | dI k rt; | _ | | _ t j j | j dJ  s
t d | j dJ d   n  t& | j dJ  | _( t< t6 t= | j(  |  j> d  t6 t? | j(  |  j> d.   d: k r,t dK t= | j(  t? | j(  |  j> d |  j> d. f   n  t j j | j dL  sct d | j dL d   n  t | j dL d  } xV t	 |  D]H \ } } | d. k rt |  | _ n  | dM k rt |  | _ qqW| j'   t j@ | j dL  jA | _ dN | _ t j j | j dO  s8t d | j dO d   n  t& | j dO d2 d, } | dH dH  d f | d |  j j | _ t j j | j dP  rt& | j dP  | _ n  x t% | j  D] } t j j | j dQ tB | d.  d  st d | j dQ tB | d.  d d   n  tC | j dQ tB | d.  d  } | t4 |  8} | j+ j, t- | | j( | | j |   qWtD |  | _) n  | dR k rt; | _ | | dS | _ t j j | j  st d | j d   n  tE jF | j d  } tG | jH    d, | _ x| jH   D]y} | dT k r&| dU k r&| jI |  jJ } t
 | d | d. | d, | dM | dV | d8 d d d f	  | _ | j |  } x t	 |  D] \ } } | j7 dW  d	 k r| j7 dW  } t | | d4 dX ! | _ n  | j7 dY  d	 k rF| j7 dY  } t | | d dZ ! d | _ n  | j7 d[  d	 k r| j7 d[  } | d\ | d] !} qqW| |  j k rt d | d |  j   n  | j'   | j |  } d } t | j  } t | j  | _( tK } tK } tK } x_t	 |  D]Q\ } } | j7 d^  } | j7 d_  d	 k rMtL } d } n  | j7 d`  d	 k rktK } n  | j7 da  d	 k rtL } d } n  | j7 db  d	 k rtK } n  | j7 dc  d	 k rtL } d } n  | j7 dd  d	 k rtK } n  | d	 k r| r%| r%t | | de df ! | | <n  | rR| rRt | | de df ! | j( | <n  | d. 7} qqW| j'   | t4 |  8} | j+ j, t- | | j( |   q&q&WWdH QXt% | j  | j |  j j | _ tD |  | _) n  | dg k rt; | _ | | d | _ t j j | j  s3t d | j d   n  dh   } t j j | d3  rt& | d3 d2 d di d. dj i | d 6| d. 6} | dH dH  d. f | _5 | j5 t4 | j5 |  j/  8_5 n  t | j d  } | jM   } | dk k rt dl   n  d. } xtL r| jM   } | s/| s/t dm   q/n  | d8  dn k ry# t
 t | do d	 !dp   | _ Wqt j jN | j  | _ qXn  | dq  dr k rt | ds d	 ! | _ n  | d4  dt k rt | du d	 ! | _ n  | d  dv k rA| dw d	 !dx |  j dy k r$t | d dw ! d | _ qAt | d d	 ! d | _ n  | d  dz k sa| d  d{ k r| d| d	 !} | |  j k rt d | d |  j   qn  | d4  d} k r| dw d	 !dx |  j dy k rt | d dw ! } qt | d d	 ! } n  | d8  d~ k rPqqW| j d k r+d. | _ n  t& | di d. dj i | d 6| d. 6} | j'   | dH dH  d f | _( | dH dH  d. f } | t4 |  8} | j+ j, t- | | j( |   | d. k rtD |  | _) qd: t0 |  | _) | t4 | |  j/  8} n  | d k r[t; | _ | | d | _ t j j | j  sYt d | j d   n  t | j d  } | jM   } | dk k rt dl   n  tC |  } | j'   t8 | j:    |  j j | _ t j jN | j  | _ | j d	 tG | j  d. | _ tO | d.  d. | _ tO | d  | _ | dH dH  d f | _( xj t* | j  D]Y } | dH dH  | d. f } | t4 |  8} | j+ j, t- | | j( | | j |   qQWtD | |  j/ d. dH  f  | _) t< t6 t= | j(  |  j> d  t6 t? | j(  |  j> d.   d: k r[t dK t= | j(  t? | j(  |  j> d |  j> d. f   q[n  | j |  jP k rt d tB | j  d tB |  jP    n  | S(   s!  
	Load data from file and create object "shot" containig all necessary informations andou the tokamak shot. It supports many different types of files. 
	Java_Data_file  - file experted from java control program HighSpeedAcquisitionSample.java
	Python_Data_file - file exported from this python program
	SpectraSuite_xml - file exported from OceanOptics SpectralSuite
	SpectraSuite_txt - acsii file exported from OceanOptics SpectralSuite
	SpectraSuite_HighSpeed - ascii file exported from OceanOptics SpectralSuite after high speed acquisition
	t   Java_Data_files   .txts
   Data file s    does not exists.R   i   s   Date and time:i   is   %Y.%m.%d %H:%M:%Si   s   Number of spectra:i   i   s   Number pixels in spectra:i   i   s   Integration time [us]:i   g     @@s   Board temperature [C]:i   s   Spectrometer S/N:s   Exact time stamps:s   wrong serial number, s    != s   \s[0-9]+\.[0-9E]+i    g    .AR   R   s   exposure times from timestampsR5   R   s   --s    exposure time R   R   s   _diff_timestamps.pngt   bbox_inchesR   s   read out times    ; i   g      ?i   s   background.txtR   i  t   skip_headers   readoutpatterns.txti   s   RMS^2 of signal:t   RMSi  i   i   i
   t   Python_Data_files   Serial Numbers   Date and time (GMT)i   s   Number of spectrai   t
   Resolutions   Integration time [ms]s   Board temperature [C]s   Time stamps [ms]s   []s	   Noise RMSs   ***************Nt   VW_javas   wavelength_axis.txts'   bad wavelength range %d - %d != %d - %ds   settings.txti   i   s
   timing.txts   temperature.txtt	   spectrum_t   SpectraSuite_xmls	   .ProcSpecs   OOISignatures.xmls   OOIVersion.txti   s   <numberOfPixels>is   <integrationTime>is   <spectrometerSerialNumber>i   is   <double>s1   <com.oceanoptics.omnidriver.spectra.OmniSpectrum>s2   </com.oceanoptics.omnidriver.spectra.OmniSpectrum>s   <channelWavelengths>s   </channelWavelengths>s   <pixelValues>s   </pixelValues>i   it   SpectraSuite_txtc         S   s   t  |  j d d   S(   Nt   ,R   (   R   Ra   (   R   (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyR   M  s    t   skip_footert
   converterss   SpectraSuite Data File
s   wrong file types   corrupted header of files   Date:i   s   %a %b %d %H:%M:%S %Z %Yi$   s$   Number of Sampled Component Spectra:i%   s   Number of Pixelsis   Integration Time (usec):it   (t   )s   Spectrometers:s   Spectrometer Serial Number:is   Spectra Averageds   >>>>>t   SpectraSuite_HighSpeeds   wrong number of pixels, (   i    i    (Q   t   ShotR=   RA   t   DataFileRR   RS   RT   RU   Ri   R   R   R   R   R   t	   n_spectrat   n_pixelsR   R   t   temperatureR   R   t   time_stampst   ret   searcht   groupR\   RD  R   R   R&   R   R0  t   ylimRo   R   t   savefigR   R$   t
   genfromtxtR|   R   R  R   RY  RF   t   Spectrumt   stdRF  R'   Ra   t   isalnumt	   darkNoiseR   t   readOutPatternsR   R&  t   float_Rb   Rc   R   R   t   nanminR   t   nanmaxR   R   R   RG   R   t   zipfilet   ZipFileR   t   namelistt   getinfot	   date_timeR   R   t   readlinet   getctimeR   R   (   RH   RS   t   typR?   t   shotR   R!   R   t   SNR/   R   R   R   t   imaxt   integ_time_backgroundt   integ_time_readoutpatternst   line_headert   indt   timest   myzipR   t   linesR  t   channelWavelengthst   sourceSpectrat   pixelValuesR   t   replaceCommaRz  t   n_avert
   first_line(    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   loadData  s`   
"()2
	#)$

0% 
#!

A		I?
	++0%-	B!
						!
,#		0"	 # !&
	
!-"IB-(   R   R   R   RI   R3  R8  R@   Rl   RR  R   RT  RW  R   R<   R  (    (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyR    s   	 	3		O	'					Rj  c           B   s   e  Z d  Z d   Z d   Z e e d  Z d e d  Z	 d e e d d  Z
 d e e d e d  Z e d  Z d e d	 e d
  Z d   Z d e e e d  Z RS(   sR   Discharge container class

    used for plotting and obtaining discharge data
    c         C   s   | |  _  | |  _ t   |  _ t |  _ t | j  |  _ t | j  |  _	 d |  _
 d |  _ d |  _ t d  |  _ d |  _ d  S(   Ni   i    i   R   (   R   t   shot_numRC   RY  R   R   R   R   Rz  Ry  R  t   maxSNRRl  Ro  Rk  (   RH   R  R   (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyRI     s    						c         C   sP   t  |  j d d } x. t |  j  D] } |  j | j   | | <q% Wt |  S(   s4   
	Detect if there was any plasma during the shot. 
	t   dtypet   bool(   R   Rl  R$   RY  t   plasmaR   (   RH   R  R!   (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt
   plasmaShot  s    c         C   sq   t  |  j j |  j f  } x@ t |  j  D]/ } |  j | j | |  | d d  | f <q+ Wt |  j  | f S(   sT  
	Return the field containig spectra from the all acqusitions during the shot.  
	Option sensitivity_correction set that the correction on the not constant sensitivity of the 
	whole device at different wavelength shold by applied. Option noiseCorrected set that the readout 
	noise patterns, dark noise and hot pixels should be removed. 
	N(	   RU  R   R   Rl  R$   RY  t   getDataR4  R   (   RH   t   sensitivity_correctiont   noiseCorrectedRY  R!   (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyR  
  s    -c         C   s+  d GH| d k r! |  j d } n  d } | d d 7} | d |  j j d 7} | d t t |  j   d 7} | d	 t |  j  d 7} | d
 t |  j j	  d 7} | d d |  j
 d 7} | d d |  j d |  j d t |  j  d d 7} | d d |  j d 7} | d t |  j  d 7} | d d |  j d 7} | d d d 7} t |  j j	 |  j d f  } |  j | d d  d f <xJ t |  j  D]9 } |  j | j d | d t  | d d  | d f <qWt | d  } | j |  t | | d d g d g |  j | j   d S(   s   
	Export data from the shot in well arranged shape. At the bigining of the file is the header and than coloumns with wavelengths and intensities. 
	s   saving data s
   _Data_.txtR   s   Spectrometer:	s   Ocean Optics Spectrometer
s   Serial Number:	s   
s   Date and time (GMT):	s   Number of spectra:	s   Resolution [px]:	s   Integration time [ms]:	s   %2.2fs   Acquisition time [ms]:	ii    i   s   Board temperature [C]:	s   %2.3fs   Time stamps [ms]:	s   Noise RMS [counts]:	s   %2.1fR    id   NR  t   linearityCorrectedR2   R   s   %1.3fs   %4i(   Ro   Rk  R   R   R   R   R   R   Rl  R   R   Ro  R   Rn  R  RU  R   R$   RY  R  R   Ri   Rj   R  R|   (   RH   R   R  t   headerRY  R!   R   (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   saveData  s.    !97$t   pdfc      	   C   s   d GHx t  |  j  D] \ } } |  j | j | d | d t d | | rZ t d  n
 t d  | d
 k rz t   n t |  j d | | d d	 t	   q Wt
   d
 S(   s  
	Plot spectra in separate files. 
	Options:
	file_type  -  file type of exported graph
	log_scale - set log scale
	sensitivity_correction -  correction on the not constant sensitivity of the whole device at different wavelength 
	w_interval - interval if the plotted wavelengths
	s   plotting separate graphst	   log_scaleR  t
   w_intervals	   units [-]s
   counts [-]s
   _Graph_%d.R[  R   N(   R   RY  R   R   R   Ro   R   Rt  Rk  R|   R   (   RH   t	   file_typeR  R  R  R!   t   spectrum(    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyR   C  s    
&

c         C   s  d GHd t  f d     Y} t d d d d d d d	 d
 d d  } t d d d d  |  j   sj t } n  |  j } x= t |  j  D], \ }	 }
 |
 j	   r | r | d 8} q q Wd } x t |  j  D] \ }	 }
 |
 j	   r | r q n  | d 7} | j
 | d |  } | j j t    | j j |    |  j |	 j | d | d t d | | rrt d d d q t d d d q W| j
 | d |  } | j j t     | d k rt   n t |  j d | d d t   d S(   s<  
	Plot spectra in to one plot
	Options:
	file_type  -  file type of exported graph
	log_scale - set log scale
	sensitivity_correction -  correction on the not constant sensitivity of the whole device at different wavelength 
	w_interval - interval if the plotted wavelengths
	plasma  - only spectra with some lines
	s   plotting all togethert   MyFormatterc           B   s   e  Z d d   Z RS(   c         S   s'   | d k r d St  j |  | |  Sd  S(   Ni    R   (   R   t   __call__(   RH   R   t   pos(    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyR  o  s    N(   R   R   Ro   R  (    (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyR  n  s   t   numt   figsizei   t   dpiiP   t	   facecolorR2   t	   edgecolorR   t   wspacei    t   hspacei   R  R  R  s   photons [-]t   rotationt
   horizontals
   counts [-]s   _Graph_.R[  R   N(   i   i   (   R   t   figureRo   t   subplots_adjustR  R   Rl  R   RY  R  t   add_subplott   xaxist   set_major_formatterR   t   yaxisR   R   R   R   Rt  Rk  R|   (   RH   R  R  R  R  R  R  t   figt   n_plotsR!   R  t   i_plott   ax(    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   plot_all`  s8    '		
&
c            sb  g       f d   }   f d   } x t    j  D] \ } } | j   } x | D] } t } xD  D]< }	 |	 d | d k ro t } | | d | | d  Pqo qo W| s\ | | d | d  | | d | | d  q\ q\ Wq= Wt d  }
  rt d  } n t d  } x5 t   j  D]$ } x | D] } |
 j |  q7Wq*W|
    j	 f S(   sP   
	prepare table of identified lines and their properties for another functions
	c            s{    j  t |  | f    r@ t d t t t t t f  } n t d  } x( t   j  D] }  d j  |  q\ Wd  S(   Ni    i(   i    (   RF   RC   R   R   Rl  (   R   R?   t	   emptyCellR!   (   RH   t   detailst
   LinesTable(    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt
   addNewLine  s    !c            s   x  D] } | d |  k r t  | d | t  rL | d | d k rL Pn  t  | d | t t f  r | d | d d k r Pn    r | | d | <n | d | d | <Pq q Wd  S(   Ni    i   (   t
   isinstanceR   RC   t   tuple(   R   t
   time_sliceR   R<  (   R  R  (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   setValue  s    +5i    i   i   R   R   t   areaRO  t   FWHMs   Delta lambda(   s
   wavelengths   line(   R  s   errR  s   errs   Delta lambdas   err(   R  (
   R   RY  t   processSpectrumR   R   RC   R   Rl  RF   Ro  (   RH   R  R  R  R!   R  t   TableR   t   existR<  t   ColoumnNamest   OtherColoumnsR?   (    (   RH   R  R  s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   processShot  s.    #i   c      	   C   s  d GHg  } g  } g  } |  j  t  \ } }	 }
 x |	 D] } | t k r^ | d d k r^ q6 n  | j | d d | d d  t |  j  } t |  j  } xS t | d  D]A \ } } |  j | j   s q n  | d | | <| d | | <q W| j |  | j |  q6 Wt |  d k r'd S|  j } x> t	 |  j d d	  D]' } |  j | d j   rF| } PqFqFWt
 | |  j d  } d } x8 t	 |  j  D]' } |  j | j   r| d } PqqWt d |  } |
 | } |
 | } t | d
 d } t | d d d d d	  } x t |  D] \ } } | | k rEPn  | | d k g } | s}t | d | <t | d | <n  t |
 | | | | d | | | d | | q)Wt d d d t  } | j   j d  t | | d t |  d g  t d  t d  t d  | rUt d d d t t |  d t |  d g  n  | d k rkt   n t |  j d | d d t   t   d S(   s   
	Identify annd plot the most intensive lines and plot their time evolution during the shot
	Options:
	file_type - file type of exported graph
	only_identified - extract only identified lines
	n_lines - number of plotted lines
	
	s   lines evolution plottingi   R   s     %3.1fi    t   nmi   NiR   t   orderR   R   R   R   t   fancyboxg      ?g?s   t [ms]s   Intensity [a.u.]s   The most intense linesR7   t   nonposyt   clipg?s   _LinesEvolution_.R[  R   (   R  R   RF   R   Rl  R   RY  R  R   R$   R   R   R}  t   argsortRo   R   R   t	   get_framet	   set_alphaR   R   R   R   t   yscaleRs  R|  R   Rt  Rk  R|   R   (   RH   R  t   only_identifiedt   n_linest   logscalet   IntensitiesTablet   ErrorsTablet
   NamesTableR  R  Ro  R   R   t	   errorbarsR  R  t
   plasma_endR!   t   plasma_startt   t_startt   t_endt	   IntensMaxt   sort_indR   t   pointst   leg(    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   plotLinesTimeEvolution  st    
!	


5 


'
c         C   s   |  j  t  \ } } } t   } xQ | D]I } x@ | d D]4 } t | d  s9 | j | d | d f  q9 q9 Wq( Wt |  } t | d d  d f | d d  d f d  t   d S(   sg   
	plot the hytorogram of positions of identified lines. It is useful for false line identifications. 
	i   i   i    Ni   R   (   R  R   RC   R   RF   R:  R   R   (   RH   R  R  Ro  t	   hist_listR   t   cell(    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   positionHistorogram3  s    	&0R   c      
   C   s  |  j  |  \ } } } t |  j d | d d  } x( | D]  }	 | j |	  | j d  q= Wg  }
 x+ t |  D] \ } } |
 j | d  qt W| r |
 j   n  x|
 D]} x
| D]} | r | d d k r q n  | d | k r | j d  x | D] } d } t | t  s,t | t	  rgx | D]- } | j t
 j d	 |  d  | d 7} q3Wqt | t  r| j t
 j d
 |  d  n | j t |  d  | d 7} qWq q Wq W| j d S(   s0   
	Export table with lines and their properties
	t
   _Resultes_s   .csvR2   s      ;i    i   R   s   
s   %5.3fs   %3.3fN(   R  Ri   Rk  Rj   R   RF   t   sortR  RC   R  t   localet   formatt   doubleR   t   closed(   RH   t   sufixR  R  R  R  R  Ro  R   R?   t   wavelengthsR!   R<  R  t   cellsR=  R  (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   tableOfResultesE  s6     N(   R   R   R   RI   R  R   R   R  Ro   R  R   R  R  R  R  R  (    (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyRj    s   		)??U	Rv  c           B   sS   e  Z d  Z d d  Z d   Z e e e d d  Z e e e d  Z	 d   Z
 RS(   sV   
    Spectrum container class  used for plotting and preparing of single spectrum
    i    c         C   s=   | |  _  | |  _ | j j |  |  _ | |  _ g  |  _ d  S(   N(   Rj  R   R   R8  R   t
   time_stampt   lineFitList(   RH   Rj  R   R   R  (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyRI     s
    			c         C   s*   t  |  j   |  j j d k  r& t St S(   s,   
	Detect if in the data are some lines    
	i   (   R   R  Rj  R  R   R   (   RH   (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyR    s    "c         C   s0  | s' t  |  j  t |  j  g } n  |  j | d k |  j | d k } |  j | |  | } |  j j } t j j j	 d |  j j
 j  } | r |  j j
 j |  j | |  j j  } n | t |  j j
 j  9} t  | |  }	 | | 9} t | |  }
 t |  j | d d d |  j d d t |  j | | d	 d d
 t d d i d d 6| rt d d d t | d | d |	 d t |
 t |   g  na t | d | d t  |  t |
 t |   g  t | d | d d t |
 t |   g  t d d d t  } | j   j d  d S(   s   
	Plot a single spectrum
	log_scale - set log scale
	sensitivity_correction -  correction on the not constant sensitivity of the whole device at different wavelength 
	w_interval - interval if the plotted wavelengths
	i    i   g?R   R   s   %2.1f msR   g333333?R   g333333?s   $\lambda$ [nm]t   fontdicti   t   fontsizeR7   R  R  i   R   s   center rightR  gffffff?N(   R|  R   R}  R  Rj  R  R   R   t   normR   R   R   R   R   R   R   R  R   R  R   R   R   R   R  R  (   RH   R  R  R  R  t   w_interval_boolR   t   noiseR   t   min_thresholdt   max_thresholdR  (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyR     s,    !$"'
&44-c         C   s   t  |  j  } | rQ | |  j j |  j j 8} t |  | t |  j j j  <n  | r t	 t
 |   } xD t |  j j j d d d   D]  \ } } | | 9} | | 7} q W| | } n  | r |  j j j |  j | |  j j  } n  | S(   sE  
	prepare intensities for  following processing. 
	In the fist step nonlinearity is removed, than if noiseCorrected is allowed the readout patterns,
	dark noise and hot pixels are removed and finally if  sensitivity_correction is allowed 
	the different sentivity of whole device for different wavelengths is corrected.  
	
	Ni(   R4  R   Rj  Rz  Ry  R   RE  R   RI  R   R   R   RH  R   R   R   (   RH   R  R  R  R   t
   correctionR!   R3   (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyR    s    "/
'c         C   sr   |  j    s t   St |  j  d k r/ |  j S|  j t t  } |  j j j	 |  j
 | |  j j t  |  _ |  j S(   Ni    (   R  RC   R   R  R  R   R   Rj  R   R  R   R  t	   debugging(   RH   t   corr_intensity(    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyR    s    *N(   R   R   R   RI   R  R   Ro   R   R   R  R  (    (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyRv    s   			-!c    	      C   s  t  d  }  t d |   } d } | |  _ d d  l } d t |  d } | j |  } t d t |  d d	  } | j | j    | j	   | j
 d
 d d t |   } | j GHx | j D] } | j GHq W| j d  d t d t | j d  d t d t | j d  d t d  S(   Nt   GOLEMs   HR+C1886i   is*   http://golem.fjfi.cvut.cz/operation/shots/sE   /diagnostics/Radiation/1111Spectrometer.ON/data/spectra.txt_Data_.txtt   data_s   .txtR2   R   R^  R  R  R  (   R=   R  RA   t   urllib2R   t   urlopenRi   Rj   RX   R|   R  R   RY  R   R   Ro   R   R   R  R  (	   R  t   HR2000R  R  t   urlt   ut	   localFileR  R  (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   Example0  s"    	
c       	   C   s  d }  t  d  } t d |  } d | _ xt ry5 t j |   } x | D] } t j |  |  qL WWn# t k
 r t j |   d GHn Xt j	 d  t
 d  d GH| j   d	 GH| j   | j d
 d  s t j	 d  GHd GHt
 d  | j   n  d GH| j d d  s!d GHq- n  d GH| j |  d d  } | j d d  d t  | j d d t d t | j d d t d t d d d g | j d d t d t | j d d t d t d d d g t | j  t | j d d d t   t | j  t | j d  d d | j d! t d" t d# t  | j d! t d$ d% d" t d# t  | j d d# t | j d d# t t d& d'  } | j d(  | j   | j |   d) t t     GHt
 d*  q- Wd  S(+   Ns   ./data/R  s   HR+C1886gQ?s   can not remove old filess   killall javag      ?s   waiting on software triggers   software trigger acceptedt   preparedi   s   java killeds   waiting on hardware triggert   readyi<   s   hardware trigger timeouts   hardware trigger acceptedRZ  RY  R   R  t   svgzR  R  R  i   i  t   pngs   _readoutnoise.pngR[  R   s   _darkNoise.pngR  R  R  R  t   fulls   ./data/statusR2   t   0s   data stored i
   (!   R=   R  R0  R   RR   R   R-  t   OSErrort   mkdirRp   R   R   R3  R   R  R  Ro   R  R   R   Rz  Rt  Rk  R   Ry  R  R  Ri   Rj   R|   Rs   R   R   (   RS   R  R  t   dir_listR!   R  t   File(    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   Example1-  sb    			



%%"
c       
   C   s  t  d  }  t d |   } | j d d d  } | j   | j d  d t d t d t | j d	 t d
 t d t  | j	 d d t d } | j
 d j d t d t  x_ t |  j  D]N \ } } x? t | d  D]- \ } } t d | d | | t |   q Wq Wt   x t |  j  D] \ } } | j
 d j d t d t  x? t | d  D]- \ } } t d | d | | t |   q^Wt | d  t   q$Wd  S(   NR  s   HR+C1886s   ./R^  s   spectra.txt_Data_R  R  R  R  R  R  Ro   R   R   R   R/   R5   R   i   i    R   (   R   R   R   R/   R5   R   (   R=   R  R  R  R  Ro   R   R   R  R  RY  R   R   RD   R   R   R   R   (   R  R  R  R  R!   R  R  R   (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   Example2  s&    
)%c    
         s   d }  t  d  } t d |    t j |   } | GH  f d   } x | D] } t j |  |  } xc | D][ } t j |  | d |  } x7 | D]/ } |  | d | d | }	 |	 GH| |	  q Wqk WqK Wd  S(   Ns   ../../data_wladaserv/t   COMPASSs   HR+C1834c            s   y   j  |  d d d  } | j d d t d t | j d  d t d t | j d d t d	 t | j d d t d	 t Wn t k
 r } d
 G| GHn Xd  S(   Nt   /R`  R   R  R  R  R  R  R  t   problem(   R  R  R   R   R   Ro   R  RU   (   t	   directoryR  Rr   (   R  (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   ReadFile  s    R  (   R=   R  RR   R   (
   RS   R  t   spectrometer_dirsR  t   s_dirt	   date_dirst   d_dirt	   shots_dirt	   data_filet   dir_path(    (   R  s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   Example3  s    c       	   C   s6  d }  t  d  } t d |  } t j |   } t   } x | D] } t j |  |  } x | D] } | d d k r` | d  } |  | d d | f } | GHyl | j |  | d d |  }	 |	 j d	 t d
 t d t  |	 j d  d t
 d t
 |	 j d d  d t
  Wq*t k
 r&}
 d G|
 GHq*Xq` q` Wq@ Wd  S(   Ns   ./data_nova/R  s   HR+C1834is	   .ProcSpeciR  Rb  R  R  R  R  R  R   R  R  (   R=   R  RR   R   RC   R  R  R   R  Ro   R   R  RU   (   RS   R  R  t   working_dirsR]   R  t   shotsR   R?   R  Rr   (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   Example4  s&    	
c          C   si  d }  t  d  } t d |  } d | _ | j   GH| j   y5 t j |   } x | D] } t j |  |  qX WWn# t k
 r t j	 |   d GHn Xx t
 rdd GHd GHd GH| j |  d	  d
 GH| j |  d d  } | j d d  d t
  | j d d t
 d t
 | j d d t d t
 | j d t d t
 d t  | j d d t
 | j |   q Wd  S(   Ns   ./data/R  s   HR+C1103i   s   can not remove old filess   waiting on software triggers   software trigger accepteds   waiting on hardware triggers   spectra.txts   hardware trigger acceptedRZ  RY  R   R  R  R  R  R  R  R  (   R=   R  R0  Rt   R3  RR   R   R-  R  R  R   R   R  R  Ro   R  R   R   R  R  Rs   (   RS   R  R  R  R!   R  (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   Example5	  s4    	
		c    
   	   C   s  d }  t  d  } t d |  } | j |  d d  } | j d  d t | j d t  \ } } t |  GHx t	 | j
  D] \ } } | j d j d t d t  x7 t	 | d  D]% \ } }	 t d	 |	 d
 d d d  q Wt | d  t d  t   q| Wd  S(   Ns   ./calibration_8-3-2012/R  s   HR+C1886Rc  t   Hg2R  R  i    R   R   s   --R   R   i   s   counts [a.u.](   R=   R  R  R   Ro   R   R  R   R+   R   RD   RY  R   R   R   R   (
   RS   R  R  R  R2   R   R!   R  R  R   (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   Example63	  s    
c          C   s>  d g t  d d  }  t d d  } t d d  } t d  } t d |  } x|  D]} d } t | | k  r} d } n  t | | k  r d	 } n  d
 t |  GH| | _ d t |  d } y t j |  } Wn d
 t |  d GHqV n Xt	 d t |  d d  }	 |	 j
 | j    |	 j   | j d d d t |   }
 t | j  } x- |
 j D]" } | j   ri| | j 7} qiqiWd } x t | j  D] \ } } x? t | d  D]- \ } } t d | d | | t |   qWt d | d d t |  d |  t |
 j |  t   qWqV Wd  S(   Ni  i{  i  i  R  s   HR+C1886R   t   Het   Hs   shot number s*   http://golem.fjfi.cvut.cz/operation/shots/sE   /diagnostics/Radiation/1111Spectrometer.ON/data/spectra.txt_Data_.txts    does not existR  s   .txtR2   R^  R   R   R   R/   R5   R   i    R   s   ion: i   s	   ;  shot #s   ; fill gas: (   R   R   R   R/   R5   R   (   R   R$   R=   R  R   R   RA   R  R  Ri   Rj   RX   R|   R  R   R   RY  R  R   R   RD   R   R   R   R   R   R   (   R$  t   He_gast   H_gasR  R  R  t   gasR  R  R  R  t   integ_spectrumR  R  R!   R  R  R   (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   Example7M	  sD    			
%(c         C   s  t  d  } t d |  } x |  D] } t j j d t |  d  rN q" n  d t |  GH| | _ d t |  d } y t j |  } Wn d t |  d GHq" n Xt	 d t |  d d	  } | j
 | j    | j   q" Wxt rd
 GHt d  } | d k rd } n  t |  } t d  }	 |	 d k rGd }	 n  t |	  }	 t   }
 x4 |  D], } |
 j | j d d d t |    qcWt   } x] |
 D]U } | j t | j   x6 | j D]+ } | j   r| d c | j   7<qqWqW|
 d j } t | | k | |	 k   } d) } d* } d } xW t t | |   D]@ \ } } t | | | d | | | d d d  d! | d" qMWx t | j  D] \ } } t d# d d! | d" d | | t |  d$ | | t |   x t | d  D]{ \ } } | |	 k s| | k  r'qn  t d# | | d | | t |  d$ | | t |   t  | | d | d"  qWqWt! d% d& d' t  } | j"   j# d(  t$ | |	  t%   q Wd  S(+   NR  s   HR+C1886R  s   .txts   shot number s*   http://golem.fjfi.cvut.cz/operation/shots/sE   /diagnostics/Radiation/1111Spectrometer.ON/data/spectra.txt_Data_.txts#    does not contain spectrometer dataR2   s=   set the wavelength interval for the spectral lines comparisons   from [nm] (200 default): R   i   s   to [nm] (1200 default): i  R^  ii    R   R   R   R/   R   R   t   -s   --s   -.t   :g?i   R   i   R   i   R   R   R   s   center rightR  gffffff?(   R   R   R   R/   R   R   (   R0  s   --s   -.R1  (&   R=   R  RR   RS   RT   R   RA   R  R  Ri   Rj   RX   R|   R   t	   raw_inputR  RC   RF   R  R   R   RY  R  R  R   R   R   t   zipR   RD   R   R   t   textR   R  R  R  R   (   R$  t   namesR  R  R  R  R  R  t   lR   t
   shots_listR.  R  R  R   t   intervalR  t   stylet   shiftR!   t   shot_spectrumR  R  R   R  (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   Example8	  sj     					*	!"8?9 c    
      C   si  t  d  }  t d |   } x@t d d  D]/} t j j d t |  d  s d t |  GH| |  _ d t |  d	 } y t j	 |  } Wn
 q+ n Xt
 d t |  d d
  } | j | j    | j   n  | j d d d t |   } | d 7} xT | j D]I } | j   r| j   d d !}	 t |	 t |	   t d d   qqWq+ Wt   d  S(   NR  s   HR+C1886i(#  i#  R  s   .txts   shot number s*   http://golem.fjfi.cvut.cz/operation/shots/sE   /diagnostics/Radiation/1111Spectrometer.ON/data/spectra.txt_Data_.txtR2   R   R^  i   i  i  i    (   R=   R  R$   RR   RS   RT   R   RA   R  R  Ri   Rj   RX   R|   R  RY  R  R  R   R   Rs  Ro   R   (
   R  R  R  R  R  R  R  R!   R  t   ha(    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   Example9	  s,     	
c       
   C   s  t  d  }  t d |   } t   } t   } t   } t   } t   } t   } t   } t d(  }	 t   }
 t   } d } d d d d	 d
 d d d g } xt d d  D]} | GH| | k r d GHq n  t j j | d t |  d  sbt j j | d t |  d  sbt j j | d t |  d  sbt j j | d t |  d  rVt j j | d t |   rd GHq n  d t |  d } d t |  d } d t |  d } d t |  d } y@ t	 j
 |  } t	 j
 |  } t	 j
 |  } t	 j
 |  } Wn6 t | d t |  d  } | j   d GHq n Xt | d t |  d d  } | j | j    | j   t | d t |  d d  } | j | j    | j   t | d t |  d d  } | j | j    | j   t | d t |  d d  } | j | j    | j   d GHn  | j d d  | d t |   } | j   sq n  d! t |  GH| |  _ | j   \ } } x | j D]y } | j   r| j t t | j      | j | j  | j t | d t |  d   | j | j  qqW|
 j |  t t | d t |  d   t t | d t |  d   } | j |  q Wt d" |  t d# |  t d$ |  t d% |  t d& |
  t d' |  d  S()   NR  s   HR+C1886i    i   s   ./net_data/i  i4  i9  i  i  i  i  i  i  i%  t   wrongR  s   .txtt   p_t   s_t   e_t   no_data_t   nenis*   http://golem.fjfi.cvut.cz/operation/shots/sE   /diagnostics/Radiation/1111Spectrometer.ON/data/spectra.txt_Data_.txts   /Initial_PfeifferMerkaVakuas   /basicdiagn/PlasmaStarts   /basicdiagn/PlasmaEndR2   t   downloadR   R^  s   shot number R   R   Rn  t   pressuret
   shot_namest   dates(   i    i   (   R=   R  RC   R   R$   RR   RS   RT   R   R  R  Ri   R|   Rj   RX   R  R  RA   R  RY  R  RF   t   singleR4  Rn  RG   R   R   t   save(   R  R  t   Nt   centerR   t	   data_fullRF  Rn  RH  t   sesion_linesRG  t   PlasmaLengthsRS   t
   wrong_shotR  R  t   url_pt   url_st   url_eR  t   u_pt   u_st   u_eR  R  R   t   intensitiesR  R  (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt	   Example10O
  s    									 
!
!
!
!
	#	%Flc           C   s   t    d  S(   N(   R  (    (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   main_  s    t   __main__(;   R   t   numpyt
   matplotlibt   uset   matplotlib.pyplotRR   R   R   R   R   R   R   R   Rp  t   scipy.optimizeR	   t   scipy.interpolateR
   R   R   t   scipy.statst   scipy.specialR   RV   R~  Ru   R.  R  R#  R  t   matplotlib.tickerR   R   t   set_printoptionst   rcParamsR   R  R   R"   R6   R<   R=   R   R  Rj  Rv  R	  R  R  R"  R%  R&  R(  R/  R<  R>  RX  RY  R   (    (    (    s\   ../includes/diagnostics/Radiation/1111Spectrometer.ON/ControlSoftware/SpectrometerControl.pyt   <module>   sj   

4
				  J    		^	#	3	$	.		?	W	l	 	