clearvars
close all
clc

%

myAnt = aesaAntenna("Config/m2AntennaConfig.yaml");
az_d = 0.0;
el_d = 0.0;
% freq_hz=9.3e9;
bwThreshold = 3;
ifreq=0;

for freq_hz = 9.3e9:1e8:9.3e9
    ifreq= ifreq+1;
    [AzPattern, angles_d] = myAnt.getAzPattern(freq_hz,az_d,el_d);
    maxVal = max(AzPattern);
    AzPattern_dB = 20 * log10(abs(AzPattern)/maxVal);
    [dAzPattern, angles_d] = myAnt.getDAzPattern(freq_hz,az_d,el_d);
    dAzPattern_dB = 20 * log10(abs(dAzPattern)/maxVal);
    [ElPattern, angles_d] = myAnt.getElPattern(freq_hz,az_d,el_d);
    maxVal = max(ElPattern);
    ElPattern_dB = 20 * log10(abs(ElPattern)/maxVal);
    [dElPattern, angles_d] = myAnt.getDElPattern(freq_hz,az_d,el_d);
    dElPattern_dB = 20 * log10(abs(dElPattern)/maxVal);
    

%

    
    figure(1);
    axis([-90 90 -60 1]);
    hold on
    plot(angles_d,AzPattern_dB);
    plot(angles_d,dAzPattern_dB);

%

%     MidPoint=ceil(length(AzPattern_dB)/2);
%     [~,jn] = min(abs(AzPattern_dB(MidPoint-1:-1:1)+bwThreshold));
%     [~,jp] = min(abs(AzPattern_dB(MidPoint+1:end)+bwThreshold));
%     AzBeamWidth= -angles_d(MidPoint-jn)+angles_d(MidPoint+jp);
%     fprintf("Az BeamWidth = %5.2f",AzBeamWidth);
    %     MidPoint=ceil(length(ElPattern_dB)/2);
    [maxVal,MidPoint]=max(AzPattern_dB);
% maxVal=0;
    [~,jn] = min(abs(AzPattern_dB(MidPoint-1:-1:1)-(maxVal-bwThreshold)));
    [~,jp] = min(abs(AzPattern_dB(MidPoint+1:end)-(maxVal-bwThreshold)));
    ElBeamWidth= -angles_d(MidPoint-jn)+angles_d(MidPoint+jp);
    fprintf("Azimuth BeamWidth = %5.2f",ElBeamWidth);
    
    figure(2)
    azMpCurve = imag(dAzPattern./AzPattern);
    plot(angles_d,(azMpCurve));
    hold on
    xlim([angles_d(MidPoint-2*jn) angles_d(MidPoint+2*jp)]);
    xlabel(gca,'Angle off axis (degrees)');
    ylabel(gca,'Monopulse Ratio imag(daz/sum)');
    mpTable.freq_hz(ifreq) = freq_hz;
    mpTable.Lookup(ifreq).azmpRatio=azMpCurve((MidPoint-2*jn):(MidPoint+2*jp));
    mpTable.Lookup(ifreq).azAngles_d=angles_d((MidPoint-2*jn):(MidPoint+2*jp));

%

    figure(3);
    axis([-90 90 -60 1]);
    hold on
    plot(angles_d,ElPattern_dB);
    plot(angles_d,dElPattern_dB);

%

%     MidPoint=ceil(length(ElPattern_dB)/2);
    [maxVal,MidPoint]=max(ElPattern_dB);
% maxVal=0;
    [~,jn] = min(abs(ElPattern_dB(MidPoint-1:-1:1)-(maxVal-bwThreshold)));
    [~,jp] = min(abs(ElPattern_dB(MidPoint+1:end)-(maxVal-bwThreshold)));
    ElBeamWidth= -angles_d(MidPoint-jn)+angles_d(MidPoint+jp);
    fprintf("Elevation BeamWidth = %5.2f",ElBeamWidth);
    figure(4);
    elMpCurve = imag(dElPattern./ElPattern);
    plot(angles_d,(elMpCurve));
    hold on
    xlim([angles_d(MidPoint-2*jn) angles_d(MidPoint+2*jp)]);
    mpTable.Lookup(ifreq).elmpRatio=elMpCurve((MidPoint-2*jn):(MidPoint+2*jp));
    mpTable.Lookup(ifreq).elAngles_d=angles_d((MidPoint-2*jn):(MidPoint+2*jp));

%

    [a3dPattern, angles3d_d] = myAnt.get3DPattern(freq_hz,az_d,el_d);

%

    
    myFigure=figure;
    myAxis=gca;
    imagesc(myAxis,'XData',angles3d_d,'YData',angles3d_d,'CData',a3dPattern);
    
    caxis(myAxis,[-60 0]);
    colormap(myFigure,jet(256));
    ylim(myAxis,[-90 90]);
    xlim(myAxis,[-90 90]);
    colorbar;
end

%

yaml.dumpFile('config/mpTable.yaml',mpTable,"auto");

%

% create a spreadsheet for Byron
xlsxT=array2table([mpTable.Lookup.azAngles_d;mpTable.Lookup.azmpRatio;mpTable.Lookup.elAngles_d;mpTable.Lookup.elmpRatio]','VariableNames',{'azDeg','azMpRatio','elDeg','elMpRatio'});

writetable(xlsxT,"mpTable.xlsx");