myRadar = mpdrRadar('./Config/mpdr2PlusRadarConfig.yaml');
myRadar.openDataRecordFile();

% preallocate the cipData ...

cipData = zeros(myRadar.getNumChan(),...
                myRadar.getPriSize(),...
                myRadar.getNumPris(),...
                'like',1j);
            
[cipData,cipMetaData] = myRadar.getCIP(cipData);

plotPris = 3; %plot a small fraction of the PRIs

%

%% Pulse Compress 
% Pulse compress the CIPs
LrChans=radChan.getLrChans();
SrChans=radChan.getSrChans();

% create pulse compressors
LrPc = PulseCompressor(waveformTypes.LfmUpChirp,...
    cipMetaData.bandWidth(uint32(radChan.sumLr)),...
    cipMetaData.pulseLength_s(uint32(radChan.sumLr)),...
    cipMetaData.sampleRate_sps,...
    cipMetaData.priSize);
SrPc = PulseCompressor(waveformTypes.LfmUpChirp,...
    cipMetaData.bandWidth(uint32(radChan.sumSr)),...
        cipMetaData.pulseLength_s(uint32(radChan.sumSr)),...
    cipMetaData.sampleRate_sps,...
    cipMetaData.priSize);

global pcData;
pcData=zeros(size(cipData));

% pulse compress long range channels...
for ii=LrChans
    pcData(ii,:,:)=LrPc.compress(cipData(ii,:,:));
end
myResult=LrPc.compress(conj(cipData(6,:,:)));

% pulse compress short range channels...
for ii=SrChans
    pcData(ii,:,:)=SrPc.compress(cipData(ii,:,:));
end
    
    

%

    %% Plot time domain data
    %
global FigOrder;
FigOrder=[uint32(radChan.sumLr),uint32(radChan.auxLr),...
    uint32(radChan.dAzLr),uint32(radChan.dElLr),...
    uint32(radChan.sumSr),uint32(radChan.auxSr),...
    uint32(radChan.dAzSr),uint32(radChan.dElSr)];

f1=figure;
set(f1,'position',[40 50 1500 600]);

set(f1,'Name',['Time Domain Complex Data: ' cipMetaData.Filename]);
tiledlayout(2,4);
% figure out y axis limits
chanData = reshape(cipData,cipMetaData.numChannels,[]);
maxValue=max(max(abs(chanData)));
rOrder=10^(floor(log10(maxValue)));
plotLimit=ceil(maxValue/rOrder)*rOrder;

for ii=1:8
    nexttile
    p1=plot(real(chanData(FigOrder(ii),1:plotPris*cipMetaData.priSize)),'b');
    hold on;
    plot(imag(chanData(FigOrder(ii),1:plotPris*cipMetaData.priSize)),'r');
    title(radChan.getString(FigOrder(ii)));
    ylim([-plotLimit plotLimit]);
    ylabel('Counts');
    xlabel('Samples');
end

%

%% Plot Magnitude in dBq
% DESCRIPTIVE TEXT

f2=figure;
set(f2,'position',[70 80 1500 600]);
set(f2,'Name',['Time Domain Magnitude data: ' cipMetaData.Filename]);
tiledlayout(2,4);
for ii=1:8
    nexttile
    plot(20*log10(abs(chanData(FigOrder(ii),1:plotPris*cipMetaData.priSize))),'b');
    title(radChan.getString(FigOrder(ii)));
    ylabel('dBq');
%     ylim([0 plotLimit]);
    xlabel('Samples');
end

%

%% Plot PC results in dB
% DESCRIPTIVE TEXT

f3=figure;
set(f3,'position',[60 70 1500 600]);
set(f3,'Name',['Pulse compressed waveforms: ' cipMetaData.Filename]);
tiledlayout(2,4);

plotPcData=reshape(pcData,8,[]);
maxValue=max(max(abs(plotPcData(:,:))));

for ii=1:8
    nexttile
    plot( 20 * log10(abs(plotPcData(FigOrder(ii),1:plotPris*cipMetaData.priSize))/maxValue));
    title(radChan.getString(FigOrder(ii)));
    ylim([-100 0]);
    ylabel('dB');
    xlabel('Samples');
end
% move first figure to front:
figure(f1);