function [hdr, data] = Read1UDAQ(fn,rec_start,recs_2_load,coh_avgs,wf_gen_freq,start_cut,cut_length)
%------------------------------------------%
% Read Accum Radar data                    %
%------------------------------------------%
% Authors:  William Blake
%           John Ledford
%           Cameron Lewis
%
% Argument Description:
% hdr  =  Header struct
% data =  Array of data
% fn   =  Input raw filename
% rec_start = Record number to start loading on
% recs_2_load = number of records to load
% coh_avgs    = number of coherent averages to perform
% wf_gen_freq = waveform generation frequency this is used
%  to accurately decipher the time vector
% start_cut   = samples to remove from start of record
% cut_length  = samples to remove from end of record
%
% Example usage:
% [Header_only] = Read1UDAQ(fn, 1, 0, 50, 125e6, 0, 0);
%  This just loads the header, no data (should be faster)
%
% [Hdr, Data]   = Read1UDAQ(fn, 50, 100, 50, 125e6, 10, 10);
%   This will load 100 records starting at record 50, and remove 10 points
%    from the start and the end.

if nargout == 1
    data_mode = 'header-only';
elseif nargout == 2
    data_mode = 'all';
end

fid = fopen(fn,'r','ieee-be');
if fid == -1; error('File:Open','Unable to open file: %s',fn); end
% Rewind the file to the beginning
fseek(fid,0,'bof');

just_fn = fliplr(strtok(fliplr(fn),'/'));
fprintf('\nLoading file %s\n',just_fn);

% Find first header location
tmp = 0;
while tmp ~= -1
    tmp = fread(fid,1,'uint32');
    if tmp == hex2dec('deadbeef');
        fseek(fid,-4,'cof');
        break;
    end
    tmp = fseek(fid,-3,'cof');
end
data_start = ftell(fid);

% Go to the first record and skip past the header
fseek(fid,data_start+28,'bof');
% Get record length
rec_byte_len = 0;
tmp = 0;
while tmp ~= -1
    tmp = fread(fid,1,'uint32');
    if tmp == hex2dec('deadbeef');
        fseek(fid,-4,'cof');
        break;
    end
    tmp = fseek(fid,-3,'cof');
    rec_byte_len = rec_byte_len+1;
end
% Remove the 4 byte checksum at the end
rec_byte_len = rec_byte_len - 4;
% Assumed uint16 (2 byte values)
rec_length = rec_byte_len/2;
fprintf('Record Length: %.0f\n',rec_length);

% Find Preallocation numbers
if ~recs_2_load
    fseek(fid,data_start,'bof');
    hdr.num_rec = 0;
    while true
        done = fseek(fid,28+rec_length*2+4,'cof');
        if done == -1
            break;
        end
        hdr.num_rec = hdr.num_rec+1;
    end
else
    hdr.num_rec = recs_2_load;
end
fprintf('Estimated number of records to load: %.0f\n',hdr.num_rec);

% Go to the first record to load
fseek(fid,data_start+rec_start*(28+rec_length*2+4),'bof');
% Preallocate variables
data           = zeros(rec_length-cut_length-start_cut,floor(hdr.num_rec/coh_avgs),'single');
hdr.ver        = zeros(1,hdr.num_rec);
hdr.num        = zeros(1,hdr.num_rec);
hdr.radar_time = zeros(1,hdr.num_rec);
hdr.time       = zeros(1,hdr.num_rec);
hdr.ID         = zeros(1,hdr.num_rec);
idx            = 0;
done           = false;
for i0 = 1:floor(hdr.num_rec/coh_avgs)
    idx = idx+1;
    tmp_data = zeros(rec_length,1,'single');
    for i1 = 1:coh_avgs
        magiccode  = fread(fid,1,'uint32');
        if (magiccode ~= hex2dec('deadbeef'))
            while magiccode ~= hex2dec('deadbeef')
                fseek(fid,-3,'cof');
                magiccode = fread(fid,1,'uint32');
            end
            hdr.ver(idx)        = fread(fid,1,'uint8');
            hdr.num(idx)        = fread(fid,1,'ubit24');
            hdr.radar_time(idx) = fread(fid,1,'uint64');
            tmp                 = fread(fid,3,'uint32');
            sec_of_day          = tmp(1);
            pps_frac            = tmp(2);
            hdr.time(idx)       = (sec_of_day+pps_frac*2/wf_gen_freq)/coh_avgs;
            hdr.ID(idx)         = tmp(3);
            warning('Records:Lost','Magic Code incorrect at: %.0f\n%.0f Records Lost', ...
                idx,hdr.num(idx)-hdr.num(idx-1));
        else
            hdr.ver(idx)        = fread(fid,1,'uint8');
            hdr.num(idx)        = fread(fid,1,'ubit24');
            hdr.radar_time(idx) = fread(fid,1,'uint64');
            tmp                 = fread(fid,3,'uint32');
            sec_of_day          = tmp(1);
            pps_frac            = tmp(2);
            hdr.time(idx)       = (sec_of_day+pps_frac*2/wf_gen_freq)/coh_avgs;
            hdr.ID(idx)         = tmp(3);
        end
        if strcmp(data_mode,'all')
            tmp_raw = fread(fid,rec_length,'uint16=>float32');
            if (length(tmp_raw) ~= rec_length);
                idx = idx-1;
                done = true;
                break;
            end
            tmp_data(1:2:end) = tmp_data(1:2:end)+tmp_raw(2:2:end)/coh_avgs;
            tmp_data(2:2:end) = tmp_data(2:2:end)+tmp_raw(1:2:end)/coh_avgs;
            filecrc1 = fread(fid, 1, 'uint32');
        else
            tmp = fseek(fid, rec_length*2+4,'cof');
            if tmp == -1
                idx = idx-1;
                done = true;
                break;
            end
        end
    end
    if done; break; end;
    if strcmp(data_mode,'all');
        % Remove last "cut_length" points because there is junk spike there
        data(:,i0) = tmp_data(1+start_cut:end-cut_length); %.*hanning(length(tmp_data)-start_cut-cut_length);
    end
end
hdr.num_rec    = idx;
hdr.ver        = hdr.ver(1:idx);
hdr.num        = hdr.num(1:idx);
hdr.radar_time = hdr.radar_time(1:idx);
hdr.time       = hdr.time(1:idx);
hdr.ID         = hdr.ID(1:idx);
data           = data(:,1:idx);

% if strcmp(data_mode,'all');    
%     data = fft(data);
% end
fprintf('Actual number of records loaded: %.0f\n',hdr.num_rec);

end