U
    d»{j2+  ã                   @   sŒ   d dl Z d dlZd dlmZ d dlmZmZ d dl	m
Z
 d dlmZ d dlmZ dgZdd	„ Zeeejd
œdd„Zddddddœdd„ZdS )é    N)Úunits)ÚTimeÚ	TimeDelta)ÚBinnedTimeSeries)Ú
TimeSeries)ÚAstropyUserWarningÚaggregate_downsamplec              	   C   sÆ   t |ƒdkrt g ¡S t|dƒr2t | | |¡¡S g }tt |ƒd ƒD ]V}||d  || d krz| || ||  ƒ¡ qF| || || ||d  … ƒ¡ qF| || |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.
    r   Úreduceaté   éÿÿÿÿN)ÚlenÚnpÚarrayÚhasattrr	   ÚrangeÚappend)r   ÚindicesÚfunctionÚresultÚi© r   ú\/var/www/html/bwave/python/venv/lib/python3.8/site-packages/astropy/timeseries/downsample.pyr	      s    

$r	   )ÚtimeÚrel_baseÚreturnc                 C   s   | | j ddd�S )NÚsecÚlong)ÚformatZsubfmt)Úto_value)r   r   r   r   r   Ú_to_relative_longdouble$   s    	r   )Útime_bin_sizeÚtime_bin_startÚtime_bin_endÚn_binsÚaggregate_funcc             
   C   s   t | tƒstdƒ‚|dk	r2t |tjtfƒs2tdƒ‚|dk	rPt |ttfƒsPt|ƒ}|dk	rnt |ttfƒsnt|ƒ}| jdd… }|dkrŽ|jd }|j	r¤|jd | j
}|dkrò|dkrò|j	rÜ|dkrÌtdƒ‚qò|| tj }nt |jd |d ¡}|j	�rt|dk	�r6|j	�rt|dk�rt| tj¡}tt || ¡ƒ}n>|dk	�rt|j	�st|}	|jdd�}|	|d< |dd… |d	d…< |dk	�rº|j	�sº|j	�sºt |d	d… |dd… k ¡�rºt d
t¡ t||||d�}
|dk�rÚtj}|
j}|
j}|dk�sø|j	�s t|ƒ}|jd }t||ƒ}t||ƒ}t|j|ƒ}||d k||d k@ }t |d	d… |dd… k¡d }|D ]4}t t ||| k|||d	  k ¡¡}d||< �qh|| }t ||¡}t|ƒ�rt |d	d… |dd… k¡�rt ||||d k ¡}t  t|ƒ¡||< t|ƒ�r4t !dt "t #|¡¡d d	 g¡}n
t $g ¡}t %|¡}|| }|j&D ]Ä}|dk�rh�qV|| }t |tj'tjfƒ�s”t dt¡ �qVt |tjƒ�rÞtjt (tj)|¡|j*d�}tjt+|j,||ƒ|j*dd�||< n2tj-j.||j/d�}d	|_0t+|||ƒ||< d|j0|< ||
|< �qV|
S )aZ
  
    Downsample a time series by binning values into bins with a fixed size or
    custom sizes, 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` or `~astropy.time.TimeDelta` ['time'], optional
        The time interval for the binned time series - this is either 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.
    time_bin_start : `~astropy.time.Time` or iterable, optional
        The start time for the binned time series - this can be either given
        directly as a `~astropy.time.Time` array or as any iterable that
        initializes the `~astropy.time.Time` class. This can also be a scalar
        value if ``time_bin_size`` or ``time_bin_end`` is provided.
        Defaults to the first time in the sampled time series.
    time_bin_end : `~astropy.time.Time` or iterable, optional
        The times of the end 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. This can only be given if ``time_bin_start``
        is provided or its default is used. If ``time_bin_end`` is scalar and
        ``time_bin_start`` is an array, time bins are assumed to be contiguous;
        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 and
        ``time_bin_start`` is scalar, bins will be contiguous. If both ``time_bin_end``
        and ``time_bin_start`` are arrays, bins do not need to be contiguous.
        If this argument is provided, ``time_bin_size`` should not be provided.
    n_bins : int, optional
        The number of bins to use. Defaults to the number needed to fit all
        the original points. If both ``time_bin_start`` and ``time_bin_size``
        are provided and are scalar values, this determines the total bins
        within that interval. If ``time_bin_start`` is an iterable, this
        parameter will be ignored.
    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 TimeSeriesNz3'time_bin_size' should be a Quantity or a TimeDeltar   r   z_With single 'time_bin_start' either 'n_bins', 'time_bin_size' or time_bin_end' must be providedT)Úcopyr
   zaOverlapping bins should be avoided since they can lead to double-counting of data during binning.)r    r!   r"   r#   Fr   z.Skipping column {0} since it has a mix-in type)Úunit)Údtype)1Ú
isinstancer   Ú	TypeErrorÚuZQuantityr   r   Zilocr   Zisscalarr   Úsr   Úmaximumr   ÚintÚceilZ	replicateÚanyÚwarningsÚwarnr   r   Znanmeanr!   r"   r   r   ÚwhereÚlogical_andZsearchsortedÚallZarangeZhstackZnonzeroÚdiffr   ÚuniqueZcolnamesZndarrayÚrepeatÚnanr&   r	   ÚvalueÚmaZzerosr'   Úmask)Ztime_seriesr    r!   r"   r#   r$   Z	ts_sortedZtime_durationZbin_size_secZscalar_start_timeZbinnedZ	bin_startZbin_endr   Zrel_bin_startZrel_bin_endZrel_ts_sorted_timeZkeepZnoncontiguous_bins_indicesÚindZdelete_indicesZrel_subset_timer   Zindices_startÚgroupsZunique_indicesZsubsetZcolnameÚvaluesÚdatar   r   r   r   0   sÞ    8

 ÿ
ÿ



ÿþýýü





ÿ
"
þÿ* ÿ
$



 ÿ  ÿ
)r0   Únumpyr   Zastropyr   r*   Zastropy.timer   r   Zastropy.timeseries.binnedr   Zastropy.timeseries.sampledr   Zastropy.utils.exceptionsr   Ú__all__r	   Z
longdoubler   r   r   r   r   r   Ú<module>   s   ù