%rd_lights
% Plot light status file from BAO

function [retval] = baotls_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\Lights\Data\';
    outpath  = 'z:\Tower\Lights\images\';
else
    inpath = '/archive/Lights/Data/';
    outpath  = '/archive/Tower/Lights/images/';
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]);

%1    2   3   4     5     6    7   8     9     10     11    12    13    14
%ID  YR  YD  hhmm  L1   L2    L3   L4    L5    L6     L7    L8    L9   Bat
%12,2009,8,1731,-.413,-.446,-.392,-.432,-.398,-.453,-.389,-.451,-.361,13.7
%12,2009,8,1741,-.413,-.446,-.392,-.433,-.397,-.453,-.389,-.451,-.361,13.66
%12,2009,8,1751,-.413,-.446,-.391,-.433,-.397,-.453,-.388,-.452,-.36,13.63

for i = bdn:edn
    dv=datevec(i);
    if i == bdn && dv(4) > 0
        continue
    end
    yday = md2yd( dv(1), dv(2), dv(3) );
    name10 = sprintf('%sBAO_TLS_%4d%03d.dat', inpath, dv(1), yday );
    fprintf(1,'*** Processing TLS daily ***\n');
    
    fid10 = fopen( name10 );
    if( fid10 > 0 )
        fclose( fid10 );
    else
        fprintf(1,'No  10m: %s\n',name10);
        continue;
    end
    
    data10 = load(name10);    %load 1 day of PSD mean logger data
    
    hhmm = data10(:,4);
    hh = fix(hhmm/100);
    mm = hhmm - (hh*100);
    data10(:,3)=data10(:,3)+(((mm/60.)+hh)/24)+datenum(data10(:,2)-1,12,31); %decimle year day
    clear hhmm hh mm
    
    if pltflg %skip plotting
        
        figure1 = figure('Position',[100 50 650 650]);
        subplot(3,1,1),plot(data10(:,3),data10(:,5:7))
        ylim([-2 2])
        xlim([data10(1,3),data10(1,3)+1])
        stitle=sprintf('BAO Tower Lights Status %4d-%02d-%02d',...
                       dv(1),dv(2),dv(3));
        title(stitle)
        ylabel('Volts')
        xlabel('Hour (UTC)')
        datetick('x','HH','keeplimits');     
        legend('1','2','3','Location','BestOutside');
        
        subplot(3,1,2),plot(data10(:,3),data10(:,8:10))
        ylim([-2 2])
        xlim([data10(1,3),data10(1,3)+1])
        ylabel('Volts')
        xlabel('Hour (UTC)')
        datetick('x','HH','keeplimits');     
        legend('4','5','6','Location','BestOutside');
        
        subplot(3,1,3),plot(data10(:,3),data10(:,11:13))
        ylim([-2 2])
        xlim([data10(1,3),data10(1,3)+1])
        ylabel('Volts')
         xlabel('Hour (UTC)')
        datetick('x','HH','keeplimits');     
        legend('7','8','9','Location','BestOutside');
        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_012_%4d%03d_LTS.png', outpath, dv(1), yday );
            set(gcf,'PaperPositionMode','auto');
            print('-dpng', print_buffer);
            %f=getframe(gcf);
            %imwrite(f.cdata,print_buffer,'png');
        end
        
    end % skip plotting
    
    clear data10 yday
    close all
    
end % loop through all files

clear all

retval = 0;

