%
% SBIR DATA RIGHTS
% Contract No.: W911QX-17-C-0038
% Basic Commerce and Industries, Inc. 
% 303 Harper Drive, Moorestown, NJ 08057
% Expiration of SBIR Data Rights Period: 30 September 2027
%
%The Government's rights to use, modify, reproduce, release, perform, display, 
% or disclose technical data or computer software marked with this legend are 
% restricted during the period shown as provided in paragraph (b)(4) of the 
% Rights in Noncommercial Technical Data and Computer Software--Small 
% Business Innovative Research (SBIR) Program clause contained in the above 
% identified contract. No restrictions apply after the expiration data shown 
% above. Any reproduction of technical data, computer software, or portions 
% thereof marked with this legend must also reproduce the markings. 

function data = messageReaderArenaV2(filename1,maxDwells,flashlite)

%messageReaderArenaV2 Reads the I/Q data file from the MPDR system and returns a data structure.
%
%   data = messageReaderArenaV2(filename1,maxDwells,flashlite) reads the content at a URL into a character array, S.  If the
%   server returns binary data, the array will contain garbage.
%
%   data = messageReaderArenaV2('filename1') reads the first 50 CPIs from
%   the I/Q file filename1 and parses the contents into a data structure
%   data.
%
%   data = messageReaderArenaV2('filename1',maxDwells) reads the first maxDwells from
%   the I/Q file filename1 and parses the contents into a data structure
%   data.
%
%   data = messageReaderArenaV2('filename1',maxDwells,flashlite) reads the first maxDwells CPIs from
%   the I/Q file filename1 and parses the contents into a data structure
%   data. Set flashlite to 1 if the data came froma  flashlite radar.
%
%   Copyright 2021 by Tim Maese. Do not distribute without explicit
%   permission from Tim Maese.


dataPath = 'D:\USCG-Day1-Morning\';
%dataPath = 'D:\Tim\Documents\Cloudstation\Army MPDR Phase II - W911QX-17-C-0038\Kymeta\Debug\';
%dataPath = 'Z:\RadarData\Kymeta\';

fid1 = fopen([dataPath,filename1],'r')

% Set up basic parameters
if nargin == 1,
    maxDwells = 50;
end;

if nargin < 3,
    flashlite = 0;
end;

data.sampleRate = 1.5625e6;
data.filename = filename1;
c= 3e8;
chDisable = hex2dec('ffffffff');

% Read the I/Q data from the CPI message file

for dwellCnt = 0:maxDwells-1;
    dwellCnt = dwellCnt + 1;

    data(dwellCnt).messageVersion = fread(fid1,1,'uint32');
    data(dwellCnt).messageId = fread(fid1,1,'uint32');
    data(dwellCnt).messageSize = fread(fid1,1,'uint32');
    data(dwellCnt).scanId = fread(fid1,1,'uint32');
    data(dwellCnt).timestamp = fread(fid1,1,'uint64');
    
    if data(dwellCnt).messageVersion >= 4       % v4 of the CPI message includes a GPS status flag
        data(dwellCnt).gpsSync = fread(fid1,1,'uint32');
    else
        data(dwellCnt).gpsSync = -999;
    end
    
    data(dwellCnt).waveformId = fread(fid1,1,'uint32');
    data(dwellCnt).transceiverType = fread(fid1,1,'uint32');
    data(dwellCnt).numBeamPosInScan = fread(fid1,1,'uint32');
    data(dwellCnt).beamPositionId = fread(fid1,1,'uint32');
    data(dwellCnt).numPulses = fread(fid1,1,'uint32');
       
    % Transmitter position structure
    data(dwellCnt).latitude = fread(fid1,1,'double');
    data(dwellCnt).longitude = fread(fid1,1,'double');
    data(dwellCnt).pitch = fread(fid1,1,'double');
    data(dwellCnt).roll = fread(fid1,1,'double');
    data(dwellCnt).yaw = fread(fid1,1,'double');
    
    if data(dwellCnt).messageVersion >= 5       % v5 of the CPI message includes positioner bearing & tilt
        data(dwellCnt).bearing = fread(fid1,1,'double');
        data(dwellCnt).tilt = fread(fid1,1,'double');
    else
        data(dwellCnt).bearing = -999;
        data(dwellCnt).tilt = -999;
    end
    
    % Steer position structure
    data(dwellCnt).azCh0 = fread(fid1,1,'double');
    data(dwellCnt).elCh0 = fread(fid1,1,'double');
    
    if flashlite == 1,
        data(dwellCnt).azCh0 = data(dwellCnt).yaw;
        data(dwellCnt).elCh0 = data(dwellCnt).pitch;
    end
    
       
    % Recieve channel strcuture (currently a max of 4 in version 3 and up of CPI)
    data(dwellCnt).idCh0 = fread(fid1,1,'uint32');close 
    data(dwellCnt).ch0numSamples = fread(fid1,1,'uint32');
    data(dwellCnt).idCh1 = fread(fid1,1,'uint32');
    data(dwellCnt).ch1numSamples = fread(fid1,1,'uint32');
    data(dwellCnt).idCh2 = fread(fid1,1,'uint32');
    data(dwellCnt).ch2numSamples = fread(fid1,1,'uint32');
    data(dwellCnt).idCh3 = fread(fid1,1,'uint32');
    data(dwellCnt).ch3numSamples = fread(fid1,1,'uint32');
    
    data(dwellCnt).prf = 1e3;                       % TEMP FOR NOW - NEED TO GET THESE VALUES FROM THE SCAN FILE

    % Read the samples for Channel 0 and Channel 1
    for pulseCnt= 1:data(dwellCnt).numPulses;
        data(dwellCnt).minRngRecSamp(pulseCnt,:) = 1;
        dataIn1 = fread(fid1,[2,data(dwellCnt).ch0numSamples],'single');    
        data(dwellCnt).iqDataCh0(pulseCnt,:) = dataIn1(1,:) + i .* dataIn1(2,:);    
    end;
    
    for pulseCnt= 1:data(dwellCnt).numPulses;
        dataIn2 = fread(fid1,[2,data(dwellCnt).ch1numSamples],'single');
        data(dwellCnt).iqDataCh1(pulseCnt,:) = dataIn2(1,:) + i .* dataIn2(2,:);
    end;
    
end;


fclose(fid1);

