%BAOPLT010_loop
% Plot data from BAO data loggers
% Systems turned back on April 2007
% Not calibrated old sensors
% 10m
%ID,YR,YearDay,hhmm,T,RH,WS,WSvec,DIR,Dsig,VDC,P,Precip,Corrected Dir
%10,2007,099,1413,7.35,87.654,.305,.305,292.84,2.03,13.981,826.61,0,111.234

clear

pltflg = 1; %flag to plot the data: 1=yes, 0=no
pngflg = 1; %flag to make a png of the plot: 1=yes, 0=no

inpath = 'z:\bao\Tower\Processed\daily\';
outpath  = 'z:\bao\Tower\Processed_Images\daily\';

cd(inpath);
m=dir('BAO_010_200802*');
lm = length(m);
fprintf(1,'number of files to process %d\n', lm);

% 8/28/2007  59 = 234 65 days
% 8/28/2007  74 = 249 74 days Sep 6th

totprecip = zeros(lm,2);

for ii = 1:lm
    clear data10 yday
    disp(['Reading ',m(ii).name]);
    data10 = load(m(ii).name);    %load 1 day of PSD mean logger data

    hhmm = data10(:,4);
    hh = fix(hhmm/100);
    mm = hhmm - (hh*100);
    data10(:,3) = data10(:,3) + (((mm/60.) + hh)/24); %decimle year day
    yday = fix(data10(1,3));
    clear hhmm hh mm

    %Total precip
    tp = cumsum(data10(:,13));
    tpin = tp/25.4;
    stp = size(tpin);
    ttpin = tpin(stp(1,1));
    %save total daily precip in an array
    totprecip(ii,1) = data10(1,3);
    totprecip(ii,2) = ttpin;
    ttpins = num2str(ttpin(1,1));

    if pltflg % do plots if pltflg=1
        figure(1)
        clf
        subplot(2,1,1),plot(data10(:,3),data10(:,5))
        %hold
        %subplot(2,1,1),plot(data50(:,3),data50(:,5),'r')
        %xlabel('Year Day (UTC)')
        ylabel('T (C)')
        stitle=sprintf('BAO %d 10m', data10(1,2));
        title(stitle)
        %axis([110,116,-10,30])
        xlim([yday,yday+1])
        %legend('10m')
        %legend('10m','50m')
        subplot(2,1,2),plot(data10(:,3),data10(:,6))
        %hold
        %subplot(2,1,2),plot(data50(:,3),data50(:,6),'r')
        xlabel('Year Day (UTC)')
        ylabel('Rh (%)')
        %axis([110,116,0,100])
        %legend('10m')
        %legend('10m','50m')
        ylim([0,109])
        xlim([yday,yday+1])
        if pngflg
            print_buffer = [outpath, m(ii).name(1:15),'_TRH.png'];
            print('-dpng ', print_buffer);
        end

        figure(2)
        clf
        subplot(2,1,1),plot(data10(:,3),data10(:,7))
        %hold
        %subplot(2,1,1),plot(data50(:,3),data50(:,7),'r')
        xlabel('Year Day (UTC)')
        ylabel('Spd (m/s)')
        title(stitle)
        %axis([110,116,0,30])
        %legend('10m')
        xlim([yday,yday+1])
        %legend('10m','50m')
        subplot(2,1,2),plot(data10(:,3),data10(:,14))
        %hold
        %subplot(2,1,2),plot(data50(:,3),data50(:,8),'r')
        xlabel('Year Day (UTC)')
        ylabel('Dir (true)')
        %axis([110,116,0,360])
        %legend('10m')
        xlim([yday,yday+1])
        %legend('10m','50m')
        if pngflg
            print_buffer = [outpath, m(ii).name(1:15),'_WS.png'];
            print('-dpng ', print_buffer);
        end

        figure(3)
        clf
        subplot(2,1,1),plot(data10(:,3),data10(:,12))
        %xlabel('Year Day (UTC)')
        ylabel('Pressure (mb)')
        title(stitle)
        xlim([yday,yday+1])
        ylim([800 880])
        %axis([110,116,820,850])
        subplot(2,1,2),plot(data10(:,3),tpin,'r.')
        xlabel('Year Day (UTC)')
        ylabel('Precip (in)')
        xlim([yday,yday+1])
        ylim([0 .5])
        text(yday+.1,2,'Total (in) = ')
        text(yday+.3,2,ttpins)
        %axis([110,116,0,.5])
        if pngflg
            print_buffer = [outpath, m(ii).name(1:15),'_PP.png'];
            print('-dpng ', print_buffer);
        end
    end  % if doing plots

end % loop through all files

if 0  % This plot is done when reading the *all file
    figure(4)
    clf
    plot(totprecip(:,1),totprecip(:,2),'k*')
    %hold
    %plot(totprecip(:,1),totprecip(:,2),'k')
    title('BAO daily total precipitation (in)')
    xlabel('Year Day (UTC)')
    ylabel('Precip (in)')
    stot = size(totprecip);
    xlim([min(totprecip(:,1)),max(totprecip(stot(1,1),1))])
    if pltflg
        print_buffer = [outpath, fn(24:38),'TP.png'];
        print('-dpng ', print_buffer);
    end
    save totprecip.dat  totprecip -ascii
end

