B
    d#                 @   s   d Z ddlmZ ddlmZ ddlmZ ddlZddlZddl	Z
ejjjZdd Zdd	 Zd1d
dZdd Zd2ddZdd Zd3ddZdd Zd4ddZdd Zd5ddZdd Zd6ddZd d! Zd7d"d#Zd$d% Zd8d&d'Zd(d) Z d9d+d,Z!d:d-d.Z"ej#ej#G d/d0 d0ej$Z%dS );ak  
Code to assist in reading and writing LAL time- and frequency series data
encoded in LIGO Light-Weight XML format.  The format recognized by the code
in this module is the same as generated by the array-related functions in
LAL's XML I/O code.  The format is also very similar to the format used by
the DMT to store time- and frequency-series data in XML files,
    )ligolw)array)paramNc       
      C   s$  t tdt| jji}|d k	r4||t  _	|t j
| jd |tjjd| jdd t| |}t| jjrttt| jj| | jjj| jjjf}n$ttt| jj| | jjf}tjj| j||d}t| j|_ |!t j"j#d }	||	_ | j|	_$||	_%|| |S )NNameepochf0zs^-1)unit)	dim_namesr   )&r   LIGO_LW
Attributessix	text_type	__class____name__appendChildCommentpcdataTimeZfrom_gpsr   ligolw_paramParamfrom_pyvaluer   getattrnpiscomplexobjdataZ	row_stackZarangelenrealimagligolw_arrayArraybuildnamestrZsampleUnitsUnitgetElementsByTagNameDimtagNameZStartScale)
seriesr	   commentZ
delta_name
delta_unitelemdeltar   aZdim0 r.   W/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/lal/series.py_build_series2   s"    
0$
r0   c             C   s  |  tjj\}|  tjj\}| tjj}t| d}|jdkrLt	d|j
}td}t|}	tt|j}
tt|d j}tt|j}|t|j||j
t|
|  |d jt||	  |t|jj}t|jjr|jd d|jd   |j_n|jd |j_|S )	Nr   ZGPSzepoch Type must be GPSzs^-1r      y              ?   )r$   r   r   r&   r   r%   r   	get_paramType
ValueErrorr   lalr#   r"   r   floatr'   r   r   Tr   r   r   )r+   ZcreatorfuncZdelta_target_unit_stringtr-   Zdimsr   r   Zinverse_seconds_unitZdelta_target_unitZf0_unitr*   Zsample_unitr(   r.   r.   r/   _parse_seriesG   s.    


r:   c             C   s    t | tjstt| d|ddS )N)zFrequency,Real	FrequencydeltaFzs^-1)
isinstancer6   ZREAL4FrequencySeriesAssertionErrorr0   )r(   r)   r.   r.   r/   build_REAL4FrequencySeriess   s    r?   c             C   s   t | tjdS )Nzs^-1)r:   r6   ZCreateREAL4FrequencySeries)r+   r.   r.   r/   parse_REAL4FrequencySeriesx   s    r@   c             C   s    t | tjstt| d|ddS )N)zFrequency,Realr;   r<   zs^-1)r=   r6   REAL8FrequencySeriesr>   r0   )r(   r)   r.   r.   r/   build_REAL8FrequencySeries|   s    rB   c             C   s   t | tjdS )Nzs^-1)r:   r6   ZCreateREAL8FrequencySeries)r+   r.   r.   r/   parse_REAL8FrequencySeries   s    rC   c             C   s    t | tjstt| d|ddS )N)zFrequency,Real,Imaginaryr;   r<   zs^-1)r=   r6   ZCOMPLEX8FrequencySeriesr>   r0   )r(   r)   r.   r.   r/   build_COMPLEX8FrequencySeries   s    rD   c             C   s   t | tjdS )Nzs^-1)r:   r6   ZCreateCOMPLEX8FrequencySeries)r+   r.   r.   r/   parse_COMPLEX8FrequencySeries   s    rE   c             C   s    t | tjstt| d|ddS )N)zFrequency,Real,Imaginaryr;   r<   zs^-1)r=   r6   ZCOMPLEX16FrequencySeriesr>   r0   )r(   r)   r.   r.   r/   build_COMPLEX16FrequencySeries   s    rF   c             C   s   t | tjdS )Nzs^-1)r:   r6   ZCreateCOMPLEX16FrequencySeries)r+   r.   r.   r/   parse_COMPLEX16FrequencySeries   s    rG   c             C   s    t | tjstt| d|ddS )N)z	Time,Realr   deltaTs)r=   r6   ZREAL4TimeSeriesr>   r0   )r(   r)   r.   r.   r/   build_REAL4TimeSeries   s    rJ   c             C   s   t | tjdS )NrI   )r:   r6   ZCreateREAL4TimeSeries)r+   r.   r.   r/   parse_REAL4TimeSeries   s    rK   c             C   s    t | tjstt| d|ddS )N)z	Time,Realr   rH   rI   )r=   r6   ZREAL8TimeSeriesr>   r0   )r(   r)   r.   r.   r/   build_REAL8TimeSeries   s    rL   c             C   s   t | tjdS )NrI   )r:   r6   ZCreateREAL8TimeSeries)r+   r.   r.   r/   parse_REAL8TimeSeries   s    rM   c             C   s    t | tjstt| d|ddS )N)zTime,Real,Imaginaryr   rH   rI   )r=   r6   ZCOMPLEX8TimeSeriesr>   r0   )r(   r)   r.   r.   r/   build_COMPLEX8TimeSeries   s    rN   c             C   s   t | tjdS )NrI   )r:   r6   ZCreateCOMPLEX8TimeSeries)r+   r.   r.   r/   parse_COMPLEX8TimeSeries   s    rO   c             C   s    t | tjstt| d|ddS )N)zTime,Real,Imaginaryr   rH   rI   )r=   r6   ZCOMPLEX16TimeSeriesr>   r0   )r(   r)   r.   r.   r/   build_COMPLEX16TimeSeries   s    rP   c             C   s   t | tjdS )NrI   )r:   r6   ZCreateCOMPLEX16TimeSeries)r+   r.   r.   r/   parse_COMPLEX16TimeSeries   s    rQ   psdc             C   sl   |dkrt  }|t td|i}x>|  D ]2\}}|t|}|dk	r2|tj	d| q2W |S )aw  
    Construct an XML document tree representation of a dictionary of
    frequency series objects containing PSDs.  See also read_psd_xmldoc()
    for a function to parse the resulting XML documents.

    If xmldoc is None (the default), then a new XML document is created and
    the PSD dictionary added to it inside a LIGO_LW element.  If xmldoc is
    not None then the PSD dictionary is appended to the children of that
    element inside a new LIGO_LW element.  In both cases, the LIGO_LW
    element's Name attribute is set to root_name.  This will be looked for
    by read_psd_xmldoc() when parsing the PSD document.
    Nr   
instrument)
r   ZDocumentr   r
   r   itemsrB   r   r   r   )Zpsddictxmldoc	root_nameZlwrS   rR   fsr.   r.   r/   make_psd_xmldoc   s    rX   c                sp    dk	r& fdd|  tjjD \} tdd |  tjjD }x(|D ] }t|| jjdkrHd||< qHW |S )a  
    Parse a dictionary of PSD frequency series objects from an XML
    document.  See also make_psd_xmldoc() for the construction of XML
    documents from a dictionary of PSDs.  Interprets an empty frequency
    series for an instrument as None.

    The XML document tree is searched for a LIGO_LW element whose Name
    attribute is root_name (default is "psd").  If root_name is None all
    REAL8Frequency series objects below xmldoc are included in the return
    value.
    Nc             3   s&   | ]}| d r|j kr|V  qdS )r   N)hasAttributer   ).0r+   )rV   r.   r/   	<genexpr>   s    z"read_psd_xmldoc.<locals>.<genexpr>c             s   s6   | ].}| d r|jdkrt|dt|fV  qdS )r   rA   rS   N)rY   r   r   Zget_pyvaluerC   )rZ   r+   r.   r.   r/   r[      s    r   )r$   r   r
   r&   dictr   r   )rU   rV   resultrS   r.   )rV   r/   read_psd_xmldoc   s    
r^   c               @   s   e Zd ZdZdS )PSDContentHandlerzA content handler suitable for reading PSD documents. Use like this:

    >>> from ligo.lw.utils import load_filename
    >>> xmldoc = load_filename('psd.xml', contenthandler=PSDContentHandler)
    >>> psds = read_psd_xmldoc(xmldoc)
    N)r   
__module____qualname____doc__r.   r.   r.   r/   r_      s   r_   )N)N)N)N)N)N)N)N)NrR   )rR   )&rb   Zligo.lwr   r   r   r   r   r6   r   numpyr   saxZ	xmlreaderZAttributesImplr   r0   r:   r?   r@   rB   rC   rD   rE   rF   rG   rJ   rK   rL   rM   rN   rO   rP   rQ   rX   r^   Zuse_inZLIGOLWContentHandlerr_   r.   r.   r.   r/   <module>   s<   
,









