function [header,data] = readKMS(cfile)
%   reads KMS data file
%   Usage: [header,data] = readKMS(cfile);

fid = fopen(cfile,'r','b');
fid
%  read first header block
header = rdKMShd(fid);
%    rewind file ....
fseek(fid,0,-1);      % bof = -1 beginning of file

%   then read data, skipping header blocks ...
%     more sophisticated reading routine will be needed, but first need to
%     understand more about what's what in the header,  and what info we
%     need for plotting (or other purposes)

blockSize = 92*512;
nData = 30*512;
nch = sum(header.chActive);
npts = nData/nch;
nMax = 10000000;
data = zeros(nch,nMax);
done  = 0;
i1 = 1;
while ~done
    [block,count] = fread(fid,[blockSize,1],'uchar'); 
    %hd = block(1:1024);
    block(1:4)
    nRead = count-1024;
    if nRead > 3*nch
        nData = floor(nRead/3);
        npts = floor(nData/nch);
        temp = reshape(block(1025:end),[3,nData]);
        d = (temp(1,:)*256+temp(2,:))*256+temp(3,:);
        d(d > 8388608) = d(d > 8388608)- 16777216;
        d = reshape(d(1:npts*nch),nch,npts);
        d = d/3355.4432;
        i2 = i1+npts-1;
        data(:,i1:i2) = d;
        done = count < blockSize;
    else
        done = true;
    end
    i1 = i2+1;
end
data = data(:,1:i2);
fclose(fid);