%This program is made daily sensitive to the data from the Vaisala
%ceilometer CT25

clear;
close('all');
fclose('all');

try

    Fluxroot='E:\AtlanticTransect_2014\PolarStern\Scientific_analysis\programs\ceilo\';  % name of the path where the 'flux' folder + read_parameters file are installed.

    cd([Fluxroot])         %Changes current directory to fluxroot/flux
    addpath(genpath(Fluxroot))     %adds flux folder and all of its subdirectories to the top of the MATLAB search path.
    
    %Gets the last uploaded parameter file from Fluxroot directory
    dirfluxroot=dir([Fluxroot 'read_parameters_*.m']);
    MostrecentPARfile=dirfluxroot(cellfun(@(x) x==max([dirfluxroot.datenum]),{dirfluxroot(:).datenum})).name;
    disp(['Most recent parameters being used: ' MostrecentPARfile])
    eval(strtok(MostrecentPARfile, '.'));

    %%%%%%%%%%%%%%%%%%%Modifies current directory and search path%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%     Ceiloroot='D:\Data\DYNAMO_2011\Revelle\Scientific_analysis\programs\';  % name of the path where the 'flux' folder + read_parameters file are installed.
    cd([Fluxroot, 'ceilo'])         %Changes current directory to fluxroot
%     addpath(genpath(Ceiloroot))     %adds flux folder and all of its subdirectories to the top of the MATLAB search path.
    
    %%%%%%%%%%%  Defines some options %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    plotit=true; % for making plots.
    prtit=true;  % for printing plots--ineffective unless plotit is also true
    graphformat='.png';  %select graphics format files
    graphdevice='-dpng'; %select graphic device
    saveit=true; % for saving data files
    clearit=true;% for clearing data sometimes to save memory
    emailconf=false; %for sending email confirmation to NOAA email recipient list (Send_email_to_PSD3group.m)
    %set to false as it does not seem to work from DAS computers :(
    report=[];
    data_ftp=false;     %to activate ftp to Boulder server
    data_backup=false;    %true;  %to activate backup to Lacie hardrive (if present)
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    cruise_name=cruise;%'WGC';

    dd = clock;  % date vector
    edn=datenum(dd(1),dd(2),dd(3),0,0,0); %MATLAB day
    bdn=edn-1;    % this is to get the prevuous days data.
%     bdn=edn-3;    % this is to get the prevuous days data.
    ddv = datevec(bdn);
%     jd=bdn-datenum(ddv(1),1,0);  %same as md2yd(V(1),V(2),V(3))
    yyyy =  sprintf('%04i',ddv(1));
    
%reads in all files for the same day (in case of reboots, etc...)    
    fname=sprintf('C%c%02d%02d*.DAT', yyyy(4), ddv(2), ddv(3)); %Aymmddhh.DAT   
%     hr = 00;   % assumes ceilometer program hasn't restarted
%     fname=sprintf('R%c%02d%02d%02d.DAT', yyyy(4), ddv(2), ddv(3), hr)  %Aymmddhh.DAT
%     
%     if exist(['D:\Data\ceilometer\' fname],'file')
%         copyfile(['D:\Data\ceilometer\' fname],[way_raw_data_ceilo]); % copy yesterdays file from ceilometer PC (Y:) to D:
%     end;

    ls([way_raw_data_ceilo '*.DAT'])   % ls files in directory
    
    eval([ 'read_daily_rawceilo_CT25_', cruise])


%     %compress ceilometer files into a zip file so it can be ftp'd
%     mn = sprintf('%02d',ddv(2));
%     dy = sprintf('%02d',ddv(3));
%     jds = sprintf('%03d',jd);
%     if exist([way_images_ceilo, cruise, '_', ship, '_ceilobackscatter_', yyyy, '_', mon, '_', dy, '_' jds,  graphformat],'file') && exist([way_images_ceilo, cruise, '_', ship, '_cloudbase_', yyyy, '_', mon, '_', dy, '_' jds,  graphformat],'file')
%         entrynames=zip([way_proc_data_flux, cruise, '_', ship, '_ceilometer_', yyyy, '_', mon, '_', dy, '_' jds, '.zip'],{[way_images_ceilo,  cruise, '_', ship, '_ceilobackscatter_', yyyy, '_', mon, '_', dy, '_' jds graphformat],[way_images_ceilo,  cruise, '_', ship, '_cloudbase_', yyyy, '_', mon, '_', dy, '_' jds graphformat]}); 
%     end

    eval([ 'ceilo_daily_plot_program_', cruise])

%%%%%%%%%%%%%%%%%%%%%%%%%% catch error if any, and creates log report %%%%%%%%%%%%%%%%%%%%%%%%%    
catch ceilostatus1    
    jd=datenum(ddv(1),ddv(2),ddv(3))-datenum(ddv(1)-1,12,31);
    report = getReport(ceilostatus1);
    fid=fopen([way_proc_data_ceilo 'ceilometer_logerror' sprintf('%03d',jd)  '.txt'],'w');
    fprintf(fid,'%s',report);
    fclose(fid);
    % DEW commented out next line Oct 12, 2011
   %entrynames=zip([way_proc_data_flux, cruise, '_', ship, '_ceilometer_', sprintf('%04d_%02d_%2d_%03d', ddv(1),ddv(2),ddv(3),jd) '.zip'],[way_proc_data_ceilo 'ceilometer_logerror' sprintf('%03d',jd)  '.txt']); 
end

exit
