B
    zdt	                 @   sN   d Z ddlmZ ddlmZmZ ejfddZejfddZejfd	d
Z	dS )a  
`gwosc.timeline` provides functions to find segments for a given dataset.

You can search for Timeline segments, based on a flag name, and a
GPS time interval as follows:

>>> from gwosc.timeline import get_segments
>>> get_segments('H1_DATA', 1126051217, 1126151217)
[(1126073529, 1126114861), (1126121462, 1126123267), (1126123553, 1126126832), (1126139205, 1126139266), (1126149058, 1126151217)]

The output is a `list` of ``[start, end)`` 2-tuples which each
represent a semi-open time interval.

For documentation on what flags are available, for example for the O1
science run, see `the O1 data release
page <https://gwosc.org/O1/>`__ (*Data Quality*).
    )
itemgetter   )apidatasetsc             C   s$   t tttt| |||dd S )a  Return the [start, end) GPS segments for this flag

    Parameters
    ----------
    flag : `str`
        name of flag, e.g. ``'H1_DATA'``

    start : `int`
        the GPS start time of your query

    end : `int`
        the GPS end time of your query

    host : `str`, optional
        the URL of the remote GWOSC server

    Returns
    -------
    segments : `list` of `(int, int)` tuples
        a list of `[a, b)` GPS segments
    )hostsegments)listmaptupler   Z
fetch_jsontimeline_url)flagstartendr    r   [/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/gwosc/timeline.pyget_segments   s    
r   c             C   s6   |  ddd }t||||d}d||| ||| S )zCReturns the Timeline JSON URL for a flag name and GPS interval
    _r   r   )r   z&{}/timeline/segments/json/{}/{}/{}/{}/)split_find_datasetformat)r   r   r   r   detectorZdatasetr   r   r   r   6   s    r   c       	      C   s   ||  }g }xZt jd|| |f|dD ]@}t j||d}t||d t| |d  }|||| f q$W |s|td| |t|t	dddd d S )Nrun)typer   segmentr   )r   r   r   z#No datasets found matching [{}, {}))key)
r   Z_iter_datasetsZrun_segmentminmaxappend
ValueErrorr   sortedr   )	r   r   r   r   durationZepochsr   r   overlapr   r   r   r   ?   s    r   N)
__doc__operatorr    r   r   ZDEFAULT_URLr   r   r   r   r   r   r   <module>   s
   	