% H1_PRMI_RH_POP18.m

clear all

FPPRMIinfile=('H1_PRMI_RH_POP18.kat');
FPPRMIoutfile=('H1_PRMI_RH_POP18_mat.kat');

FT=FT_init_Finesse();

ITMXlenspows=linspace(-15e-6,15e-6,9)+1e-20;
ITMXlensfs=1./ITMXlenspows;

% Find initial tunings for each ITMX lens power and then run over ITMY lens
% powers with the locks engaged

%for k=1:10
for k=1:length(ITMXlensfs);
    tic
    % MICH tuning
    inblocks=FT_read_blocks_from_file(FPPRMIinfile);
    outblocks=inblocks(1:13);
    outblocks(14)=inblocks(16);
    outblocks(15)=inblocks(18);
    outblocks(10)=FT_replace_const_in_blocks(outblocks(10),'nonTLfX',ITMXlensfs(k));
    FT_write_blocks_into_file(outblocks,FPPRMIoutfile);
    MICHtuningrun=FT_create_new_kat_run();
    MICHtuningrun.filename=FPPRMIoutfile;
    MICHtuningrun=FT_run_kat_simulation(FT,MICHtuningrun);
    REFL45Q=abs(MICHtuningrun.y(:,2));
    [MICHtuningval,MICHtuningel]=min(REFL45Q);
    MICHtuning=MICHtuningrun.x(MICHtuningel);
    clear outblocks
    % PRCL tuning
    outblocks=inblocks(1:13);
    outblocks(14)=inblocks(15);
    outblocks(15)=inblocks(16);
    outblocks(16)=inblocks(19);
    outblocks(10)=FT_replace_const_in_blocks(outblocks(10),'nonTLfX',ITMXlensfs(k));
    outblocks(11)=FT_replace_const_in_blocks(outblocks(11),'phi_ITMX',MICHtuning);
    outblocks(11)=FT_replace_const_in_blocks(outblocks(11),'phi_ITMY',-MICHtuning);
    FT_write_blocks_into_file(outblocks,FPPRMIoutfile);
    PRCLtuningrun=FT_create_new_kat_run();
    PRCLtuningrun.filename=FPPRMIoutfile;
    PRCLtuningrun=FT_run_kat_simulation(FT,PRCLtuningrun);
    REFL45I=abs(PRCLtuningrun.y(:,1));
    [PRCLtuningval,PRCLtuningel]=min(REFL45I);
    PRCLtuning=PRCLtuningrun.x(PRCLtuningel);
    % RH tuning
    clear outblocks
    outblocks=inblocks(1:17);
    outblocks(10)=FT_replace_const_in_blocks(outblocks(10),'nonTLfX',ITMXlensfs(k));
    outblocks(11)=FT_replace_const_in_blocks(outblocks(11),'phi_ITMX',MICHtuning);
    outblocks(11)=FT_replace_const_in_blocks(outblocks(11),'phi_ITMY',-MICHtuning);
    outblocks(11)=FT_replace_const_in_blocks(outblocks(11),'phi_PRM',PRCLtuning);
    FT_write_blocks_into_file(outblocks,FPPRMIoutfile);
    RHtuningrun(k)=FT_create_new_kat_run();
    RHtuningrun(k).filename=FPPRMIoutfile;
    RHtuningrun(k)=FT_run_kat_simulation(FT,RHtuningrun(k));
    save('lockedPRMIbuildupdata2','RHtuningrun');
    time=toc;
    timeremaining=(time*(length(ITMXlensfs)-k))/60;
    sprintf('Time remaining roughly %g minutes',timeremaining)
end


%%

load('lockedPRMIbuildupdata2');
POP18I=zeros(length(RHtuningrun),length(RHtuningrun(1).y(:,1)));
POP18Q=zeros(length(RHtuningrun),length(RHtuningrun(1).y(:,1)));
ASport=zeros(length(RHtuningrun),length(RHtuningrun(1).y(:,1)));
REFL45I=zeros(length(RHtuningrun),length(RHtuningrun(1).y(:,1)));
REFL45Q=zeros(length(RHtuningrun),length(RHtuningrun(1).y(:,1)));
PRCLlock=zeros(length(RHtuningrun),length(RHtuningrun(1).y(:,1)));
MICHlock=zeros(length(RHtuningrun),length(RHtuningrun(1).y(:,1)));

for p=1:length(RHtuningrun)
    
POP18I(p,:)=RHtuningrun(p).y(:,3);
POP18Q(p,:)=RHtuningrun(p).y(:,4);
ASport(p,:)=RHtuningrun(p).y(:,5);
REFL45I(p,:)=RHtuningrun(p).y(:,2);
REFL45Q(p,:)=RHtuningrun(p).y(:,3);
PRCLlock(p,:)=RHtuningrun(p).y(:,11);
MICHlock(p,:)=RHtuningrun(p).y(:,12);
end
ITMYRHPower=-RHtuningrun(1).x/13.6e-6;

map=colormap;
uW=1e6;
figure()
for s=1:length(RHtuningrun)
plot(ITMYRHPower,POP18I(s,:)'*uW,'color',map(s*round(64/length(RHtuningrun)),:))
hold on
plot(ITMYRHPower,POP18Q(s,:)'*uW,'color',map(s*round(64/length(RHtuningrun)),:),'LineStyle',':')
plot(ITMYRHPower,ASport(s,:)'*uW*1e-3,'color',map(s*round(64/length(RHtuningrun)),:),'LineStyle','--')
end
grid on
axis tight
legend('POP18 I signal','POP18 Q signal','AS port DC power /1000','Location','NorthWest')
xlabel('ITMY ring heater power [W]')
ylabel('Signal power [\muW]')
title('H1 SB PRMI signals with ITMY RH, various ITMX nonTL values (blue = -15\muD to brown = +15\muD)')
%plotalltitle('H1SBPRMIPOPs')


%%