% SWEEPSRC (signal recycling cavity length) in DRFPMI and generate plots.
%
% [xPos, sigDC, fDC] = sweepSRC(Nsweep)
% see sweepDC for information about the parameters
%
% This displays some DC signals, and some demod signals, during
% a SRC sweep.  From this sweep, the phases of the POX_IP and POX_IM
% double-demod signals can be set to give the correct zero crossing.

function [xPos, sigDC, fDC] = sweepSRC(Nsweep)

  % Check number of input arguments
  if nargin < 1
    Nsweep = 100;
  elseif nargin > 1
    error('Too Many Arguments!')
  end

  % prepare some variables
  sweepSetup

  % adding this to sweepSetup causes matlab to crash, so do it here
  nDC = [nREFL(1) nAS(1) nPOX(1) nPOY(1) nTRX(1) nTRY(1)];

  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % SRCL sweep
  pos = zeros(opt.Noptic, 1);
  pos(nSR) = 50e-9;
  
  
  [xPos, sigDC, fDC] = sweepDC(opt, -pos, pos, Nsweep);

  % compute phase errors for double-demod signals
  Nmid = find(xPos(nSR, :) >= -1e-19, 1);
  phaseM = 180 * atan2(sigDC(nPOX(4), Nmid), sigDC(nPOX(5), Nmid)) / pi;
  phaseP = 180 * atan2(sigDC(nPOX(6), Nmid), sigDC(nPOX(7), Nmid)) / pi;
  checkPhases = (phaseM > 1) | (phaseP > 1);
  str = sprintf('double demod phase errors: %4.3f deg for %s, %4.3f deg for %s', ...
                phaseM, prbNames{nPOX(4)}, phaseP, prbNames{nPOX(6)});
  disp(str)
  phaseM = opt.probe(nPOX(4)).phase - phaseM;
  phaseP = opt.probe(nPOX(6)).phase - phaseP;
  str = sprintf('            good phases: %4.3f deg for %s, %4.3f deg for %s', ...
                phaseM, prbNames{nPOX(4)}, phaseP, prbNames{nPOX(6)});
  disp(str)
  phase166 = 180 * atan2(sigDC(nREFL(8), Nmid), sigDC(nREFL(9), Nmid)) / pi; ...
  disp('   ')
  
  if (~checkPhases)
    disp('Demod Phases are good enough')
  else
    disp('Fix demod phases in opt file')
  end
  disp('    ')
  
  % make some plots
  fDCp = permute(fDC, [2, 3, 1]);  
  n = getFieldAt(opt, nSR, 'fr');		% SR front input field

  PODD = sigDC(nPOX(4:5), :) + sigDC(nPOX(6:7), :);
  REFL166 = sigDC(nREFL(8:9),:)/3e4;
  
  subplot('position',[0.05 0.475 0.9 0.45])
  semilogy(xPos(nSR, :), abs(fDCp(nSBa, :, n)), ...
           xPos(nSR, :), abs(fDCp(nSBb, :, n)),'--')
  title('SRC sweep: RF powers (top) & Demods (bottom)',...
        'FontWeight','bold',...
        'FontSize',18)
  legend(num2str([vFrf(nSBa); vFrf(nSBb)] / 1e6))
  grid on
  axis([-pos(nSR) pos(nSR) 1e-4 .3])
  set(gca,'XTickLabel',[])
  %lineAtX(0, 0.3 * [1 1 1]);
  
  subplot('position',[0.05 0.06 0.9 0.4])
  plot(xPos(nSR, :)*1e9, sigDC(nPOX(4:7), :)*1e6,...
       xPos(nSR, :)*1e9, [PODD; REFL166]*1e6,'--')
  legend(prbNames{nPOX(4:7)}, 'POX IM + IP', 'POX QM + QP','REFL166 I','REFL166 Q')
  xlabel('SRM motion [nm]',...
                 'FontWeight','bold')
  grid on
  axis([-pos(nSR)*1e9 pos(nSR)*1e9 -1 1])
  %lineAtX(0, 0.3 * [1 1 1]);

  % Error routine for output arguments
  if nargout > 3
    error('Too Many Output Arguments. Max = 3')
  end
  
  orient tall
  print -dpdf SRCsweep-40m.pdf
  
