%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PLOT CEILOMETER DAILY AND COMPOSITE TIME SERIES AND BACKSCATTER INTENSITY
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
pltflg = 1; % flag to control whether plots are made or not.

ceil_series = load(sta);
load(stb);

ceil_time = ceil_series(:,5)+ceil_series(:,6)/60+ceil_series(:,7)/3600;
nindx=find(isnan(ceil_time));
ceil_time(nindx)=0.004167*nindx;
if ceil_time(length(ceil_time))==0
    step=diff(ceil_time);
    ceil_time(length(ceil_time))=ceil_time(length(ceil_time)-1)+step(1);
end;

%figure()
figure1 = figure('Position',[10 50 650 650]);
set(gcf,'DefaultAxesFonts',12);
plot(ceil_time,ceil_series(:,2),'b.');
xlim([0 24]); set(gca,'xtick',0:4:24);ylim([0 7700]);
xlabel('Time (UTC)','FontName','times','FontSize',16);
ylabel('Height (m)','FontName','times','FontSize',12);
title([cruise ' ' year ' ' jdn '/' mon '-' dy '-' year ', Ceilometer Cloud Base'],'FontName','times','FontSize',12);
annotation(figure1,'textbox',[0.007154 0.01077 0.4498 0.02462],...
    'String',{'NOAA/ESRL/PSD/Weather & Climate Physics'},...
    'FontSize',8,'FitBoxToText','off','LineStyle','none');
if pltflg
    print_buffer=sprintf('%s%s%s_%s_%s_%s_cloudbase.png',way_images_ceilo,cruise,jdn,mon,dy,year);
    set(gcf,'PaperPositionMode','auto');
    print('-dpng ', print_buffer);
end

%figure()
figure2 = figure('Position',[10 50 650 650]);
set(gcf,'DefaultAxesFonts',12);
imagesc(ceil_time,ceil_range*1000,log10(max(0,ceil_b)),[-5.0 0.0]); % abs was goofy
axis xy; colorbar;
xlim([0 24]); set(gca,'xtick',0:4:24);ylim([0 7700]);
xlabel('Time (UTC)','FontName','times','FontSize',12);
ylabel('Height (m)','FontName','times','FontSize',12);
title([cruise ' ' year ' ' jdn '/' mon '-' dy '-' year ', Ceilometer Backscatter'],'FontName','times','FontSize',12);
annotation(figure2,'textbox',[0.007154 0.01077 0.4498 0.02462],...
    'String',{'NOAA/ESRL/PSD/Weather & Climate Physics'},...
    'FontSize',8,'FitBoxToText','off','LineStyle','none');
if pltflg
    print_buffer=sprintf('%s%s%s_%s_%s_%s_backscatter.png',way_images_ceilo,cruise,jdn,mon,dy,year);
    set(gcf,'PaperPositionMode','auto');
    print('-dpng ', print_buffer);
end

%figure()
v1=find(ceil_series(:,1) == 1);
v2=find(ceil_series(:,1) == 2);
v3=find(ceil_series(:,1) == 3);
v4=find(ceil_series(:,1) == 4);
lenv1=length(v1);
lenv2=length(v2);
lenv3=length(v3);
lenv4=length(v4);
%vcb=find(ceil_series(:,1) > 0 & ceil_series(:,1) < 4);

figure3 = figure('Position',[10 50 650 650]);
set(gcf,'DefaultAxesFonts',14);
imagesc(ceil_time,ceil_range*1000,log10(max(0,ceil_b)),[-5.0 0.0]); % abs was goofy
axis xy;
colorbar;
xlim([0 24]); set(gca,'xtick',0:4:24);ylim([0 7700]);
xlabel('Time (UTC)','FontName','times','FontSize',14);
ylabel('Height (m)','FontName','times','FontSize',14);
title([cruise ' ' year ' ' jdn '/' mon '-' dy '-' year ,...
    ', Ceilometer Backscatter and Cloud Base'],'FontName','times','FontSize',12);
hold
if lenv1 > 0
    plot(ceil_time(v1),cloud1(v1),'k.','MarkerSize',5);
end
if lenv4 > 0
    plot(ceil_time(v4),cloud2(v4),'m.','MarkerSize',5);
end
if lenv2 > 0
    plot(ceil_time(v2),cloud2(v2),'k.','MarkerSize',5);
end
if lenv3 > 0
    plot(ceil_time(v3),cloud3(v3),'k.','MarkerSize',5);
end
if lenv1 > 0 && lenv4 > 0
    legend('Cloudbase','Visibility','Location','North','Orientation','Horizontal');
elseif lenv1 > 0
    legend('Cloudbase','Location','North','Orientation','Horizontal');
elseif lenv4 > 0
    legend('Visibility','Location','North','Orientation','Horizontal');
end
annotation(figure3,'textbox',[0.007154 0.01077 0.4498 0.02462],...
    'String',{'NOAA/ESRL/PSD/Weather & Climate Physics'},...
    'FontSize',8,'FitBoxToText','off','LineStyle','none');
if pltflg
    print_buffer=sprintf('%s%s%s_%s_%s_%s_BCB.png',way_images_ceilo,cruise,jdn,mon,dy,year);
    set(gcf,'PaperPositionMode','auto');
    print('-dpng ', print_buffer);
end

%Specail plot for Dan XPIA
% outpath  = 'Z:\Tower\Programs\Dan\images\';
% 
% figure;
% set(gcf,'DefaultAxesFonts',14);
% imagesc(ceil_time,ceil_range,log10(max(0,ceil_b)),[-5.0 0.0]); % abs was goofy
% axis xy;
% colorbar;
% xlim([0 24]); set(gca,'xtick',0:4:24);ylim([0 7.7]);
% xlabel('Time (UTC)','FontName','times','FontSize',14);
% ylabel('Height (km)','FontName','times','FontSize',14);
% title([cruise ' ' year ' ' jdn '/' mon '-' dy '-' year],'FontName','times','FontSize',14);
% hold
% if lenv1 > 0
%     plot(ceil_time(v1),cloud1(v1)/1000,'k.','MarkerSize',5);
% end
% if lenv4 > 0
%     plot(ceil_time(v4),cloud2(v4)/1000,'m.','MarkerSize',5);
% end
% if lenv2 > 0
%     plot(ceil_time(v2),cloud2(v2)/1000,'k.','MarkerSize',5);
% end
% if lenv3 > 0
%     plot(ceil_time(v3),cloud3(v3)/1000,'k.','MarkerSize',5);
% end
% if lenv1 > 0 && lenv4 > 0
%     legend('Cloudbase','Visibility','Location','North','Orientation','Horizontal');
% elseif lenv1 > 0
%     legend('Cloudbase','Location','North','Orientation','Horizontal');
% elseif lenv4 > 0
%     legend('Visibility','Location','North','Orientation','Horizontal');
% end
% % annotation(figure3,'textbox',[0.007154 0.01077 0.4498 0.02462],...
% %     'String',{'NOAA/ESRL/PSD/Weather & Climate Physics'},...
% %     'FontSize',8,'FitBoxToText','off','LineStyle','none');
% if pltflg
%     print_buffer=sprintf('%s%s%s_%s_%s_%s_BCB.png',outpath,cruise,jdn,mon,dy,year);
%     set(gcf,'PaperPositionMode','auto');
%     print('-dpng ', print_buffer);
% end
% %