% function [retval] = bao10100300_month_loop_func( by, bm, bd, ey, em, ed )
% Plot combined wind and temp/Rh monthly data from BAO data loggers
% DEW2/13/2014 changed path so it will run on my desktop PC


function [retval] = bao10100300_month_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\monthly\';
    outpath  = 'z:\Tower\Processed_Images\monthly\';
else
    inpath = '/archive/BAO/Tower/Processed/monthly/';
    outpath  = '/archive/BAO/Tower/Processed_Images/monthly/';
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 end year to process (2007) ','s');
    ems = input('Input the end 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]);
bdv=datevec(bdn);
edv=datevec(edn);
yr = bdv(1);
mo = bdv(2);

nloops = edv(2) + (edv(1) - bdv(1)) * 12 - bdv(2) + 1;

for i = 1:nloops
    name10 = sprintf('%sBAO_010_%4d%02d.dat', inpath, yr, mo );
    name100 = sprintf('%sBAO_100_%4d%02d.dat', inpath, yr, mo );
    name300 = sprintf('%sBAO_300_%4d%02d.dat', inpath, yr, mo );
    fprintf(1,'*** Processing 10m, 100m and 300m monthly ***\n');
    
    %%%%%%%10m data
    %1    2   3   4     5     6      7      8       9      10        11     12     13    14
    %ID  YR  YD  hhmm  T      RH     WSv   WSs     Dir    WDt       Bat    press  precip WDT
    %10,2007,165,1405,15.095,50.858,1.810,-999.000,238.61,-999.0000,13.515,843.01,0.000,212.610
    %10,2007,165,1406,15.115,50.448,1.692,-999.000,245.47,-999.0000,13.514,842.98,0.000,219.470
    
    %%%%%%%100m data
    %1    2   3   4     5     6      7      8       9      10        11     12     13    14
    %ID  YR  YD  hhmm  T      RH     WSv   WSs     Dir    WDt       Bat    press  precip WDT
    %100,2007,244,5,29.848,19.414,0,0,0,0,13.132
    %100,2007,244,6,29.849,19.333,0,0,0,0,13.132
    
    
    %%%%300m data
    %"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"
    %"TS","RN","mV","Deg","Deg","mV","mV","","Deg C","Deg C","%","Counts","mV","Volts"
    %"","","WVc","WVc","WVc","Std","Avg","Avg","Avg","Avg","Avg","Avg","Smp","Min"
    %"2007-10-01 00:00:00",312159,5.06,201.1,8.43,0.523,15.68,106.923,17,17.91,23.68,0,110.7,13.26,17.8,163.1
    %"2007-10-01 00:00:30",312160,5.008,183.5,3.734,0.294,15.49,106.9227,17,17.9,25.13,0,110.7,13.26,17.8,145.5
    
    fid10 = fopen( name10 );
    if( fid10 > 0 )
        fclose( fid10 );
    else
        fprintf(1,'No  10m: %s\n',name10);
        continue;
    end
    
    fid100 = fopen( name100 );
    if( fid100 > 0 )
        have100 = 1;
        fclose( fid100 );
    else
        have100 = 0;
        fprintf(1,'No 100m: %s\n',name100);
    end
    
    fid300 = fopen( name300 );
    if( fid300 > 0 )
        fclose( fid300 );
    else
        fprintf(1,'No 300m: %s\n',name300);
        continue;
    end
    
    data10 = load(name10);
    
    if have100
        data100 = load(name100);
    end
    
    nmax = 90000;
    
    [data300 dt n] = load300( name300, nmax);
    
    hhmm = data10(:,4);
    hh = fix(hhmm/100);
    mn = hhmm - (hh*100);
    data10(:,3)=data10(:,3)+(((mn/60.)+hh)/24)+datenum(data10(:,2)-1,12,31); %Julian day
    clear hhmm hh mm
    
    if have100
        hhmm = data100(:,4);
        hh = fix(hhmm/100);
        mn = hhmm - (hh*100);
        data100(:,3)=data100(:,3)+(((mn/60.)+hh)/24)+datenum(data100(:,2)-1,12,31); %Julian day
        clear hhmm hh mm
    end
    
    if pltflg %check for plotting
        pltflg
        figure1 = figure('Position',[100 50 650 650]);
        subplot(2,1,1),plot(data10(:,3),data10(:,7))
        hold
        subplot(2,1,1),plot(data100(:,3),data100(:,7),'k')
        subplot(2,1,1),plot(dt(1:n,7),data300(1:n,1),'r')
        stitle=sprintf('BAO %4d-%02d 10m, 100m and 300m', yr, mo);
        title(stitle)
        ylabel('Speed (m/s)')
        elim = max(max(data10(:,7)),max(data300(1:n,1)))+2.0;
        xlabel('Day (UTC)')
        datetick('x','mm/dd','keeplimits','keepticks');     
        legend('10m','100m','300m','Location','NorthEast','Orientation','horizontal');
        legend('boxoff');
        axis tight
        grid on
        ylim([0 elim])
        
        subplot(2,1,2),plot(data10(:,3),data10(:,12),'bx','MarkerSize',4)
        hold
        subplot(2,1,2),plot(data100(:,3),data100(:,12),'kx','MarkerSize',4)
        subplot(2,1,2),plot(dt(1:n,7),data300(1:n,14),'rx','MarkerSize',4)
        set(gca,'YTick',[0 90 180 270 360])
        ylabel('Dir (True)')
        xlabel('Day (UTC)')
        datetick('x','mm/dd','keeplimits','keepticks');     
        axis tight
        grid on
        ylim([0 360])
        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_10300_%4d%02d_WS.png',...
                                   outpath, yr, mo );
            set(gcf,'PaperPositionMode','auto');
            print('-dpng', print_buffer);
        end
        
%        if have100
            figure2 = figure('Position',[100 50 650 650]);
            subplot(2,1,1),plot(data10(:,3),data10(:,5))
            hold
            subplot(2,1,1),plot(data100(:,3),data100(:,5),'k')
            subplot(2,1,1),plot(dt(1:n,7),data300(1:n,13),'r')
            stitle=sprintf('BAO %4d-%02d 10m, 100m and 300m', yr, mo);
            title(stitle)
            ylabel('Temp (C)')
            %gis = find( data10(:,5) > -50. );
            gih = find( data100(:,5) > -50. );
            gip = find( data300(:,13) > -50. );
            blims(1) = min(data300(gip,13))-5;
            elims(1) = max(data300(gip,13))+5;
            blims(2) = min(data100(gih,5))-5;
            elims(2) = max(data100(gih,5))+5;
            %blims(1) = min(data10(gis,5))-5;
            %elims(1) = max(data10(gis,5))+5;
            xlabel('Day (UTC)')
            datetick('x','mm/dd','keeplimits','keepticks');
            legend('10m','100m','300m','Location','NorthEast','Orientation','horizontal');
            legend('boxoff');
            axis tight
            grid on
            ylim([min(blims) max(elims)])
            
            subplot(2,1,2),plot(data10(:,3),data10(:,6))
            hold
            subplot(2,1,2),plot(data100(:,3),data100(:,6),'k')
            subplot(2,1,2),plot(dt(1:n,7),data300(1:n,9),'r')
            ylabel('RH (%)')
            xlabel('Day (UTC)')
            datetick('x','mm/dd','keeplimits','keepticks');
            axis tight
            grid on
            ylim([0 105])
            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_10100300_%4d%02d_TRH.png',...
                    outpath, yr, mo );
                set(gcf,'PaperPositionMode','auto');
                print('-dpng', print_buffer);
            end
        %end % if have 100m data
        
    end % skip plotting
    
    %clear data300 dt data10 data100
    %close all
    
    mo = mo + 1;
    if mo > 12
        mo = mo - 12;
        yr = yr + 1;
    end
    
end % loop through all files

%clear all;
%save data10.mat data10
%whos data*
retval = 0;
