% Reads in raw ceilometer .DAT files as written by the Vaisala logging program
%Writes out the old style cloud base height file and the backscatter file
%It ignores files smaller than 8k to avoid the log files

disp('read_daily_rawceilo_CT25: 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 ..)     

%******************  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
        date=[year month day];%get year from read_parameters %SPdeS changed data to date
        jd=datenum(year,month,monthday)-datenum(year,1,0);
        jdn=sprintf('%3d',jd);
       
        [ceil_utc,ceil_date,ceil_hr,ceil_mn,ceil_sc,cloudcode,cloud1,cloud2,cloud3,ceil_range,ceil_b]=load_CT25_2009(date,ceil_filename); % SPdeS re-implement as function
        ceilo_15_sec=[cloudcode cloud1 cloud2 cloud3 ceil_hr ceil_mn ceil_sc];
        
        if wrtit
            %fn=[year '_' dy '_' yr];
            sta=[way_proc_data_ceilo sprintf('cloudbase%04i%02i%02i_%03i.txt',year,month,monthday,yearday) ];
            fid=fopen(sta,'wt');
            fprintf(fid,'%6i%6i%6i%6i%6i%6i%6i%6i\n',[ceilo_15_sec jd+0*ceilo_15_sec(:,1)]');% compact readable ascii format: [code height1 height2 height3 hour min sec yearday]
            fclose(fid);
            %save( sta, 'ceilo_15_sec', '-ascii','-tabs')

            stb=[way_proc_data_ceilo sprintf('backscatter%04i%02i%02i_%03i.mat',year,month,monthday,yearday)];
            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