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

% read file
disp(['Reading son file for hour ',int2str(hhh)]);
flist = fopen(dfl,'r');
st1 = [];               % data array
temp = {};              % cell array for textscan output
bad=[];
while feof(flist)==0,   % read entire file into cell array
    try
        % temp will be 1x8 cell array, cells will be ~36000x1 arrays
        temp = textscan(flist,'%2f%2f%3f %*2c %f %f %f %*c %f %f %*2c %*[^\n]','delimiter',', ','headerlines',1,'emptyvalue',NaN,'treatAsEmpty','Sonic');
        st1 = [st1 cell2mat(temp)'];  % 8x~36000 array
    catch
        for ii=1:8  % length of last cell reflects missing values from any/all fields
            if length(temp{1,ii})~=length(temp{1,8})
                temp{1,ii}(length(temp{1,8})+1) = [];  %truncate length of all cells to length(temp{1,8})
            end;
        end;
        if ~isempty(temp{1,1});
            st1 = [st1 cell2mat(temp)']; % append truncated cells to st1 and return for more
        end;
    end;
end;
fclose (flist);
tsons =  ddd + (hhh + (st1(1,:)+(st1(2,:)+st1(3,:)/1000)/60)/60)/24;
N = length(tsons);
% compute delta T and scan for jumps > 0.25 seconds
dt = diff((tsons-floor(tsons(1)))*86400);
ii = find(dt>0.25); % indices for big gaps or jumps in timestamp
jj = length(find(dt<0.05)); % # points from rapid buffer dumps
if ~isempty(ii)
    hours = floor((tsons(ii)-floor(tsons(1)))*24);
    bad = [tsons(ii)',hours',dt(ii)',ones(length(ii),1)*length(tsons),ones(length(ii),1)*jj];
end;
