% 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

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

if n == 0
%     exit
    error(sprintf('No file detected %s',s))
end

%******************  setup read data loop  **********
%ss=way_raw_data_ceilo;
wrtit=1;
ceilo_15_sec=[];
ceil_utc=[];
% ceil_range_cat=[];
ceil_b=[];

%jamx=1;
for ibg = 1:n 			%major read loop (no appending!)
    if dr(ibg).bytes>8000
        ff=dr(ibg).name;		 		%names of files
        
%         if exist(['D:\Data\ceilometer\' ff],'file')
%             copyfile(['D:\Data\ceilometer\' ff],[way_raw_data_ceilo]); % copy yesterdays file from ceilometer PC (Y:) to D:
%         end;
    
        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=[ddv(1) month day];%get year %SPdeS changed data to date
        jd=datenum(ddv(1),month,day)-datenum(ddv(1)-1,12,31);
        jdn=sprintf('%03d',jd);
       
        [ceil_utc_cat,ceil_date,ceil_hr,ceil_mn,ceil_sc,cloudcode,cloud1,cloud2,cloud3,ceil_range,ceil_b_cat]=load_CT25_2009(date,ceil_filename); 
%         ceilo_15_sec=[cloudcode cloud1 cloud2 cloud3 ceil_hr ceil_mn ceil_sc];

        ceilo_15_sec=[ceilo_15_sec; cloudcode cloud1 cloud2 cloud3 ceil_hr ceil_mn ceil_sc];
        ceil_utc=[ceil_utc; ceil_utc_cat];
%         ceil_range_cat=[ceil_range_cat; ceil_range];
        ceil_b=[ceil_b'; ceil_b_cat']';
       
%         
       
    end;     % if file is big enough
end;         % data line(file?) loop

jds = sprintf('%03d',jd);

if wrtit
    %fn=[year '_' dy '_' yr];
    sta=[way_proc_data_ceilo sprintf('cloudbase_%04s_%02s_%02s_%03i.txt',yyyy,mon,dy,jd) ];
    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_%04s_%02s_%02s_%03i.mat',yyyy,mon,dy,jd)];
%     save(stb,'ceil_utc_cat','ceil_range','ceil_b_cat');  % height 
    save(stb,'ceil_utc','ceil_range','ceil_b');  % height 
end; % write
