clear all;
% ~40mW injected into in-air fiber, ~27.5mW out of in-vac.

% transmissivity of BS, including 2nd beam from AR, is T0.
% reflectivity of BS, including 2nd beam from AR, is R0.

% Thorlabs PD sensitivity is A volt/W with 40dB gain, A/10 with 20dB gain.
% Power meter sensitivity (of analog out) is B volt/W with 3mW "range",
% 10B with 300uW "range".
% (If we use power reading of the power meter instead, we can ignore the power meter
% sensitivity.)

% 1st measurement, PD in transmission with 50dB gain, PM in refl with 300uW
% range.
bT1=3.48; %bright Trans measured by Thorlabs
bR1=0.232; % bright Refl measured by power meter
dT1=38.5e-3; % dark Trans
dR1=14.4e-3; % dark Refl, this is very much suspicious because it's huge.
% I suspect that this was read 

bR1_pwr=0.695e-3; % bright Refl read by power meter in Watts, 3mW range.
dR1_pwr=5e-6;% dark refl read by power meter in Watts, auto range (probably 30uW range?).
% We'll take these Watt numbers because dark voltage was suspicious.

T11=bT1-dT1; % If the total power impinging BS is P1, this is A*T0*P1.
R11=bR1-dR1; % and this is B*R0*P1.
R11_pwr=bR1_pwr-dR1_pwr;
TRRatioRaw1=T11/R11; % this is A*T0/B/R0.
TRRatioRaw1_pwr = T11/R11_pwr; % this is A*T0/R0.

% 2nd measurement, PD in REFL with 30dB gain, PM in trans with 30uW
% range.
bT2=.245; %bright Trans measured by power meter
bR2=3.29; % bright Refl measured by Thorlabs
dT2=270e-6; % dark Trans
dR2=25.1e-3; % dark Refl
bT2_pwr=73.6e-6; %Watt
dT2_pwr=82.8e-9;

T22=bT2-dT2; % If the total power impinging BS is P2, this is 10B*T0*P2.
T22_pwr=bT2_pwr-dT2_pwr;

R22=bR2-dR2; % and this is A/10*R0*P2.
TRRatioRaw2=T22/R22; % This is 100*B*T0/A/R0.
TRRatioRaw2_pwr=T22_pwr/R22; % This is 10*T0/A/R0.

T0R0Ratio=sqrt(TRRatioRaw1*TRRatioRaw2/100); %This is T0/R0.
T0R0Ratio_pwr=sqrt(TRRatioRaw1_pwr*TRRatioRaw2_pwr/10); %This is also T0/R0.

% Assuming no loss, R0=1-T0, T0/(1-T0)=T0R0Ratio,
% T0=T0R0Ratio/(1+T0R0Ratio).
T0=T0R0Ratio/(1+T0R0Ratio); % T0=.0982
R0=1-T0; % R0=.9018

T0_pwr=T0R0Ratio_pwr/(1+T0R0Ratio_pwr); % T0=.0958
R0_pwr=1-T0_pwr; % R0=.9042

% According to https://dcc-lho.ligo.org/E1700102, AR reflection is 0.1% so
% I just ignore the effect. This is totally negligible compared to the
% accuracy of the pd and power meter.