% Plot UM 100m logger data
% 150m sonic 1, 100m sonic 3, 50m sonic 5 even = S
% 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] = UM50100150S_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 beginning year to process (2007) ','s');
    bms = input('Input beginning month to process (1-12) ','s');
    bds = input('Input beginning day to process (1-31) ','s');
    eys = input('Input ending year to process (2007) ','s');
    ems = input('Input ending month to process (1-12) ','s');
    eds = input('Input ending 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]);

for i = bdn:edn
    dv=datevec(i);
    name50 = sprintf('sonic_050s_%4d%02d%02d.dat',dv(1),dv(2),dv(3));
    name100 = sprintf('sonic_100s_%4d%02d%02d.dat',dv(1),dv(2),dv(3));
    name150 = sprintf('sonic_150s_%4d%02d%02d.dat',dv(1),dv(2),dv(3));

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

    fid50 = fopen( name50 );
    if( fid50 > 0 )
        fclose( fid50 );
    else
        fprintf(1,'No 50m: %s\n',name50);
        continue;
    end

    fid100 = fopen( name100 );
    if( fid100 > 0 )
        fclose( fid100 );
    else
        fprintf(1,'No 100m: %s\n',name100);
        continue;
    end

    fid150 = fopen( name150 );
    if( fid150 > 0 )
        fclose( fid150 );
    else
        fprintf(1,'No 150m: %s\n',name150);
        continue;
    end

    % 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

    d50 = load(name50);
    yd50 = datenum(d50(:,1),d50(:,2),d50(:,3),d50(:,4),d50(:,5),d50(:,6))-datenum(d50(:,1)-1,12,31);
    s50=sqrt(d50(:,7).*d50(:,7) + d50(:,8).*d50(:,8));
    dir50=(atan2(d50(:,7),d50(:,8))*180./pi);
    indx = find(dir50 < 0. & dir50 >= -180.);
    if length(indx)
        dir50(indx) = 360. + dir50(indx);
    end
    dir50 = dir50 - 206.;
    indx = find(dir50 < 0.);
    if length(indx)
        dir50(indx) = 360. + dir50(indx);
    end

    d100 = load(name100);
    yd100 = datenum(d100(:,1),d100(:,2),d100(:,3),d100(:,4),d100(:,5),d100(:,6))-datenum(d100(:,1)-1,12,31);
    s100=sqrt(d100(:,7).*d100(:,7) + d100(:,8).*d100(:,8));
    dir100=(atan2(d100(:,7),d100(:,8))*180./pi);
    indx = find(dir100 < 0. & dir100 >= -180.);
    if length(indx)
        dir100(indx) = 360. + dir100(indx);
    end
    dir100 = dir100 - 206.;
    indx = find(dir100 < 0.);
    if length(indx)
        dir100(indx) = 360. + dir100(indx);
    end

    d150 = load(name150);
    yd150 = datenum(d150(:,1),d150(:,2),d150(:,3),d150(:,4),d150(:,5),d150(:,6))-datenum(d150(:,1)-1,12,31);
    s150=sqrt(d150(:,7).*d150(:,7) + d150(:,8).*d150(:,8));
    dir150=(atan2(d150(:,7),d150(:,8))*180./pi);
    indx = find(dir150 < 0. & dir150 >= -180.);
    if length(indx)
        dir150(indx) = 360. + dir150(indx);
    end
    dir150 = dir150 - 206.;
    indx = find(dir150 < 0.);
    if length(indx)
        dir150(indx) = 360. + dir150(indx);
    end

    %  Plot Temperatures
    if pltflg % do plots if pltflg=1
        figure('Position',[10 750 650 650]);
        plot(yd50,d50(:,10),'k')
        hold
        plot(yd100,d100(:,10),'r')
        plot(yd150,d150(:,10),'b')
        legend('50','100','150','Location','Best','Orientation','horizontal');
        legend('boxoff');
        ylabel('T (C)')
        xlabel('Year Day')
        mx = max(yd50);
        xlim([yd50(1) mx])
        blims(1) = min(d50(:,10))-5;
        elims(1) = max(d50(:,10))+5;
        blims(2) = min(d100(:,5))-5;
        elims(2) = max(d100(:,5))+5;
        blims(3) = min(d150(:,5))-5;
        elims(3) = max(d150(:,5))+5;
        ylim([min(blims) max(elims)])
        stitle = sprintf('BAO UMASS South %4d-%02d-%02d', dv(1),dv(2),dv(3));
        title(stitle);
        if pngflg
            pngpath = sprintf('%sUMPSD_%4d%02d%02d_50S_100S_150S_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(yd50,s50,'k')
        hold
        subplot(2,1,1),plot(yd100,s100,'r')
        subplot(2,1,1),plot(yd150,s150,'b')

        legend('50','100','150','Location','Best','Orientation','horizontal');
        legend('boxoff');
        ylabel('Spd (m/s)')
        xlabel('Year Day')
        xlim([yd50(1) mx])
        blims(1) = min(s50)-5;
        elims(1) = max(s50)+5;
        blims(2) = min(s100)-5;
        elims(2) = max(s100)+5;
        blims(3) = min(s150)-5;
        elims(3) = max(s150)+5;
        ylim([min(blims) max(elims)])
        title(stitle);

        subplot(2,1,2),plot(yd50(:),dir50,'k')
        hold
        subplot(2,1,2),plot(yd100(:),dir100,'r')
        subplot(2,1,2),plot(yd150(:),dir150,'b')
        ylabel('Direction')
        xlabel('Year Day')
        xlim([yd50(1) mx])
        ylim([0 360])
        if pngflg
            pngpath = sprintf('%sUMPSD_%4d%02d%02d_50S_100S_150S_WS.png',outpath,dv(1),dv(2),dv(3));
            f=getframe(gcf);
            imwrite(f.cdata,pngpath,'png');
        end

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

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

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

    %  Plot number of points for 50, 100 and 150 m
        figure('Position',[10 750 650 650]);
        plot(yd50,d50(:,11),'k.')
        hold
        plot(yd100,d100(:,11),'r.')
        plot(yd150,d150(:,11),'b.')
        legend('50','100','150','Location','Best','Orientation','horizontal');
        legend('boxoff');
        ylabel('Number of points')
        xlabel('Year Day')
        mx = max(yd50);
        xlim([yd50(1) mx])
        ylim([0 34]);
        stitle = sprintf('BAO UMASS South sonics npt %4d-%02d-%02d', dv(1),dv(2),dv(3));
        title(stitle);
        if pngflg
            pngpath = sprintf('%sUMPSD_%4d%02d%02d_50S_100S_150S_N.png',outpath,dv(1),dv(2),dv(3));
            f=getframe(gcf);
            imwrite(f.cdata,pngpath,'png');
        end
    end
    
    clear d50 yd50 s50 dir50 indx
    clear d100 yd100 s100 dir100 
    clear d150 yd150 s150 dir150
    close all

end  % loop through daily files
retval = 0;
