% function yd2calab(yr);
%
% yr= 2-digit year
%
% routine to label xaxis with calender rather than yearday
%
% for cases of less than two days it puts hour labels as well
%
function yd2calab(yr);
disp('Hardcoded to "yday0" convention (ie noon Jan 1st = 0.5)');

% get the current xtick labels
xtl=get(gca,'xticklabels');
[nr,nc]=size(xtl);
%xtl=xtl(1:nr-1,:);

% calulate the number of days we have
ndays=str2num(xtl(nr,:))-str2num(xtl(1,:));


if ndays<2;
  str=num2str(ones(nr,8));
else;
  str=num2str(ones(nr,2));
end;


% define month string
mth=['  January';' February';'    March';'    April';'      May';'     June';...
     '     July';'   August';'September';'  October';' November';' December'];
dpm=[31 28 31 30 31 30 31 31 30 31 30 31];
ha=axis;

% figure the number of yeardays in the startoff year
nyd=julian(12,31,yr,2359);
if nyd>365.0;nyd=366;end;

kk=0;
for k=1:nr;

% fix the yd if it strattles the new year
  ydl=str2num(xtl(k,:));
  if ~isempty(ydl);
    if ydl>nyd;ydl=ydl-nyd;kk=kk+1;end;
    if kk==1;yr=yr+1;end;
    xtlc(k,:)=calday(yr,ydl);

    % in the case of midnight we need to correct the yearday
    if xtlc(k,4)==0&xtlc(k,5)==0;
      xtlc(k,3)=xtlc(k,3)+1;
      % correct for end of month
      if xtlc(k,3)>dpm(xtlc(k,2));
        xtlc(k,2)=xtlc(k,2)+1;
        xtlc(k,3)=1;
      end;
    end;

    % post month label
    if k==1;
      ht=text(str2num(xtl(k,:)),ha(3)-(ha(4)-ha(3))/10,[mth(xtlc(k,2),:),' ',num2str(yr)]);
    elseif k>1&xtlc(k,3)<xtlc(k-1,3);
      ht=text(str2num(xtl(k,:)),ha(3)-(ha(4)-ha(3))/10,[mth(xtlc(k,2),:),' ',num2str(yr)]);
    end;
    %  htp=get(ht,'position')
    %  set(ht,'position',[htp(1) htp(2)-(htp htp(3)]);

    % in the cast of < two days put hour labels
    if ndays<=2;
      str(k,1:2)=sprintf('%2s',num2str(xtlc(k,3))); % allows for days with one digit
      str(k,3:8)=['@',sprintf('%2s',num2str(xtlc(k,4))),...
                sprintf('%2s',num2str(xtlc(k,5))),'Z'];
    else; % make indivdual day labels
      if xtlc(k,4)==0&xtlc(k,5)==0;
        %      str(k,:)=num2str(xtlc(k,3),2); % allows for days with one digit
      else;
        str(k,:)='  ';
      end;
    end;
  end;
end;

if ndays<2;fs=8,else;fs=12;end;

xtick=get(gca,'xtick');
ax=axis;
set(gca,'xticklabels',[ ]);

[lr,lc]=size(xtick);
[lrc,lcc]=size(xtlc);
for ki=1:ceil(lrc/5):lc;
   if ndays<=2;
      text(xtick(ki),ax(3),str(ki,:),...
               'verticalalignment','top',...
      'horizontalalignment','center',...
      'fontsize',fs);
   end;
   if xtlc(ki,4)==0&xtlc(ki,5)==0&ndays>2;
     text(xtick(ki),ax(3),num2str(xtlc(ki,3)),...
      'verticalalignment','top',...
      'horizontalalignment','center',...
      'fontsize',fs);
   end;
end;

