B
    d                 @   sH   d Z ddlZddlZddlZddlmZ dd ZdddZdd
dZ	dS )z#Utilities to read PSDs from files.
    N)FrequencySeriesc             C   s>  | d |krt dt| t|| }|| }| d |kr@dnt| |d }| |d  |krh|d7 }| |d } ||d }|d | | d krtd|d | | d  t| d | d }t| }t|}	tj	j
||	|	d |	d fdd}
tj|tjd	}tt||| }t|
|||d< t||d
S )aq  Interpolate n PSD (as two 1-dimensional arrays of frequency and data)
    to the desired length, delta_f and low frequency cutoff.

    Parameters
    ----------
    freq_data : array
        Array of frequencies.
    noise_data : array
        PSD values corresponding to frequencies in freq_arr.
    length : int
        Length of the frequency series in samples.
    delta_f : float
        Frequency resolution of the frequency series in Herz.
    low_freq_cutoff : float
        Frequencies below this value are set to zero.

    Returns
    -------
    psd : FrequencySeries
        The generated frequency series.
    r   zNLowest frequency in input data  is higher than requested low-frequency cutoff    NzRequested number of samples exceeds the highest available frequency in the input data, will use max available frequency instead. (requested %f Hz, available %f Hz)F)Z
fill_valueZbounds_error)Zdtype)delta_f)
ValueErrorstrintnumpyZsearchsortedloggingwarninglogscipyZinterpolateZinterp1dZzerosZfloat64arangeexpr   )	freq_data
noise_datalengthr   low_freq_cutoffZkminZflowZ
data_startZflogZslogZ
psd_interppsdvals r   [/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/pycbc/psd/read.pyfrom_numpy_arrays   s,     

r   Tc             C   sr   t | }|dk  s*t t | r6td|  |dddf }|dddf }|rb|d }t|||||S )a  Read an ASCII file containing one-sided ASD or PSD  data and generate
    a frequency series with the corresponding PSD. The ASD or PSD data is
    interpolated in order to match the desired resolution of the
    generated frequency series.

    Parameters
    ----------
    filename : string
        Path to a two-column ASCII file. The first column must contain
        the frequency (positive frequencies only) and the second column
        must contain the amplitude density OR power spectral density.
    length : int
        Length of the frequency series in samples.
    delta_f : float
        Frequency resolution of the frequency series in Herz.
    low_freq_cutoff : float
        Frequencies below this value are set to zero.
    is_asd_file : Boolean
        If false assume that the second column holds power spectral density.
        If true assume that the second column holds amplitude spectral density.
        Default: True

    Returns
    -------
    psd : FrequencySeries
        The generated frequency series.

    Raises
    ------
    ValueError
        If the ASCII file contains negative, infinite or NaN frequencies
        or amplitude densities.
    r   zInvalid data in Nr      )r	   ZloadtxtanyZlogical_notisfiniter   r   )filenamer   r   r   Zis_asd_file	file_datar   r   r   r   r   from_txtU   s    "

r   r   c          	   C   s   ddl }ddlm} t| d.}|jj}	|j|d|	d}
|jj|
|d}W dQ R X |dk	rd|| }n:t|	 dkr|t
|	 d  }nd	}|d
7 }t||jjdd }tt||j }t|||||S )a  Read an ASCII file containing one-sided ASD or PSD  data and generate
    a frequency series with the corresponding PSD. The ASD or PSD data is
    interpolated in order to match the desired resolution of the
    generated frequency series.

    Parameters
    ----------
    filename : string
        Path to a two-column ASCII file. The first column must contain
        the frequency (positive frequencies only) and the second column
        must contain the amplitude density OR power spectral density.
    length : int
        Length of the frequency series in samples.
    delta_f : float
        Frequency resolution of the frequency series in Herz.
    low_freq_cutoff : float
        Frequencies below this value are set to zero.
    ifo_string : string
        Use the PSD in the file's PSD dictionary with this ifo string.
        If not given and only one PSD present in the file return that, if not
        given and multiple (or zero) PSDs present an exception will be raised.
    root_name : string (default='psd')
        If given use this as the root name for the PSD XML file. If this means
        nothing to you, then it is probably safe to ignore this option.

    Returns
    -------
    psd : FrequencySeries
        The generated frequency series.

    r   N)utilsrbauto)compressZcontenthandler)	root_namer   z4No ifo string given and input XML file contains not z3exactly one PSD. Specify which PSD you want to use.)Z
lal.seriesZligo.lwr   openZseriesZPSDContentHandlerZload_fileobjZread_psd_xmldoclenkeystupler   datar	   r   ZdeltaFr   )r   r   r   r   Z
ifo_stringr#   ZlalZligolw_utilsfpZ
ct_handlerZxml_docZpsd_dictZpsd_freq_serieserr_msgr   r   r   r   r   from_xml   s$    !

r+   )T)Nr   )
__doc__r
   r	   Zscipy.interpolater   Zpycbc.typesr   r   r   r+   r   r   r   r   <module>   s   <
/ 