% Get Data Simple Ver 1.0 Richard.N.Grubb

% This script is a simplified reader for RIQ files
% It opens a file select window, then sets up the SCT values in memory
% and then steps through the file reading the PCT's and associated IQ data 
% verus range for each receiver.
% Add your own arrays and procedures for the data. Run from within MATLAB
% by typing "run Get_Data_Simple" at the prompt.

SCT_Map % call the script to set up the memory structures for the SCT and select the file to read

% read the SCT values we need into memory for faster access
Start_Year = SCT.data.start_year;
Start_DayNumber = SCT.data.start_daynumber;
Start_Month = SCT.data.start_month;
Start_Day = SCT.data.start_day;
Start_Minute = SCT.data.start_minute;
Start_Second = SCT.data.start_second;
PCT_Offset = SCT.data.sounding_table_size; % bytes
PCT_Count = SCT.data.pulse_table_size; % bytes
Num_Receivers = Receiver.data.rx_chan;
Num_Gates = Timing.data.gate_count;
Echo_Count = Num_Receivers * Num_Gates; % per pulse, bytes
Total_pulse_Count = Timing.data.pri_count;
Frequency_Count = Frequency.data.base_steps;
Max_Range = Timing.data.gate_end * 0.15; % km
Min_Range = Timing.data.gate_start * 0.15; % km
Tune_Type = Frequency.data.tune_type; % 1= log 2 = linear 3 = table 4 = shuffle mode

TempIQ = zeros(2, Num_Receivers, Num_Gates);

% create the additional arrays in memory here to hold the unpacked and processed data
% as you desire


Data_Offset = PCT_Count + (4 * Echo_Count); % total size of pulse table and data block in bytes
h = waitbar(0, 'Getting the data, Please wait');

for PulseNum = 1 : Total_pulse_Count %step through the file reading the PCT's and placing the data in the arrays
    waitbar(PulseNum / Total_pulse_Count, h);
    Byte_Offset = PCT_Offset + ( (PulseNum - 1) * Data_Offset);

PCT = memmapfile (Full_Name, 'Offset', Byte_Offset, 'Repeat', 1, 'Format', {    
% Pulse configuration Table Version 1.2
    'int32'  [1 1]    'record_id'          ;  % Sequence number of this PCT
    'int32'  [1 4]    'invalid'            ;  % skip the doubles, see PCT_Time below
    'int32'  [1 1]    'base_id'            ;  % Base Frequency counter
    'int32'  [1 1]    'pulse_id'           ;  % pulse set element for this PRI
    'int32'  [1 1]    'ramp_id'            ;  % ramp set element for this PRI
    'int32'  [1 1]    'repeat_id'          ;  % ramp repeat element for this PRI
    'int32'  [1 1]    'loop_id'            ;  % Outer loop element for this PRI
    'single' [1 1]    'frequency'          ;  % Frequency of observation (kHz)
    'int32'  [1 1]    'nco_tune_word'      ;  % Tuning word sent to the receiver
    'single' [1 1]    'drive_attenuation'  ;  % Low-level drive attenuation [dB]
    'int32'  [1 1]    'pa_flags'           ;  % Status flags from amplifier
    'single' [1 1]    'pa_forward_power'   ;  % Forward power from amplifier
    'single' [1 1]    'pa_reflected_power' ;  % Reflected power from amplifier
    'single' [1 1]    'pa_vswr'            ;  % Voltage Standing Wave Ratio from amplifier
    'single' [1 1]    'pa_temperature'     ;  % Amplifier temperature
    'int32'  [1 1]    'proc_range_count'   ;  % Number of range gates kept this PRI
    'single' [1 1]    'proc_noise_level'   ;  % Estimated noise level for this PRI
    'uint8'  [4 16]   'user'               ;  % Spare space for user-defined information    
    });   % Format and object end. Length 144 bytes

% A separate overlaid map for the two 'doubles' as they cannot fit in the main PCT
% array map if you need to use them.

% PCT_Time = memmapfile (Full_Name, 'Offset', Byte_Offset + 32, 'Repeat', 1, 'Format', { 
%     'double' [1 1] 'pri_ut'             ;  % UT of this pulse
%     'double' [1 1] 'pri_time_offset'    ;  % Time read from system clock, not precise.
%     });   % Format and object end.

PCT_Data = memmapfile (Full_Name, 'Offset', Byte_Offset + PCT_Count, 'Repeat', 1, 'Format', { 
    'int16' [2 eval(num2str(Num_Receivers)) eval(num2str(Num_Gates))] 'IQRxRG' ;  % I Q for RX 1:8 for Range Gate 1:512
    });   % Format and object end.

    TempIQ = PCT_Data.Data(1).IQRxRG; % read the data into memory for faster access
    TempIQ = swapbytes(TempIQ); % Data in the RIQ file is Big Endian
    
    % For Instance if these variables have been created
    % Current_Frequency = PCT.data.frequency;
    % I(GateNum, RXNum) = (TempIQ(1,RXNum,GateNum));
    % Q(GateNum, RXNum) = (TempIQ(2,RXNum,GateNum));

                    
% Now you have the array of data versus range for this particular pulse. Save the data in your
% arrays as you wish.



end;

% Use your data arrays here as you desire
delete(h)
display 'Competed.'

