
% Reads in raw ceilometer .DAT files as written by the Vaisala logging program
%Writes out the old stye cloud base height file and the backscatter file
% This version is looking for data on  E:\
%It ignores files smaller than 8k to avoid the log files
%Uses LCload_1 adapted from Pavlos Kollias on Stratus 03 Revelle cruise
%Uses LCload_CL31_2007.m adapted from Ludovic Bariteau for the new
%ceilometer CL31.

% program written by C. Fairall.  
% on 8 Aug 2004.  

disp('Reading ceilo files');

s=[way_raw_data_ceilo fname];
dr=dir(s);			%lists all files in directory
[n m]=size(dr);
nx=n;				%# of files in data directory (includes . and ..) 

if n == 0
    exit
end

%******************  setup read data loop  **********
%ss=way_raw_data_ceilo;
wrtit=1;

%jamx=1;
for ibg = 1:n 			%major read loop (no appending!)
    if dr(ibg).bytes>8000
        ff=dr(ibg).name;		 		%names of files
        ceil_filename=[way_raw_data_ceilo ff];
        mon=ff(3:4);	%get month
        month=str2double(mon);	%get month
        dy=ff(5:6);		%get day
        day=str2double(dy);		%get day
        yr=['0' ff(2)];	
        year=str2double(yr);
        date=[year month day];%get year %SPdeS changed data to date
        jd=datenum(str2double(['20' yr]),month,day)-datenum(str2double(['20' yr])-1,12,31);
        jdn=sprintf('%3d',jd);
%         jdn=num2str(jd);
%         if length(jdn)==2
%             jdn=['0' jdn];
%         end;
        
        % and now a very slow read of a horrendously formatted Vaisala
        % ceilometer file...
        [ceil_utc,ceil_date,ceil_range,ceil_b,cloudcode,cloud1,cloud2,cloud3,ceilo_30_sec]=load_CT25_2009(date,ceil_filename); % SPdeS re-implement as function
        %LCload_CL31_stratus07a
        %LCload_CL31_2007; 
        if wrtit
            fn=[mon '_' dy '_' yr];
            sta=[way_proc_data_ceilo jdn '_' fn '.txt'];
            save( sta, 'ceilo_30_sec', '-ascii','-tabs')

            stb=[way_proc_data_ceilo 'backscatter_' jdn '_' fn '.mat'];
            %save(stb,'ceil_utc','ceil_b','-ascii','-tabs');
            save(stb,'ceil_utc','ceil_range','ceil_b');  % height --> ceil_range SPdeS
        end; % write
    end;     % if file is big enough
end;         % data line(file?) loop