function avg_hr(infile,outfile)

in = netcdf(infile,'nowrite'); %open the hr3 nc file
vnames = ncnames(var(in)); %get the varnames
v = var(in);
d_in = dim(in); %find where dim vars stop
d_names = ncnames(d_in);
rec_var = length(d_in) + 1; %find start of record variables
a=att(in);
d_time = strmatch('time',vnames); %find the dim vars that have to change

HR = struct([]); %create a stucture to hold the nc vals

for i=1:length(vnames)
    HR=setfield(HR,{1},char(vnames(i)),[]);
    
    if i < rec_var & (sum(i~=d_time) == length(d_time))
        HR=setfield(HR,char(vnames(i)),in{i}(:));
    end
       
end;

%start averaging routine

TimeLim = 2/24; %2 hours												     % Look for data +/- 2 hours from Noon
Fix = .25/24;
epoch = datenum('01 Jan 1970 00:00:00');


for tv_ctr = 1:length(d_time)
    tv_name = ncnames(in{d_time(tv_ctr)});
    tv_size = length(in{char(tv_name)});
    t=utc2sdn(in{d_time(tv_ctr)}(:)); %get the first time variable
    [Year, Month, Day, Hour, Minute, Second] = datevec(t);  % Get the year 
    Hour = Hour + (Minute + Second./60)./60;			  		  % Get the decimal hour of the day 
    lon  = in{'longitude'}(:);	   		 						  % Get the longitude (degrees)
    Day  = date2jul(t); 										        % Get the day of the year
    index = 0;
    
    %Pretty GUI So that you know whats going on
    WB = waitbar(0,'Processing...');
    set(WB,'Name',['Working with ' upper(char(tv_name)) ' Variables']);
    Child_WB1 = get(WB,'Children');
    Child_WB2 = get(Child_WB1,'Children');
    set(Child_WB2(2),'FaceColor',[0.8 0.8 0.8],'EdgeColor',[0.8 0.8 0.8]);
    waitbar(100);
    
    for y = min(Year):max(Year)		 	 % Loop through each year
        id = find(Year == y);
        
        for d = min(Day(id)):max(Day(id)) % Loop from the earliest to the lastest day found in each year
            LAN = noon(jul2date(d,y),lon,1);								  % Local Area Noon in GMT
            LAN = jul2date(d,y) + LAN/24; 
            StartTime = (LAN - abs(TimeLim));       				  	  % in hours
            EndTime   = (LAN + abs(TimeLim));       				  	  % in hours
            i = find(t >= StartTime & t <= EndTime ); % Find all points on the desired day
            %i = find(Year == y & Day == d & Hour >= StartTime & Hour <= EndTime ); % Find all points on the desired day
            if ~isempty(i)
                index = index + 1;
                HR = setfield(HR,char(tv_name),{index},(mean(t(i))- epoch)*24*60*60);
                for n = rec_var:length(vnames)
                    wbtitle = sprintf('%s',datestr(mean(t(i))));
                    waitbar(100,WB,wbtitle);
                    d_var =  ncnames(dim(in{n}));
                    match = sum(strmatch(tv_name,d_var));
                    
                    if match
                            
                            d_size = size(in{n});
                            
                            if d_size(1) == tv_size
                                dat = in{n}(i);             
                                bad = find(dat == -999);
                                dat(bad) = NaN;
                                dat = nanmean(dat);
                                
                                if (isnan(dat) | isempty(dat))
                                    HR=setfield(HR,{1},char(vnames(n)),{index},-999);
                                else
                                    HR=setfield(HR,{1},char(vnames(n)),{index},dat);
                                end
                                
                            else
                                
                                dat = (in{n}(1:d_size(1),i))';
                                bad = find(dat == -999);
                                dat(bad) = NaN;
                                dat = nanmean(dat);
                                
                                if (isnan(dat) | isempty(dat))
                                    HR=setfield(HR,{1},char(vnames(n)),{1:d_size(1),index},-999);
                                else
                                    HR=setfield(HR,{1},char(vnames(n)),{1:d_size(1),index},dat);
                                end
                                
                            end %size
                    end %match
                end %n to vnames
            end %if isempty(i)
        end  %day    
    end %year
    close(WB);
end % tv_ctr

%make the netcdf
fprintf('Writing to file.')
out = netcdf(outfile,'clobber');

   % Copy global attributes
   if ~isempty(a)
      for i = 1:length(a)
          fprintf('.')
         copy(a{i}, out);   
      end
   end

   out.dataComment0 = ['This archive contains averaged values from data in ' infile];
   
   % Copy the good dimensions make the new avg sizes
   for i = 1:length(d_in)
      if i (sum(i~=d_time) == length(d_time))
         copy(d_in{i}, out)
         fprintf('.')
      else
         make_d = sprintf('out(''%s'')=%d',char(d_names(i)),length(getfield(HR,char(d_names(i)))));
         trash = evalc(make_d);
         fprintf('.')
         clear trash;
      end
   end
   
   %Copy good variables make the new avg vars
    for i=1:length(vnames)
        if i < rec_var & (sum(i~=d_time) == length(d_time))
            copy(v{i}, out, 1, 1); %vars with in data
            fprintf('.')
        else
           copy(v{i}, out, 0, 1);
           out{char(vnames(i))}(:) = getfield(HR,char(vnames(i)));
           fprintf('.')
       end
   end
   
   ncquit(out);