function [nstruct] = struct_gap_nan(ostruct, nok)
% find mday gaps, insert NaNs
% nok is factor - how many missing are OK
% dt in minutes
if isfield(ostruct,'mday')
  dt=diff(ostruct.mday)*24*60;
  madd= median(diff(ostruct.mday));
else
  dt=diff(ostruct.yday)*24*60;
  madd= median(diff(ostruct.yday));
end

gaps=find(dt > (median(dt) * nok));
gaps=gaps + 1;
ngaps=length(gaps);  % need this later
gaps=[gaps length(ostruct.mday)];

if length(gaps) > 1
  fns=fieldnames(ostruct);
  for i=1:size(fns,1)
    fn=fns{i};
    eval(['invar = ostruct.' fn ';']);
    outvar=invar(1:gaps(1));
    for igg=1:(length(gaps)-1)
      outvar=[outvar invar(gaps(igg):gaps(igg+1))];
      % fprintf('igg %d gaps(igg) %d len outvar %d\n',...
      %  igg, gaps(igg),length(outvar));
    end
    if strcmp(fn,'mday') == 0 & strcmp(fn,'yday')== 0
        outvar(gaps(1:ngaps)) = NaN;
    else
        outvar(gaps(1:ngaps)) = outvar(gaps(1:ngaps))+ madd;
    end
    eval(['nstruct.' fn '= outvar;']);
  end
  
else
    nstruct=ostruct;
end

