B
    dh                 @   sJ   d Z ddlmZ dZdddZdd Zd	d
 Zdd Zdd Zdd Z	dS )z@
This modules contains functions for getting data from the LOSC
    )get_filez>https://www.gw-openscience.org/archive/links/%s/%s/%s/%s/json/Nc             C   s   d|   krdkr$n n|dkr$dS d|   kr8dkr@n ndS d|   krTd	kr\n nd
S d|   krpdkrxn ndS d|   krdkrn ndS d|   krdkrn ndS d|   krdkrn ndS t d|  dS )a   Return the run name for a given time

    Parameters
    ----------
    time: int
        The gps time
    ifo: str
        The ifo prefix string. Optional and normally unused,
        except for some special times where data releases
        were made for a single detector under
        unusual circumstances. For example, to get the data around GW170608
        in the Hanford detector.
    iHcFi\dFZH1ZBKGW170608_16KHZ_R1i,JiH%NZO3b_16KHZ_R1iIi,JZO3a_16KHZ_R1iiEir`FZO2_16KHZ_R1i-Ci CZO1i0iL,4ZS5iz7i9ZS6z)Time %s not available in a public datasetN)
ValueError)timeifo r   ]/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/pycbc/frame/losc.pyget_run   s     r   c             C   s   | dk rdS dS d S )NiiEzLOSC-STRAINzGWOSC-16KHZ_R1_STRAINr   )r   r   r   r   _get_channel9   s    r	   c       	   
   C   s   ddl }yddlm} W n  tk
r8   ddlm} Y nX t|}t|}||krbtd||f t|| t|t|f }y|	||
  S  tk
r } z t| td| |||f W dd}~X Y nX dS )a   Get the information about the public data files in a duration of time

    Parameters
    ----------
    ifo: str
        The name of the IFO to find the information about.
    start_time: int
        The gps time in GPS seconds
    end_time: int
        The end time in GPS seconds

    Returns
    -------
    info: dict
        A dictionary containing information about the files that span the
        requested times.
    r   N)urlopenzbSpanning multiple runs is not currently supported.You have requested data that uses both %s and %sz:Failed to find gwf files for ifo=%s, run=%s, between %s-%s)jsonurllib.requestr
   ImportErrorurllibr   r   	_losc_urlintloadsreaddecode	Exceptionprint)	r   
start_timeend_timer   r
   runZrun2urler   r   r   losc_frame_json?   s"    r   c             C   s   t | ||d }dd |D S )a   Get a list of urls to losc frame files

    Parameters
    ----------
    ifo: str
        The name of the IFO to find the information about.
    start_time: int
        The gps time in GPS seconds
    end_time: int
        The end time in GPS seconds

    Returns
    -------
    frame_files: list
        A dictionary containing information about the files that span the
        requested times.
    Zstrainc             S   s    g | ]}|d  dkr|d qS )formatZgwfr   r   ).0dr   r   r   
<listcomp>y   s    z#losc_frame_urls.<locals>.<listcomp>)r   )r   r   r   datar   r   r   losc_frame_urlsf   s    r!   c       	         s   ddl m t| ts| g} dd | D }i }x8|D ]0}t| ||< t|| dkr4td| q4W dd |D x8|D ]0}x*|| D ]}t|dd	}| | qW q|W  fd
d| D }t|dkr|d S |S dS )a|   Read channels from losc data

    Parameters
    ----------
    channels: str or list
        The channel name to read or list of channel names.
    start_time: int
        The gps time in GPS seconds
    end_time: int
        The end time in GPS seconds

    Returns
    -------
    ts: TimeSeries
        Returns a timeseries or list of timeseries with the requested data.
    r   )
read_framec             S   s   g | ]}|d d qS )r      r   )r   cr   r   r   r      s    z#read_frame_losc.<locals>.<listcomp>z6No data found for %s so we can't produce a time seriesc             S   s   i | ]
}g |qS r   r   )r   r   r   r   r   
<dictcomp>   s    z#read_frame_losc.<locals>.<dictcomp>T)cachec                s(   g | ] }|d d  | dqS )r   r#   )r   r   r   )r   channel)r   fnamesr"   r   r   r   r      s      N)	Zpycbc.framer"   
isinstancelistr!   lenr   r   append)	Zchannelsr   r   Zifosurlsr   r   fnametsr   )r   r(   r"   r   r   read_frame_losc{   s(    


r1   c             C   s   t |}td| |f ||S )ah   Get the strain data from the LOSC data

    Parameters
    ----------
    ifo: str
        The name of the IFO to read data for. Ex. 'H1', 'L1', 'V1'
    start_time: int
        The gps time in GPS seconds
    end_time: int
        The end time in GPS seconds

    Returns
    -------
    ts: TimeSeries
        Returns a timeseries with the strain data.
    z%s:%s)r	   r1   )r   r   r   r'   r   r   r   read_strain_losc   s    r2   )N)
__doc__Zpycbc.ior   r   r   r	   r   r!   r1   r2   r   r   r   r   <module>   s   
"')