%BAOPLT010_loop_test
% 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

function [retval] = BAOGMDPLT10_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
    inpathb = 'z:\bao\Tower\Processed\daily\';
    inpathg = 'z:\bao\Tower\SURFMET\';
    boutpath  = 'z:\bao\Tower\Processed_Images\daily\';
else
    inpathb = '/archive/BAO/Tower/Processed/daily/';
    inpathg = '/archive/BAO/Tower/Processed/daily/';
    boutpath  = '/archive/BAO/Tower/Processed_Images/daily/';
end

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]);

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

ii = 0;
for i = bdn:edn
    dv=datevec(i);
    yday = md2yd( dv(1), dv(2), dv(3) );
    fnb10 = sprintf('BAO_010_%4d%03d.dat', dv(1), yday );
    fng10 = sprintf('gmdsurf_%4d%02d%02d', dv(1), dv(2), dv(3) );
    fprintf(1,'*** Processing %s and %s ***\n', fnb10, fng10 );

%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

    cd(inpathb);

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

    cd(inpathg);

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

    cd(inpathb);

    bd10 = load(fnb10);    %load 1 day of PSD mean logger data

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

    %Total precip Before 2007 year-day 270 precip in mm after inches
    btp = cumsum(bd10(:,13));
    if yday < 270 && dv(1) < 2008
        btpin = btp;
    else
        btpin = btp*25.4;
    end
    sbtp = size(btpin);
    tbtpin = btpin(sbtp(1,1));
    %save total daily precip in an array
    ii = ii + 1;
    tobtprecip(ii,1) = bd10(1,3);
    tobtprecip(ii,2) = tbtpin;
    tbtpins = num2str(tbtpin(1,1));

    cd(inpathg);

    gd10 = load(fng10);    %load 1 day of PSD mean logger data
    gtp = cumsum(gd10(:,8));

    yday = md2yd(gd10(1,1), gd10(1,2), gd10(1,3));
    ydays = yday + ((((gd10(:,6)/3600.)+gd10(:,5)/60.)+gd10(:,4))/24); %decimal year day

    if pltflg % do plots if pltflg=1
        figure('Position',[10 1020 650 650]);
        subplot(2,1,1),plot(bd10(:,3),bd10(:,12),'r')
        hold
        subplot(2,1,1),plot(ydays,gd10(:,7),'g')
        %xlabel('Year Day (UTC)')
        ylabel('Pressure (mb)')
        stitle=sprintf('BAO GMD %4d-%02d-%02d 10m', dv(1),dv(2),dv(3));
        title(stitle)
        xlim([yday,yday+1])
        ylim([800 880])
        legend('BAO','GMD','Location','Best','Orientation','horizontal');
        legend('boxoff');

        %axis([110,116,820,850])
        subplot(2,1,2),plot(bd10(:,3),btpin,'r.')
        hold
        subplot(2,1,2),plot(ydays,gtp,'g.')
        xlabel('Year Day (UTC)')
        %ylabel('Precip (in)')
        ylabel('Precip (mm)')
        xlim([yday,yday+1])
        ylim([0 max(btpin)+0.1])
        text(yday+.1,2,'Total (mm) = ')
        text(yday+.3,2,tbtpins)
        %axis([110,116,0,.5])
        if pngflg
            %print_buffer = [boutpath, fnb10(1:15),'_PP.png'];
            print_buffer = sprintf('%sBAO_GMD_%4d%03d_PP.png', boutpath, dv(1), yday(1) );
            %print('-dpng ', print_buffer);
            f=getframe(gcf);
            imwrite(f.cdata,print_buffer,'png');
        end
    end  % if doing plots

    clear bd10 gd10 yday ydays
    close all

end % loop through all files

if 0  % This plot is done when reading the *all file
    figure('Position',[670 1020 650 650]);
    plot(tobtprecip(:,1),tobtprecip(:,2),'k*')
    %hold
    %plot(tobtprecip(:,1),tobtprecip(:,2),'k')
    title('BAO daily total precipitation (in)')
    xlabel('Year Day (UTC)')
    ylabel('Precip (in)')
    stot = size(tobtprecip);
    xlim([min(tobtprecip(:,1)),max(tobtprecip(stot(1,1),1))])
    if pltflg
        print_buffer = [boutpath, fn(24:38),'btp.png'];
        %print('-dpng ', print_buffer);
        f=getframe(gcf);
        imwrite(f.cdata,print_buffer,'png');
    end
    save tobtprecip.dat  tobtprecip -ascii
end
retval = 0;

