function hax = roms_timebar(dnum,hax)
% Place a Jay O'Reilly style time bar showing year and month on the plot
% 
% John Wilkin

% should make this more flexible by allowing the time bar to optionally
% show a user defined range of dates e.g. multiple years, short number of
% days within a few months, etc

currentaxes = gca;

if nargin < 2
  % create the axes for the timebar
  hax = axes;
  set(hax,'pos',[.2 .89 .25 .03])
end

axes(hax)

% segments for time bar box
seg    = 0:(365/12):365;
subseg = 0:(365/4):365;
N = length(subseg);

% plot the boxes
hp = plot(repmat(subseg,[2 1]),[zeros([1 N]);ones([1 N])],...
  [0 max(seg)],0.99*[1 1]);
set(hp,'color','k')

% the moving bar
dv = datevec(dnum);
dm = dnum-datenum([dv(1) 1 1 0 0 0]);
hold on
hm = plot([dm dm],[0 1],'m');set(hm,'linewidth',3)
hold off

% the segment labels
ht = text(av2(seg),0.51*ones([1 12]),...
  {'J','F','M','A','M','J','J','A','S','O','N','D'});
set(ht,'horizontalalignment','center','verticalalignment','middle')
set(hax,'xlim',[0 365.1],'xtick',[],'xticklabel',[])

% the year text label
hy = ylabel(datestr(dnum,'yyyy'));
set(hy,'rotation',0,'verticalalignment','middle','horizontalalignment','right')
set(hax,'ytick',[],'yticklabel',[])

axes(currentaxes)

