function [gpsStartMin,alpha,beta]=read_alphabetafile(filename,gpsRange,duration)
%
% Reads a file containing the alpha and beta coefficients
% and returns them in an array.
%
%

% Assign Boolean values (note not the usual ones)
true = 0;
false = ~0;
    
% Input data checking
if nargin ~= 3
  error('[gpsStartSec,alpha,beta] = read_alphabetafile(filename,gpsRange,durationSec)');
end

%filename = varargin{1};
%gpsStart = varargin{2};
%durationSec = varargin{3};

%P  IF # columns in GPS Range entry = 1 (indicating #,#)
%P      IF there is a duration numeric entry
%P          SET gpsStartSec to first entry
%P          SET durationSec to second entry
%P          SET GPS range flag
%P          END
%P      ELSE
%P          SET gpsOffsetSec to 1st element of input vector
%P          SET gpsEndSec to last element of input vector
%P          SET durationSec = gpsEndSec - gpsOffsetSec
%P      ENDIF
%P  ENDIF
    isGpsRange = false;
    [mrows,ncols] = size(gpsRange);
    if(ncols == 1)
        if(nargin>2)
            if(isa(duration,'numeric'))
                gpsOffsetSec = gpsRange;
                durationSec = duration;
                gpsEndSec = gpsOffsetSec + durationSec;
                isGpsRange = true;
            end
        end
    else
        gpsOffsetSec = gpsRange(1);
        gpsEndSec = gpsRange(ncols);
        durationSec = gpsEndSec - gpsOffsetSec;
        isGpsRange = true;
    end
%P  IF gps Range flag not set
%P      FLAG as error
%P      exit
%P  ENDIF
%P  SET start time = channel start time + GPS offset
%P  USE framelist function to get list of frame files covering
%P                    start time to (start time + duration)
    if(isGpsRange ~= true)
        warning('Bad GPS range format');
        return;
    end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Supported format types
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Patrick's alphabeta file format

% format_string='%d%f%f';
% parameter='headerlines';
% no_of_header_lines = 4;

% calibration team's file format
format_string='%f%f%f%f%f';
% Previous versions of this had '%d%f%f%d%f' which returned extra lines
% with 1's or 0's in them - fixed 1/22/03 by stuver
parameter='headerlines';
no_of_header_lines = 1;

%
%
% Main starts here
%
%

% Load in the alphabeta data file
%[gpsStartMin,alpha,alphabeta] = ctextread(filename,format_string,parameter,no_of_header_lines);
%patrick
%[gpsStartMin,alpha,alphabeta] = textread(filename,format_string,parameter,no_of_header_lines);
%calib team
[gpsStartMin,alphabeta,alpha,beta,calline] = ctextread(filename,format_string,parameter,no_of_header_lines);
% !!!  Previous versions of this had alphabeta and alpha reversed and gave
% wrong results - fixed 1/22/03 by stuver


%
% Search for the gps times of interest.
%
% WARNING: Heavily file format dependent! and NO ERROR checking included
%

% Set maximum length to search
gps_Vector_Length = size(gpsStartMin,1);

% Search gpsStartMin vector for gpsRange elements
for gps_Start_row=1:gps_Vector_Length
    if (gpsStartMin(gps_Start_row) >= gpsOffsetSec)
        gps_Start_row = gps_Start_row - 1; % decrement so that first minute includes more than the first gps second
        break;
    end
end

% Search gpsStartMin vector for band end element
for gps_Stop_row=gps_Start_row:gps_Vector_Length
    if (gpsStartMin(gps_Stop_row) > gpsEndSec)
        if(gps_Stop_row > gps_Start_row)
            gps_Stop_row=gps_Stop_row - 1;  % decrement so that last minute includes more than the last gps second
        end
        break;
    end
end

% Return the resulting gpsinfo and alpha and alpha beta vectors

alpha = alpha(gps_Start_row:gps_Stop_row);
beta = beta(gps_Start_row:gps_Stop_row);
gpsStartMin = gpsStartMin(gps_Start_row:gps_Stop_row);

return
