Datetime index localisation in matrix with dsearch?

Hello all,

when using datetime objects from experiment and simulation data outputs, I’m facing frequent situations where i need to localize an arbitrary sample time within another table of discrete datetimes and I found workarounds quite laborious until now. I realize this situation corresponds to an extension dsearch : do you feel that it is realistic to extend dsearch to handle datetime objects ?
Thanks,

David

Hi David,
I’m not sure to understand exactly your problem, but this could be help you.
I usually use the min() function to search and locate a datetime element.

[v,k]=min(abs(#reference# - ts.vars(1).data.date))

if the reference value exists in your time series then, the min value is 0 and you get the location in ‘k’. If the reference doesn’t exists, you get the closer value that exists

the function below works for a date but date + time could be searched with :

 ts.vars(1).data.date+ts.vars(1).data.time/24/60/60

Regards,

Hello David,

I think I already gave an easy/fast method to do this in a previous post. If you have a given monotone time vector, e.g.

rand("seed",7);
t=cumsum(rand(10,1))
 t = [10x1 double]

   0.9602185
   1.4797080
   2.1291572
   3.0296724
   3.7623799
   3.8891781
   4.3160498
   4.9970620
   5.3215305
   5.5705334

then it is quite easy to obtain the position of an arbitrary value, say 4.0, using linear interpolation, i.e.

u=interpln([t';1:length(t)],4.0)

 u = 

   7.1514646

which says that 4.0 is between t(7) and t(8), and moreover that 4.0 is equal to

t(7)+(u-7)*(t(8)-t(7))

 ans = 

   4.

Hi Laurent,
thank you for your reply and proposal: the syntax to get the result is becoming a bit heavy to access the double value date and time in the object (and we lose the sign when calculating the difference between two datatimes, equivalent to positive duration ), thus I thought it could be integrated in some existing functions working with index in tables, matrix… instead of writing my own one just for datetime.

David

1 Like

Can you illustrate the feature that you need ?

Hi Stephane,

indeed your proposal around linear interpolation (instead of looking at dsearch) is yielding the solution using vector of real numbers that I can calculate indeed from a vector of datetime objects: however the linear interpolation function doesn’t accept datetime object, only real numbers, then I need to regenerate vector of reals (retrieving the date and time value of datetime object then converting in decimal number of days like datenum values) while I thought it could be useful not only for me to work directly with datetime input argument.

--> tpscenmanip

 tpscenmanip = [5x1 datetime]

   2024-08-02 08:10:33
   2024-08-07 08:10:33
   2024-08-09 15:53:10
   2024-08-19 11:59:08
   2024-09-06 15:14:51

--> dtm= datetime([2024 8 26 8 39 0])

 dtm = [datetime]

   2024-08-26 08:39:00

--> u= interpln(tpscenmanip', 1:length(tpscenmanip), dtm )
à la ligne     2 de la fonction interpln ( C:\Users\dc227480\AppData\Local\scilab-2024.1.0\modules\interpolation\macros\interpln.sci ligne 15 )

Nombre d'arguments d'entrée erroné.

Currently I see that gsort is handling directly datetime objects, so this is the way I go until now.

David

Can you try the following function:

function [i_bin, counts, outside] = %datetime_dsearch(DT, bins, varargin)
    num_DT = datenum(datevec(DT.date+DT.time/3600));
    num_bins = datenum(datevec(bins.date+bins.time/3600));
    [i_bin, counts, outside] = dsearch(num_DT, num_bins, varargin(:))
endfunction

Playing with it seems to do what you want:

dt = datetime("2022-09-01 08:10"):duration(0,1,0):datetime("2022-09-01 9:10");
dsearch(datetime("2022-09-01 08:33:30"),dt)
 ans = 

   24.
dt(24:25)
 ans = [1x2 datetime]

   2022-09-01 08:33:00   2022-09-01 08:34:00

Hello Stephane,
thank you for overloading dsearch, indeed it performs what I want.
I also tested the interpolation approach, adapted from your previous post, which performs well :

function yp= interp0(x,y,xp)
    //Stephane Mottelet  décembre 2023 - 0-D interpolation, piecewise constant function definition - https://scilab.discourse.group/t/0-d-interpolation-piecewise-constant-function-definition/308

    //    i = floor(interp1(x,1:length(x),xp,"linear","edgevalue"))
    i = floor(linear_interpn(xp,x,1:length(x),"C0"))   //a priori plus rapide qu'interp1

    yp = y(i)    
endfunction
//------------------------------------------------------------------------------

function ip= interp0dtm(dtm,dtmp)
    //trouver les index des intervalles bornes dtm ([..[) contenant dtmp

    dtn= dtm.date+dtm.time/3600/24
    dtnp= dtmp.date+dtmp.time/3600/24

    ip= interp0(dtn,1:length(dtn),dtnp)

endfunction
//-----------------------------------------------------------------------------

Thanks,
David

1 Like