function Cal=ReadCalFileHR(calfile)
% ReadCalFileHR - Reads an HR calibration file. An empty value is returned
% if not succesful


% $Id: ReadCalFileHR.m,v 1.1 2005/04/13 20:30:58 brian Exp $
% Daniel Cosgrove
% 16 Apr 2002
% Modified for Argos HR Data (ED) 14 May 2002
%-----------------------------------------------------------------------------
%Read in calibration file specified in calfile
%-----------------------------------------------------------------------------
Cal = [];
if isfield(calfile,'sr')
    %this assumes a two piece HR3 with a surface unit and subsurface unit
    for calfiles = 1:2

        if calfiles == 1
            calfileID = fopen(calfile.hr2,'r');
            type = 1;
        else
            calfileID = fopen(calfile.sr,'r');
            type = 2;
        end

        %fprintf('\nReading calibration file...');

        while ~feof(calfileID)
            S = fgetl(calfileID);

            if findstr(S,'WAVE]')
                wave = S(2);
                for p = 1:3
                    S = fgetl(calfileID);
                    cal.W(p) = sscanf(S, '%f');
                end
            end



            if findstr(S, '<-F, C, epsilon,')
                pix = 1;
                while ~isempty(S)
                    calstore = sscanf(S,'%f,%f,%f,%f');
                    caldata(pix).F = calstore(1);
                    caldata(pix).C = calstore(2);
                    caldata(pix).e = calstore(3);
                    caldata(pix).immer = calstore(4);
                    pix = pix + 1;
                    S = fgetl(calfileID);
                end

                if wave == 'A' & type == 1
                    Cal.HrA.W=cal.W;
                    Cal.HrA.Caldat=caldata;
                elseif wave == 'B' & type == 1
                    Cal.HrB.W=cal.W;
                    Cal.HrB.Caldat=caldata;
                elseif wave == 'A' & type == 2
                    Cal.SrA.W=cal.W;
                    Cal.SrA.Caldat=caldata;
                end;

            end



        end
        fclose(calfileID);
    end;

else
    %assumes a single unit HR3. The fall back is an hr2. If no HR's are
    % found then this step is skipped
    OK = 1;
    try
        calfileID = fopen(calfile.hr3,'r');
    catch
        try
            calfileID = fopen(calfile.hr2,'r');
        catch
            OK = 0;
        end
    end

    % This block is only executed if an hr3 or hr2 were found.
    if OK
        while ~feof(calfileID)
            S = fgetl(calfileID);
            if findstr(S,'WAVE]')
                wave = S(2);
                for p = 1:3
                    S = fgetl(calfileID);
                    cal.W(p) = sscanf(S, '%f');
                end
            end


            if findstr(S, '<-F, C, epsilon,')
                pix = 1;
                while ~isempty(S)
                    calstore = sscanf(S,'%f,%f,%f,%f');
                    caldata(pix).F = calstore(1);
                    caldata(pix).C = calstore(2);
                    caldata(pix).e = calstore(3);
                    caldata(pix).immer = calstore(4);
                    pix = pix + 1;
                    S = fgetl(calfileID);
                end

                if wave == 'A'
                    Cal.HrB.W=cal.W;
                    Cal.HrB.Caldat=caldata;
                elseif wave == 'B'
                    Cal.HrA.W=cal.W;
                    Cal.HrA.Caldat=caldata;
                elseif wave == 'C'
                    Cal.SrA.W=cal.W;
                    Cal.SrA.Caldat=caldata;
                end;
            end
        end
        fclose(calfileID);
    end
end
%fprintf('done\nCalabrating data...');