function d = read_rawcfg(instclass, file,  add_to_ens )
% d = read(instclass, file, add_to_ens)
% 
% 'add_to_ens' [add to ens_num(1)]   % defaults to 0; use with leftover
% use this to scan a file and get all cfg info

% from read.m - just to get cfg changes

disp('reading all ens_nums ...')

%global RDIyearbase 

RDIyearbase = 1997;  %a non-leap year for RDI's calendar
                     %NOTE: we're going to use this to guarentee a 1s/s
                     %dt in the RDI dday; the config.yearbase will
                     %come from p.yearbase (to make to_date reasonable)

if (nargin < 2)
   help read 
   error('must set instclass and file')
end

if (~exist('add_to_ens','var'))
   add_to_ens = 0;
end

if (~exist(file, 'file'))
   error(sprintf('cannot open %s\n', file))
end

fid = fopen(file, 'r', 'ieee-be');  % the OS processor writes little-endian
[config, buf] = read_buf(fid, RDIyearbase);  % First time: read config.
if ischar(buf)
   d.error = buf;
   return
end

fseek(fid, 0, 'eof');
len_file = ftell(fid);
nbytes = config.nbytes + 2;
n_indexs = floor(len_file / nbytes);
start = 1;
stop =  n_indexs - 1;


%initialize everything; may have to stop prematurely if the end of the file
%was badly terminated (may have to change NP later)
NP = n_indexs; 

% Always include dday and ens_num.
d.RDIymdhms = zeros(NP, 6);    %RDI ymdhms (i.e. relative to 1997)
d.dday = zeros(NP, 1);         %1s/s dday 
d.ens_num = zeros(NP, 1);      %cumulative ens_num for (group of files)
d.RDIens_num = zeros(NP, 1);   %what is stored in the raw data file
d.add_to_ens = add_to_ens;     %start with what was passed in; update if req.
d.config_nbytes = zeros(NP, 1);
d.config_nleader = zeros(NP, 1);


%changes here:
% (1) r.ens_num will always be r.ens_num + add_to_ens
% (2) if r.ens_num(n) > r.ens_num(n+1)  (or if r.ens_num == 65535)
%              we'll add enough to r.ens_num(n+1:end) to make it increase by 1
% (3) there is a new output structure r.add_to_ens which is 
%     add_to_ens + (sum of jumps found this time, see (2))
%
% d.ens_num is the nondecreasing counter for the whole filelist
% d.RDIens_num is what was read for this buffer
% d.add_to_ens is a continually updated record of what we add to the 
%      stored value (RDIens_ens) to get the updated value (ens_num)


fseek(fid,0, 'bof'); % start at the beginning of the file
[config, buf] = read_buf(fid, RDIyearbase, config);
if ischar(buf),
   d.error = buf;
   brokeout = 1;
   break
end

if (buf.ens_num == 1)
   d.add_to_ens = 0;  %start over if first RDIens_num is 1
end

brokeout = 0;
for ii = 1:NP
   fseek(fid, nbytes * (ii - start), 'bof'); 
   [config, buf] = read_buf(fid, RDIyearbase, config);
%   d.config_nbytes(ii) = config.nbytes;
%   d.config_nleader(ii) = config.n.leader;
   if ischar(buf),
      d.error = buf;
      brokeout = 1;
      break
   end

   d.RDIymdhms(ii,:) = buf.ymdhms;
   d.dday(ii)       = buf.dday;
   d.RDIens_num(ii) = buf.ens_num;  %this is what was read
   d.ens_num(ii) = d.RDIens_num(ii) + d.add_to_ens;  % add the current offset
   %add logic to change add_to_ens if necessary
   if (d.RDIens_num(ii) == 65535)    %case 1: it's about to wrap
      d.add_to_ens = d.add_to_ens + 65536;   % then boost d.add_to_ens by 65535
   end
end

if (brokeout) %we broke out early
      fprintf('read error found  early at %d (out of %d):\n', ii, NP)
      fprintf('%s\n',d.error)
      d = cutadcp(instclass, d, [1:ii-1]);
end


fclose(fid);
d.error = [];


%=========================================================================
function [config, buf] = read_buf(fid, RDIyearbase,  config)


start = ftell(fid);
[hdr, count] = fread(fid, 7, 'uint16');

if count ~= 7,
   buf = sprintf('error 1, only %d bytes at start of buffer\n', count);
   return
end
config.nbytes = hdr(1);
config.n.leader = hdr(2);
config.n.vel = hdr(3);
config.n.sw = hdr(4);
config.n.amp = hdr(5);
config.n.pg = hdr(6);
config.n.status = hdr(7);

fseek(fid, start, 'bof');
[rec, count] = fread(fid, config.nbytes, 'uint8');
if count ~= config.nbytes,
   buf = sprintf('error 3, only %d bytes read, %d expected\n', ...
                     count, config.nbytes);
   return
end
test_cs = mod(sum(rec), 65536);
cs = fread(fid, 1, 'uint16');
if test_cs ~= cs,
   buf = ['error 4, Checksum failure; CS is ', int2str(cs), ' versus calculated ', ...
            int2str(test_cs)];
   return
end

%%% variable part of leader:
ofs = 14;
buf.ens_num = rec(ofs + 17) + 256 * rec(ofs + 16);

% Use 1997 to get a 1s/s dday from RDI
tt = decode_time(RDIyearbase, rec(ofs + (1:5))).';

%??? is this wrong?
buf.dday = to_day(RDIyearbase, tt);
buf.ymdhms = tt;


%----------------------------------------------
function td = decode_time(yrbase, t)
% t is an array with 5 "packed binary coded decimal"
% numbers: month, day, hour, minute, second
upper = bitshift(t, -4);
lower = bitand(t, 15);
td = upper * 10 + lower;
td = [yrbase; td(:)];

