B
    |‹d,  ã               @   s|   d Z ddlZddlmZ ddlmZ ddlmZ	 ddl
mZ ej dd	¡Zd
d„ Zefdd„Zefdd„Zedfdd„ZdS )zQuery methods for DQSEGDB2
é    N)Úsegments)Úrequests)Útarget_audienceé   )ÚapiÚDEFAULT_SEGMENT_SERVERzhttps://segments.ligo.orgc              O   s   t j| |Ž}| ¡  | ¡ S )N)Úigwn_requestsÚgetÚraise_for_statusÚjson)ÚargsÚkwargsÚresponse© r   ú[/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/dqsegdb2/query.pyÚ	_get_json"   s    r   c                s.   t  |ˆ ¡}t|f|Žd }‡ fdd„|D ƒS )aÕ  Query for all defined flags for the given ``ifo``

    Parameters
    ----------
    ifo : `str`
        the interferometer prefix for which to query

    host : `str`, optional
        the URL of the database, defaults to ``DEFAULT_SEGMENT_SERVER``

    Returns
    -------
    flags : `set`
        the set of all define flag names in the format ``{ifo}:{name}``

    Examples
    --------
    >>> from dqsegdb2.query import query_names
    >>> query_names('G1')
    Úresultsc                s   h | ]}d   ˆ |¡’qS )z{0}:{1})Úformat)Ú.0Úname)Úifor   r   ú	<setcomp>?   s    zquery_names.<locals>.<setcomp>)r   Zname_query_urlr   )r   ÚhostÚrequest_kwargsÚurlÚnamesr   )r   r   Úquery_names(   s    r   c             K   s2   |   dd¡\}}t |||¡}tt|f|Žd ƒS )aÞ  Query for defined versions for the given flag

    Parameters
    ----------
    flag : `str`
        the name for which to query

    host : `str`, optional
        the URL of the database, defaults to ``DEFAULT_SEGMENT_SERVER``

    Returns
    -------
    versions : `list` of `int`
        the list of defined versions for the given flag

    Examples
    --------
    >>> from dqsegdb2.query import query_versions
    >>> query_versions('G1:GEO-SCIENCE')
    [1, 2, 3]
    ú:r   Úversion)Úsplitr   Zversion_query_urlÚsortedr   )Úflagr   r   r   r   r   r   r   r   Úquery_versionsB   s    r"   Tc             K   st  t  t  t|ƒt|ƒ¡g¡}y |  dd¡\}}}	t|	ƒg}
W nJ tk
r†   |  d¡rf|  dd¡d } |  dd¡\}}t	| |d}
Y nX t
t  ¡ t  ¡ |||
d d}|rº| dt|ƒ¡ tjf |Ž¤}xœtt|
ƒƒD ]Œ\}}	tj||||	||d	}t||d
}xJdD ]B}||  t  tt j| |¡ƒ¡¡ |r||  ¡ |@ ||< qW | |¡ |rÖd|d< qÖW W dQ R X |S )aß  Query for segments for the given flag in a ``[start, stop)`` interval

    Parameters
    ----------
    flag : `str`
        the name for which to query, see _Notes_ for information on how
        versionless-flags are queried.

    start : `int`
        the GPS start time.

    end : `int`
        the GPS end time.

    host : `str`, optional
        the URL of the database, defaults to ``DEFAULT_SEGMENT_SERVER``.

    coalesce : `bool`, optional
        if `True`, coalesce the segmentlists returned by the server,
        and restrict them to lie fully within the ``[start, end)``
        request segment, otherwise return the 'raw' result,
        default: `True`.

    Returns
    -------
    segmentdict : `dict`
        a `dict` with the following keys

        - ``'ifo'`` - the interferometer prefix (`str`)
        - ``'name'`` - the flag name (`str`)
        - ``'version'`` - the flag version (`int`)
        - ``'known'`` - the known segments (`~ligo.segments.segmentlist`)
        - ``'active'`` - the active segments (`~ligo.segments.segmentlist`)
        - ``'metadata'`` - a `dict` of flag information (`dict`)
        - ``'query_information'`` - a `dict` of query information (`dict`)

    Notes
    -----
    If ``flag`` is given without a version (e.g. ``'X1:FLAG-NAME'``) or the
    version is given as ``'*'`` (e.g. ``'X1:FLAG-NAME:*'``) the result of
    the query will be the intersection of queries over all versions found
    in the database.
    In that case the ``'metadata'`` and ``'query_information'`` in the output
    will be preserved for the highest version number only.

    Examples
    --------
    >>> from dqsegdb2.query import query_segments
    >>> query_segments('G1:GEO-SCIENCE:1', 1000000000, 1000001000)
    r   é   z:*r   r   )r   )ÚknownÚactiver   r   r   Ztoken_audience)ÚstartÚend)Úsession)r%   r$   Nr   )r   ZsegmentlistÚsegmentÚfloatr   ÚintÚ
ValueErrorÚendswithÚrsplitr"   ÚdictÚ
setdefaultÚscitoken_audiencer   ÚSessionÚ	enumerater    r   Zsegment_query_urlr   ÚextendÚmapÚpopÚcoalesceÚupdate)r!   r&   r'   r   r7   r   Úrequestr   r   r   ÚversionsÚoutÚsessÚir   ÚresultÚkeyr   r   r   Úquery_segments]   sF    :




r@   )Ú__doc__ÚosÚligor   Zigwn_auth_utilsr   r   Zigwn_auth_utils.scitokensr   r1   Ú r   Úenvironr0   r   r   r   r"   r@   r   r   r   r   Ú<module>   s   