% Sweep PRC (power recycling cavity length) in DRFPMI and generate plots.
%
% [xPos, sigDC, fDC] = sweepPRCL(Nsweep)
% see sweepDC for information about the parameters
%
% This displays some DC signals, and some demod signals, during
% a PRC sweep.  From this sweep, one might try to find a good
% signal for PRC.

function [xPos, sigDC, fDC] = PSL-OSA-OSA(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)];

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

    

  % make some plots
  fDCp = permute(fDC, [2, 3, 1]);  
  n = getFieldAt(opt, nPR, 'fr');		% PR front input field

  subplot('position',[0.05 0.48 0.9 0.45])
  semilogy(xPos(nPR, :), abs(fDCp(nSBa, :, n)), ...
           xPos(nPR, :), abs(fDCp(nSBb, :, n)), '--')
  title('PRC sweep: RF powers in the PRC (top) & REFL demods (bottom)')
  legend(num2str([vFrf(nSBa); vFrf(nSBb)] / 1e6))
  set(gca,'XTickLabel',[])
  grid on
  axis([-pos(nPR) pos(nPR) 1e-4 .5])
  
  subplot('position',[0.05 0.06 0.9 0.4])
  plot(xPos(nPR,:)*1e9, sigDC(nREFL(2:7), :),...
       xPos(nPR,:)*1e9, [sigDC(nREFL(8:end),:);REFLDD(1,:)],'--')
  legend(prbNames(nREFL(2:end)), 'REFL IM + IP')
  grid on
  axis([-pos(nPR)*1e9 pos(nPR)*1e9 -0.01 0.01])
  xlabel('PRC motion [nm]')
  
  % Error routine for output arguments
  if nargout > 3
    error('Too Many Output Arguments. Max = 3')
  end
  
  orient tall
  print -dpdf PRCsweep-40m.pdf
  
  
