function [] = hmaxis(t,du);

%HMAXIS. Set hour:minute axis.
% hmaxis(t,du).
% Set ticklabels of the current horizontal axis as clock times (hour:min).
% Give hmaxis command after plotting.
% Input arguments:
% t: vector defining the time in (decimal) hours.
% du: optional time step in minutes (if missing, defined automatically).
% Note: it is a good idea to give du in full minutes.

% Limitation: t must be positive.

% Example:
%
% close all;
% t = 1.38:0.233:7.234; y = rand(size(t));
% subplot(311); plot(t,y,'o');
% subplot(312); plot(t,y,'o'); hmaxis(t);
% subplot(313); plot(t,y,'o'); hmaxis(t,45);

% Written in MatLab 4.1 by Ari Viljanen (FMI/GEO).

%********************************************************************************

t0 = t(1); t1 = t(length(t));

automatic=0;
if nargin==1,
  automatic=1;
elseif nargin==2,
  if du<=0 | length(du)>1, % Changed to get rid of call to isscalar
    disp(' Time step must be a positive scalar. Automatic value will be used.');
    automatic=1;
  end;
end;


% Automatic determination of the time step (vectorized version).
% In diffstep, the first column gives the difference limit t1-t0 (hours),
% and the second column gives du (minutes).

if automatic,

  diffstep =...
  [ 5/60    1;...
   10/60    2;...
   30/60    5;...
       1   10;...
	   2   15;...
	   3   20;...
	 3.9   30;...
	   7   60;...
	  12  120;...
	  18  180;...
	  24  240]; 

  timediff = diffstep(:,1);
  timestep = diffstep(:,2);

  kind = find(timediff<=(t1-t0));
  k = max([kind.' 1]);   % kind may be []

  if k==length(timediff),
    du = 60*fix((t1-t0)/8) + 60;   % experimental
  else
    du = timestep(k);
  end;

end;


% Redefine t0 to get nice tick values.

apu1 = ceil(60*rem(t0,1));
apu2 = t0 - rem(t0,1);

if du<60 & du==fix(du),
  ut = 0:du:(60-du);
  p = find(ut-apu1>=0);
  if length(p)==0, p = length(ut); end;
  t0 = apu2 + ut(p(1))/60;
elseif du>=60 & rem(du,60)==0,
  t0 = (du/60)*ceil(60*t0/du);
else
  disp(' Warning: your time step is strange. Result may not look nice.');
  t0 = (du/60)*ceil(60*t0/du);
end;
  
u = t0:du/60:t1;

uticks = setstr(zeros(length(u),5));

for n=1:length(u),
  hour = rem(floor(u(n)),24);
  sh = int2str(hour);
  if length(sh)==1, sh = ['0' sh]; end;
  minute = 60*(u(n)-floor(u(n)));
  sm = int2str(minute);
  if length(sm)==1, sm = ['0' sm]; end;
  uticks(n,:) = [sh ':' sm];
end;

set(gca,'XTick',u,'XTickLabel',uticks);
