% Read engineering Data produced
% by the PRIMA Metrology Phase-Meter LCU lprmac
% Read photodetector DC, RMS and fringe visibility (from fringe board)

% Created: SLE, Feb'10
%               Nov'10: SLE, define figure positions on the screen

% Structure of the file:
% Date/Start time: 2009-07-25T15.08.01.746051
% Sampling frequency in Hz: 8000
% Configuration:
% CHA: UNDEF
%   Ip3 (+38.65 MHz)
%   Ip1 (+38.00 MHz)
% CHB: FSU B
%   Ip4 (-39.55 MHz)
%   Ip2 (-40.00 MHz)
% Laser Frequency (nm): 1319.00
% Frequency Shift (MHz): 78.00
% Index of refraction: n=1
% Number of Samples: 2
% Definition of the columns: RelativeTime,DC_REF_450kHz,RMS_REF_450kHz,V_REF_450kHz,DC_REF_650kHz,RMS_REF_650kHz,V_REF_650kHz,DC_PROBE_450kHz,RMS_PROBE_450kHz,V__PROBE450kHz,DC_PROBE_650kHz,RMS_PROBE_650kHz,V_PROBE_650kHz
% 
% 0.668837,0,0.0003946,0,0,0,0,0,7.175e-05,0,0,0,0
% 1.668837,0,0.0004185,0,0.0001568,0,0,0,4.783e-05,0,0,0,0


% Get the right file
%----------------------
clear all
close all
pack

[FileName, PathName] = uigetfile('lprma*Fringe*', 'Click on the data File to process'); 
eval([ 'cd ''',PathName,'''']);
fid = fopen(FileName,'r');



% Read Header
%----------------------
    %first line
[date_time_txt,count]=fscanf(fid,'%% Date/Start time: %s',[1,1]);
fgets(fid);% line 2  (alternative line2=fgets(fid), in that case line2 contains the test of line 2
[fs,count]=fscanf(fid,'%% Sampling frequency in Hz: %f',[1,1]);
    % Jump line 3 to line 10
      for i=3:10;
          fgets(fid);
      end
[lambda,count]=fscanf(fid,'%% Laser Frequency (nm): %f',[1,1]);
lambda=lambda*1.e-9;% wavelength in meter
fgets(fid);%line11
[delta_nu,count]=fscanf(fid,'%% Frequency Shift (MHz): %f',[1,1]);
delta_nu=delta_nu*1.e6;% frequency shift in Hz
fgets(fid);%line12
[n,count]=fscanf(fid,'%% Index of refraction: n=%f',[1,1]);
fgets(fid);%line13
[N_samples,count]=fscanf(fid,'%% Number of Samples: %d',[1,1]);
fgets(fid);%line14
fgets(fid);%line15-> after modif from Robert to include description of columns

fprintf('\n')
fprintf(' **************** \n')
fprintf(' Phase Meter Data \n')
fprintf(' ==> Filename: %s \n    Sampling frequency= %.0f Hz \n    Number of samples= %.0f \n',FileName,fs,N_samples);

% Read data
%----------------
[data,count]=fscanf(fid,'%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f',[13,inf]);
fclose(fid);

seconds = data(1,:)';
DC_REF450= data(2,:)';
RMS_REF450 = data(3,:)';
V_REF450 = data(4,:)';
V_REF450_calc=sqrt(2)*RMS_REF450./DC_REF450;

DC_REF650= data(5,:)';
RMS_REF650 = data(6,:)';
V_REF650 = data(7,:)';
V_REF650_calc=sqrt(2)*RMS_REF650./DC_REF650; %check that recorded V is correctly computed


DC_PROBE450= data(8,:)';
RMS_PROBE450 = data(9,:)';
V_PROBE450 = data(10,:)';
V_PROBE450_calc=sqrt(2)*RMS_PROBE450./DC_PROBE450;

DC_PROBE650= data(11,:)';
RMS_PROBE650 = data(12,:)';
V_PROBE650 = data(13,:)';
V_PROBE650_calc=sqrt(2)*RMS_PROBE650./DC_PROBE650;

% check for block lost: NaN in data data or fs mismatch at 5% level
% and generate warning window
    if length(seconds)~=N_samples
       fprintf(' WARNING for NaN: Check  block lost  \n')
       warndlg('WARNING for NaN: Check  block lost ','!! Warning !!')
    end

    if (1/mean(diff(seconds))>=fs*(1+0.05) | 1/mean(diff(seconds))<=fs*(1-0.05))
     fprintf(' WARNING for fs: fs written in header and computed from data set do not match  \n')
     warndlg('WARNING for fs: fs written in header and computed from data set do not match ','!! Warning !!')
     end


% Plot Data
%-------------------
 Nfig=8;
 scrsz = get(0,'ScreenSize'); %[1 1 width height]
 SizeFig=[scrsz(3)/(Nfig/2) scrsz(4)/2.5];%[width height]



h=figure(1);set(h,'Position',[1 scrsz(4)/2 SizeFig]); %[left bottom width height]
set(gca,'FontSize',13);
plot(seconds,DC_REF450,'b.-',seconds,DC_REF650,'c.-',seconds,DC_PROBE450,'r.-',seconds,DC_PROBE650,'m.-');grid
xlabel('time in sec');ylabel('PRIMET DC in Volt');
axis([0 length(seconds) 0 1])
legend('DC REF 450','DC REF 650','DC PROBE 450','DC PROBE 650');
title({['Fringes   ', date_time_txt],['Fs=',num2str(fs), ' Hz']})

h=figure(2);set(h,'Position',[SizeFig(1) scrsz(4)/2 SizeFig]);
set(gca,'FontSize',13);
plot(seconds,RMS_REF450,'b.-',seconds,RMS_REF650,'c.-',seconds,RMS_PROBE450,'r.-',seconds,RMS_PROBE650,'m.-');grid
xlabel('time in sec');ylabel('PRIMET RMS in Volt');
axis([0 length(seconds) 0 1])
legend('V REF 450','V REF 650','V PROBE 450','V PROBE 650');
title({['Fringes   ', date_time_txt],['Fs=',num2str(fs), ' Hz']})

h=figure(3);set(h,'Position',[2*SizeFig(1) scrsz(4)/2 SizeFig]);
set(gca,'FontSize',13);
plot(seconds,V_REF450,'b.-',seconds,V_REF650,'c.-',seconds,V_PROBE450,'r.-',seconds,V_PROBE650,'m.-');grid
xlabel('time in sec');ylabel('PRIMET Visibility');
axis([0 length(seconds) 0 1])
legend('V REF 450','V REF 650','V PROBE 450','V PROBE 650');
title({['Fringes   ', date_time_txt],['Fs=',num2str(fs), ' Hz']})

h=figure(4);set(h,'Position',[3*SizeFig(1) scrsz(4)/2 SizeFig]);
set(gca,'FontSize',13);
plot(seconds,V_REF450-V_REF450_calc,'b.-',seconds,V_REF650-V_REF650_calc,'c.-',seconds,V_PROBE450-V_PROBE450_calc,'r.-',seconds,V_PROBE650-V_PROBE650_calc,'m.-');grid
xlabel('time in sec');ylabel(' Visibility-VisibilityCalc');
axis([0 length(seconds) 0 1])
legend('V REF 450','V REF 650','V PROBE 450','V PROBE 650');
title({['CHECK COMPUTATION Fringes   ', date_time_txt],['Fs=',num2str(fs), ' Hz']})


