%------------------------%
% Start Read1UDAQ        %
%------------------------%
% By: William Blake
% Date: 5/21/2009
%  This script is used to setup and call the Read1UDAQ function

clc; clear; close all;
format short; format compact;

%% Data Paths/File nameing info
in_folder    = '/d0';
file_prefix  = 'data.07302009';
out_folder   = './Pic';
out_fn_pre   = 'Test-1';
start_num    = 0;   % Start raw file number
stop_num     = 1;   % Stop raw file number

%% Processing options for the data
coh_avgs     = 1000;   % Number of coherent averages to perform
inc_avgs     = 1;   % Number of incoherent averages to perform
start_cut    = 10;   % How many samples to remove from the front
cut_length   = 10;   % How many samples to remove from the end

%% Displaying of the data
num_per_pic  = 2;   % Number of raw files per picture
min_depth    = 0;    % Minimum plotted depth
max_depth    = 3000; % Maximum plotted depth

%% Signal Characteristics
f_samp       = 125e6;   % Data sampling frequency (Hz)
BW           = 1e6;     % TX Signal BW (Hz)
tau          = 1e-6;  % TX pulse duration (s)
chirp_rate   = BW/tau;  % Calculated Chirp rate
wf_gen_freq  = 125e6;   % Waveform generator frequency (used for time)

for i0 = start_num:num_per_pic:stop_num
    Data = [];
    Header.num_rec = 0;
    Header.time    = [];
    tic;
    for i1 = i0:min(i0+num_per_pic-1,stop_num)
        % Create file name
        fn = sprintf('%s/%s.%04.0f.dat',in_folder,file_prefix,i1);
        recs_2_load  = 5000;  % Load records (0 means all recs)
        num_wfs      = 1;  % Only 1 wf supported
        rec_start    = 1;  % Load records starting from the first one.
        % Read in data
        [hdr, tmp_data] = Read1UDAQ(fn,rec_start,recs_2_load,coh_avgs,wf_gen_freq,start_cut,cut_length);
%         tmp_data = fft(tmp_data);
        % Incoherently average data
        tmp_data = IncoherentDec(tmp_data,inc_avgs);
        % Coherently average time (to get same length)
        hdr.time = CoherentDec(hdr.time,inc_avgs);
        % Concatenate data and time values;
        Data = [Data, tmp_data];
        Header.time    = [Header.time, hdr.time];
        Header.num_rec = Header.num_rec+size(Data,2);
        clear tmp_data;
    end
    
    % Create image
    faxis = linspace(0,f_samp,size(Data,1));
    
    figure; plot(fftshift(faxis/1e6,10*log10(abs(fft(Data(:,1))))
    Depth = 1/f_samp*3e8/sqrt(3.15)/2*(0:size(Data,1));
%     Depth = 1/f_samp*3e8/2*(0:size(Data,1));
    hf = figure;
    imagesc([],Depth,20*log10(abs(Data)));
    colorbar on;
%     imagesc(1:hdr.num_rec,Depth,10*log10(abs(Data)));
%     axis([1 hdr.num_rec min_depth max_depth]);
    xlabel('Record');  ylabel('Depth [m]');
    fig_title = sprintf('%s_%02.0f_to_%02.0f',out_fn_pre,i0,i1);
    title(fig_title,'Interpreter','none');
%     colormap(1-gray);
    out_fn = sprintf('%s/%s_%02.0f_to_%02.0f',out_folder,out_fn_pre,i0,i1);

    %Save Image
    print(hf,'-dpng',out_fn,'-r150');
%     close(hf);
    save(out_fn,'Data','Header','Depth')
    toc
end