%%BAOPLT10100300
% Plot daily combined data for levels 10, 100 and 300 meters.

function [retval] = bao10100300_24hour_func( )
clear
close all

if ispc
    inpath = 'z:\bao\Tower\Processed\daily\';
    outpath  = 'Z:\bao\Tower\Processed_Images\daily\';
else
    inpath = '/archive/BAO/Tower/Processed/daily/';
    outpath  = '/archive/BAO/Tower/Processed_Images/daily/';
end

%cd(inpath);

doplot = 1;  % do the plot 1=yes 0=no
pngflg = 1;  % save data to png's flag  1= yes 0 = no

dd = clock;

edn=datenum(dd(1),dd(2),dd(3),0,0,0);
bdn=edn-1;

nmax= 3000;

%% Get yesterday's and today's data
for i = bdn:edn
    dv=datevec(i);
    if i == bdn
        dvp = dv;
    else
        dvt = dv;
    end
    yday = md2yd( dv(1), dv(2), dv(3) );
    name10 = sprintf('%sBAO_010_%4d%03d.dat', inpath, dv(1), yday );
    name100 = sprintf('%sBAO_100_%4d%03d.dat', inpath, dv(1), yday );
    name300 = sprintf('%sBAO_300_%4d%03d.dat', inpath, dv(1), yday );
    
    fprintf(1,'*** Processing 10m, 100m and 300m current ***\n');
    fid10 = fopen( name10 );
    if( fid10 > 0 )
        fclose( fid10 );
        have10 = 1;
    else
        fprintf(1,'No  10m: %s\n',name10);
        have10 = 0;
        continue;
    end
    
    fid100 = fopen( name100 );
    if( fid100 > 0 )
        have100 = 1;
        fclose( fid100 );
    else
        have100 = 0;
        fprintf(1,'No 100m: %s\n',name100);
    end
    
    fid300 = fopen( name300 );
    if( fid300 > 0 )
        fclose( fid300 );
        have300 = 1;
    else
        fprintf(1,'No 300m: %s\n',name300);
        have300 = 0;
        continue;
    end
    
    if have10 == 1
        if i == bdn
            data10p = load(name10);
            hhmm = data10p(:,4);
            hh = fix(hhmm/100);
            mn = hhmm - (hh*100);
            data10p(:,3)=bdn+(((mn/60.)+hh)/24); %Julian day
            clear hhmm hh mm
        else
            data10t = load(name10);
            hhmm = data10t(:,4);
            hh = fix(hhmm/100);
            mn = hhmm - (hh*100);
            data10t(:,3) = edn + (((mn/60.) + hh)/24); %Julian day
            clear hhmm hh mm
        end
    end
    
    if have100
        if i == bdn
            data100p = load(name100);
            hhmm = data100p(:,4);
            hh = fix(hhmm/100);
            mn = hhmm - (hh*100);
            data100p(:,3) = bdn+(((mn/60.)+hh)/24); %Julian day
            clear hhmm hh mm
        else
            data100t = load(name100);
            hhmm = data100t(:,4);
            hh = fix(hhmm/100);
            mn = hhmm - (hh*100);
            data100t(:,3) = edn + (((mn/60.) + hh)/24); %Julian day
            clear hhmm hh mm
        end
    end
    
    if have300 == 1
        if i == bdn
            [data300p dtp np] = load300( name300, nmax);
            dtp(1:np,7) = dtp(1:np,7);
        else
            [data300t dtt nt] = load300( name300, nmax);
            dtt(1:nt,7) = dtt(1:nt,7);
        end
    end
    
end

%% combine 2 days and plot out only last 24 hours
nr10t = size(data10t,1);
nr10p = size(data10p,1);
nr100t = size(data100t,1);
nr100p = size(data100p,1);
%ep = dtt(nt,7); %find last year day in file
%ep=datenum(dtt(nt,1),dtt(nt,2),dtt(nt,3),dtt(nt,4)+1,0,0);
%ep=bao10(l2,3);
%bp = ep - 1;
bao10 = data10p;
bao100 = data100p;
bao300 = data300p;
dt = dtp;

l1 = nr10p+1;
l2 = nr10p+nr10t;
bao10(l1:l2,:)  = data10t(1:nr10t,:);

%ep = dtt(nt,7); %find last year day in file
%ep=datenum(dtt(nt,1),dtt(nt,2),dtt(nt,3),dtt(nt,4)+1,0,0);
ep=bao10(l2,3);
bp = ep - 1;

l1 = nr100p+1;
l2 = nr100p+nr100t;
bao100(l1:l2,:)  = data100t(1:nr100t,:);

if have300 == 1
    l1 = np+1;
    l2 = np+nt;
    bao300(l1:l2,:)  = data300t(1:nt,:);
    dt(l1:l2,:) = dtt(1:nt,:);
end

stitle =sprintf('BAO %4d-%02d-%02d to %4d-%02d-%02d', ...
    dvp(1),dvp(2),dvp(3),dvt(1),dvt(2),dvt(3));
%ymd =sprintf('%4d%02d%02d',dvt(1),dvt(2),dvt(3));

if doplot %skip plotting
    
    figure1 = figure('Position',[100 50 650 650]);
    clf
    subplot(2,1,1),plot(bao10(:,3),bao10(:,7))
    elim = max(bao10(:,7))+2.0;
    hold
    
    subplot(2,1,1),plot(bao100(:,3),bao100(:,7),'k')
    if have300 == 1
        subplot(2,1,1),plot(dt(1:l2,7),bao300(1:l2,1),'r')
        elim = max(max(bao10(:,7)),max(bao300(1:l2,1)))+2.0;
    end
    ylim([0 elim])
    xlim([bp ep])
    title(stitle)
    %xlim([jdb,jde])
    xlabel('Hour (UTC)')
    ylabel('Speed (m/s)')
    datetick('x','HH','keepticks','keeplimits');
    legend('10m','100m','300m','Location','Best','Orientation','horizontal');
    legend('boxoff');
    grid on
    
    subplot(2,1,2),plot(bao10(:,3),bao10(:,12),'bx','MarkerSize',4)
    hold
    subplot(2,1,2),plot(bao100(:,3),bao100(:,12),'kx','MarkerSize',4)
    if have300 == 1
        subplot(2,1,2),plot(dt(1:l2,7),bao300(1:l2,14),'rx','MarkerSize',4)
    end
    set(gca,'YTick',[0 90 180 270 360])
    %axis([110,116,0,.5])
    xlabel('Hour (UTC)')
    ylabel('Dir (True)')
    xlim([bp ep])
    datetick('x','HH','keepticks','keeplimits');
    grid on
    ylim([0 360])
    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 pngflg
        print_buffer = sprintf('%sBAO_10300_current_WS.png',outpath);
        set(gcf,'PaperPositionMode','auto');
        print('-dpng ', print_buffer);
    end
    
    if have100
        figure2 = figure('Position',[100 50 650 650]);
        clf
        subplot(2,1,1),plot(bao10(:,3),bao10(:,5))
        hold
        subplot(2,1,1),plot(bao100(:,3),bao100(:,5),'k')
        if have300 == 1
        subplot(2,1,1),plot(dt(1:l2,7),bao300(1:l2,13),'r')
        end
        title(stitle)
        xlim([bp,ep])
        gis = find( bao10(:,5) > -50. );
        gih = find( bao100(:,5) > -50. );
        gip = find( bao300(1:l2,13) > -50. );
        blims(3) = min(bao300(gip,13))-5;
        elims(3) = max(bao300(gip,13))+5;
        blims(1) = min(bao100(gih,5))-5;
        elims(1) = max(bao100(gih,5))+5;
        blims(2) = min(bao10(gis,5))-5;
        elims(2) = max(bao10(gis,5))+5;
        ylim([min(blims) max(elims)])
        xlabel('Hour (UTC)')
        ylabel('Temp (C)')
        datetick('x','HH','keepticks','keeplimits');
        legend('10m','100m','300m','Location','Best','Orientation','horizontal');
        legend('boxoff');
        grid on
        
        subplot(2,1,2),plot(bao10(:,3),bao10(:,6))
        hold
        subplot(2,1,2),plot(bao100(:,3),bao100(:,6),'k')
        if have300 == 1
            subplot(2,1,2),plot(dt(1:l2,7),bao300(1:l2,9),'r')
        end
        xlim([bp,ep])
        ylim([0 105])
        xlabel('Hour (UTC)')
        ylabel('RH (%)')
        datetick('x','HH','keepticks','keeplimits');
        grid on
        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 pngflg
            print_buffer = sprintf('%sBAO_10100300_current_TRH.png',outpath);
            set(gcf,'PaperPositionMode','auto');
            print('-dpng ', print_buffer);
        end
    end % if have 100 data
end % skip plotting

close all
clear all

retval = 0;
