% Plot UM base logger data
% 10m sonic 4, 22m sonic 2 even = N
% UMass sonic 2D Model 81000
% u is E/W + fm E
% v is N/S + fm N
% w -s up/dn + is up
% on N boom +v = 334
% on S boom +v = 154

function [retval] = UM1022_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

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

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

% yy  mm dd hh mn ss    u       v     w       t     npt bat
%2007 08 20 00 00 00    3.89   0.50  -0.40   27.10  32 11.1
%2007 08 20 00 00 01    3.51   0.85  -0.17   27.09  32 11.1

for i = bdn:edn
    dv=datevec(i);
    name10 = sprintf('sonic_010n_%4d%02d%02d.dat',dv(1),dv(2),dv(3));
    name22 = sprintf('sonic_022n_%4d%02d%02d.dat',dv(1),dv(2),dv(3));

    fprintf(1,'*** Processing %4d-%02d-%02d ***\n', dv(1),dv(2),dv(3));

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

    fid22 = fopen( name22 );
    if( fid22 > 0 )
        fclose( fid22 );
    else
        fprintf(1,'No  22m: %s\n',name22);
        continue;
    end

    d10 = load(name10);
    yd10 = datenum(d10(:,1),d10(:,2),d10(:,3),d10(:,4),d10(:,5),d10(:,6))-datenum(d10(:,1)-1,12,31);
    s10=sqrt(d10(:,7).*d10(:,7) + d10(:,8).*d10(:,8));
    dir10=(atan2(d10(:,7),d10(:,8))*180./pi) - 26.;
    indx = find(dir10 < 0. & dir10 >= -180.);
    if length(indx)
        dir10(indx) = 360. + dir10(indx);
    end

    d22 = load(name22);
    yd22 = datenum(d22(:,1),d22(:,2),d22(:,3),d22(:,4),d22(:,5),d22(:,6))-datenum(d22(:,1)-1,12,31);
    s22=sqrt(d22(:,7).*d22(:,7) + d22(:,8).*d22(:,8));
    dir22=(atan2(d22(:,7),d22(:,8))*180./pi) - 26.;
    indx = find(dir22 < 0. & dir22 >= -180.);
    if length(indx)
        dir22(indx) = 360. + dir22(indx);
    end

    %  Plot Temperatures
    if pltflg % do plots if pltflg=1
        figure('Position',[10 750 650 650]);
        plot(yd10,d10(:,10),'k')
        hold
        plot(yd22,d22(:,10),'r')
        legend('10','22','Location','Best','Orientation','horizontal');
        legend('boxoff');
        ylabel('T (C)')
        xlabel('Year Day')
        mx = max(yd10);
        xlim([yd10(1) mx])
        blims(1) = min(d10(:,10))-5;
        elims(1) = max(d10(:,10))+5;
        blims(2) = min(d22(:,5))-5;
        elims(2) = max(d22(:,5))+5;
        ylim([min(blims) max(elims)])
        stitle = sprintf('BAO UMASS North %4d-%02d-%02d', dv(1),dv(2),dv(3));
        title(stitle);
        if pngflg
            pngpath = sprintf('%sUMPSD_%4d%02d%02d_10_22_T.png',outpath,dv(1),dv(2),dv(3));
            f=getframe(gcf);
            imwrite(f.cdata,pngpath,'png');
        end

    %  Plot wind speed and direction
        figure('Position',[10 750 650 650]);
        subplot(2,1,1),plot(yd10,s10,'k')
        hold
        subplot(2,1,1),plot(yd22,s22,'r')

        legend('10','22','Location','Best','Orientation','horizontal');
        legend('boxoff');
        ylabel('Spd (m/s)')
        xlabel('Year Day')
        xlim([yd10(1) mx])
        blims(1) = min(s10)-5;
        elims(1) = max(s10)+5;
        blims(2) = min(s22)-5;
        elims(2) = max(s22)+5;
        ylim([min(blims) max(elims)])
        title(stitle);

        subplot(2,1,2),plot(yd10,dir10,'k')
        hold
        subplot(2,1,2),plot(yd22,dir22,'r')
        ylabel('Direction')
        xlabel('Year Day')
        xlim([yd10(1) mx])
        ylim([0 360])
        if pngflg
            pngpath = sprintf('%sUMPSD_%4d%02d%02d_10_22_WS.png',outpath,dv(1),dv(2),dv(3));
            f=getframe(gcf);
            imwrite(f.cdata,pngpath,'png');
        end

    %  Plot u, v and w at 10m
        figure('Position',[10 750 650 650]);
        plot(yd10,d10(:,7:9))
        legend('u','v','w','Location','Best','Orientation','horizontal');
        legend('boxoff');
        ylabel('Spd (m/s)')
        xlabel('Year Day')
        xlim([yd10(1) mx])
        blims = min(d10(:,7:9))-5;
        elims = max(d10(:,7:9))+5;
        ylim([min(blims) max(elims)])
        stitle = sprintf('BAO UMASS North 10m %4d-%02d-%02d', dv(1),dv(2),dv(3));
        title(stitle);
        if pngflg
            pngpath = sprintf('%sUMPSD_%4d%02d%02d_10_UVW.png',outpath,dv(1),dv(2),dv(3));
            f=getframe(gcf);
            imwrite(f.cdata,pngpath,'png');
        end

    %  Plot u, v and w at 22m
        figure('Position',[10 750 650 650]);
        plot(yd22,d22(:,7:9))
        legend('u','v','w','Location','Best','Orientation','horizontal');
        legend('boxoff');
        ylabel('Spd (m/s)')
        xlabel('Year Day')
        xlim([yd22(1) mx])
        blims = min(d22(:,7:9))-5;
        elims = max(d22(:,7:9))+5;
        ylim([min(blims) max(elims)])
        stitle = sprintf('BAO UMASS North 22m %4d-%02d-%02d', dv(1),dv(2),dv(3));
        title(stitle);
        if pngflg
            pngpath = sprintf('%sUMPSD_%4d%02d%02d_22_UWV.png',outpath,dv(1),dv(2),dv(3));
            f=getframe(gcf);
            imwrite(f.cdata,pngpath,'png');
        end

    %  Plot number of points for 10 and 22 m
        figure('Position',[10 750 650 650]);
        plot(yd10,d10(:,11),'ko')
        hold
        plot(yd22,d22(:,11),'r.')
        legend('10','22','Location','Best','Orientation','horizontal');
        legend('boxoff');
        ylabel('Number of points')
        xlabel('Year Day')
        xlim([yd10(1) mx])
        ylim([0 34]);
        stitle = sprintf('BAO UMASS North sonic npt %4d-%02d-%02d', dv(1),dv(2),dv(3));
        title(stitle);
        if pngflg
            pngpath = sprintf('%sUMPSD_%4d%02d%02d_10_22_N.png',outpath,dv(1),dv(2),dv(3));
            f=getframe(gcf);
            imwrite(f.cdata,pngpath,'png');
        end
    end
    
    clear d10 d22 yd10 yd22 s10 dir10 indx s22 dir22
    close all

end
retval = 0;