Thank you for sending the files. I've had a look and have come up with a solution, I think.
The reason your data and log don't sync very well is that the laser seems to ablate much longer than data are actually recorded. A screenshot illlustrating this is below:
I'm not sure if that is on purpose or not? It seems a bit strange to me...
Anyways, this creates two problems that the local sync script does not anticipate:
1) There is a 'not a number' (nan) in the data used for local sync because this value is inserted between data files in the time series. This can easily be fixed by replacing the nan by a number, e.g.
da = np.nan_to_num(da, nan=np.nanmean(tb.dataForSelection(sel)))
Adding this one line would cause the sync to actually do something rather than return no offset for all samples, but there is another problem...
2) Because the laser log square wave and the total beam signal are not the same duration, the sync offset will depend entirely on the background surrounding your data. If the noise is higher before ablation begins, it'll 'sync' too early. One way we can solve this problem is by adjusting the signal so it is higher at the end, e.g.
da *= np.linspace(1, 2, len(da))
With these two changes, I see much improved syncronization. There are a still a couple cases where large spikes just before ablation began cause it to sync to that, but for the most part it looks very good.
Here is the complete modified code:
import numpy as np
tb = data.timeSeries('TotalBeam')
sg = data.createSelectionGroup('LocalAdjustTemp', data.Sample)
ts = np.median(np.diff(tb.time()))
n = int(round(10/ts)) # 10 = number of seconds to search around log sample
for i, lls in enumerate(data.laserLogSamples()):
sel = data.createSelection(sg, lls.startTime(), lls.endTime(), 'Temp')
si = tb.selectionIndices(sel)
try:
da = tb.data()[si[0]-n:si[-1]+n] # Take n points before or after sample
da = np.nan_to_num(da, nan=np.nanmean(tb.dataForSelection(sel)))
da *= np.linspace(1, 2, len(da))
lla = np.pad(np.ones(si[-1] - si[0]), (n, n), 'constant', constant_values=(0, 0))
offset = data.timeOffset(np.arange(len(da)), da, np.arange(len(lla)), lla)
except Exception as e:
print('problem for %i, %s'%(i, e))
continue
print('%i = %i, %f s, len(da) = %i, len(lla) = %i'%(i, offset, offset*ts, len(da), len(lla)))
lls.setStartTime(lls.startTime().addMSecs(offset*ts*1000))
lls.setEndTime(lls.endTime().addMSecs(offset*ts*1000))
data.removeSelectionGroup('LocalAdjustTemp')
All the best,