B
    d,                 @   s   d Z ddlZddlZddlmZ ddlmZmZ yddlmZ	 W n e
k
rX   dZY nX dZdd	lmZmZ d
ZejZejZdgZdddZdddZdddZdddZdddZdd Zdd ZdS )a  Wrappings of the LALFrame input methods for reading GWF files

This module provides the read_timeseries function, the primary method
by which users can load TimeSeries data from a variety of sources,
including:
    - a Gravitational-Wave Frame file (.gwf file extension)
    - a LAL-format cache file (.lcf or .cache file extension)
    - a LAL-format cache object (either XLALCache() or glue.lal.Cache [1]

[1] https://www.lsc-group.phys.uwm.edu/daswg/projects/glue/doc/glue.lal.Cache-class.html
    N)string_types)utilslal)r   FT   )lalframegit_versionz(Duncan Macleod <duncan.macleod@ligo.org>read_timeseriesc             C   s   t |tr|g}nt|}t | trBtd| rBttj	| } t
r\t | tjr\t| } t | tr| drt| |||||d}n.t | tjrt| |||||d}ntd|  t |tr|d S |S dS )ac	  Read a TimeSeries of channel data from a source.

    Acceptable sources are:
        - a .gwf-format framefile (string ending in '.gwf')
        - a LAL-format cache file (string ending in '.lcf' or '.cache')
        - a lal.Cache object (either from SWIG-LAL or GLUE)

    @param source
        input source, see above for details
    @param channel
        string name of channel, e.g. 'L1:LDAS-STRAIN', or a list of channel
        names
    @param start
        LIGOTimeGPS start time for output TimeSeries
    @param duration
        float duration (seconds) for output TimeSeries
    @param datatype
        datatype, either an integer from the LALTYPECODE, a string
        matchine the corresponding type, or a numpy dtype
    @param verbose
        print verbose output, default: False

    @returns a TimeSeries of the imported data

    Example 1, reading from a frame file:

    \code
    >>> out = read_timeseries('L-R-1061499968-32.gwf', 'L1:PSL-ISS_PDB_OUT_DQ')
    >>> print(type(out))
    <type 'REAL4TimeSeries'>
    >>> print(out.name, float(out.epoch), out.deltaT)
    ('L1:PSL-ISS_PDB_OUT_DQ', 1061499968.0, 3.0517578125e-05)
    \endcode

    Example 2, reading from a cache:

    \code
    >>> import lal
    >>> cache = lal.CacheGlob('/scratch/ER4/L0/L1/L-R-10614', 'L-R-1061499968*')
    >>> out = frread.read_timeseries(cache, 'L1:PSL-ISS_PDB_OUT_DQ')
    >>> print(out.name, float(out.epoch), out.deltaT)
    ('L1:PSL-ISS_PDB_OUT_DQ', 1061499968.0, 3.0517578125e-05)
    \endcode

    Example 3, restricting data input:

    \code
    >>> out = read_timeseries('L-R-1061499968-32.gwf', 'L1:PSL-ISS_PDB_OUT_DQ',
                              start=1061499970, duration=10)
    >>> print(out.name, float(out.epoch), out.data.length)
    ('L1:PSL-ISS_PDB_OUT_DQ', 1061499970.0, 327680)
    \endcode

    Example 4, specifying data type:

    \code
    >>> out = read_timeseries('L-R-1061499968-32.gwf',
                              'L1:PSL-ODC_CHANNEL_OUT_DQ')
    >>> print(type(out), out.data.data[:4])
    (<type 'REAL4TimeSeries'>,
     array([ 4259839.,  4259839.,  4259839.,  4259839.], dtype=float32))
    >>> out = read_timeseries('L-R-1061499968-32.gwf',
                              'L1:PSL-ODC_CHANNEL_OUT_DQ', datatype='int8')
    >>> print(type(out), out.data.data[:4])
    (<type 'INT8TimeSeries'>, array([4259839, 4259839, 4259839, 4259839]))
    \endcode

    z(.lcf|.cache)\Zz.gwf)startdurationdatatypeverbosezCannot interpret source '%s'.r   N)
isinstancer   listresearchr   ZCacheImportospath
expanduser	_HAS_GLUEgcacheCachelalutilsZlalcache_from_gluecacheendswith_ts_from_frame_file_ts_from_cache
ValueError)sourcechannelr	   r
   r   r   channelsout r    \/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/lalframe/frread.pyr   4   s(    G





c             C   s   t | }t||||||dS )a*  Read a TimeSeries of channel data from a LAL Cache object

    @param cache
        XLALCache() containing list of GWF file paths
    @param channels
        list of channel names
    @param start
        LIGOTimeGPS start time for output TimeSeries
    @param duration
        float duration (seconds) for output TimeSeries
    @param datatype
        datatype, either an integer from the LALTYPECODE, a string
        matchine the corresponding type, or a numpy dtype
    @param verbose UNDOCUMENTED

    @returns a TimeSeries of the imported data
    )r	   r
   r   r   )r   ZFrCacheOpen_ts_from_stream)cacher   r	   r
   r   r   streamr    r    r!   r      s    

r   c             C   s,   t j| } td| }t||||||dS )aI  Read a TimeSeries of channel data from a GWF-format framefile

    @param framefile
        path to GWF-format framefile to read
    @param channels
        list of channel names
    @param start
        LIGOTimeGPS start time for output TimeSeries
    @param duration
        float duration (seconds) for output TimeSeries
    @param datatype
        datatype, either an integer from the LALTYPECODE, a string
        matchine the corresponding type, or a numpy dtype
    @param verbose
        print verbose output, default: False

    @returns a TimeSeries of the imported data
     )r	   r
   r   r   )r   r   abspathr   ZFrStreamOpenr"   )Z	framefiler   r	   r
   r   r   r$   r    r    r!   r      s    
r   c       
   
   C   s   t |rt jpt j|  t| j}|dkr0|}|sPt|| }tt| | }g }x0|D ](}	|	t
| |	|||d t | | qZW |S )aE  Read a TimeSeries of channel data from an open FrStream

    @param stream
        XLALFrStream() of data from which to read
    @param channels
        list of channel names
    @param start
        LIGOTimeGPS start time for output TimeSeries
    @param duration
        float duration (seconds) for output TimeSeries
    @param datatype
        datatype, either an integer from the LALTYPECODE, a string
        matchine the corresponding type, or a numpy dtype
    @param verbose
        print verbose output, default: False

    @returns a TimeSeries of the imported data
    N)r   )r   Z	FrSetModeZFR_STREAM_VERBOSE_MODEZFR_STREAM_DEFAULT_MODEr   LIGOTimeGPSepochfloatget_stream_durationappendread_channel_from_streamFrStreamSeek)
r$   r   r	   r
   r   r   r(   Zstartoffsetr   r   r    r    r!   r"      s    


r"   c       
      C   s   t || }|dkr|}n
t|}tt dt| }|| |||d}||krT|S ||krtddt| }||||j|j|j	|j
j}	|j
j
t||	j
_
|	S dS )z@Read the TimeSeries of a single channel from an open stream
    NzFrStreamRead%sTimeSeriesr   createz%stimeseries)r   ZFrStreamGetTimeSeriesTyper   Zget_lal_typegetattrZget_lal_type_strZfunc_factoryZf0ZdeltaTZsampleUnitsdatalengthZastypeZget_numpy_type)
r$   r   r	   r
   r   Z
frdatatypereadoriginr.   Zseriesr    r    r!   r,      s"    
r,   c             C   sz   t | jj| jj}| jj}d}xHt|D ]<}x6tt	| j
D ]"}|t| j
|d7 }t|  q@W q*W t| | |S )zFind the number of samples represented in a frame stream

    @param stream
        XLALFrStream() of data to measure
    @param channel
        string name of channel to measure

    @returns the integer length of the data for this channel
    r   )r   r'   r(   
gpsSecondsgpsNanoSecondsr#   r1   ranger   FrFileQueryNFramefileZFrFileQueryChanVectorLengthFrStreamNextr-   )r$   r   r(   nfiler1   ijr    r    r!   get_stream_length  s    




r=   c             C   sx   t | jj| jj}| jj}d}xFt|D ]:}x4tt	| j
D ] }|t| j
d7 }t|  q@W q*W t| | |S )zFind the duration of time stored in a frame stream

    @param stream
        XLALFrStream() of data to measure

    @returns the float duration (seconds) of the data for this channel
    r   )r   r'   r(   r4   r5   r#   r1   r6   r   r7   r8   ZFrFileQueryDtr9   r-   )r$   r(   r:   r
   r;   r<   r    r    r!   r*   .  s    

r*   )NNNF)NNNF)NNNF)NNNF)N)__doc__r   r   sixr   r   r   r   Zgluer   ImportErrorr   r%   r   r   
__author__id__version__date__date____all__r   r   r   r"   r,   r=   r*   r    r    r    r!   <module>   s2   
 
h 
 
 
&
