%
% 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 rangeVect = plotDwell_v2(data,dwell2plot)

sampleRate = data.header.sampleRate;
c = 3e8;

ch0DataPwr = (abs(data.rawIQ.iqDataCh0(:,:,dwell2plot))).^2;
ch1DataPwr = (abs(data.rawIQ.iqDataCh1(:,:,dwell2plot))).^2;

ch0DataPwrdB = 10.*log10(ch0DataPwr);
ch1DataPwrdB = 10.*log10(ch1DataPwr);

% Convert the nanoseconds since epoch to date-time with fractional seconds
%wholeSecs = floor(double(data(dwell2plot).timestamp) / 1e9);
%fracSecs  = double(data(dwell2plot).timestamp - uint64(wholeSecs)*1e9)/1e9;

wholeSecs = floor((data.header.timestamp(dwell2plot)) / 1e9);
fracSecs  = (data.header.timestamp(dwell2plot) - (wholeSecs)*1e9)/1e9;
%cpiTimestamp = datetime(wholeSecs,'ConvertFrom','posixTime','Format','yyyy.MM.dd HH:mm:ss.SSSSSSSSS') + seconds(fracSecs);
cpiTimestamp=datenum([0 0 0 0 0 wholeSecs]);

fig1 = figure('Name', ['Filename: ',data.header.filename]) ;

%hax=axes; 
s1 = subplot(3,1,1);
plot(ch0DataPwrdB.');
title(['Power Output - CH0 - Dwell ',num2str(dwell2plot)]);
xlabel('Range Sample');
ylabel('Sample Power (dB)');
hold on 
plot(mean(ch0DataPwrdB),'k-')
axis([0 max([length(ch0DataPwrdB) length(ch1DataPwrdB)]) 0 120])
%line([data(dwell2plot).minRngRecSamp(1) data(dwell2plot).minRngRecSamp(1)],get(hax,'YLim'),'Color',[0 0 0])

s2 = subplot(3,1,2);
%hax1=axes; 
plot(ch1DataPwrdB.');
title(['Power Output - CH1 - Dwell ',num2str(dwell2plot)]);
xlabel('Range Sample');
ylabel('Sample Power (dB)');
hold on 
plot(mean(ch1DataPwrdB),'k-')
axis([0 max([length(ch0DataPwrdB) length(ch1DataPwrdB)]) 0 120])
%line([data(dwell2plot).minRngRecSamp(1) data(dwell2plot).minRngRecSamp(1)],get(hax1,'YLim'),'Color',[0 0 0])

s3 = subplot(3,1,3);
dataString1 = {
        ['Waveform ID:      ', num2str(data.header.waveformId)];
        ['Number of Pulses: ', num2str(data.header.numPulses)];
        [''];
        ['GPS Sync:         ', ''];
        ['Timestamp:        ', datestr(cpiTimestamp)];
        ['Fractional Secs:  ', num2str(fracSecs,'%.9f')]
        };
dataString2 = {
        ['Beam Position ID: ', num2str(data.header.beamPositionId(dwell2plot))];        
        ['Beam Azimuth:     ', num2str(data.header.az(dwell2plot))];
        ['Beam Elevation:   ', num2str(data.header.el(dwell2plot))];
        ['GPS Yaw:          ', num2str(data.header.yaw)];
        ['GPS Pitch:        ', num2str(data.header.pitch)];
        };
text(0, 0.5, dataString1); axis off    
text(0.6, 0.5, dataString2); axis off 

%Generate a Range-Doppler Map
dopplerMap = fft(data.rawIQ.iqDataCh0(:,:,dwell2plot));
[pulses,samples] = size(dopplerMap);
freqVect = [-data.header.prf/2: data.header.prf/pulses: data.header.prf/2- data.header.prf/pulses];
rangeVect = [1:samples] .* c/(2*sampleRate) ./ 1000 ;

dopplerMap2 = fft(data.rawIQ.iqDataCh1(:,:,dwell2plot));
[pulses2,samples2] = size(dopplerMap2);
rangeVect2 = [1:samples2] .* c/(2*sampleRate) ./ 1000 ;


fig2 = figure('Name', ['Filename: ',data.header.filename]) ;
title(['Filename: ',data.header.filename]);
subplot(211);
[X,Y] = meshgrid(rangeVect,freqVect);
dopplerMapPower = fftshift(20*log10(abs(dopplerMap)),1);
surf(X,Y,dopplerMapPower);
caxis([0 120]);
axis([0 max([rangeVect(end) rangeVect2(end)]) min(freqVect) max(freqVect)]);
shading flat
view([0 90])
xlabel('Range (km)');
ylabel('Doppler Frequency (Hz)');
title(['Range Doppler Map - CH0 - Dwell ',num2str(dwell2plot)]);
colorbar

subplot(212);
[X,Y] = meshgrid(rangeVect2,freqVect);
dopplerMapPower2 = fftshift(20*log10(abs(dopplerMap2)),1);
surf(X,Y,dopplerMapPower2);
caxis([0 120]);
axis([0 max([rangeVect(end) rangeVect2(end)]) min(freqVect) max(freqVect)]);
shading flat
view([0 90])
xlabel('Range (km)');
ylabel('Doppler Frequency (Hz)');
title(['Range Doppler Map - CH1 - Dwell ',num2str(dwell2plot)]);
colorbar
