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,
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.
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 :
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.
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.
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
//-----------------------------------------------------------------------------