Beispiel #1
0
def time_win_basic(start,end,Tlisin,Datalisin,outposix=True,invert=False,
                   out_array=False , out_boolis = False , only_boolis = False):
    """
    In Intern, we works in POSIX

    only_boolis : To gain speed, no operation on Tlis & Datalisin is be done
                  None is outputed for Tlisout , Datalisout

    Outputs :
        If out_boolis == True:
            Tlisout , Datalisout , boolis
        If out_boolis == False:
            Tlisout , Datalisout
    """

    if isinstance(Tlisin[0],dt.datetime):
        Tlis  = conv.dt2posix(Tlisin)
    else:
        Tlis = Tlisin


    if isinstance(start,dt.datetime):
        start = conv.dt2posix(start)
    if isinstance(end,dt.datetime):
        end   = conv.dt2posix(end)



    if not isinstance(Tlis,np.ndarray) or not isinstance(Datalisin,np.ndarray):
        Tlis    = np.array(Tlis)
        Datalis = np.array(Datalisin)
    else:
        Tlis    = Tlis
        Datalis = Datalisin

    boolis = (start <= Tlis) * (Tlis <= end)

    if invert:
        boolis = np.logical_not(boolis)

    if only_boolis:
        Datalisout = None
        Tlisout    = None
    else:
        Datalisout = Datalis[boolis]
        Tlisout    = Tlis[boolis]

        if not outposix:
            Tlisout = conv.posix2dt(Tlisout)

        if out_array:
            Tlisout , Datalisout = np.array(Tlisout) , np.array(Datalisout)


    if out_boolis:
        out_tuple = (Tlisout , Datalisout , boolis)
    else:
        out_tuple = (Tlisout , Datalisout)

    return out_tuple
Beispiel #2
0
    def find_point(self,tin,tol=0.001,stop_when_found=True):

        find = False

        if type(tin) is dt.datetime:
            tin = conv.dt2posix(tin)

        tmin = self.pts[0].T
        tmax  = self.pts[-1].T

        if not ( tmin <= tin <= tmax ):
            print("WARN : find_point : t !€ [tmin , tmax]")

        if stop_when_found:
            for i,p in enumerate(self.pts):
                if np.abs(p.T - tin) < tol:
                    find = True
                    break
            if find:
                return p,i
            else:
                return Point(),np.nan

        else:
            pts_stk = []
            i_stk = []
            for i,p in enumerate(self.pts):
                if np.abs(p.T - tin) < tol:
                    pts_stk.append(p)
                    i_stk.append(i)
            return pts_stk  , i_stk
Beispiel #3
0
    def from_uniq_point(self, Point, startdate, enddate, pas=1):
        self.del_data()

        if type(startdate) == dt.datetime:
            startdate = conv.dt2posix(startdate)
        if type(enddate) == dt.datetime:
            enddate = conv.dt2posix(enddate)

        N = int(np.round(enddate - startdate / pas))
        #datelist = np.arange(startdate,enddate,pas)

        Point.Tset(startdate)

        for i in range(N):
            Point.Tset(Point.T + pas)
            self.add_point(copy.copy(Point))

        self.i_nomi = pas
Beispiel #4
0
    def Tset(self, T=0):
        if T == 0:
            T = dt.datetime.now()

        if type(T) == dt.datetime:
            self.Tdt = T
            self.T = conv.dt2posix(T)

        else:
            self.T = float(T)
            self.Tdt = conv.posix2dt(float(T))
Beispiel #5
0
    def find_point(self, tin, tol=0.001, stop_when_found=True):
        """
        Method to find a specific point according to its timestamp

        Parameters
        ----------
        tin : float or datetime
            timestamp of the researched point.
        tol : float, optional
            tolerence of the research. The default is 0.001.
        stop_when_found : bool, optional
            Stop the research when a point is found. The default is True.

        Returns
        -------
        Point Object
            Point Found.
        int or list of int
            index of the point.

        """

        find = False

        if type(tin) is dt.datetime:
            tin = conv.dt2posix(tin)

        tmin = self.pts[0].T
        tmax = self.pts[-1].T

        if not (tmin <= tin <= tmax):
            print("WARN : find_point : t !€ [tmin , tmax]")

        if stop_when_found:
            for i, p in enumerate(self.pts):
                if np.abs(p.T - tin) < tol:
                    find = True
                    break
            if find:
                return p, i
            else:
                return Point(), np.nan

        else:
            pts_stk = []
            i_stk = []
            for i, p in enumerate(self.pts):
                if np.abs(p.T - tin) < tol:
                    pts_stk.append(p)
                    i_stk.append(i)
            return pts_stk, i_stk
Beispiel #6
0
def lagrange_interpolate(Tdata,Ydata,Titrp,n=10):
    """
    Perform a temporal lagrangian interpolation
    the X-component is a time

    Parameters
    ----------
    Tdata : iterable of datetime
        X/T component of the known points.
    Ydata : iterable of floats
        Y component of the known points..
    Titrp : iterable of datetime
        Epochs of the wished points.
    n : int, optional
        degree of the polynom. Better if even. The default is 10.

    Returns
    -------
    Yintrp : float array
        output interpolated data.

    Tips
    ----
    Use conv.dt_range to generate the wished epochs range

    """

    Tdata = np.array(Tdata)
    Ydata = np.array(Ydata)
    Titrp = np.array(Titrp)

    nn = int(n/2)

    Tdata_px = conv.dt2posix(np.array(Tdata))
    Titrp_px = conv.dt2posix(np.array(Titrp))

    tref = Tdata_px[0]

    ### we substract a ref time to avoid numerical instability
    Tdata_px = Tdata_px - tref
    Titrp_px = Titrp_px - tref

    sur_val = (np.nan,np.nan)
    sur_idx = (np.nan,np.nan)

    ### some checks
    if np.any(np.diff(Tdata_px) == 0):
        print("WARN: lagrange_interpolate: some Tdata are equals")

    if np.any(np.diff(Ydata) == 0):
        print("WARN: lagrange_interpolate: some Ydata are equals")

    if np.any(Titrp_px < 0):
        print("WARN: lagrange_interpolate: some wanted values are outside the data interval!!!!")

    Yintrp = []

    for tintrp in Titrp_px:

        if ( sur_val[0]  <= tintrp ) & ( tintrp <= sur_val[1] ):
            ### the Polynom is alread determined
            pass
        else:
            sur_val , sur_idx = utils.find_surrounding(Tdata_px, tintrp)

            if (sur_idx[0] - nn < 0):  # manage side effect for first points
                imin = 0
                imax = n+1
            elif (sur_idx[1] + nn > len(Ydata)): # manage side effect for last points
                imin = len(Ydata) - n-1
                imax = len(Ydata)
            else: # regular case
            ### if (sur_idx[0] - nn >= 0) and (sur_idx[1] + nn >= len(Ydata)):
                imin = sur_idx[0] - nn
                imax = sur_idx[1] + nn


            Tuse = Tdata_px[imin:imax]
            Yuse = Ydata[imin:imax]

            Poly = lagrange1(list(zip(Tuse,Yuse)))
            #Poly = lagrange2(Tuse,Yuse)


        yintrp = Poly(tintrp)
        Yintrp.append(yintrp)

    return np.array(Yintrp)