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
 dZG d	d
 d
eddZdd ZG dd deddZdd Zdd ZdS )z(I/O (mainly O) routines for GWDataFind.
    N)
namedtuple)
attrgetter)urlparse   )filename_metadataz(Duncan Macleod <duncan.macleod@ligo.org>c               @   s$   e Zd ZdZdd Zedd ZdS )LalCacheEntryzuSimplified version of `lal.utils.CacheEntry`.

    This is provided so that we don't have to depend on lalsuite.
    c          	   C   s.   | j }dtt| j| j|d t|| jfS )N r   )segmentjoinmapstrobstagabsurl)selfseg r   Z/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/gwdatafind/io.py__str__'   s    zLalCacheEntry.__str__c             K   s   t |\}}}| ||||S )zKCreate a new `LalCacheEntry` from a URL that follows LIGO-T050017.
        )r   )clsr   kwargsr   r   r   r   r   r   from_url1   s    zLalCacheEntry.from_urlN)__name__
__module____qualname____doc__r   classmethodr   r   r   r   r   r       s   
r   
CacheEntry)r   r   r	   r   c             C   s   dd | D S )zlConvert a list of URLs into a LAL cache.

    Returns
    -------
    cache : `list` of `LalCacheEntry`
    c             S   s   g | ]}t |qS r   )r   r   ).0r   r   r   r   
<listcomp>@   s    zlal_cache.<locals>.<listcomp>r   )urlsr   r   r   	lal_cache9   s    r"   c               @   s   e Zd ZdZdd ZdS )OmegaCacheEntryzCacheEntry for an omega-style cache.

    Omega-style cache files contain one entry per contiguous directory of
    the form:

        <obs> <tag> <dir-start> <dir-end> <file-duration> <directory>
    c          
   C   s0   d tt| j| j| jd | jd | j| jfS )Nr   r   r   )r
   r   r   r   r   r	   durationr   )r   r   r   r   r   N   s    zOmegaCacheEntry.__str__N)r   r   r   r   r   r   r   r   r   r#   C   s   
r#   )r   r   r	   r$   r   c             C   s   g }|j }d}xt| tddddD ]}tj|j}|r|j|jkr|j|jkr||jkrt	|j
|jkr|j
|j
s|j
|j
rt|j|j|j
|j
B |j|j |d< }q$t|j|j|j
t	|j
|}|| q$W |S )zConvert a list of `LalCacheEntry` into a list of `OmegaCacheEntry`.

    Returns
    -------
    cache : `list` of `OmegaCacheEntry`
    Nr   r   r	   )key)appendsortedr   ospathdirnamer   r   r   r   r	   r$   ZconnectsZ
intersectsr#   )cacheZwcacher'   ZwentryentryZdir_r   r   r   omega_cacheY   s:    

r.   c             C   s\   |dkr| S |dkr"dd | D S |dkr8dd | D S |dkrHt | S td| d	d
S )zFormat a list of `LalCacheEntry` into a different format.

    Valid formats:

    - ``omega`` - return a list of Omega-format cache entries
    Zlalr!   c             S   s   g | ]
}|j qS r   )r   )r   er   r   r   r       s    z format_cache.<locals>.<listcomp>namesc             S   s   g | ]}t |jjqS r   )r   r   r*   )r   r/   r   r   r   r       s    omegazinvalid format ''N)r.   
ValueError)r,   fmtr   r   r   format_cache   s    r5   )r   os.pathr)   collectionsr   operatorr   urllib.parser   utilsr   
__author__r   r"   r#   r.   r5   r   r   r   r   <module>   s   

0