3
_
                 @   s   d Z dd ZdS )a<  
******************************************************    
***   Program for the convolution of line profile  ***
***         with the instrumental response         ***         
****************************************************** 
This code is part of the LIFELINE program.

Copyright (C) 2020-2021 University of Liege (Belgium)
Enmanuelle Mossoux (STAR Institute)
This program 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 3 of the License, or any later version.
This program 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 this program.
If not, see <http://www.gnu.org/licenses/>.

Run: from observed_line_profile_main import convolve
     p=convolve(direct_LP, file_LP, direct_rmf_arf, RMF, ARF, distance)

# Parameters 
# ==========
direct_LP - Directory where the line profile file is located
file_LP - file containing the theoretical line profile
direct_rmf_arf - Directory where the response matrix file (RMF) and ancillary response file (ARF) are located
RMF - Response matrix file
ARF - Ancillary response file
Distance - Distance to the observed binary [kpc].

For Athena, you can use the files computed for a XIFU mirror module radius Rmax=1190mm, a 2.3mm rib spacing and a on-axis case given in the main directory of the code.
    RMF: XIFU_CC_BASELINECONF_THICKFILTER_2018_10_10.rmf.
    ARF: XIFU_CC_BASELINECONF_THICKFILTER_2018_10_10.arf.

# Output
# ======
p - numpy array containing the convolved emission in each bin

# Versions
# ========
v1 - 03/03/2020
c       B      C   s  ddl m} dd l}dd l}	dd l}
dd l}ddlm} dd lj	}dd l
}dd l}t| d | d}g }g }g }xp|j }|sP |jd}|d dkrt|d	 }|d d
kr|jt|d  |jt|d  qr|j| qrW |j  |j||j|j|  }|j|j|  }d| }|| d |d | }d| }d| d|j |d |d d   }|dd  |d d8  d }|j|d |d  g||d9 |d:  gf}|j|}|j|d | }|d j}d}d}d|krd}d}|| jjd} || jjd}!|| jjd}"|| jjd}#t|#jdkrP|#d d  d }#|| jjd}$t|$jdkr|$d d  d }$|| jjd}%|| jjd}&|| jjd}'|'|& d }(g })t| t||d  kst|!t||d  k rtd td |
j   xt!t"t|D ]}*||* }+|j#| |+|d  k d },|j#|!|+|d  kd }-|j$t"t|%t"|-d |,d;  d f}.x(t!t"|-d |,d<  d D ]}/|#t"|/|,d=   }0|$t"|/|,d>   }1|j%|1}2t&|1t'rxd}3t|1dkrB|j(|"t"|/|,d?   d d  }4|j#|2|4k d }3|3d d }3|0|3 }0|1|3 }1|"t"|/|,d@   |3|3|1 d  }5n|"t"|/|,dA   d d  }5dd |5D |.|0|0|1 |/f< qW |j)|.dd }6||(j*d!|6j*d!d"d#}7|7|}8|)j|8 q W |d$ d% }9|d&kr~|j|d | }:|:d jjd} |:d jjd}!|:d jjd'};| |! d }(||(j*d!|;j*d!d"d#}7|7|}9g }<xBt!t"t|D ].}*|<jt+|j,||)d d  |*  |9  qW ||j|< d( |j| }<|j-|<}=td)t.|d  d* t.t"|=  |<| }<t| d |d dB  d, d-}>xpt!t"t|D ]\}?|?t|d kr|>j/d.t.|d  d/ t.t"|= d0  |>j/d1 P |>j/||?  qHW x@t!t"t|D ],}@|>j/t.|dC d t.|<|@  d0  qW |>j  |<|j-|< }<t|<d2 }A|j0||< |j0||gd$d%gdd3 |j1d$|A |j2d4 |j3d5 |j4| d |d dD  d6  |j  td& td7|d dE  d6  d S )FN    )interp1d)	constante/r    ZEnergy   #g0O(@g     @@cgqىEg      @g     j@pc_m   g       @ZENERG_LOZENERG_HIZMATRIXZF_CHANZN_CHANZCHANNELZE_MINZE_MAXz<At least part of the line profile is not covered by the RMF.z>Change the RMF file and run 'python observed_line_profile.py'.c             S   s   g | ]}|qS  r   ).0xr   r   C/local/network/htdocs/Lifeline/codes3/observed_line_profile_main.py
<listcomp>   s    zconvolve.<locals>.<listcomp>)axisf8cubic)kindg        g      ? ZSPECRESPg   DAz&The number of photons observed during zks is:    z_convolved.datawz(# The number of photons observed during zks is 
z3# tangential velocity (km/s) | spectrum (photon/s)
g?)r
   zEnergy (keV)zNormalized convolved emissionz_convolved.pdfzThe output file is r   r   r   r   r   r   r   r   r   r   r   )5scipy.interpolater   numpyossysmath
constantesr   matplotlib.pyplotpyplotpandaspyfitsopenreadlinesplitfloatappendclosearrayargsortsortpiconcatenatemedianheaderdatafieldlenshapeminmaxprintexitrangeintwherezeroscumsum
isinstancelistargmaxmeanastypeabssumnansumstrwriteplotylimxlabelylabelsavefig)BZ	direct_LPZfile_LPdirect_rmf_arfRMFARFdistance	expo_timer   npr   r    r!   r   pltpdr&   ZfLPvtangZemiss_thZphraseslinevecenergy
wavelengthwaveZ	energy_thbinsZ
bin_lengthZrmfZ
rmf_headerZext_1Zext_2Z	energy_loZ	energy_himatrixZf_chanZn_chanchannelZe_minZe_maxZenergy_lo_hiZmatrix_totalibinZ
energy_binind1ind2Z
matrix_newZiindZf_chan_hereZn_chan_herecum_sumZ
ind_cumsumindZmatrix_hereZmean_matrixZfinterpZmatrix_interpZarf_binZarfZspecrespZLP_convZ
nbr_photonfprofileZiphraseiy_limitr   r   r   convolve,   s    
 
&,


4*&
"&$
.
$*
,

ri   N)__doc__ri   r   r   r   r   <module>*   s   