%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function reads the QE (Quantum Efficiency) file data and       %
% and plots the results.                                              %
%                                                                     %
% Author: Mihai Cara, mcara@purdue.edu                                %
% Last Updated by Mihai Cara on 06/29/2004                            %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Reading file for the quantum efficiency test
function [avqe, figh, UserID, TestDate, TubeManufacturer,...
        TubeModel, SerialNumber, TubeGain, HighVoltage, LSPower]...
    = qe(fd, file_ver)  %the function for running the quantum efficiency test
fgetl(fd); % reads the space
fgetl(fd); % reads the info section (** Info: **)
UserID = fgetl(fd);% reads the user identification
TestDate = fgetl(fd);%reads the test date
TubeManufacturer = fgetl(fd);%reads the manufacturer of the tube
TubeModel = fgetl(fd);%reads the model of the tube
SerialNumber = fgetl(fd);%reads the serial number
TubeGain = fgetl(fd);%reads the gain of the tube
Comments = fgetl(fd);%reads the comments section related to the experiment
HighVoltage = fgetl(fd);%reads the high voltage setting
LSPower = fgetl(fd);%reads the Light Source Power setting

switch file_ver
case 1
    nl_skip = 62;
case 2
    nl_skip = 64;
case 3
    nl_skip = 66;
case 4
    nl_skip = 67;
otherwise
    disp('Unknown File Version!');
    return;
end

for i=1:nl_skip
    s = fgetl(fd); % reading of the rest of the header
end
temp = fscanf(fd, 'Data Section. Rows: %d, Columns: %d');

N = temp(1); % number of rows = size(Y)
M = temp(2); % number of columns = size(X)

%M=M+2; % to remove when C program is fixed

s = fgetl(fd); % skip line
s = fgetl(fd); % skip line

data = abs(reshape(fscanf(fd, '%g', N*M), M, N))'; % arranges data in a N*M matrix

fclose(fd); % closes the program that read the file

% set(gcf,'Position',[25 25 800 600]); %sets the positon and the dimensions 
% of the window that displays the final information

% The plot (as an erroebar) of the quantum efficiency vs. the wavelength (in nm) 
% and the error of the quantum efficiency

% subplot(1,2,1);
scrnsz = get(0,'ScreenSize');
figh = figure('Position', [5 42 scrnsz(3)-10 scrnsz(4)-100]);

errorbar(data(:,1), data(:,8), data(:,9), 'bx');
% AXIS control:
set(gca,'YScale','log');
% set(gca,'YTick',[1:50]);
% set(gca,'YTickLabel',{'1';'';'';'';'5';'';'';'';'';'10';...
%     '';'';'';'';'15';'';'';'';'';'20';'';'';'';'';'25';'';'';'';'';'30';...
%     '';'';'';'';'35';'';'';'';'';'40';'';'';'';'';'45';'';'';'';'';'50'});
% set(gca,'YTick',[1 5 10 15 20 25 30 35 40 45 50]);
% set(gca,'YTickLabel',{'1';'5';'10';'15';'20';'25';'30';'35';'40';'45';'50'});

if (max(data(:,8)) < 50)
    set(gca,'YTick',[2 5 10 20 25 30 40]);
    set(gca,'YTickLabel',{'2';'5';'10';'20';'25';'30';'40'});
    maxy = 50;
else
    set(gca,'YTick',[2 5 10 20 25 30 40 50 60 70 80 90]);
    set(gca,'YTickLabel',{'2';'5';'10';'20';'25';'30';'40';...
            '50';'60';'70';'80';'90'});
    maxy = 50;
end

set(gca, 'YGrid',     'on')
set(gca, 'XGrid',     'on')
set(gca, 'GridLineStyle',':');

xlabel('Wavelength (nm)')
ylabel('Efficiency (%)')
title('Efficiency for different wavelenghts')
plot_axis = axis;
plot_axis(1) = min(data(:,1));
plot_axis(2) = max(data(:,1));
plot_axis(3) = 2;
plot_axis(4) = 50;
axis(plot_axis);

% axis tight
% axis([min(data(:,1)) max(data(:,1)) 0 10*ceil(max(data(:,8))/10)]);

avqe = 0;
n    = 0;
for i = 1:size(data(:,1))
    if (data(i,1)>=380 & data(i,1)<=450)
        avqe = avqe + data(i,8);
        n = n + 1;
    end
end
avqe = avqe / n;

xc = (min(data(:,1))+max(data(:,1)))/2;
% xc = xc - (max(data(:,1))-min(data(:,1)))/6; % for linear y-axis
xc = xc - (max(data(:,1))-min(data(:,1)))/4;
yb = 5*ceil(max(data(:,8))/10)/10; % half of y-axis height and / by 10 - number of lines to output +2
% y =  3*ceil(max(data(:,8))/10); % for linear y-axis
y =  1.5*ceil(max(data(:,8))/10);
y = 5;
text(xc, y, sprintf('%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n\n%s\n%s%.3g (%%)',...
    '******** INFO: ********', TestDate, TubeManufacturer, ...
    TubeModel, SerialNumber, HighVoltage, LSPower, TubeGain, ...
    '******** STAT: ********', 'Av. QE (380-450nm): ~', avqe),'BackgroundColor',[0.9 0.9 0.9]);
