function [bad,N] = scan_motion_CAPRICORN_2016(dfl,ddd,hhh)
%{
scans motion file for gaps and bad timestamps
%}

% read file
disp(['Reading mot file for hour ',int2str(hhh)]);
flist = fopen(dfl,'r');
temp = {};    % cell array will hold one line of data, as read from file
z = [];       % destination matrix for data read from file
bad = [];
while ~feof(flist)
    try
        % temp will be 1x9 cell array, cells will be ~36000x1 arrays
        temp = textscan(flist,'%2f%2f%3f %f %f %f %f %f %f %*[^\n]','delimiter', ', ','headerlines', 1,'emptyvalue',NaN);
        z = [z cell2mat(temp)'];  % 9x~36000 array
    catch
        for ii=1:9      % length of last cell reflects missing values from any/all fields
            if length(temp{1,ii})~=length(temp{1,9})
                temp{1,ii}(length(temp{1,9})+1) = [];  %truncate length of all cells to length(temp{1,9})
            end;
        end;
        if ~isempty(temp{1,1});
            z = [z cell2mat(temp)'];  % dim 2 will be < 36000 here
        end;
    end;
end;
fclose(flist);
tmotion =  ddd + (hhh + (z(1,:)+(z(2,:)+z(3,:)/1000)/60)/60)/24;
N = length(tmotion);
% compute delta T and scan for jumps > 0.25 seconds
dt = diff((tmotion-floor(tmotion(1)))*86400);
ii = find(dt>0.25);
jj = length(find(dt<0.05)); % # points from rapid buffer dumps
if ~isempty(ii)
    hours = floor((tmotion(ii)-floor(tmotion(1)))*24);
    bad = [tmotion(ii)',hours',dt(ii)',ones(length(ii),1)*length(tmotion),ones(length(ii),1)*jj];
end;
