%BAOPLT010_loop_func
% 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
% DEW2/13/2014 changed path so it will run on my desktop PC

function [retval] = BAOPLT10_loop_func( by, bm, bd, ey, em, ed )

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

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

%cd(inpath);

if( nargin == 0 )
    bys = input('Input year to process (2007) ','s');
    bms = input('Input the month to process (1-12) ','s');
    bds = input('Input the beginning day to process (1-31) ','s');
    eys = input('Input year to process (2007) ','s');
    ems = input('Input the month to process (1-12) ','s');
    eds = input('Input end day to process (1-31) ','s');
    by = str2double( bys );
    bm = str2double( bms );
    bd = str2double( bds );
    ey = str2double( eys );
    em = str2double( ems );
    ed = str2double( eds );
end

bdn = datenum([by bm bd 0 0 0]);
edn = datenum([ey em ed 0 0 0]);
%dd = clock;  

lm = edn - bdn + 1;
totprecip = zeros(lm,2);

ii = 0;
for i = bdn:edn
    dv=datevec(i);
    %if i == bdn && dd(4) > 0
    %    continue
    %end   
    yday = md2yd( dv(1), dv(2), dv(3) );
    name10 = sprintf('%sBAO_010_%4d%03d.dat', inpath, dv(1), yday );
    fprintf(1,'*** Processing 10m daily ***\n');

    fid10 = fopen( name10 );
    if( fid10 > 0 )
        fclose( fid10 );
    else
        fprintf(1,'No  10m: %s\n',name10);
        continue;       
    end

    data10 = load(name10);    %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)+datenum(data10(:,2)-1,12,31); %decimal year day
    clear hhmm hh mm

    %Total precip Before 2007 year-day 270 precip in mm after inches
    tp = cumsum(data10(:,13));
    if yday < 270 && dv(1) < 2008
        totpmm = tp;
    else
        totpmm = tp*25.4;
    end
    tpmm = totpmm(length(totpmm));
    tpin = tpmm / 25.4;
    stp = size(tpmm);
    ttpmm = tpmm(stp(1,1));
    %save total daily precip in an array
    ii = ii + 1;
    totprecip(ii,1) = data10(1,3);
    totprecip(ii,2) = ttpmm;

    if pltflg % do plots if pltflg=1
        figure1 = figure('Position',[100 50 650 650]);
        subplot(2,1,1),plot(data10(:,3),data10(:,5))
        stitle=sprintf('BAO %4d-%02d-%02d 10m', dv(1),dv(2),dv(3));
        gi = find( data10(:,5) > -50. );
        blim = min(data10(gi,5))-5;
        elim = max(data10(gi,5))+5;
        ylim([blim elim])
        xlim([data10(1,3),data10(1,3)+1])
        title(stitle)
        ylabel('T (C)')
        xlabel('Hour (UTC)')
        datetick('x','HH','keeplimits');     
        grid on
        subplot(2,1,2),plot(data10(:,3),data10(:,6))
        ylim([0,109])
        xlim([data10(1,3),data10(1,3)+1])
        xlabel('Hour (UTC)')
        datetick('x','HH','keeplimits');     
        ylabel('Rh (%)')
        grid on
        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_010_%4d%03d_TRH.png',...
                                   outpath, dv(1), yday );
            set(gcf,'PaperPositionMode','auto');
            print('-dpng', print_buffer);
            %f=getframe(gcf);
            %imwrite(f.cdata,print_buffer,'png');
        end

        figure2 = figure('Position',[100 50 650 650]);
        subplot(2,1,1),plot(data10(:,3),data10(:,7))
        xlim([data10(1,3),data10(1,3)+1])
        title(stitle)
        xlabel('Hour (UTC)')
        datetick('x','HH','keeplimits');     
        ylabel('Spd (m/s)')
        grid on
        subplot(2,1,2),plot(data10(:,3),data10(:,14),'bx','MarkerSize',4)
        xlim([data10(1,3),data10(1,3)+1])
        ylim([0 360])
        xlabel('Hour (UTC)')
        datetick('x','HH','keeplimits');     
        ylabel('Dir (true)')
        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_010_%4d%03d_WS.png',...
                                   outpath, dv(1), yday );
            set(gcf,'PaperPositionMode','auto');
            print('-dpng', print_buffer);
            %f=getframe(gcf);
            %imwrite(f.cdata,print_buffer,'png');
        end

        figure3 = figure('Position',[100 50 650 650]);
        subplot(2,1,1),plot(data10(:,3),data10(:,12))
        xlim([data10(1,3),data10(1,3)+1])
        ylim([800 880])
        stitle=sprintf('BAO %4d-%02d-%02d Surface', dv(1),dv(2),dv(3));
        title(stitle)
        ylabel('Pressure (mb)')
        xlabel('Hour (UTC)')
        datetick('x','HH','keeplimits');     
        grid on
        subplot(2,1,2),plot(data10(:,3),totpmm,'r.')
        xlim([data10(1,3),data10(1,3)+1])
        ylim([0 max(tpmm)+1.0])
        xlabel('Hour (UTC)')
        datetick('x','HH','keeplimits');     
        ylabel('Precip (mm)')
        ctp = sprintf('Total precipitation %.2f mm (%.2f in)',tpmm,tpin);
        title(ctp)
        grid on
        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 pngflg
            print_buffer = sprintf('%sBAO_SFC_%4d%03d_PP.png',...
                                   outpath, dv(1), yday );
            set(gcf,'PaperPositionMode','auto');
            print('-dpng', print_buffer);
        end
    end  % if doing plots

    clear data10 yday
    close all

end % loop through all files

%if 0  % This plot is done when reading the *all file
%    figure('Position',[100 50 650 650]);
%    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'];
%        f=getframe(gcf);
%        imwrite(f.cdata,print_buffer,'png');
%    end
%    save totprecip.dat  totprecip -ascii
%end
retval = 0;

