%function [mm,scale,resolution]=LCload_CL31_BAO_2010a(ceil_filename)
% LCload_CL31: load a Lidar Ceilometer CL31 file and return time series of
% cloud heights and a time-height series of backscatter.
%
% USAGE:
%  [ceil_utc,ceil_date,ceil_range,ceil_b,cloud1,cloud2,cloud3,ceilo_30_sec]=LCload_CL31(date,ceil_filename);
%
% INPUTS:
%    date=[yyyy mm dd];
%    ceil_filename is the full-path name of the file to load
% OUTPUTS:
%    ceil_utc is the time in decimal hours of each ray
%    ceil_date is the date of each ray as a float that looks like yyyymmdd
%    ceil_range is the range of each gate in metres
%    ceil_b is the backscatter coefficient in the units (srad km)**(-1)
%    cloud1, cloud2 and cloud3 are the heights of the first three cloud
%         bases as determined by the CL31 data aquisition system from
%         the backscatter profile.
%
% AUTHORS:
% Dana, Chris, Dan, Ludovic, and Simon
%
% 2008 September 17
% -eliminated hex2dec calls, sped code up ~30x
% -declared temporary arrays in fast orientation, truncated at end of read
% -fixed feet/meters unit conversion
% -cleaned up loop and logic
% -fixed 2's complement unwrapping for 20-bit integers
% -reads range gates from file
% -returns ceil_b in VOLUME BACKSCATTER COEFFICIENT (p.75 CL31 manual)
% -alarms flagged with cloudcode=-1

check_status=1;

% open file
fid=fopen(ceil_filename,'r');
if fid == -1
    error(['Cannot read ' ceil_filename]);
end

% frewind(fid) % start reading at top of file here

% Start gargantuan read loop
%k=0; % count time samples
while ~feof(fid)
    %for pp = 1:20  % short loop for testing
    %if strcmp(fscanf(fid,'%s',[1 1]),'-File'); break; end; %dump -2007-02-24 and break on last '-File Closed:' line
    line=fgetl(fid);
    %if strcmp(line,'-File'); break; end;
% dump header lines
    if strcmp(line(1:6),'-Ceilo'); continue; end;
    if strcmp(line(1:5),'-File'); continue; end;
    %k=k+1;
    time=sscanf(line,'-%4d-%2d-%2d %2d:%2d:%2d',[6 1]); %dump date, read hh:mm:ss
    %fprintf(1,'%02d:%02d:%02d\n',time(4),time(5),time(6));
    if ~isempty(time);% catch other errors and empty at last line of file
        if time(3)~= dv(3)
            break;
        end
        k=fix((time(4)*3600.+time(5)*60.+time(6))/15.+1.0);
        tempceil_hour(k)=time(4);
        tempceil_min(k)=time(5);
        tempceil_sec(k)=time(6);
        
        %fgetl(fid);       %go to end of line
        temp=fgetl(fid);  %get rid of CL013011
        %range_flag=str2num(temp(8));
        tempht=fgetl(fid); %cloud base heights
        if tempht(1)=='/' || tempht(2)=='A' % Alarms present!
            tempcloudcode(k)=-1; % flag error
        else % no alarms present
            tempcloudcode(k)=sscanf(tempht(1:2),'%1d%*1c'); % data is number of clouds detected: 0,1,2, or 3
            cloudcode=tempcloudcode(k);
            if cloudcode == 4
                cloudcode = 2;
            end
            if cloudcode > 0
                tempcloud1(k)=sscanf(tempht(4:20),'%5d',1);
                if cloudcode > 1
                    tempcloud2(k)=sscanf(tempht(10:20),'%5d',1);
                    if cloudcode == 3
                        tempcloud3(k)=sscanf(tempht(16:20),'%5d',1);
                    end
                end
            end
        end % no alarms present
        % position 21 is a space before the detection status bits in 12
        if check_status == 1 % only check detection status once per file
            Detection_status=tempht(22:22+12-1); % hexadecimal digits
            units_bit=sscanf(Detection_status(11),'%1x',1)>7;
            % units_bit true if meters; see table of hexadecimal digits and detection bits in ceilo manual
            if units_bit  %units in cloud heights line are meters
                mm=1;     %no conversion coefficient
            else          %units in cloud heights line are feet
                mm=0.303; %conversion coefficient from feet to meters
            end;
        end
        line2p5=fgetl(fid); % line2.5: -1 ///  0 ///  0 ///  0 ///  0 ///
        line3=fgetl(fid);   % line3: 00100 10 0770 100 +28 097 02 0005 L0016HN15 238
        if check_status == 1 % update scale and resolution once per file
            scaleres=sscanf(line3,'%5d %2d %4d');
            scale=scaleres(1)/100;
            resolution=scaleres(2);
            gates_number=scaleres(3);
            check_status=0;
        end
        % read the long line of ranged data...
        line = fgetl(fid);
        if size(line,2)==5*gates_number
            tceil=sscanf(line,'%5x');
            if length(tceil) == 770
                tempceil_b(:,k)=tceil(:);
            end
            %else
        %    tempceil_b(:,k)=NaN;
        end;
    end %if ~isempty(time)
    fgetl(fid); % dump 6 character line
    fgetl(fid);  %get rid of blank line (if k>1?)
end % while

fclose(fid);
