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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QTableTable)Time	TimeDelta)units)Quantity
UnitsError)deprecated_renamed_argument)BaseTimeSeriesautocheck_required_columns
TimeSeriesc                   s   e Zd ZdZdgZdddddd fddZedd Zed	d
ddddZ	 fddZ
 fddZ fddZedddZdd Zed  fdd	Z  ZS )!r   a  
    A class to represent time series data in tabular form.

    `~astropy.timeseries.TimeSeries` provides a class for representing time
    series as a collection of values of different quantities measured at specific
    points in time (for time series with finite time bins, see the
    `~astropy.timeseries.BinnedTimeSeries` class).
    `~astropy.timeseries.TimeSeries` 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, a method
    for folding time series, and a ``time`` attribute for easy access to the
    time values.

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

    Parameters
    ----------
    data : numpy ndarray, dict, list, `~astropy.table.Table`, or 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 a column called ``'time'`` to be automatically recognized.
    time : `~astropy.time.Time`, `~astropy.time.TimeDelta` or iterable
        The times at which the values are sampled - this can be either given
        directly as a `~astropy.time.Time` or `~astropy.time.TimeDelta` 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.
    time_start : `~astropy.time.Time` or str
        The time of the first sample in the time series. This is an alternative
        to providing ``time`` and requires that ``time_delta`` is also provided.
    time_delta : `~astropy.time.TimeDelta` or `~astropy.units.Quantity` ['time']
        The step size in time for the series. This can either be a scalar if
        the time series is evenly sampled, or an array of values if it is not.
    n_samples : int
        The number of time samples for the series. This is only used if both
        ``time_start`` and ``time_delta`` are provided and are scalar values.
    **kwargs : dict, optional
        Additional keyword arguments are passed to `~astropy.table.QTable`.
    timeN)r   
time_start
time_delta	n_samplesc         	      s&  t  jf d|i| |d kr@|d kr@|d kr@|d kr@d| _d S |d k	rn|d k	rf|t| krntdnt| }d| jkr|d kr| jd }ntd|d kr|d krtdn|d k	r|d k	rtd|d k	rt|tt	fst|}|d k	rt|tt	fst|}|d k	r(t|t
t	fs(tdt|t	r@|jtj }|d k	r|d kr\td	|jrpt||}t|}t|d
}dtj |d< || }nJt| jdkrt|t| krtdt||n|d k	rtd|  , d| jkr| d | j|ddd W d Q R X d S )NdataTzP'n_samples' has been given both and it is not the same length as the input data.r   zA'time' has been given both in the table and as a keyword argumentz1Either 'time' or 'time_start' should be specifiedz+Cannot specify both 'time' and 'time_start'z0'time_delta' should be a Quantity or a TimeDeltaz-'time' is scalar, so 'time_delta' is required   g        r   z3Length of 'time' ({}) should match data length ({})z='time_delta' should not be specified since 'time' is an array)indexname)super__init__Z_required_columns_relaxlen	TypeErrorcolnamescolumns
isinstancer   r   r	   secusZisscalarnprepeatZcumsumZroll
ValueErrorformat_delay_required_column_checksremove_column
add_column)selfr   r   r   r   r   kwargs)	__class__ g/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/astropy/timeseries/sampled.pyr   =   sV     






"


zTimeSeries.__init__c             C   s   | d S )z"
        The time values.
        r   r+   )r(   r+   r+   r,   r      s    zTimeSeries.timeZmidpoint_epoch
epoch_timez4.0r   Fc          	   C   s  t |tr|jjdkrtd|  }|dkr:| jd }nt|}|t	j
}|rzt |trp|jjdkrptd|| }n8|dkrd}n*t |tr|jjdkrtd|t	j
}|dkr|d	 }n|rt |tr|jt	jstd
n$|dk s|dkrtdn|| }nNt |trX|jjdkrX|dk s@||krJtdn|t	j
}ntd| j| j| ||  | ||  }	t|	t	j
 }
|r|
|  }
d }}|   |d |j|
ddd W dQ R X |S )a  
        Return a new `~astropy.timeseries.TimeSeries` folded with a period and
        epoch.

        Parameters
        ----------
        period : `~astropy.units.Quantity` ['time']
            The period to use for folding
        epoch_time : `~astropy.time.Time`
            The time to use as the reference epoch, at which the relative time
            offset / phase will be ``epoch_phase``. Defaults to the first time
            in the time series.
        epoch_phase : float or `~astropy.units.Quantity` ['dimensionless', 'time']
            Phase of ``epoch_time``. If ``normalize_phase`` is `True`, this
            should be a dimensionless value, while if ``normalize_phase`` is
            ``False``, this should be a `~astropy.units.Quantity` with time
            units. Defaults to 0.
        wrap_phase : float or `~astropy.units.Quantity` ['dimensionless', 'time']
            The value of the phase above which values are wrapped back by one
            period. If ``normalize_phase`` is `True`, this should be a
            dimensionless value, while if ``normalize_phase`` is ``False``,
            this should be a `~astropy.units.Quantity` with time units.
            Defaults to half the period, so that the resulting time series goes
            from ``-period / 2`` to ``period / 2`` (if ``normalize_phase`` is
            `False`) or -0.5 to 0.5 (if ``normalize_phase`` is `True`).
        normalize_phase : bool
            If `False` phase is returned as `~astropy.time.TimeDelta`,
            otherwise as a dimensionless `~astropy.units.Quantity`.

        Returns
        -------
        folded_timeseries : `~astropy.timeseries.TimeSeries`
            The folded time series object with phase as the ``time`` column.
        r   z,period should be a Quantity in units of timeNr   ZdimensionlesszSepoch_phase should be a dimensionless Quantity or a float when normalize_phase=Trueg        zLepoch_phase should be a Quantity in units of time when normalize_phase=False   z<wrap_phase should be dimensionless when normalize_phase=Truer   z$wrap_phase should be between 0 and 1z-wrap_phase should be between 0 and the periodzKwrap_phase should be a Quantity in units of time when normalize_phase=False)r   r   )r   r	   unitZphysical_typer
   copyr   r   Zto_valuer   r    Zis_equivalentoner#   r   r   Z	decomposer%   r&   r'   )r(   Zperiodr-   Zepoch_phaseZ
wrap_phaseZnormalize_phasefoldedZ
period_secZepoch_phase_secZrelative_time_secZfolded_timer+   r+   r,   fold   sN    &








zTimeSeries.foldc                s`     |rTd|krTt fdd|D t j jd}tj| jj jjd|_	|S t
 |S )Nr   c                s   g | ]} | qS r+   r+   ).0x)r(   r+   r,   
<listcomp>   s    z*TimeSeries.__getitem__.<locals>.<listcomp>)metaZcopy_indices)indiceskeys)Z_is_list_or_tuple_of_strr   r   r7   Z_copy_indicesr   ZTableGroupsZ_indicesZ_keysZ_groupsr   __getitem__)r(   itemout)r*   )r(   r,   r:      s    

zTimeSeries.__getitem__c                s4   t  j||}t| jdkr0d| jkr0| d |S )z>
        See :meth:`~astropy.table.Table.add_column`.
        r   r   )r   r'   r   r8   r   	add_index)r(   argsr)   result)r*   r+   r,   r'     s    
zTimeSeries.add_columnc                s4   t  j||}t| jdkr0d| jkr0| d |S )z?
        See :meth:`~astropy.table.Table.add_columns`.
        r   r   )r   add_columnsr   r8   r   r=   )r(   r>   r)   r?   )r*   r+   r,   r@     s    
zTimeSeries.add_columnsutcc             C   sZ   ddl m}m} t||s"tdt|j|s6tdt|j|d}t|}t	||dS )aj  
        Convert a :class:`~pandas.DataFrame` to a
        :class:`astropy.timeseries.TimeSeries`.

        Parameters
        ----------
        df : :class:`pandas.DataFrame`
            A pandas :class:`pandas.DataFrame` instance.
        time_scale : str
            The time scale to pass into `astropy.time.Time`.
            Defaults to ``UTC``.

        r   )	DataFrameDatetimeIndexz"Input should be a pandas DataFramez'DataFrame does not have a DatetimeIndex)scale)r   r   )
ZpandasrB   rC   r   r   r   r   r   from_pandasr   )r(   df
time_scalerB   rC   r   tabler+   r+   r,   rE     s    

zTimeSeries.from_pandasc             C   s   t | jddS )a"  
        Convert this :class:`~astropy.timeseries.TimeSeries` to a
        :class:`~pandas.DataFrame` with a :class:`~pandas.DatetimeIndex` index.

        Returns
        -------
        dataframe : :class:`pandas.DataFrame`
            A pandas :class:`pandas.DataFrame` instance
        r   )r   )r   	to_pandas)r(   r+   r+   r,   rI   2  s    
zTimeSeries.to_pandasc       
         s   yt  j|f|d|i|S  tk
r   |dkr<tdtj|f|d|i|}||jkrt|j| ||d}	|| ntd| dt	|	|dS X dS )a  
        Read and parse a file and returns a `astropy.timeseries.TimeSeries`.

        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.TimeSeries` 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 import TimeSeries
            >>> ts = TimeSeries.read('sampled.dat', format='ascii.ecsv',
            ...                      time_column='date')  # doctest: +SKIP

        Parameters
        ----------
        filename : str
            File to parse.
        format : str
            File format specifier.
        time_column : str, optional
            The name of the time column.
        time_format : str, optional
            The time format for the time column.
        time_scale : str, optional
            The time scale for the time column.
        *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.sampled.TimeSeries`
            TimeSeries corresponding to file contents.

        Notes
        -----
        r$   NzR``time_column`` should be provided since the default Table readers are being used.)rD   r$   zTime column 'z' not found in the input data.)r   r   )
r   readr   r#   r   r   r   r   r&   r   )
r(   filenameZtime_columntime_formatrG   r$   r>   r)   rH   r   )r*   r+   r,   rJ   >  s    -
zTimeSeries.read)N)NNr   NF)rA   )NNNN)__name__
__module____qualname____doc__Z_required_columnsr   propertyr   r   r3   r:   r'   r@   classmethodrE   rI   rJ   __classcell__r+   r+   )r*   r,   r      s    (K
 e

)r0   r   numpyr!   Zastropy.tabler   r   r   Zastropy.timer   r   Zastropyr   r   Zastropy.unitsr	   r
   Zastropy.utils.decoratorsr   Zastropy.timeseries.corer   r   __all__r   r+   r+   r+   r,   <module>   s   