%BAOPLT300_loop_func
% Plot data from GMD 300m BAO data loggers
% Dave has split these data into daily files and corrected the PRT and Wind Dir
%You are correct on all counts.  yes, m/s on the wind speed.
%The PRT temperature is (accurate to within 0.1 deg C) is:  a0 +
%a1*(D3_PRTRES_Avg) + a2*(D3_PRTRES_Avg)^2 + a3*(D3_PRTRES_Avg)^3,
%a0 =    -246.058
%a1 =      2.36626
%a2 =  0.000923807
%a3 =  2.43906e-007

%The sonic anemometer is on the south boom
% The north pole points to 322 degrees.
%The the true wind direction should then be (SE2_SONICWD_Avg) - 38 degrees
%
% 300m
%"TOA5","CR1000","CR1000","2846","CR1000.Std.11","CPU:060516b_Met.CR1","10044","wkt30m"
%"TIMESTAMP","RECORD","SE1_SONICWS_Avg","SE2_SONICWD_Avg","SE2_SONICWD_Std","SE1_SONICWS_Std",
%"SE3_SONICTEMP_Avg","D3_PRTRES_Avg","D3_PRTTEMPAPPROX_Avg","SE4_HMPTEMP_Avg","SE9_HMPRH_Avg",
%"P1_AIRFLOW_Avg","SE10_ACPOWER","BT_BATVOLT_Min","PRT_AVG","SONICWDT_AVG"
%"TS","RN","ms","Deg","Deg","ms","mV","","Deg C","Deg C","%","Counts","ms","Volts","DegC","Deg True"
%"2007-06-14 14:22:00",0,3.37,230.5,0.502,0.012,13.42,105.8471,14.24,15.09,42.15,0,110.6,13.31,15.0,192.5
%"2007-06-14 14:22:30",1,3.418,231.1,0.269,0.026,13.38,105.8718,14.3,15.11,42.26,0,110.6,13.31,15.1,193.1
% DEW2/13/2014 changed path so it will run on my desktop PC

function [retval] = BAOPLT300_loop_func( by, bm, bd, ey, em, ed ) 
%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

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

nmax = 3000;

for i = bdn:edn
    dv=datevec(i);
    if i == bdn && dv(4) > 0
        continue
    end
    yday = md2yd( dv(1), dv(2), dv(3) );
    name300 = sprintf('%sBAO_300_%4d%03d.dat', inpath, dv(1), yday );
    fprintf(1,'*** Processing 300m daily ***\n');
    
    fid300 = fopen( name300 );
    if( fid300 > 0 )
        fclose( fid300 );
    else
        fprintf(1,'No 300m: %s\n',name300);
        continue;
    end

    [data300 dt n] = load300( name300, nmax);

    jdb = fix(dt(1,7));
    jde = fix(dt(n,7)) + 1;

    if pltflg % do plots if pltflg=1
        figure1 = figure('Position',[100 50 650 650]);
        subplot(2,1,1),plot(dt(1:n,7),data300(1:n,5),'k')
        hold
        subplot(2,1,1),plot(dt(1:n,7),data300(1:n,8),'g')
        subplot(2,1,1),plot(dt(1:n,7),data300(1:n,13),'b')
        stitle=sprintf('BAO %4d-%02d-%02d 300m', dv(1),dv(2),dv(3));
        title(stitle)
        ylabel('T (C)')
        %gis = find( data300(:,5) > -50. );
        %gis = find( data300(:,8) > -50. );
        gih = find( data300(:,8) > -50. );
        gip = find( data300(:,13) > -50. );
        blims(1) = min(data300(gip,13))-5;
        elims(1) = max(data300(gip,13))+5;
        blims(2) = min(data300(gih,8))-5;
        elims(2) = max(data300(gih,8))+5;
        %blims(3) = min(data300(gis,5))-10;
        %elims(3) = max(data300(gis,5))+10;
        ylim([min(blims) max(elims)])
        xlim([jdb,jde])
        xlabel('Hour (UTC)')
        datetick('x','HH','keeplimits');     
        legend('Sonic T','HMP','PRTc','Location','Best','Orientation','horizontal')
        legend('boxoff');
        grid on

        subplot(2,1,2),plot(dt(1:n,7),data300(1:n,9))
        ylim([0,109])
        xlim([jdb,jde])
        ylabel('Rh (%)')
        xlabel('Hour (UTC)')
        datetick('x','HH','keeplimits');     
        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_300_%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(dt(1:n,7),data300(1:n,1))
        title(stitle)
        ylim([0,max(data300(1:n,1))+5])
        xlim([jdb,jde])
        ylabel('Spd (m/s)')
        xlabel('Hour (UTC)')
        datetick('x','HH','keeplimits');     
        grid on
        subplot(2,1,2),plot(dt(1:n,7),data300(1:n,14),'bx','MarkerSize',4)
        xlim([jdb,jde])
        ylim([0 360])
        ylabel('Dir (true)')
        xlabel('Hour (UTC)')
        datetick('x','HH','keeplimits');     
        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_300_%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]);
        plot(dt(1:n,7),data300(1:n,5),'k')
        hold
        plot(dt(1:n,7),data300(1:n,7),'r')
        plot(dt(1:n,7),data300(1:n,8),'b')
        plot(dt(1:n,7),data300(1:n,13),'g')
        title(stitle)
        %gis = find( data300(:,5) > -50. );
        gie = find( data300(:,7) > -50. );
        gih = find( data300(:,8) > -50. );
        gip = find( data300(:,13) > -50. );
        blims(1) = min(data300(gip,13))-10;
        elims(1) = max(data300(gip,13))+10;
        blims(2) = min(data300(gie,7))-10;
        elims(2) = max(data300(gie,7))+10;
        blims(3) = min(data300(gih,8))-10;
        elims(3) = max(data300(gih,8))+10;
        %blims(4) = min(data300(gis,5))-10;
        %elims(4) = max(data300(gis,5))+10;
        ylim([min(blims) max(elims)])
        xlim([jdb,jde])
        ylabel('T (C)')
        xlabel('Hour (UTC)')
        datetick('x','HH','keeplimits');     
        legend('Sonic T','PRT est','HMP T','PRT corrected','Location','Best','Orientation','horizontal')
        legend('boxoff');
        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_300_%4d%03d_Tall.png',...
                                   outpath, dv(1), yday );
            set(gcf,'PaperPositionMode','auto');
            print('-dpng', print_buffer);
            %f=getframe(gcf);
            %imwrite(f.cdata,print_buffer,'png');
        end
    % Scatter plot of HMP vs corrected PRT at 300m
        figure4 = figure('Position',[100 50 650 650]);
        plot(data300(1:n,8),data300(1:n,13),'k.')
        xlabel('HMP (C)')
        ylabel('PRT (C)')
        title(stitle)
        axis('square')
        grid on
        annotation(figure4,'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_300_%4d%03d_HMPPRT.png',...
                                   outpath, dv(1), yday );
            set(gcf,'PaperPositionMode','auto');
            print('-dpng', print_buffer);
            %f=getframe(gcf);
            %imwrite(f.cdata,print_buffer,'png');
        end
    end % if doing plots

    clear data300 dt
    close all
    
end % loop though each day

clear all

retval = 0;
