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Zddl	m
Z dd	lmZ d
ZdgZdZdZdZdZdZdddddddddd	ZedZd"ddZdd  ZG d!d deZdS )#z7Extend :mod:`astropy.table` with the `GravitySpyTable`
    N)JSONDecodeError)Path)	urlencode   )mp   )
EventTablez(Scott Coughlin <scott.coughlin@ligo.org>GravitySpyTablezapplication/jsonz.https://gravityspytools.ciera.northwestern.eduz/searchz/similarity_search_restful_API)g      ?g      ?g       @g      @)i #Cij^)i #CiC)iLAEihE)ihEiH)i>IiCI)iIiI)iIi)K)iIiJ)iJi)K)	ALLZO1ZER10ZO2aZER13ZER14ZO3ZO3aZO3bz\Aurl[0-9]+\ZFc             C   sL   | d }| d }|r| d nd}|r,| d nd}t || | d|| S )zConstruct where to download a GravitySpy image

    This returns a `str.format`-style template that just needs
    the duration to form a complete target path.
    ifogravityspy_idZml_label Zsample_typez{}_{}_spectrogram_{{}}.png)strformat)rowZoutdirtraining_setlabelled_samplesr   Zid_labelstype r   b/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/gwpy/table/gravityspy.py_image_download_targetI   s    r   c             C   s   | \}}| t|jS )N)write_bytesrequestsgetcontent)Zbundleurltargetr   r   r   _download_image`   s    r   c               @   s<   e Zd ZdZddeddfddZeddd	d
efddZdS )r	   a3  A container for a table of Gravity Spy Events (as well as
    Events from the O1 Glitch Classification Paper whcih includes

    - PCAT
    - PC-LIB
    - WDF
    - WDNN
    - Karoo GP

    See also
    --------
    astropy.table.Table
        for details on parameters for creating a `GravitySpyTable`
    downloadr   Fc             C   s   t |}tttj| j}t }xr| D ]j}|d dkr8q&t||||d}	xF|D ]>}
t|
dd }t	|d  }||krNt |	
||||
 < qNW q&W x | D ]}|jjddd qW t|t|  ttt| S )	a  Download image files associated with entries in a `GravitySpyTable`.

        Parameters
        ----------
        nproc : `int`, optional, default: 1
            number of CPUs to use for parallel file reading

        download_path : `str` optional, default: 'download'
            Specify where the images end up.

        download_durs : `list` optional, default: [0.5, 1.0, 2.0, 4.0]
            Specify exactly which durations you want to download
            default is to download all the avaialble GSpy durations.

        training_set : `bool`, optional
            if `True` download training set data

        labelled_samples : `bool`, optional
            if `True` download only labelled samples
        url1>   r   N?)r   r      Nr   T)exist_okparents)r   listfilter
URL_COLUMNmatchZcolnamesdictr   int	DURATIONSr   valuesparentmkdirmp_utilsZmultiprocess_with_queuesr   itemssortedmapr   )selfdownload_pathZnprocZdownload_dursr   r   Zurl_columnsurlsr   Z_download_targetcolidxdurationpathr   r   r   r   w   s.    


zGravitySpyTable.download
   r
   )ZH1ZL1Zsimilarity_index_o3c          	      s   |t  t }t tr4 fddtdt dD  t||djt|  d	t
t |d}	d||	}
tj|
f|}|  yt| S  tk
r   |jd	 tkrtjd
|j|d Y nX dS )a  Perform a GravitySpy 'Similarity Search' for the given ID.

        Parameters
        ----------
        gravityspy_id : `str`
            the unique 10 character hash that identifies a Gravity Spy image

        howmany : `int`, optional
            number of similar images you would like

        era : `str`, optional
            which era to search, see online for more information

        ifos : `tuple`, str`, optional
            the list of interferometers to include in the search

        database : `str`, optional
            the database to query

        host : `str`, optional
            the URL (scheme and FQDN) of the Gravity Spy host to query

        **kwargs
            all other kwargs are passed to `requests.get` to perform
            the actual remote communication.

        Returns
        -------
        table : `GravitySpyTable`
            a `GravitySpyTable` containing similar events based on
            an evaluation of the Euclidean distance of the input image
            to all other images in some Feature Space

        Notes
        -----
        For an online version, and documentation of the search, see

        https://gravityspytools.ciera.northwestern.edu/search/
        c                s   g | ]} ||d   qS )r   r   ).0i)ifosr   r   
<listcomp>   s    z*GravitySpyTable.search.<locals>.<listcomp>r   r   zevent_time BETWEEN {} AND {}z, )howmanyZimageiderar   databasez{}/?{}zContent-TypezZresponse from {} was '200 OK' but the content is HTML, please check the request parameters)responseN)SEARCH_PATHSIMILARITY_SEARCH_PATH
isinstancer   rangelenr   r   ERAjoinr2   reprr   r   raise_for_statusr	   jsonr   headersJSON_CONTENT_TYPE	HTTPErrorr   )clsr   r?   r@   r=   rA   hostkwargsbasequeryr   rB   r   )r=   r   search   s*    2

zGravitySpyTable.searchN)	__name__
__module____qualname____doc__r+   r   classmethodDEFAULT_HOSTrU   r   r   r   r   r	   e   s   ?)FF)rY   rerL   r   pathlibr   urllib.parser   r   utilsr   r/   tabler   
__author____all__rN   r[   rC   rD   r+   rH   compiler'   r   r   r	   r   r   r   r   <module>   s8   
 
