function [hrstr] = structHavg(minstr)
%
% get hourly average from nominally 1 minute data
% minstr must contain mday 
% any directional params will be bogus in the returned structure; be sure to generate
% vectors before averaging!!! see md2vect
%
% to do 2-minute data, move the creation of the
% 2-d array to the top, and interp that first.

% filter to 1 minute
onemin = 1/(60 * 24);

% interp data to true 1 minute
% yi = interp1(x,Y,xi,method)
myd = minstr.mday(1):onemin:minstr.mday(end);
nptsmin=length(myd);
fno=fieldnames(minstr);
for i=1:size(fno,1)
    f_n=fno{i};
    if ~ strcmp(f_n,'mday'),
      eval(['tmp=minstr.' f_n ';']);
      %if find(isnan(tmp)==1) < length(tmp)
      goodvals=find(isnan(tmp)==0);
      if  length(goodvals) == length(tmp)
          % all good
         tint=interp1(minstr.mday, tmp, myd, 'linear');
         eval(['minstr.' f_n '= tint;' ]);
      else
          if goodvals
              % some good values
              tint=interp1(minstr.mday(goodvals), tmp(goodvals), myd, 'linear');
              eval(['minstr.' f_n '= tint;' ]);
          else
              % all bad
             eval(['minstr.' f_n '= ones(nptsmin,1)*NaN;']);
          end
      end
    end
end
minstr.mday=myd;
  
% fprintf('length myd %d size m
% min_arr=interp1(minstr.mday,inarr, myd,'linear');

% now truncate to even hours
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));
        for i=1:size(fno,1)
            f_n=fno{i};
            cmd=sprintf(...
                'minstr.%s= minstr.%s(%d:%d);', f_n,f_n,stp,ept);
            eval(cmd);
        end
        % now have truncated minute data, get hourly values
        np = length(minstr.mday);
        nhrs = floor(np/60);
        npts=nhrs*60;

        %hryday = reshape(myd(1:npts),60,nhrs);
         for i=1:size(fno,1)
            f_n=fno{i};
            cmd=sprintf( 'tmp= minstr.%s(1:npts);', f_n);
            eval(cmd);
            t2=reshape(tmp,60,nhrs);
            t3=mean(t2)';
            cmd=sprintf(...
               'hrstr.%s = t3;',f_n);
           eval(cmd)
        end
       
        % does not work:hrstr.yday=mean(hryday)';
    end
end



