%------------------------%
% Start Read1UDAQ        %
%------------------------%
% By: Victor Jara
% Date: August 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_file_num    = 0;   % Start raw file number
stop_file_num     = 1;   % Stop raw file number
er           = 3.17; % Dielectric constant

%% 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    = 4;   % How many samples to remove from the front
cut_length   = 40;   % 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 pic = start_file_num:num_per_pic:(stop_file_num)
    tic;
% Create file name
    filename = sprintf('%s/%s.%04d.dat',in_folder,file_prefix,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');
end