B
    â‹dZ…  ã               @   s²  d 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	Z	ddl
Z
ddlZddlmZ ddlmZmZ ejejejejejejejgejejejejejejejgejej ej!ej"ej#ej$ej%gej&ej'ej(ej)ej*ej+ej,gej-ej.ej/ej0ej1ej2ej3gej4ej5ej6ej7ej8ej9ej:giZ;dd„ Z<dd„ Z=dd
d„Z>ddd„Z?ddd„Z@d dd„ZAd!dd„ZBddddgZCdd„ ZDG dd„ deEƒZFG dd„ deFƒZGG dd„ deEƒZHdS )"zP
This modules contains functions for reading in data from frame files or caches
é    N)Úurlparse)Ú
TimeSeriesÚzerosc             C   sJ   t  | |¡}t| d }t| d }||| ||dƒ}t|jj|j||dS )z Get channel using lalframe r   é   )Údelta_tÚepochÚdtype)ÚlalframeÚFrStreamGetTimeSeriesTypeÚ_fr_type_mapr   ÚdataÚdeltaT)ÚchannelÚstreamÚstartÚdurationÚchannel_typeÚ	read_funcZd_typer   © r   ú^/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/pycbc/frame/frame.pyÚ_read_channelM   s    r   c          	   C   sF   y,t | dƒ}| d¡dkr dS W dQ R X W n tk
r@   Y nX dS )zcTest if a file is a frame file by checking if its contents begins with
    the magic string 'IGWD'.Úrbé   s   IGWDTNF)ÚopenÚreadÚIOError)Ú	file_pathÚfr   r   r   Ú_is_gwfW   s    r   Fc             C   sÌ   t  ¡ }x¾| D ]¶}t |¡}|rFdd„ }|s8td| ƒ‚t||dg}x||D ]t}tj |¡\}}tj |¡\}	}
|
dkr„t  	|¡}n0|
dks”t
|ƒr¬t t|ƒt|ƒ¡j}ntdƒ‚t  ||¡}qLW qW |S )a&   Return a cumulative cache file build from the list of locations

    Parameters
    ----------
    locations : list
        A list of strings containing files, globs, or cache files used to
        build a combined lal cache file object.
    latest : Optional, {False, Boolean}
        Only return a cache with the most recent frame in the locations.
        If false, all results are returned.

    Returns
    -------
    cache : lal.Cache
        A cumulative lal cache object containing the files derived from the
        list of locations.
    c             S   s&   yt j | ¡S  tk
r    dS X d S )Nr   )ÚosÚpathÚgetctimeÚOSError)Úfnr   r   r   Úrelaxed_getctimey   s    z,locations_to_cache.<locals>.relaxed_getctimez!no frame or cache files found in )Úkey)z.lcfz.cachez.gwfzInvalid location name)ÚlalÚCacheÚglobÚ
ValueErrorÚmaxr   r    ÚsplitÚsplitextZCacheImportr   r	   ZFrOpenÚstrÚcacheÚ	TypeErrorZ
CacheMerge)Ú	locationsÚlatestÚ	cum_cacheÚsourceÚflistr$   r   Údir_nameÚ	file_nameÚ_Zfile_extensionr.   r   r   r   Úlocations_to_cachec   s$    


r8   c             C   s:  |r|rt dƒ‚t| ƒtkr"| }n| g}t|ƒ}|rTt d|¡ t |dddd|¡ |dk	r¨|dk	r¨tt	 
|¡ƒt|ƒ dkr†t dƒ‚t |t|ƒtt	 
|¡ƒddd¡ t |¡}	tj|	_|rÌ|	jtjB |	_t |	|	j¡ t|ƒtkrð|d }
n|}
t |
|	¡}t |
|	¡}t| d }t| d }||
|	jddtjdƒ}|||	ƒ |d |j }|dkrf|	jd	 }|dkrx|| }t|ƒtjk	r’t |¡}t|ƒtjk	r¬t |¡}|dkrÄt|| ƒ}nt|ƒ}|dkrÞt dƒ‚t|ƒtkr(g }x2|D ]*}t||	||ƒ}t |	|¡ | |¡ qöW |S t||	||ƒS dS )
aØ  Read time series from frame data.

    Using the `location`, which can either be a frame file ".gwf" or a
    frame cache ".gwf", read in the data for the given channel(s) and output
    as a TimeSeries or list of TimeSeries.

    Parameters
    ----------
    location : string
        A source of gravitational wave frames. Either a frame filename
        (can include pattern), a list of frame files, or frame cache file.
    channels : string or list of strings
        Either a string that contains the channel name or a list of channel
        name strings.
    start_time : {None, LIGOTimeGPS}, optional
        The gps start time of the time series. Defaults to reading from the
        beginning of the available frame(s).
    end_time : {None, LIGOTimeGPS}, optional
        The gps end time of the time series. Defaults to the end of the frame.
        Note, this argument is incompatible with `duration`.
    duration : {None, float}, optional
        The amount of data to read in seconds. Note, this argument is
        incompatible with `end`.
    check_integrity : {True, bool}, optional
        Test the frame files for internal integrity.
    sieve : string, optional
        Selects only frames where the frame URL matches the regular
        expression sieve

    Returns
    -------
    Frame Data: TimeSeries or list of TimeSeries
        A TimeSeries or a list of TimeSeries, corresponding to the data from
        the frame file/cache for a given channel or channels.
    z,end time and duration are mutually exclusivez"Using frames that match regexp: %sr   NzNegative or null durationé   é   g      à?r   )r)   ÚtypeÚlistr8   ÚloggingÚinfor&   Z
CacheSieveÚintÚmathÚceilr	   ÚFrStreamCacheOpenZFR_STREAM_VERBOSE_MODEÚmodeZFR_STREAM_CHECKSUM_MODEZFrStreamSetModeÚFrStreamGetVectorLengthr
   r   r   ÚADCCountUnitr   ZLIGOTimeGPSÚfloatr   ZFrStreamSeekÚappend)ÚlocationÚchannelsÚ
start_timeÚend_timer   Úcheck_integrityÚsiever0   r2   r   Zfirst_channelZdata_lengthr   Úcreate_series_funcÚget_series_metadata_funcÚseriesZdata_durationZall_datar   Zchannel_datar   r   r   Ú
read_frame“   sf    '












rQ   c             C   s   t j| dS )a2   Return a connection to the datafind server

    Parameters
    -----------
    server : {SERVER:PORT, string}, optional
       A string representation of the server and port.
       The port may be ommitted.

    Returns
    --------
    connection
        The open connection to the datafind server.
    )Úhost)Ú
gwdatafindÚconnect)Úserverr   r   r   Údatafind_connection  s    rV   Úfilec             C   sD   | d }t |ƒ}|j|| ||d |j|| |||d}dd„ |D ƒS )a¡  Return the paths to a span of frame files

    Parameters
    ----------
    frame_type : string
        The string representation of the frame type (ex. 'H1_ER_C00_L1')
    start_time : int
        The start time that we need the frames to span.
    end_time : int
        The end time that we need the frames to span.
    server : {None, SERVER:PORT string}, optional
        Optional string to specify the datafind server to use. By default an
        attempt is made to use a local datafind server.
    url_type : string
        Returns only frame URLs with a particular scheme or head such
        as "file" or "gsiftp". Default is "file", which queries locally
        stored frames. Option can be disabled if set to None.
    Returns
    -------
    paths : list of paths
        The list of paths to the frame files.

    Examples
    --------
    >>> paths = frame_paths('H1_LDAS_C02_L2', 968995968, 968995968+2048)
    r   )ZgpsstartZgpsend)Úurltypec             S   s   g | ]}t |ƒj‘qS r   )r   r    )Ú.0Úentryr   r   r   ú
<listcomp>3  s    zframe_paths.<locals>.<listcomp>)rV   Z
find_timesZfind_frame_urls)Ú
frame_typerJ   rK   rU   Úurl_typeÚsiteÚ
connectionr.   r   r   r   Úframe_paths  s    
r`   c       	         s¾   | dkrRddl m‰ t|tƒs$|g}‡ ‡‡fdd„|D ƒ}t|ƒdkrJ|S |d S | dkrrddl m} ||ˆˆ ƒS t d	¡ t| t	ˆƒt	t
 ˆ ¡ƒƒ}t d
d |¡ ¡ t||ˆˆ ||dS )a  Read time series from frame data.

    Query for the locatin of physical frames matching the frame type. Return
    a time series containing the channel between the given start and end times.

    Parameters
    ----------
    frame_type : string
        The type of frame file that we are looking for.
    channels : string or list of strings
        Either a string that contains the channel name or a list of channel
        name strings.
    start_time : LIGOTimeGPS or int
        The gps start time of the time series. Defaults to reading from the
        beginning of the available frame(s).
    end_time : LIGOTimeGPS or int
        The gps end time of the time series. Defaults to the end of the frame.
    sieve : string, optional
        Selects only frames where the frame URL matches the regular
        expression sieve
    check_integrity : boolean
        Do an expensive checksum of the file before returning.

    Returns
    -------
    Frame Data: TimeSeries or list of TimeSeries
        A TimeSeries or a list of TimeSeries, corresponding to the data from
        the frame file/cache for a given channel or channels.

    Examples
    --------
    >>> ts = query_and_read_frame('H1_LDAS_C02_L2', 'H1:LDAS-STRAIN',
    >>>                               968995968, 968995968+2048)
    ZLOSC_STRAINr   )Úread_strain_loscc                s    g | ]}ˆ|d d… ˆˆ ƒ‘qS )Nr9   r   )rY   Úc)rK   ra   rJ   r   r   r[   _  s   z(query_and_read_frame.<locals>.<listcomp>r   ZLOSC)Úread_frame_losczquerying datafind serverzfound files: %sú )rJ   rK   rM   rL   )Zpycbc.frame.loscra   Ú
isinstancer<   Úlenrc   r=   r>   r`   r?   ÚnumpyrA   ÚjoinrQ   )	r\   rI   rJ   rK   rM   rL   r   rc   Úpathsr   )rK   ra   rJ   r   Úquery_and_read_frame5  s$    &

rj   c          
   C   sr  t |ƒtkr"t |ƒtkr"|}|}n|g}|g}dd„ |D ƒ}dd„ |D ƒ}t|ƒdksbt|ƒdkrjtdƒ‚| ¡ }| ¡ }t|| ƒ}|d s–|d ržtdƒ‚tj||dddtj	d}xªt
|ƒD ]ž\}	}
xHt ¡ D ]<}t| d |
jkrÒt| d	 }t| d
 }t| d }P qÒW |||	 |
jd|
jtjt|
 ¡ ƒƒ}|t|
 ¡ ƒƒ|_|
 ¡ |j_|||ƒ qÀW t || ¡ dS )a°  Write a list of time series to a single frame file.

    Parameters
    ----------
    location : string
        A frame filename.
    channels : string or list of strings
        Either a string that contains the channel name or a list of channel
        name strings.
    timeseries: TimeSeries
        A TimeSeries or list of TimeSeries, corresponding to the data to be
        written to the frame file for a given channel.
    c             S   s   h | ]
}|j ’qS r   )rJ   )rY   rP   r   r   r   ú	<setcomp>Š  s    zwrite_frame.<locals>.<setcomp>c             S   s   h | ]
}|j ’qS r   )rK   )rY   rP   r   r   r   rk   ‹  s    r   z4Start and end times of TimeSeries must be identical.z:Start and end times of TimeSeries must be integer seconds.Ú )r   r   ÚprojectÚrunZfrnumZdetectorFlagsr9   r   é   r   N)r;   r<   rf   r)   Úpopr?   r	   ZFrameNewr&   ZLALDETECTORTYPE_ABSENTÚ	enumerater   Úkeysr   rJ   r   rE   rg   r   Z
FrameWrite)rH   rI   Z
timeseriesZgps_start_timesZgps_end_timesZgps_start_timeZgps_end_timer   ÚframeÚiZtseriesZ
seriestyperN   Zcreate_sequence_funcZadd_series_funcrP   r   r   r   Úwrite_frames  s>    

ru   c               @   sb   e Zd ZdZdddejfdd„Zdd„ Zed	d
„ ƒZ	dd„ Z
dd„ Zdd„ Zdd„ Zddd„ZdS )Ú
DataBufferzBA linear buffer that acts as a FILO for reading in frame data
    i   TNc       	      C   s€   || _ || _|| _|| _|| _| d¡d | _|  ¡  |  | j	| j¡\| _
| _| j| }tt||dd|| d| j d| _dS )aE   Create a rolling buffer of frame data

        Parameters
        ---------
        frame_src: str of list of strings
            Strings that indicate where to read from files from. This can be a
        list of frame files, a glob, etc.
        channel_name: str
            Name of the channel to read from the frame files
        start_time:
            Time to start reading from.
        max_buffer: {int, 2048}, Optional
            Length of the buffer in seconds
        dtype: {dtype, numpy.float32}, Optional
            Data type to use for the interal buffer
        ú:r   )r   Fg      ð?)Úcopyr   r   N)Ú	frame_srcÚchannel_nameÚread_posÚforce_update_cacheÚincrement_update_cacher+   ÚdetectorÚupdate_cacheÚ_retrieve_metadatar   r   Úraw_sample_rater   r   Ú
raw_buffer)	Úselfry   rz   rJ   Ú
max_bufferr|   r}   r   Zraw_sizer   r   r   Ú__init__´  s    
zDataBuffer.__init__c             C   s"   t | jdd}t |¡}|| _dS )z©Reset the lal cache. This can be used to update the cache if the
        result may change due to more files being added to the filesystem,
        for example.
        T)r1   N)r8   ry   r	   rB   r   )rƒ   r.   r   r   r   r   r   Û  s    
zDataBuffer.update_cachec             C   sb   t  || ¡ t  || ¡}t| d }t| d }||| jddtjdƒ}||| ƒ |td|j ƒfS )a  Retrieve basic metadata by reading the first file in the cache

        Parameters
        ----------
        stream: lal stream object
            Stream containing a channel we want to learn about
        channel_name: str
            The name of the channel we want to know the dtype and sample rate of

        Returns
        -------
        channel_type: lal type enum
            Enum value which indicates the dtype of the channel
        sample_rate: int
            The sample rate of the data within this channel
        r9   r:   r   g      ð?)	r	   rD   r
   r   r   r&   rE   r?   r   )r   rz   r   rN   rO   rP   r   r   r   r€   ä  s    

zDataBuffer._retrieve_metadatac             C   sx   yNt | j d }t | j d }|| j| j| jt|ƒdƒ}t|jj|j| j|dS  t	k
rr   t
d | j¡ƒ‚Y nX dS )a¤  Try to read the block of data blocksize seconds long

        Parameters
        ----------
        blocksize: int
            The number of seconds to attempt to read from the channel

        Returns
        -------
        data: TimeSeries
            TimeSeries containg 'blocksize' seconds of frame data

        Raises
        ------
        RuntimeError:
            If data cannot be read for any reason
        r   r   )r   r   r   zCannot read {0} frame dataN)r   r   r   rz   r{   r?   r   r   r   Ú	ExceptionÚRuntimeErrorÚformat)rƒ   Ú	blocksizer   r   r   r   r   r   Ú_read_frameÿ  s    
zDataBuffer._read_framec             C   s:   | j  t|| j ƒ ¡ |  j|7  _| j  j|7  _dS )z¥Advance and insert zeros

        Parameters
        ----------
        blocksize: int
            The number of seconds to attempt to read from the channel
        N)r‚   Úrollr?   r   r{   rJ   )rƒ   r‰   r   r   r   Únull_advance  s    zDataBuffer.null_advancec             C   sZ   |   |¡}| j t|ƒ ¡ |dd… | jt|ƒ d…< |  j|7  _| j j|7  _|S )zéAdd blocksize seconds more to the buffer, push blocksize seconds
        from the beginning.

        Parameters
        ----------
        blocksize: int
            The number of seconds to attempt to read from the channel
        N)rŠ   r‚   r‹   rf   r{   rJ   )rƒ   r‰   Útsr   r   r   Úadvance(  s    	
zDataBuffer.advancec          	   C   s  t | jjƒ}t || ƒ}t| dƒsˆt | jd ¡d }tj tj 	|¡¡d  
d¡}d |d |d g¡| _t|d ƒ| _t|d ƒ| _t| jt || j t | jƒ ¡| j  ƒ}t ||| j¡ t¡}g }xŽ|D ]†}| j}	d|	krt|	t|	 d¡d ƒ ƒ}
|	 d|
 t|ƒd|
… ¡}	|	› d	| j› d|› d| j› d
}tj |¡sNt‚| |¡ qÒW t|ƒ}t |¡}|| _|  | j| j ¡\| _!| _"dS )aÃ  Update the internal cache by starting from the first frame
        and incrementing.

        Guess the next frame file name by incrementing from the first found
        one. This allows a pattern to be used for the GPS folder of the file,
        which is indicated by `GPSX` where x is the number of digits to use.

        Parameters
        ----------
        blocksize: int
            Number of seconds to increment the next frame file.
        Údurr   ú-r   r9   r:   ZGPSzGPS%sú/z.gwfN)#rF   r‚   rK   Úhasattrr(   ry   r   r    r,   Úbasenamer+   rh   Úbegr?   Úrefr   rg   ÚfloorZarangeÚastyper}   ÚindexÚreplacer-   Úexistsr‡   rG   r8   r	   rB   r   r€   rz   r   r   )rƒ   r‰   r   ÚendÚfnameZfstartÚstartsrr   ÚsÚpatternÚnÚnamer.   r   r   r   r   Úupdate_cache_by_increment9  s0    
*

 
z$DataBuffer.update_cache_by_incrementé
   c             C   sx   | j r|  ¡  xdy| jr"|  |¡ t | |¡S  tk
rn   t ¡ || j	j
 kr`t | |¡ dS t d¡ Y qX qW dS )aç   Attempt to advance the frame buffer. Retry upon failure, except
        if the frame file is beyond the timeout limit.

        Parameters
        ----------
        blocksize: int
            The number of seconds to attempt to read from the channel
        timeout: {int, 10}, Optional
            Number of seconds before giving up on reading a frame

        Returns
        -------
        data: TimeSeries
            TimeSeries containg 'blocksize' seconds of frame data
        Ngš™™™™™¹?)r|   r   r}   r¢   rv   rŽ   r‡   ÚpycbcZgps_nowr‚   rK   rŒ   ÚtimeÚsleep)rƒ   r‰   Útimeoutr   r   r   Úattempt_advanceg  s    
zDataBuffer.attempt_advance)r£   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__rg   Úfloat64r…   r   Ústaticmethodr€   rŠ   rŒ   rŽ   r¢   r¨   r   r   r   r   rv   ¯  s   !	.rv   c               @   s@   e Zd ZdZddd„Zddd	„Zdd
d„Zddd„Zdd„ ZdS )ÚStatusBufferz7 Read state vector or DQ information from a frame file é   r:   FNc	       	   
   C   s,   t j| ||||||tjd || _|| _dS )a   Create a rolling buffer of status data from a frame

        Parameters
        ---------
        frame_src: str of list of strings
            Strings that indicate where to read from files from. This can be a
            list of frame files, a glob, etc.
        channel_name: str
            Name of the channel to read from the frame files
        start_time:
            Time to start reading from.
        max_buffer: {int, 2048}, Optional
            Length of the buffer in seconds
        valid_mask: {int, HOFT_OK | SCIENCE_INTENT}, Optional
            Set of flags that must be on to indicate valid frame data.
        valid_on_zero: bool
            If True, `valid_mask` is ignored and the status is considered
            "good" simply when the channel is zero.
        )r„   r|   r}   r   N)rv   r…   rg   Úint32Ú
valid_maskÚvalid_on_zero)	rƒ   ry   rz   rJ   r„   r²   r|   r}   r³   r   r   r   r…   Œ  s    
zStatusBuffer.__init__c             C   sD   | j r| ¡ dk}n"|dkr"| j}t | ¡ |¡|k}tt |¡ƒS )a¶  Check if the data contains any non-valid status information

        Parameters
        ----------
        values: pycbc.types.Array
            Array of status information
        flag: str, optional
            Override the default valid mask with a user defined mask.

        Returns
        -------
        status: boolean
            Returns True if all of the status information if valid,
             False if any is not.
        r   N)r³   rg   r²   Úbitwise_andÚboolÚall)rƒ   ÚvaluesÚflagZvalidr   r   r   Úcheck_valid°  s    zStatusBuffer.check_validc             C   sL   | j j}t|| j j | ƒ}|t|| ƒ d }| j ||… }| j||dS )a  Check if the duration contains any non-valid frames

        Parameters
        ----------
        start_time: int
            Beginning of the duration to check in gps seconds
        duration: int
            Number of seconds after the start_time to check
        flag: str, optional
            Override the default valid mask with a user defined mask.

        Returns
        -------
        status: boolean
            Returns True if all of the status information if valid,
            False if any is not.
        r   )r¸   )r‚   Úsample_rater?   rJ   r¹   )rƒ   rJ   r   r¸   Úsrrž   Úer   r   r   r   Úis_extent_validÈ  s
    zStatusBuffer.is_extent_validr   c             C   s¼   ddl m} | jj}t|| jj | | ƒd }|t|| | ƒ d }| j||… }	|	j ¡ }
| jrt|	 ¡ dk}nt 	|	 ¡ | j
¡| j
k}|
| | }|d|  |d  }||||ƒ}|S )a   Return the indices of the times lying in the flagged region

        Parameters
        ----------
        start_time: int
            Beginning time to request for
        duration: int
            Number of seconds to check.
        padding: float
            Number of seconds to add around flag inactive times to be considered
        inactive as well.

        Returns
        -------
        indices: numpy.ndarray
            Array of indices marking the location of triggers within valid
        time.
        r   )Úindices_outside_timesr   g      ð?g       @)Úpycbc.events.vetor¾   r‚   rº   r?   rJ   Úsample_timesrg   r³   r´   r²   )rƒ   rJ   r   ÚtimesÚpaddingr¾   r»   rž   r¼   r   ÚstampsÚinvalidr   ÚendsÚidxr   r   r   Úindices_of_flagà  s    
zStatusBuffer.indices_of_flagc             C   sJ   y&| j r|  |¡ t | |¡}|  |¡S  tk
rD   |  |¡ dS X dS )aˆ   Add blocksize seconds more to the buffer, push blocksize seconds
        from the beginning.

        Parameters
        ----------
        blocksize: int
            The number of seconds to attempt to read from the channel

        Returns
        -------
        status: boolean
            Returns True if all of the status information if valid,
            False if any is not.
        FN)r}   r¢   rv   rŽ   r¹   r‡   rŒ   )rƒ   r‰   r   r   r   r   rŽ     s    


zStatusBuffer.advance)r°   r:   FNF)N)N)r   )	r©   rª   r«   r¬   r…   r¹   r½   rÇ   rŽ   r   r   r   r   r¯   ˆ  s       



%r¯   c               @   s4   e Zd ZdZddd„Zddd	„Zd
d„ Zdd„ ZdS )Ú	iDQBufferz' Read iDQ timeseries from a frame file é   FNc	       	      C   s6   || _ t||||||d| _t||||||d| _dS )ag  
        Parameters
        ----------
        frame_src: str of list of strings
            Strings that indicate where to read from files from. This can be a
            list of frame files, a glob, etc.
        idq_channel_name: str
            Name of the channel to read the iDQ statistic from
        idq_status_channel_name: str
            Name of the channel to read the iDQ status from
        idq_threshold: float
            Threshold which triggers a veto if iDQ channel falls below this threshold
        start_time:
            Time to start reading from.
        max_buffer: {int, 512}, Optional
            Length of the buffer in seconds
        force_update_cache: {boolean, True}, Optional
            Re-check the filesystem for frame files on every attempt to
            read more data.
        increment_update_cache: {str, None}, Optional
            Pattern to look for frame files in a GPS dependent directory. This
            is an alternate to the forced updated of the frame cache, and
            apptempts to predict the next frame file name without probing the
            filesystem.
        )r„   r|   r}   N)Ú	thresholdrv   ÚidqÚ	idq_state)	rƒ   ry   Zidq_channel_nameZidq_status_channel_nameZidq_thresholdrJ   r„   r|   r}   r   r   r   r…   !  s    !
ziDQBuffer.__init__r   c             C   sÜ   ddl m} | jjj}t|| jjj | | ƒd }|t|| | ƒ d }| jj||… }	|	j ¡ }
|	 ¡ | j	k}| j
j||… }| ¡  t¡}t ||¡}t |¡}|
| }|| }|d|  |d  }||||ƒ}|S )a   Return the indices of the times lying in the flagged region

        Parameters
        ----------
        start_time: int
            Beginning time to request for
        duration: int
            Number of seconds to check.
        padding: float
            Number of seconds to add around flag inactive times to be considered
        inactive as well.

        Returns
        -------
        indices: numpy.ndarray
            Array of indices marking the location of triggers within valid
        time.
        r   )r¾   r   g      ð?g       @)r¿   r¾   rË   r‚   rº   r?   rJ   rÀ   rg   rÊ   rÌ   r—   rµ   Úlogical_andZflatnonzero)rƒ   rJ   r   rÁ   rÂ   r¾   r»   rž   r¼   Zidq_faprÃ   Zlow_fapZ	idq_validZvalid_low_fapZ
glitch_idxZglitch_timesr   rÅ   rÆ   r   r   r   rÇ   L  s     


ziDQBuffer.indices_of_flagc             C   s(   | j  |¡}| j |¡}|dk	o&|dk	S )a^   Add blocksize seconds more to the buffer, push blocksize seconds
        from the beginning.

        Parameters
        ----------
        blocksize: int
            The number of seconds to attempt to read

        Returns
        -------
        status: boolean
            Returns True if advance is succesful,
            False if not.
        N)rË   r¨   rÌ   )rƒ   r‰   Zidq_tsZidq_state_tsr   r   r   rŽ   p  s    ziDQBuffer.advancec             C   s   | j  |¡ | j |¡ dS )z˜Advance and insert zeros

        Parameters
        ----------
        blocksize: int
            The number of seconds to advance the buffers
        N)rË   rŒ   rÌ   )rƒ   r‰   r   r   r   rŒ   ƒ  s    ziDQBuffer.null_advance)rÉ   FN)r   )r©   rª   r«   r¬   r…   rÇ   rŽ   rŒ   r   r   r   r   rÈ     s     
$
$rÈ   )F)NNNFN)N)NrW   )NF)Ir¬   r	   r=   r&   rg   r@   Úos.pathr   r(   r¥   rS   r¤   Úurllib.parser   Zpycbc.typesr   r   ZS_TYPE_CODEZFrStreamReadREAL4TimeSeriesZfloat32ZCreateREAL4TimeSeriesZ"FrStreamGetREAL4TimeSeriesMetadataZCreateREAL4SequenceZFrameAddREAL4TimeSeriesProcDataZD_TYPE_CODEZFrStreamReadREAL8TimeSeriesr­   ZCreateREAL8TimeSeriesZ"FrStreamGetREAL8TimeSeriesMetadataZCreateREAL8SequenceZFrameAddREAL8TimeSeriesProcDataZC_TYPE_CODEZFrStreamReadCOMPLEX8TimeSeriesZ	complex64ZCreateCOMPLEX8TimeSeriesZ%FrStreamGetCOMPLEX8TimeSeriesMetadataZCreateCOMPLEX8SequenceZ"FrameAddCOMPLEX8TimeSeriesProcDataZZ_TYPE_CODEZFrStreamReadCOMPLEX16TimeSeriesZ
complex128ZCreateCOMPLEX16TimeSeriesZ&FrStreamGetCOMPLEX16TimeSeriesMetadataZCreateCOMPLEX16SequenceZ#FrameAddCOMPLEX16TimeSeriesProcDataZU4_TYPE_CODEZFrStreamReadUINT4TimeSeriesZuint32ZCreateUINT4TimeSeriesZ"FrStreamGetUINT4TimeSeriesMetadataZCreateUINT4SequenceZFrameAddUINT4TimeSeriesProcDataZI4_TYPE_CODEZFrStreamReadINT4TimeSeriesr±   ZCreateINT4TimeSeriesZ!FrStreamGetINT4TimeSeriesMetadataZCreateINT4SequenceZFrameAddINT4TimeSeriesProcDatar   r   r   r8   rQ   rV   r`   rj   Ú__all__ru   Úobjectrv   r¯   rÈ   r   r   r   r   Ú<module>   s~   


0  
n

#
9< Z 