B
    d==                 @   s~   d dl mZ d dlZd dlmZmZmZ d dlm	Z	m
Z
 d dlmZ d dlmZ d dlmZmZ dgZeG d	d deZdS )
    )deepcopyN)groupsTableQTable)Time	TimeDelta)units)Quantity)BaseTimeSeriesautocheck_required_columnsBinnedTimeSeriesc                   s   e Zd ZdZddgZdddddd fddZedd	 Zed
d Zedd Z	edd Z
 fddZed fdd	Z  ZS )r   a  
    A class to represent binned time series data in tabular form.

    `~astropy.timeseries.BinnedTimeSeries` provides a class for
    representing time series as a collection of values of different
    quantities measured in time bins (for time series with values
    sampled at specific times, see the `~astropy.timeseries.TimeSeries`
    class). `~astropy.timeseries.BinnedTimeSeries` is a sub-class of
    `~astropy.table.QTable` and thus provides all the standard table
    maniplation methods available to tables, but it also provides
    additional conveniences for dealing with time series, such as a
    flexible initializer for setting up the times, and attributes to
    access the start/center/end time of bins.

    See also: https://docs.astropy.org/en/stable/timeseries/

    Parameters
    ----------
    data : numpy ndarray, dict, list, table-like object, optional
        Data to initialize time series. This does not need to contain the
        times, which can be provided separately, but if it does contain the
        times they should be in columns called ``'time_bin_start'`` and
        ``'time_bin_size'`` to be automatically recognized.
    time_bin_start : `~astropy.time.Time` or iterable
        The times of the start of each bin - this can be either given
        directly as a `~astropy.time.Time` array or as any iterable that
        initializes the `~astropy.time.Time` class. If this is given, then
        the remaining time-related arguments should not be used. This can also
        be a scalar value if ``time_bin_size`` is provided.
    time_bin_end : `~astropy.time.Time` or iterable
        The times of the end of each bin - this can be either given directly
        as a `~astropy.time.Time` array or as any value or iterable that
        initializes the `~astropy.time.Time` class. If this is given, then the
        remaining time-related arguments should not be used. This can only be
        given if ``time_bin_start`` is an array of values. If ``time_bin_end``
        is a scalar, time bins are assumed to be contiguous, such that the end
        of each bin is the start of the next one, and ``time_bin_end`` gives
        the end time for the last bin. If ``time_bin_end`` is an array, the
        time bins do not need to be contiguous. If this argument is provided,
        ``time_bin_size`` should not be provided.
    time_bin_size : `~astropy.time.TimeDelta` or `~astropy.units.Quantity`
        The size of the time bins, either as a scalar value (in which case all
        time bins will be assumed to have the same duration) or as an array of
        values (in which case each time bin can have a different duration).
        If this argument is provided, ``time_bin_end`` should not be provided.
    n_bins : int
        The number of time bins for the series. This is only used if both
        ``time_bin_start`` and ``time_bin_size`` are provided and are scalar
        values.
    **kwargs : dict, optional
        Additional keyword arguments are passed to `~astropy.table.QTable`.
    time_bin_starttime_bin_sizeN)r   time_bin_endr   n_binsc      	   	      s  t  jf d|i| |d krH|d krH|d krH|d krH|d krHd| _d S d| jkrn|d krf| jd }ntdd| jkr|d kr| jd }ntd|d krtd|d kr|d krtdt|ttfst|}|d k	rt|ttfst|}|d k	rt|t	tfstd	t|tr*|j
tj }|d k	rV|d k	rV|jrN|jsVtd
|jr|d krptd|jr|d k	r|d k	r|t| krtdnt| }t||}t|}|| }t|d}dtj |d< || }nt| jdkr(t|t| kr(tdt|t| |d k	rr|jrb| }|dd  |d d< ||d< |}|| j
tj }|jrt|t| }|  \ d| jkr| d d| jkr| d | j|ddd | d | j|ddd W d Q R X d S )NdataTr   zK'time_bin_start' has been given both in the table and as a keyword argumentr   zJ'time_bin_size' has been given both in the table and as a keyword argumentz''time_bin_start' has not been specifiedz<Either 'time_bin_size' or 'time_bin_end' should be specifiedz3'time_bin_size' should be a Quantity or a TimeDeltazS'n_bins' cannot be specified if 'time_bin_start' or 'time_bin_size' are not scalar'z:'time_bin_start' is scalar, so 'time_bin_size' is requiredzH'n_bins' has been given and it is not the same length as the input data.   g        r   z>Length of 'time_bin_start' ({}) should match table length ({}))indexname)super__init__Z_required_columns_relaxcolnamescolumns	TypeError
isinstancer   r   r	   secusisscalarlennprepeatZcumsumZroll
ValueErrorformatcopyZ_delay_required_column_checksremove_column
add_columnZ	add_index)	selfr   r   r   r   r   kwargsZ
time_deltatimes)	__class__ f/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/astropy/timeseries/binned.pyr   J   s~    







"




zBinnedTimeSeries.__init__c             C   s   | d S )z7
        The start times of all the time bins.
        r   r,   )r(   r,   r,   r-   r      s    zBinnedTimeSeries.time_bin_startc             C   s   | d | d d  S )z8
        The center times of all the time bins.
        r   r   g      ?r,   )r(   r,   r,   r-   time_bin_center   s    z BinnedTimeSeries.time_bin_centerc             C   s   | d | d  S )z5
        The end times of all the time bins.
        r   r   r,   )r(   r,   r,   r-   r      s    zBinnedTimeSeries.time_bin_endc             C   s   | d S )z1
        The sizes of all the time bins.
        r   r,   )r(   r,   r,   r-   r      s    zBinnedTimeSeries.time_bin_sizec                sh     |r\d|ksd|kr\t fdd|D t j jd}tj| jj jjd|_	|S t
 |S )Nr   r   c                s   g | ]} | qS r,   r,   ).0x)r(   r,   r-   
<listcomp>   s    z0BinnedTimeSeries.__getitem__.<locals>.<listcomp>)metaZcopy_indices)indiceskeys)Z_is_list_or_tuple_of_strr   r   r2   Z_copy_indicesr   ZTableGroupsZ_indicesZ_keysZ_groupsr   __getitem__)r(   itemout)r+   )r(   r-   r5      s    

zBinnedTimeSeries.__getitem__c	          
      s  yt  j|f|	d|i|
S  tk
r   |dkr>td|dkrX|dkrXtdn|dk	rp|dk	rptdtj|f|	d|i|
}||jkrt|j| ||d}|| ntd| d|dk	r||jkrt|j| ||d}|| ntd	| dd}n|dk	r||jkr:|j| }|| ntd
| d|j	dkr|dksnt
|tjsvtd|| }n
t|}d}|jr|jrt||||t|dS t||||dS Y nX dS )a	  
        Read and parse a file and returns a `astropy.timeseries.BinnedTimeSeries`.

        This method uses the unified I/O infrastructure in Astropy which makes
        it easy to define readers/writers for various classes
        (https://docs.astropy.org/en/stable/io/unified.html). By default, this
        method will try and use readers defined specifically for the
        `astropy.timeseries.BinnedTimeSeries` class - however, it is also
        possible to use the ``format`` keyword to specify formats defined for
        the `astropy.table.Table` class - in this case, you will need to also
        provide the column names for column containing the start times for the
        bins, as well as other column names (see the Parameters section below
        for details)::

            >>> from astropy.timeseries.binned import BinnedTimeSeries
            >>> ts = BinnedTimeSeries.read('binned.dat', format='ascii.ecsv',
            ...                            time_bin_start_column='date_start',
            ...                            time_bin_end_column='date_end')  # doctest: +SKIP

        Parameters
        ----------
        filename : str
            File to parse.
        format : str
            File format specifier.
        time_bin_start_column : str
            The name of the column with the start time for each bin.
        time_bin_end_column : str, optional
            The name of the column with the end time for each bin. Either this
            option or ``time_bin_size_column`` should be specified.
        time_bin_size_column : str, optional
            The name of the column with the size for each bin. Either this
            option or ``time_bin_end_column`` should be specified.
        time_bin_size_unit : `astropy.units.Unit`, optional
            If ``time_bin_size_column`` is specified but does not have a unit
            set in the table, you can specify the unit manually.
        time_format : str, optional
            The time format for the start and end columns.
        time_scale : str, optional
            The time scale for the start and end columns.
        *args : tuple, optional
            Positional arguments passed through to the data reader.
        **kwargs : dict, optional
            Keyword arguments passed through to the data reader.

        Returns
        -------
        out : `astropy.timeseries.binned.BinnedTimeSeries`
            BinnedTimeSeries corresponding to the file.

        r$   Nz\``time_bin_start_column`` should be provided since the default Table readers are being used.zJEither `time_bin_end_column` or `time_bin_size_column` should be provided.zECannot specify both `time_bin_end_column` and `time_bin_size_column`.)scaler$   zBin start time column 'z' not found in the input data.zBin end time column 'zBin size column 'zVThe bin size unit should be specified as an astropy Unit using ``time_bin_size_unit``.)r   r   r   r   r   )r   r   r   r   )r   readr   r#   r   r   r   r   r&   unitr   r   ZUnitBaser	   r   r   r    )r(   filenameZtime_bin_start_columnZtime_bin_end_columnZtime_bin_size_columnZtime_bin_size_unittime_formatZ
time_scaler$   argsr)   tabler   r   r   )r+   r,   r-   r9      sV    8











zBinnedTimeSeries.read)N)NNNNNNN)__name__
__module____qualname____doc__Z_required_columnsr   propertyr   r.   r   r   r5   classmethodr9   __classcell__r,   r,   )r+   r-   r      s   5p  )r%   r   numpyr!   Zastropy.tabler   r   r   Zastropy.timer   r   Zastropyr   r   Zastropy.unitsr	   Zastropy.timeseries.corer
   r   __all__r   r,   r,   r,   r-   <module>   s   