B
    dA                 @   s   d Z ddlZddlZddlZddlmZmZ ddlmZ ddl	m
Z
 dd ZdZd	d
 ZdddZdddZdd ZdddZdS )z Utilities to query archival instrument status information of
gravitational-wave detectors from public sources and/or dqsegdb.
    N)segmentlistsegment)get_run)get_filec          	   C   s  ddl m}m} ddlm} i }xF|D ]>}i ||< g || d< x$tddD ]}g || d|< qJW q&W |j| d|d	}|j	|d
}	|	
d}
|	
d}t|	
d}t|	
d}t|	
d}t|	
d}t|	
d}t|	
d}xtt|	D ]}|
| |krq|| dkr@d|| d }n"|| dkrTd}nd|| }|| || || d t||  || || || || d}||
|  | | qW |S )a   Parse a veto definer file from the filename and return a dictionary
    indexed by ifo and veto definer category level.

    Parameters
    ----------
    veto_def_filename: str
        The path to the veto definer file
    ifos: str
        The list of ifos for which we require information from the veto
        definer file

    Returns
    --------
    parsed_definition: dict
        Returns a dictionary first indexed by ifo, then category level, and
        finally a list of veto definitions.
    r   )tableutils)LIGOLWContentHandlerZCAT_H      zCAT_{}F)Zcontenthandlerveto_definerifonameversioncategory
start_timeend_time	start_padend_pad   :)r   r   	full_namestartendr   r   )Zligo.lwr   r   Zpycbc.io.ligolwr   rangeformatZload_filenameTableZ	get_tableZgetColumnByNamenumpyarraylenstrappend)Zveto_def_filenameifosr   Zligolw_utilshdataifo_nameZcat_numZindocZ
veto_tabler   r   r   r   r   r   r   r   iZcurr_catZ	veto_info r&   U/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/pycbc/dq.pyparse_veto_definer$   sH    


r(   zEhttps://www.gw-openscience.org/timeline/segments/json/{}/{}_{}/{}/{}/c       	   
   C   s~   ddl m} | d | }y ||t|t||d}|d S  tk
rx } z"td|t|t|| |W dd}~X Y nX dS )zGUtility function for better error reporting when calling dqsegdb2.
    r   )query_segmentsr   )hostactivezPCould not query segment database, check name (%s), times (%d-%d) and server (%s)N)Zdqsegdb2.queryr)   int	Exceptionloggingerror)	detector	flag_namer   r   serverr)   Zcomplete_flagZ	query_reser&   r&   r'   query_dqsegdb2k   s    r4   anyhttps://segments.ligo.orgFc             C   s  t g }|dkr*d|kr"d|ks*d|krtt| d||}	d|krJd| }
n|dd}
t| |
|||d	}|	|  S || }yZtt||d
  | | |t|t|}t||dd}t	
t|d}	d|	kr|	d }W nN tk
r$ } z.|dkrtd|t| |||d||dS d}~X Y nX nX|dkrt|dk	rJt|| g}|dk	r|||  krx||  | D ]}t g }t| |d |||}x>|D ]6}|d |d  }|d |d  }|t|| qW |d }|d }|dkrt|}t t||g}|| |@ 7 }qpW n8t| ||||}x&|D ]}|t|d |d  q0W | t tt|t|g@ }ntd|t | S )a  Return the times where the flag is active

    Parameters
    ----------
    ifo: string
        The interferometer to query (H1, L1).
    segment_name: string
        The status flag to query from LOSC.
    start_time: int
        The starting gps time to begin querying from LOSC
    end_time: int
        The end gps time of the query
    source: str, Optional
        Choice between "GWOSC" or "dqsegdb". If dqsegdb, the server option may
        also be given. The default is to try GWOSC first then try dqsegdb.
    server: str, Optional
        The server path. Only used with dqsegdb atm.
    veto_definer: str, Optional
        The path to a veto definer to define groups of flags which
        themselves define a set of segments.
    cache: bool
        If true cache the query. Default is not to cache

    Returns
    ---------
    segments: ligo.segments.segmentlist
        List of segments
    )ZGWOSCr5   Z_HW_INJNOZVETOZDATAZNO_Z_VETO )cache   
   )r9   timeoutrsegmentsr5   z=Unable to find {} segments in GWOSC, check flag name or timesZdqsegdb)sourcer2   r   Nr   r   r   r	   r   r   r   z2Source must be `dqsegdb`, `GWOSC` or `any`. Got {})r   
query_flagreplacecoalesce	GWOSC_URLr   r   r,   r   jsonloadopenr-   
ValueErrorr(   r4   r    r   )r   Zsegment_namer   r   r?   r2   r   r9   Zflag_segmentsr#   r   negatedurationurlfnamer3   Zveto_defflagpartialsegsZrsegZ	seg_startZseg_endZ
flag_startZflag_endsendr&   r&   r'   r@   ~   sj    











r@   c             C   s   t g }x|D ]}| }|	dk	r.||	kr.|	| }t||||||||
d}|r||kr|| \}}t g }x,|D ]$}|t|d | |d |  qlW |}|dk	r||kr|| \}}t t||gg}||@  }||  }qW |S )am  Return the times where any flag is active

    Parameters
    ----------
    ifo: string or dict
        The interferometer to query (H1, L1). If a dict, an element for each
        flag name must be provided.
    segment_name: list of strings
        The status flag to query from LOSC.
    start_time: int
        The starting gps time to begin querying from LOSC
    end_time: int
        The end gps time of the query
    source: str, Optional
        Choice between "GWOSC" or "dqsegdb". If dqsegdb, the server option may
        also be given. The default is to try GWOSC first then try dqsegdb.
    server: str, Optional
        The server path. Only used with dqsegdb atm.
    veto_definer: str, Optional
        The path to a veto definer to define groups of flags which
        themselves define a set of segments.
    bounds: dict, Optional
        Dict containing start-end tuples keyed by the flag name which
        indicate places which should have a distinct time period to be active.
    padding: dict, Optional
        Dict keyed by the flag name. Each element is a tuple
    (start_pad, end_pad) which indicates how to change the segment boundaries.
    override_ifos: dict, Optional
        A dict keyed by flag_name to override the ifo option on a per flag
        basis.

    Returns
    ---------
    segments: ligo.segments.segmentlist
        List of segments
    N)r?   r2   r   r9   r   r	   )r   r@   r    r   rB   )r   Zsegment_namesr   r   r?   r2   r   boundspaddingoverride_ifosr9   Z
total_segsr1   r$   rN   sr3   Zsegs2segZvalidr&   r&   r'   query_cumulative_flags   s*    +


$rU   c             C   s  |  dd d}i }i }i }i }g }x|D ]~}|d dksz|d dkszd}|d|7 }|d	7 }|d
7 }t||d dk}	|dd }d }
 }}t|dd dkr|dd }
|dd }d|kr"|dd dd }|d\}}t|t|f}| |d dd}d|krv|dd dd }|d\}}t|t|f}| |d dd}|
r|
||< |r|||< |r|||< || |	||< q2W |||||fS )a<   Parse a dq flag query string

    Parameters
    ----------
    flag_str: str
        String to be parsed

    Returns
    -------
    flags: list of strings
        List of reduced name strings which can be passed to lower level
        query commands
    signs: dict
        Dict of bools indicating if the flag should add positively to the
        segmentlist
    ifos: dict
        Ifo specified for the given flag
    bounds: dict
        The boundary of a given flag
    padding: dict
        Any padding that should be applied to the segments for a given flag
     r8   ,r   +-z3DQ flags must begin with a '+' or a '-' character. zYou provided {}. z=See http://pycbc.org/pycbc/latest/html/workflow/segments.htmlz for more information.r	   Nr   r:   r   <>z<>[]z[])	rA   stripsplitr   rG   r   floatr,   r    )flag_strflagsZsignsr!   rP   rQ   ZbflagsrL   err_msgsignr   padboundZpoptZspadZepadZboptr   r   r&   r&   r'   parse_flag_str7  sL    


rg   c                s   t |\} }}	}
 fdd|D } fdd|D }t|t| t|krVtdt| |||||||	|
|d
}t| |||||||	|
|d
}||  }|S )a   Query for flags based on a special str syntax

    Parameters
    ----------
    ifo: str
        The ifo to query for (may be overridden in syntax)
    flag_str: str
        Specification of how to do the query. Ex. +H1:DATA:1<-8,8>[0,100000000]
        would return H1 time for the DATA available flag with version 1. It
        would then apply an 8 second padding and only return times within
        the chosen range 0,1000000000.
    start_time: int
        The start gps time. May be overridden for individual flags with the
        flag str bounds syntax
    end_time: int
        The end gps time. May be overridden for individual flags with the
        flag str bounds syntax
    source: str, Optional
        Choice between "GWOSC" or "dqsegdb". If dqsegdb, the server option may
        also be given. The default is to try GWOSC first then try dqsegdb.
    server: str, Optional
        The server path. Only used with dqsegdb atm.
    veto_definer: str, Optional
        The path to a veto definer to define groups of flags which
        themselves define a set of segments.

    Returns
    -------
    segs: segmentlist
        A list of segments corresponding to the flag query string
    c                s   g | ]} | r|qS r&   r&   ).0f)rd   r&   r'   
<listcomp>  s    zquery_str.<locals>.<listcomp>c                s   g | ]} | s|qS r&   r&   )rh   ri   )rd   r&   r'   rj     s    z/Not all flags could be parsed, check +/- prefix)r?   r2   r   rP   rQ   rR   )rg   r   rG   rU   rB   )r   ra   r   r   r?   r2   r   rb   r!   rP   rQ   upZdownrN   Zmsegr&   )rd   r'   	query_str  s*    !

rl   )r5   r6   NF)r5   r6   NNNNF)r5   r6   N)__doc__r.   rD   r   Zligo.segmentsr   r   Zpycbc.frame.loscr   Zpycbc.ior   r(   rC   r4   r@   rU   rg   rl   r&   r&   r&   r'   <module>   s(   D 
r     
@K 