B
    d                  @   sf   d dl 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gZdd Zddddd	d
dZdS )    N)units)AstropyUserWarning)
TimeSeries)BinnedTimeSeriesaggregate_downsamplec          	   C   s   t |drt|| |S g }xjtt|d D ]V}||d  || d krf||| ||   q2||| || ||d    q2W ||| |d d  t|S dS )z
    Manual reduceat functionality for cases where Numpy functions don't have a reduceat.
    It will check if the input function has a reduceat and call that if it does.
    reduceat   N)hasattrnparrayr   rangelenappend)r   indicesfunctionresulti r   j/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/astropy/timeseries/downsample.pyr      s    
&r   )time_bin_sizetime_bin_startn_binsaggregate_funcc         	   C   s  t | tstdt |tjs&td|tj}| jdd }|dkrR|jd }|j| j	}|dkr|t
t|d | }|dkrtj}ttdt||g}||tj  }	||d k||d k @ }
||
 }t|||
 d }d|||
 |d k< t|	dd |	d d}tdtt|d d g}t|}x|jD ]}|dkrZqH|| }t |tjtjfstd	t qHt |tjrtjttj||jd
}tjt|j|||jdd||< n2tjj ||j!d}d|_"t|||||< d|j"|< |||< qHW |S )a  
    Downsample a time series by binning values into bins with a fixed size,
    using a single function to combine the values in the bin.

    Parameters
    ----------
    time_series : :class:`~astropy.timeseries.TimeSeries`
        The time series to downsample.
    time_bin_size : `~astropy.units.Quantity` ['time']
        The time interval for the binned time series.
    time_bin_start : `~astropy.time.Time`, optional
        The start time for the binned time series. Defaults to the first
        time in the sampled time series.
    n_bins : int, optional
        The number of bins to use. Defaults to the number needed to fit all
        the original points.
    aggregate_func : callable, optional
        The function to use for combining points in the same bin. Defaults
        to np.nanmean.

    Returns
    -------
    binned_time_series : :class:`~astropy.timeseries.BinnedTimeSeries`
        The downsampled time series.
    z"time_series should be a TimeSeriesz/time_bin_size should be a astropy.unit quantityNr   r	   r   )r   Ztime_bin_endtimez.Skipping column {0} since it has a mix-in type)unitF)copy)dtype)#
isinstancer   	TypeErroruZQuantityZto_valuesZilocr   secintr   ceilZnanmeanZcumsumZhstackrepeatZsearchsortedr   ZnonzerodiffuniqueZcolnamesZndarraywarningswarnr   nanr   r   valuemaZzerosr   mask)Ztime_seriesr   r   r   r   Zbin_size_secsortedZrelative_time_secZrelative_bins_secZbinsZkeepZsubsetr   ZbinnedgroupsZunique_indicesZcolnamevaluesdatar   r   r   r   !   sP    


"


)r(   numpyr   Zastropyr   r    Zastropy.utils.exceptionsr   Zastropy.timeseries.sampledr   Zastropy.timeseries.binnedr   __all__r   r   r   r   r   r   <module>   s   