% SWEEPMICH (Michelson length) in DRFPMI and generate plots.
%
% [xPos, sigDC, fDC] = sweepMICH(Nsweep)
% see sweepDC for information about the parameters
%
% This displays some DC signals, and some demod signals, during
% a MICH sweep.  

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

  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
% 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 +
%    
%   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
  % MICH sweep
  pos = zeros(opt.Noptic, 1);
  pos(nPR) = -65e-9*3;
  pos(nBS) = 100e-9*3;
  pos(nSR) = 65e-9*3;
  
  [xPos, sigDC, fDC] = sweepDC(opt, -pos, pos, Nsweep);

  % compute phase errors for double-demod signals
  Nmid = find(xPos(nBS, :) >= -1e-16, 1);
  phaseM = 180 * atan2(sigDC(nAS(5), Nmid), sigDC(nAS(4), Nmid)) / pi;
  phaseP = 180 * atan2(sigDC(nAS(7), Nmid), sigDC(nAS(6), 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{nAS(4)}, phaseP, prbNames{nAS(6)});
  disp(str)
  phaseM = opt.probe(nAS(4)).phase - phaseM;
  phaseP = opt.probe(nAS(6)).phase - phaseP;
  str = sprintf('            good phases: %4.3f deg for %s, %4.3f deg for %s', ...
                phaseM, prbNames{nAS(4)}, phaseP, prbNames{nAS(6)});
  disp(str)
  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, nPR, 'fr')		% PR front input field
  n = 9;
  sDD = sigDC(nAS(4:5), :) + sigDC(nAS(6:7), :);
  REFL166 = sigDC(nREFL(8:9),:)/30;
  
  figure(1)
 % subplot('position',[0.05 0.475 0.9 0.45])
 subplot(311)
 semilogy(xPos(nBS, :), abs(fDCp(nSBa, :, n)))%, ...
          % xPos(nBS, :), abs(fDCp(nSBb, :, n)),'--')
  title('MICH sweep: RF powers (top) & Demods (bottom)',...
        'FontWeight','bold',...
        'FontSize',18)
  legend(num2str([vFrf(nSBa)] / 1e6))%;% vFrf(nSBb)] / 1e6))
  grid on
  %axis([pos(nPR) -pos(nPR) 1e-3 1])
  set(gca,'XTickLabel',[])
 
  subplot(312)
%  subplot('position',[0.05 0.06 0.9 0.4])
  plot(xPos(nBS, :), [sigDC(nAS(2:7), :); sDD],...
       xPos(nBS, :), REFL166(1,:),'--')
  legend(prbNames{nAS(2:7)}, 'AS IM + IP', 'AS QM + QP','REFL 166 I')
  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(nBS, :), 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
  
  figure(4)
  subplot(211)
%  subplot('position',[0.05 0.475 0.9 0.45])
  semilogy(xPos(nBS, :), abs(fDCp(nSBa, :, n)), ...
           xPos(nBS, :), abs(fDCp(nSBb, :, n)),'--')
  title('MICH sweep: RF Fields (top) & Demods (bottom)',...
        'FontWeight','bold',...
        'FontSize',18)
  legend(num2str([vFrf(nSBa); vFrf(nSBb)] / 1e6))
  grid on
 % axis([pos(nPR) -pos(nPR) 1e-3 1])
  set(gca,'XTickLabel',[])
 
  subplot(212)
 % subplot('position',[0.05 0.06 0.9 0.4])
  plot(xPos(nBS, :), [sigDC(nREFL(1:7), :); sDD],...
       xPos(nBS, :), REFL166(1,:),'--')
  legend(prbNames{nREFL(1:7)}, 'AS IM + IP', 'AS QM + QP','REFL 166 I')
  xlabel('MICH motion [m]',...
         'FontWeight','bold')
  %axis([pos(nPR)*1e9 -pos(nPR)*1e9 -3e-3 3e-3])
  grid on
  
  
  
    figure(5)
    subplot(311)
%  subplot('position',[0.05 0.475 0.9 0.45])
  semilogy(xPos(nBS, :), abs(fDCp(nSBa, :, n)))%, ...
 %          xPos(nBS, :), abs(fDCp(nSBb, :, n)),'--')
  title('MICH sweep: RF Fields (top) & Demods (bottom)',...
        'FontWeight','bold',...
        'FontSize',18)
  legend(num2str([vFrf(nSBa)]/1E6));% vFrf(nSBb)] / 1e6))
  grid on
 % axis([pos(nPR) -pos(nPR) 1e-3 1])
  set(gca,'XTickLabel',[])
  
  subplot(312)
  %subplot('position',[0.05 0.06 0.9 0.4])
  plot(xPos(nBS, :), [sigDC(nREFL(2:3), :)],...
       xPos(nBS, :), REFL166(1,:)*100,'--',...
       xPos(nBS, :), [sigDC(nAS(3), :)])
  legend(prbNames{nREFL(2:3)},'REFL 166 I X 100',prbNames{nAS(3)})
  xlabel('MICH motion [nm]',...
         'FontWeight','bold')
  %axis([pos(nPR)*1e9 -pos(nPR)*1e9 -3e-3 3e-3])
  grid on
  
  
    subplot(313)
%  subplot('position',[0.05 0.06 0.9 0.4])
  plot(xPos(nBS, :), 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
  
  % Error routine for output arguments
  if nargout > 3
    error('Too Many Output Arguments. Max = 3')
  end
  
  orient tall
  print -dpdf MICHsweep-40m.pdf
  
