function [RL,med,an]=RL_avg(iiAN)
%
%
% COPYRIGHT (c) 2008
% 
% Applied Research Laboratories, The University of Texas at Austin
% (ARL:UT)
% All rights reserved.
% 
% This material may be reproduced by or for the U.S. Government
% only. This notice must appear in all copies of this file and its
% derivatives. Further distribution outside the U.S. Government is
% prohibited without the explicit written authorization from the
% Director of the Environmental Sciences Laboratory, ARL:UT.
% 
% THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
% EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO THE
% IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
% PURPOSE, OR NONINFRINGEMENT.
% 
% IN NO EVENT WILL ARL:UT BE LIABLE FOR ANY DAMAGES WHATSOEVER ARISING
% OUT OF THE USE OR INABILITY TO USE THIS SOFTWARE, EVEN IF ARL:UT HAS
% BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
if nargin<1, iiAN=0; end
%
view_global
%
D=dir('*RL_AN*.mat');
A=dir('*all_an*AN*.mat');
nfile=length(D);
for jfile=1:nfile,
    disp(['File ',int2str(jfile),': ',D(jfile).name]);
end
iif=str2num(input('Enter file numbers to process: ','s'));
D=D(iif);
%
tmin1=4; tmin2=14;
nfile=length(D);
for jfile=1:nfile,
	filename=D(jfile).name;
% Open and read HDF file:
    eval(['load ',filename]);
% Average over phone number:
    SP=data_avg(AN,[2],{},[4],0,0);
% Set up image and view:
    SP.vdims=[1 3 2]; SP.size=[8 1.5]; 
    cmax=ceil(max(SP.data(:))/5)*5;
    SP.clim=[cmax-30,cmax]; SP.ii_img=-1;
    view5(SP,[],0); RL(jfile)=SP;
% Save figure number for overlaying line plots:
    fig_no(jfile)=gcf;
% Compute Median Levels During Up Times:
    dim_lims1={[] [] [tmin1,tmin1+2] []};
    M1=data_avg(SP,3,dim_lims1,4,0,0),
    dim_lims2={[] [] [tmin2,tmin2+2] []};
    M2=data_avg(SP,3,dim_lims2,4,0,0),
    med(:,jfile)=[M1.data;M2.data];
    
    if iiAN,
        for jfile=1:nfile,
            disp(['File ',int2str(jfile),': ',D(jfile).name]);
		end
		jfile=str2num(input('Enter AN file number to process: ','s'));
% Open and read HDF file:
		filename=A(jfile).name;
        eval(['load ',filename]);
% Average over phone number:
        M1=data_avg(AN,[1 2 3],dim_lims1,[1 4 4],0,0);
        M2=data_avg(AN,[1 2 3],dim_lims2,[1 4 4],0,0);
        an(:,jfile)=[M1.data;M2.data];
    else
        an=[];
    end
    tmin1=tmin1 + 2; tmin2=tmin2+2;
end
disp('RL = '),
disp(num2str(med,3));
disp('AN = '),
disp(num2str(an,3)),
disp('RL-AN= '),
disp(num2str(med-an,3))
% Overlay line plots;
ovlay(fig_no);
close(fig_no);
