B
    zd1                 @   s  d Z ddlZddlZddlZddlmZ ddlmZ ddlZe	dZ
e Zeeej e
e e
eedej dZd	Zi Zed
Zdd ZefddZefddZefddZdeefddZefddZefddZ defddZ!efddZ"efddZ#efd d!Z$defd"d#Z%efd$d%Z&defd&d'Z'dddefd(d)Z(efd*d+Z)efd,d-Z*efd.d/Z+efd0d1Z,efd2d3Z-ddefd4d5Z.ddefd6d7Z/dS )8zl
:mod:`gwosc.api` provides the low-level interface functions
that handle direct requests to the GWOSC host.
    N)	urlencode   )__version__z	gwosc.apiZGWOSC_LOG_LEVELl   g] zhttps://gwosc.org)z>=z=>z<=z=<c             K   sj   yt |  S  tk
rd   td|  ddt i}tj| fd|i|}|  t 	| |
 S X dS )a  Fetch JSON data from a remote URL

    Parameters
    ----------
    url : `str`
        the remote URL to fetch

    **kwargs
        other keyword arguments are passed directly to :func:`requests.get`

    Returns
    ------
    data : `dict` or `list`
        the data fetched from ``url`` as parsed by
        :meth:`requests.Response.json`

    See also
    --------
    json.loads
        for details of the JSON parsing
    zfetching {}z
User-Agentzpython-gwosc/headersN)
JSON_CACHEKeyErrorloggerdebugformatr   requestsgetraise_for_status
setdefaultjson)urlkwargsZclient_headersresp r   V/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/gwosc/api.py
fetch_json'   s    r   c             C   s   d || |S )Nz{}/archive/{:d}/{:d}/json/)r
   )startendhostr   r   r   _dataset_urlL   s    r   c             C   s   t t| ||dS )a  Returns the JSON metadata for all datasets matching the GPS interval

    Parameters
    ----------
    gpsstart : `int`
        the GPS start of the desired interval

    gpsend : `int`
        the GPS end of the desired interval

    host : `str`, optional
        the URL of the GWOSC host to query, defaults to
        https://gwosc.org

    Returns
    -------
    data : `dict` or `list`
        the JSON data retrieved from GWOSC and returned by `json.loads`
    )r   )r   r   )gpsstartgpsendr   r   r   r   fetch_dataset_jsonP   s    r   c             C   s   d || |||S )Nz&{}/archive/links/{}/{}/{:d}/{:d}/json/)r
   )rundetectorr   r   r   r   r   r   _run_urlg   s    r   c             C   s   t t| ||||dS )a_  Returns the JSON metadata for the given science run parameters

    Parameters
    ----------
    run : `str`
        the name of the science run, e.g. ``'O1'``

    detector : `str`
        the prefix of the GW detector, e.g. ``'L1'``

    gpsstart : `int`
        the GPS start of the desired interval

    gpsend : `int`
        the GPS end of the desired interval

    host : `str`, optional
        the URL of the GWOSC host to query, defaults to
        https://gwosc.org

    Returns
    -------
    data : `dict` or `list`
        the JSON data retrieved from GWOSC and returned by `json.loads`
    )r   )r   r   )r   r   r   r   r   r   r   r   fetch_run_jsonm   s    r    c             C   s
   d | S )Nz{}/eventapi/json/query/params/)r
   )r   r   r   r   _allowed_params_url   s    r!   c             C   s   t t| dS )N)r   )r   r!   )r   r   r   r   fetch_allowed_params_json   s    r"   Fc             C   s   | rdnd}d ||S )NZjsonfullr   z{}/eventapi/{}/)r
   )fullr   jr   r   r   _eventapi_url   s    r%   c             C   s   t t| dS )a2  Returns the JSON metadata for the catalogue list.

    Parameters
    ----------
    host : `str`, optional
        the URL of the GWOSC host to query

    Returns
    -------
    data : `dict` or `list`
        the JSON data retrieved from GWOSC and returned by
        :meth:`requests.Response.json`
    )r   )r   r%   )r   r   r   r   fetch_cataloglist_json   s    r&   c             C   s   d t|d| S )Nz{}{}/)r   )r
   r%   )catalogr   r   r   r   _catalog_url   s    r(   c             C   s   t t| |dS )a  "Returns the JSON metadata for the given catalogue

    Parameters
    ----------
    catalog : `str`
        the name of the event catalog, e.g. `GWTC-1-confident`

    host : `str`, optional
        the URL of the GWOSC host to query, defaults to
        https://gwosc.org

    Returns
    -------
    data : `dict` or `list`
        the JSON data retrieved from GWOSC and returnend by
        :meth:`requests.Response.json`
    )r   )r   r(   )r'   r   r   r   r   fetch_catalog_json   s    r)   c             C   s   d t| |dS )Nz{}allevents/)r#   r   )r
   r%   )r#   r   r   r   r   _allevents_url   s    r*   c             C   s   t d| dtkS )NT)r#   r   )r*   r   )r   r   r   r   _has_jsonfull_allevents   s    r+   c             C   s2   | dkr"t |dr"ttd|dS tt| |dS )aP  "Returns the JSON metadata for the allevents API

    Parameters
    ----------
    host : `str`, optional
        the URL of the GWOSC host to query, defaults to https://gwosc.org

    Returns
    -------
    data : `dict` or `list`
        the JSON data retrieved from GWOSC and returned by
        :meth:`requests.Response.json`
    N)r   T)r#   r   )r+   r   r*   )r#   r   r   r   r   fetch_allevents_json   s    r,   c                s,  t ||dd }g } fdd}tt|| }tdd |D }|krtfdd|D }td	d |D }t|d
krt|dd dd \}	}
d|	|
iiS t|d
krtdd	|d} dkr|d}dk	r|d7 }t||d} dk	r |d 7 }t|dS )zReturns the JSON metadata from the allevents view for a specific event

    The raw JSON data are packaged to look the same as if they came from
    a full event API query, i.e. nested under `'events`'.
    )r#   r   eventsc                sb   | \}}||d |d  ddd hkr.d S d k	rF|d krFd S  d k	r^|d  kr^d S dS )N
commonName_r   r   versionzcatalog.shortNameT)split)ZkeyvalueZdsetmetadata)r'   eventr0   r   r   _match   s    z+_fetch_allevents_event_json.<locals>._matchc             s   s   | ]}|d  d V  qdS )r   r.   Nr   ).0xr   r   r   	<genexpr>   s    z._fetch_allevents_event_json.<locals>.<genexpr>c                s    g | ]}|d  d  kr|qS )r   r.   r   )r5   r6   )r3   r   r   
<listcomp>  s    z/_fetch_allevents_event_json.<locals>.<listcomp>c             s   s   | ]}|d  d V  qdS )r   r.   Nr   )r5   r6   r   r   r   r7     s    r   c             S   s   | d d S )Nr   r0   r   )r6   r   r   r   <lambda>	      z-_fetch_allevents_event_json.<locals>.<lambda>)keyz&multiple events matched for {!r}: '{}'z', 'z$failed to identify {} for event '{}'Nr'   z at version {}r0   z in catalog '{}')
r,   listfilteritemssetlensorted
ValueErrorr
   join)r3   r'   r0   r#   r   Z	alleventsZmatchedr4   namesr;   metamsgr   )r'   r3   r0   r   _fetch_allevents_event_json   s4    
rH   c       
      C   s   t |d}|  }|d |d  }}t||fts>td|d |d |d   }}}||krvtd| d	| g }	d
|kr|	d| |f d|kr|	d| |f d
|kr|	d| |f d|kr|	d| |f |	S )N)r   op1op2z1Could not parse select string.
Unknown operators.paramval1val2z7Could not parse select string.
Unrecognized parameter: z.
Use one of:
>zmax-<zmin-)r"   	groupdictr@   issubset_ALLOWED_OPSrC   append)

compiled_mr   allowed_paramsmdrI   rJ   rK   rL   rM   queriesr   r   r   _parse_two_ops   s(    
rX   c             C   s   t |d}|  }|d }t|fts2td|d |d  }}||kr`td| d| g }d|kr|d	| |f d
|kr|d| |f |S )N)r   opz0Could not parse select string.
Unknown operator.rK   valz7Could not parse select string.
Unrecognized parameter: z.
Use one of:
rN   zmin-rO   zmax-)r"   rP   r@   rQ   rR   rC   rS   )rT   r   rU   rV   rY   rK   rZ   rW   r   r   r   _parse_one_op<  s     
r[   c       	      C   s   t d}t d}g }x^| D ]V}xP|tf|tffD ].\}}||}|dk	r4||||d P q4W td| qW t|S )z9Parse select string and translate into URL GET parameterszm^\s*(?P<val1>[\d.+-eE]+)\s*(?P<op1>[<>=]{2})\s*(?P<param>[\w-]+)\s*(?P<op2>[<>=]+)\s*(?P<val2>[\d.+-eE]+)\s*$zB^\s*(?P<param>[\w-]+)\s*(?P<op>[<>=]{2})\s*(?P<val>[\d.+-eE]+)\s*$N)r   zCould not parse select string: )recompiler[   rX   matchextendrC   r   )	selectr   Ztwo_opsZone_oprW   sregex_parsemr   r   r   _select_to_queryT  s    

re   c             C   s   d |t| |dS )Nz{}/eventapi/json/query/show?{})r   )r
   re   )r`   r   r   r   r   _query_events_urlp  s    rf   c             C   s   t t| |dS )a  "Return the JSON metadata for the events constrained by select

    Parameters
    ----------
    select : `list-like`
        a list of range constrains for the events.
        All ranges should have inclusive ends (<= and >= operators).

    host : `str`, optional
        the URL of the GWOSC host to query, defaults to
        https://gwosc.org

    Returns
    -------
    data : `dict` or `list`
        the JSON data retrieved from GWOSC and returnend by
        :meth:`requests.Response.json`

    Example
    -------
    >>> fetch_filtered_events_json(
    ...     select=[
    ...         "mass-1-source <= 5",
    ...         "mass-2-source =< 10",
    ...         "10 <= luminosity-distance <= 100",
    ...     ]
    ... )
    )r   )r   rf   )r`   r   r   r   r   fetch_filtered_events_jsonv  s    rg   c             C   s&   t t| ||d |dd  d d S )N)r'   r0   r#   r   r-   r   Zjsonurl)r=   rH   values)r3   r'   r0   r   r   r   r   
_event_url  s    ri   c             C   s   t t| |||dS )a  Returns the JSON metadata for the given event.

    By default, this function queries across all catalogs and all data-release
    versions, returning the highest available version, unless the
    ``version`` and/or ``catalog`` keywords are specified.

    Parameters
    ----------
    event : `str`
        the name of the event to query

    catalog : `str`, optional
        name of catalogue that hosts this event

    version : `int`, `None`, optional
        restrict query to a given data-release version

    host : `str`, optional
        the URL of the GWOSC host to query, defaults to
        https://gwosc.org

    Returns
    -------
    data : `dict` or `list`
        the JSON data retrieved from GWOSC and returned by `json.loads`
    )r'   r0   r   )r   ri   )r3   r'   r0   r   r   r   r   fetch_event_json  s     rj   )0__doc__loggingosr\   urllib.parser    r   r   	getLoggerr   StreamHandlerZ_loghandlersetFormatter	FormatterBASIC_FORMAT
addHandlersetLevelintgetenvNOTSETZ_MAX_GPSZDEFAULT_URLr   r@   rR   r   r   r   r   r    r!   r"   r%   r&   r(   r)   r*   r+   r,   rH   rX   r[   re   rf   rg   ri   rj   r   r   r   r   <module>   s\   

%:"