
    IR-e1+                         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 Zd	ed
edej        fdZdddddddZdS )    N)units)Time	TimeDelta)BinnedTimeSeries)
TimeSeries)AstropyUserWarningaggregate_downsamplec           
      x   t          |          dk    rt          j        g           S t          |d          r(t          j        |                    | |                    S g }t          t          |          dz
            D ]z}||dz            ||         dz   k    r+|                     || ||                                       E|                     || ||         ||dz                                          {|                     || |d         d                              t          j        |          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is        =lib/python3.11/site-packages/astropy/timeseries/downsample.pyr   r      s/   
 7||qx||	:	&	& 
 x))%99:::s7||a'(( 	L 	LAq1u~a//hhuWQZ'899::::hhuWQZ'!a%.-H'IJJKKKKhhuWR[]]344555x    timerel_basereturnc                 6    | |z
                       dd          S )Nseclong)formatsubfmt)to_value)r   r   s     r   _to_relative_longdoubler#   $   s!     8O%%U6%BBBr   )time_bin_sizetime_bin_starttime_bin_endn_binsaggregate_funcc          
         t          | t                    st          d          |0t          |t          j        t
          f          st          d          |+t          |t          t
          f          st          |          }|+t          |t          t
          f          st          |          }| j        dd         }||j        d         }|j	        r|j        d         |z
  j
        }|S|Q|j	        r$|t          d          ||z  t          j        z  }n&t          j        |j        d         |d                   }|j	        r|M|j	        rE|C|                    t          j                  }t          t          j        ||z                      }n5|3|j	        s,|}	|                    d          }|	|d<   |dd         |d	d<   |P|j	        sI|j	        sBt          j        |d	d         |dd         k               rt'          j        d
t*                     t-          ||||          }
|t          j        }|
j        }|
j        }||j	        st5          |          }|j        d         }t7          ||          }t7          ||          }t7          |j        |          }||d         k    ||d         k    z  }t          j        |d	d         |dd         k              d         }|D ]E}t          j        t          j        |||         k    |||d	z            k                         }d||<   F||         }t          j        ||          }t5          |          rqt          j        |d	d         |dd         k              rIt          j        ||||d         k                       }t          j         t5          |                    ||<   t5          |          rDt          j!        dt          j"        t          j#        |                    d         d	z   g          }nt          j$        g           }t          j%        |          }||         }|j&        D ])}|dk    r
||         }t          |t          j'        t          j        f          st'          j        dt*                     St          |t          j                  rlt          j        t          j(        t          j)        |          |j*                  }t          j        tW          |j,        ||          |j*        d          ||<   nKt          j-        .                    ||j/                  }d	|_0        tW          |||          ||<   d|j0        |<   ||
|<   +|
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uQuantityr   r   ilocr   isscalarr   sr   maximumr"   intceil	replicateanywarningswarnr   r   nanmeanr%   r&   r   r#   wherelogical_andsearchsortedallarangehstacknonzerodiffr   uniquecolnamesndarrayrepeatnanr+   r   valuemazerosr,   mask)time_seriesr$   r%   r&   r'   r(   	ts_sortedtime_durationbin_size_secscalar_start_timebinned	bin_startbin_endr   rel_bin_startrel_bin_endrel_ts_sorted_timekeepnoncontiguous_bins_indicesinddelete_indicesrel_subset_timer   indices_startgroupsunique_indicessubsetcolnamevaluesdatas                                 r   r	   r	   0   s   n k:.. ><=== 
I.* *  MNNN!*^dIEV*W*W!n--
<$	AR(S(SL))  #I "*  B"+n<A!5" 
	N~H   !. 6 <:inR&8.:LMML 7$% H>#0#9#9!##>#>L )E!F!FGGF%( 7$2!!-!7!7T!7!B!B$5q!%1#2#%6qrr" %		"+		 ~abb)L",==>>		
 MF"   #%!	  F  %I!G ~^4~Y ~a H+Ix@@M)'8<<K0JJ-"22k"o-D "$-*;k#2#>N*N!O!OPQ!R) % %N"[%55"]37%;; 
 
  %^(.O ok?;;G 7|| ?}QRR0K4DDEE ?]=<Nr<R+RS
 
 "$3}+=+=!>!> 7|| Arz"''*:*:;;A>BCDD" Yw''N t_F?  f &2:qz":;; 	M@BT   faj)) 		*:bi77fkJJJD#$:v~>>RW$ $ $D   5;;vV\;::DDI#+FFN#K#KD ()DIn%wMr   )r9   numpyr   astropyr   r/   astropy.timer   r   astropy.timeseries.binnedr   astropy.timeseries.sampledr   astropy.utils.exceptionsr   __all__r   
longdoubler#   r	    r   r   <module>rm      s               ( ( ( ( ( ( ( ( 6 6 6 6 6 6 1 1 1 1 1 1 7 7 7 7 7 7!
"     (	C$ 	C$ 	C2= 	C 	C 	C 	C ] ] ] ] ] ] ]r   