

clc; clear; close all;

folder = '/d0/1213_7.5';
start_file = 'data.12132009';
pic_folder = './Pic';
fig_filename = 'Pic-1';
start_num = 6;
stop_num = 10;
coh_avgs = 1000;
start_cut = 10;
cut_length = 10;

num_per_pic = 1;
min_depth = 0;
max_depth = 4000;

num_wfs = 1;        % not included at StartRead1UDAQ_TIDSoR
rec_start = 1;      % not included at StartRead1UDAQ_TIDSoR

f_samp = 125E6;
BW = 1e6;
tau = 1e-6;
chirp_rate = BW/tau;

for pic = start_num:num_per_pic:(stop_num)
    tic;
    % Create file name
    filename = sprintf('%s/%s.%04d.dat',folder,start_file,pic);

    % Open file
    fid = fopen(filename,'r','ieee-be');
    fprintf('Opening File: %s\n',filename);

    % Rewind the file to the beginning
    fseek(fid,0,'bof');

    % Find first header section
    i=0;
    while true
        tmp = fread(fid,1,'uint32');
        if tmp == hex2dec('deadbeef');
            fseek(fid,-4,'cof');
            break;
        end
        fseek(fid,-3,'cof');
    end
    magiccode  = fread(fid, 1, 'uint32');
    header_ver = fread(fid, 1, 'uint8');
    header_cnt = fread(fid,1,'ubit24');
    epritime1  = fread(fid, 1, 'uint64');
    tmp        = fread(fid,3,'uint32');
    UTC_hr     = tmp(1);
    UTC_min    = tmp(2);
    UTC_sec    = tmp(3);
    fseek(fid,-28,'cof');
    data_start = ftell(fid);
    fseek(fid,data_start+28,'bof');

    % Get record length
    rec_byte_len = 0;
    while true
        tmp = fread(fid,1,'uint32');
        if tmp == hex2dec('deadbeef');
            fseek(fid,-4,'cof');
            break;
        end
        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;
    % Find number of records
    found_num_recs = 0;
    while true
        done = fseek(fid,28+num_wfs*rec_length*2+4,'cof');
        if done == -1
            break;
        end
        found_num_recs = found_num_recs+1;
    end
    recs_2_load = found_num_recs;

    
    data = Read_TIDSoR(fid,num_wfs,coh_avgs,recs_2_load,rec_start,start_cut,cut_length,rec_length,data_start);
    data_fft = 20*log10(abs(fft(data)));
    fclose(fid);
    total_recs = recs_2_load;
    for index = (pic + 1):(pic + num_per_pic - 1)
        % Create file name
        filename = sprintf('%s/%s.%04d.dat',folder,start_file,index);

        % Open file
        fid = fopen(filename,'r','ieee-be');
        fprintf('Opening File: %s\n',filename);

        % Rewind the file to the beginning
        fseek(fid,0,'bof');

        % Find first header section
        i=0;
        while true
            tmp = fread(fid,1,'uint32');
            if tmp == hex2dec('deadbeef');
                fseek(fid,-4,'cof');
                break;
            end
            fseek(fid,-3,'cof');
        end
        magiccode  = fread(fid, 1, 'uint32');
        header_ver = fread(fid, 1, 'uint8');
        header_cnt = fread(fid,1,'ubit24');
        epritime1  = fread(fid, 1, 'uint64');
        tmp        = fread(fid,3,'uint32');
        UTC_hr     = tmp(1);
        UTC_min    = tmp(2);
        UTC_sec    = tmp(3);
        fseek(fid,-28,'cof');
        data_start = ftell(fid);
        fseek(fid,data_start+28,'bof');

        % Get record length
        rec_byte_len = 0;
        while true
            tmp = fread(fid,1,'uint32');
            if tmp == hex2dec('deadbeef');
                fseek(fid,-4,'cof');
                break;
            end
            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;
        % Find number of records
        found_num_recs = 0;
        while true
            done = fseek(fid,28+num_wfs*rec_length*2+4,'cof');
            if done == -1
                break;
            end
            found_num_recs = found_num_recs+1;
        end
        recs_2_load = found_num_recs;
        data = Read_TIDSoR(fid,num_wfs,coh_avgs,recs_2_load,rec_start,start_cut,cut_length,rec_length,data_start);
        data_fft_tmp = 20*log10(abs(fft(data)));
        data_fft = [data_fft data_fft_tmp];
        fclose(fid);
        total_recs = total_recs + recs_2_load;
    end
    
    faxis = linspace(0,f_samp,size(data,1));
    depth = (faxis/chirp_rate)*3E8/2/sqrt(3.17);
    hf = figure;
    imagesc(1:(total_recs/coh_avgs),depth,data_fft);
    axis([0 (total_recs/coh_avgs) min_depth max_depth]);
    xlabel('Record');
    ylabel('Depth [m]');
    fig_title = sprintf('%s %d',fig_filename,pic);
    title(fig_title);
%     colormap(1-gray);
    output_filename = sprintf('%s/%s_%d',pic_folder,fig_filename,pic);

    %Save Image
    print(hf,'-dpng',output_filename,'-r150');
    close(hf);
    toc
end