function [hrstr] = struct_hourly_avg(minstr)
% get hourly average (hrstr) from nominally 1 minute input struct 
% input struct **must** contain mday 
%
% NOT INTENDED FOR USE ON DIRECTIONAL VARIALBES (gyro, wndd)
% For winds and currents, generate vectors (md2vect) first 


% 20080729: note
% to do 2-minute data, move the creation of the
% 2-d array to the top, and interp that first.
%
% Dbug=1;
Dbug=0;
onemin = 1/(60 * 24);
mday=minstr.mday;
try
 minstr=rmfield(minstr,{'year' 'month' 'day' 'hour' 'minu' 'sec'});
catch
    
end
ntpts=length(mday);
pts=1:length(mday);
fno=fieldnames(minstr);
inarr=ones(length(mday),length(fno));
iofldno=1;
for ii=1:size(fno,1)
    field_name=fno{ii};
    
    cmd=['tempvar =  minstr.' field_name ';' ];
   % disp(['cmd: ' cmd ' field_name: ' field_name])
    eval (cmd);
    nanis = find(isnan(tempvar)   ==   1); 
    if length(nanis) < length(tempvar), 
       tempvar(nanis(2:end)) = tempvar(nanis(2:end) - 1);
       nanis = find(isnan(tempvar)   ==   1); 
       if nanis, 
           fprintf(' nans in %s ', field_name);
           try
            tempvar(nanis) = tempvar(nanis + 1); 
           catch
%            disp('can not fix nans');
           fprintf('can not fix. %d pts, %d nans, first nan %d last %d\n',...
               length(tempvar),length(nanis),nanis(1),nanis(end));
           end
       end
    end
    [m,n]=size(tempvar);
    if Dbug ==1,
      fprintf(' size %d %d\n',n,m);  end
    if m == ntpts && n == 1
            % time is first dimension
            cmd=sprintf( 'inarr(:,%d)= tempvar;',iofldno);
            eval(cmd);
            iofldno=iofldno+1;
         
    else
        if n == ntpts && m == 1 ,
            % time is second dimension
           cmd=sprintf( 'inarr(:,%d)= tempvar'';',iofldno);
            eval(cmd);
           iofldno=iofldno+1;
        else
            % if no dimension matches time, or if the field is 2 dimensional,
            % don't sort
            if m==ntpts || n ==ntpts
                disp(['can not sort ' field_name ' 2d field !!!'])
            end
            cmd=['nstruct.' field_name ' =  tempvar;' ];
            eval(cmd);
        end
    end
    
    % disp(['cmd is ' cmd])
    eval(cmd);
   
end

% grid/interp to 1 minute
% yi = interp1(x,Y,xi,method)
myd = minstr.mday(pts(1)):onemin:minstr.mday(pts(end));
% fprintf('length myd %d size m
% this generates errors if here are nans
min_arr=interp1(minstr.mday,inarr, myd,'linear');

dv=datevec(myd);  % size of dv is npts,6
% number of points
np = length(myd);
if (np  < 60),
    fprintf('less than an hour, cannot average\n');
    hrstr = [];
else
    % find first,last, to have actual hourly averages
    stp=find(dv(:,5) == 0,1,'first');
    ept=find(dv(:,5) == 59,1,'last');
    pts=stp:1:ept;
    if (length(pts)  < 60),
        fprintf('less than an hour, cannot average\n');
        hrstr = [];
    else
        % fprintf('start %d end %d length\n',stp,ept,length(pts));
        % truncate all the fields, place into an array
        % want the data array to be [np,nv] = size(dd);
        min_arr=min_arr(pts,:);

        % now have truncated minute data, get hourly values
        [np,nv] = size(min_arr);
        nhrs = floor(np/60);
        npts=nhrs*60;

        hr_arr=ones(nhrs,nv);
        %hryday = reshape(myd(1:npts),60,nhrs);
        for ii=1:nv,
            vv=min_arr(1:npts,ii);
            tmp = reshape(vv,60,nhrs);
            hr_arr(:,ii) = mean(tmp)';
        end
        % now, back to a struct
        for ii=1:size(fno,1)
            field_name=fno{ii};
            cmd=['hrstr.' field_name ' =  hr_arr(:,' num2str(ii) ');' ];
            eval(cmd);
        end
        % does not work:hrstr.yday=mean(hryday)';
    end
end





