% Sweep DARM (differential arm length) in DRFPMI and generate plots.
%
% [xPos, sigDC, fDC] = sweepDARM(Nsweep)
% see sweepDC for information about the parameters
%
% This displays some DC signals, and some demod signals, during
% a DARM sweep.  The AS_I2 and Q2 signals are used for DARM, but
% the phase at DC is only part of the story (see respDARM).

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

% Check number of input arguments
  if nargin < 1
    Nsweep = 1000;
  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)];

  % SET SOME DETUNINGS TO MIMIC LOCK AQ STATUS REACHED BY RUNNING
  % smoo_prfpmi (T_SRM = 1)
  
  
  %%%%%%%%%%%%%%%%%%%%%%%%%ARMS AT FULL RESONANCE
      % XARM DeToon in degrees
  dTunePhase_XARM = 0; %.07; % XARM micro detuning (see note) (.07 = 1/2 fringe )
  % set position offsets for XARM
  dTune = dTunePhase_XARM * (opt.lambda / 360);
  opt = setPosOffset(opt, nEX,  dTune); %opt.lambda / 4 +

     % YARM DeToon in degrees
  dTunePhase_YARM = 0; %.07; % YARM micro detuning (see note)
  % set position offsets for YARM
  dTune = dTunePhase_YARM * (opt.lambda / 360);
  opt = setPosOffset(opt, nEY,  dTune); %opt.lambda / 4 +
  %%%%%%%%%%%%%%%%%%%%%%%%%%%
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%MICH AT 0.8 of Dark fringe
  
      % MICH DeToon in degrees
  dTunePhase_MICH = 45; % MICH micro detuning (see note)
  % set position offsets for MICH
  dTune = dTunePhase_MICH * (opt.lambda / 360);
  opt = setPosOffset(opt, nBS,  dTune); %opt.lambda / 4 +
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%PRC ON RESONANCE FOR CARRIER (CARRIER POWER BUILDS UP) 
         % PRC DeToon in degrees
   dTunePhase_PRC = -32; % PRC micro detuning (see note)
   % set position offsets for PRC
   dTune = dTunePhase_PRC * (opt.lambda / 360);
   opt = setPosOffset(opt, nPR,  dTune); %opt.lambda / 4 +

   
  %%%%%%%%%%%%%%%%%%%%%%%%%%% 
         % DARM DeToon in degrees
   dTunePhase_DARM = 0;%^.045; % PRC micro detuning (see note)
   % set position offsets for PRC
   dTune = dTunePhase_DARM * (opt.lambda / 360);
   opt = setPosOffset(opt, nEX,  dTune); %opt.lambda / 4 +
   opt = setPosOffset(opt, nEY,  -dTune);
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
  
  % DARM sweep
  pos = zeros(opt.Noptic, 1);
  pos(nEX) = 5e-10;
  pos(nEY) = -5e-10;
  
  [xPos, sigDC, fDC] = sweepDC(opt, -pos, pos, Nsweep);
  
    REFL166 = sigDC(nREFL(8:9),:)/30;
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%FIGURES
  
  figure(1)
  
  subplot(311)
  semilogy(xPos(nEX, :), sigDC(nDC, :))
  legend(prbNames(nDC))
  title('DARM sweep')
  xlabel('DARM Detuning [m]')
  grid on

  subplot(312)
  plot(xPos(nEX, :), sigDC(nAS(8:9), :))
  legend(prbNames(nAS(8:9)))
  xlabel('DARM Detuning [m]')
  grid on
 
  subplot(313)
  plot(xPos(nEX, :), sigDC(nAS(8), :)./(sigDC(nDC(5), :).^(1/2)),...
      xPos(nEX, :), sigDC(nAS(9), :)./(sigDC(nDC(5), :)).^(1/2))

      legend('ASI2/sqrt(TRX)','ASQ2/sqrt(TRX)')
  xlabel('DARM Detuning [m]')
  grid on

 figure(2)
 subplot(211) 
 plot(xPos(nEX, :), sigDC(nPOX(2:end), :))
  legend(prbNames(nPOX(2:end)))
  title('DARM sweep: POX demod signals')
    xlabel('DARM Detuning [m]')
  grid on


  subplot(212) 
 plot(xPos(nEX, :), sigDC(nREFL(2:end), :))
  legend(prbNames(nREFL(2:end)))
  title('DARM sweep: REFL demod signals')
    xlabel('DARM Detuning [m]')
  grid on
  
      figure(5)
  
  subplot(211)
  %subplot('position',[0.05 0.06 0.9 0.4])
  plot(xPos(nEX, :), [sigDC(nREFL(2:3), :)],...
       xPos(nEX, :), REFL166(1:2,:),'--',...
       xPos(nEX, :), [sigDC(nAS(2:end), :)])
  legend(prbNames{nREFL(2:3)},'REFL 166I','REFL 166Q ',prbNames{nAS(2:end)})
  xlabel('MICH motion [nm]',...
         'FontWeight','bold')
  %axis([pos(nPR)*1e9 -pos(nPR)*1e9 -3e-3 3e-3])
  grid on
  
  
    subplot(212)
%  subplot('position',[0.05 0.06 0.9 0.4])
  plot(xPos(nEX, :), sigDC(nAS(1), :))
  legend(prbNames{nAS(1)})
  xlabel('MICH motion [m]',...
         'FontWeight','bold')
  %axis([pos(nPR)*1e9 -pos(nPR)*1e9 -3e-4 3e-4])
  grid on
  
%       subplot(313)
% %  subplot('position',[0.05 0.06 0.9 0.4])
%   plot(xPos(nEX, :), sigDC(nAS(1), :))
%   legend(prbNames{nAS(1)})
%   xlabel('MICH motion [m]',...
%          'FontWeight','bold')
%   %axis([pos(nPR)*1e9 -pos(nPR)*1e9 -3e-4 3e-4])
%   grid on