function    x2time(ax,nmax,inter)
% @(#)x2time.m	1.1 98/03/05
% Function to convert the time axis ticks
% from seconds to the beginning of epoch (1970/01/01)
% to Universal Time
%
% ax    :    name of the input plot (set to current plot if not specified)
% nmax  :    max number of ticks to print on graph (set in line 28 if not specified)
% inter :    time interval in seconds from the beginning of epoch
%	     [ start_time end_time ]
%	     Read from the graph if not specified
%
% x2time uses Vincent Denot's routines toepoch.m and fromepoch.m to convert time
% from/to form [Year Month Day Hour Min Sec] to/from seconds to the beginning of
% epoch (1970/01/01)
%
% Philippe GAMET - EISCAT Group 96/09/20
		

  if nargin == 0
	ax = gca;
  end;

  if nargin < 2
%
%
% Set here the default max number of ticks
	nmax = 5;
%
%
  end;

  if nargin < 3
	ut = get(ax,'XLim');
	  if ( ut(1) == 0 )
	    disp('Please specify the time interval');
	    return;
	  end;
  else
	ut = inter;
	set(ax,'XLim',ut);
  end;

if nmax == 0
	set(ax,'XTickLabels','');
	return;
end;

is_time = fromepoch(ut(1));
 
ut = ut/3600; % time in hour
htot = ut(2)-ut(1);
noon = floor(ut(1)/24); % Time until the beginning of the day
ut = ut - noon*24;
hh = floor(ut);

start_h   = min(floor(ut));
end_h     = max(ceil(ut));
inter_h   = end_h - start_h;
div_index = [30 20 12 6 5 4 3 2 1];
div	  = 1;
step      = 1;

  if htot <= nmax
	ix   = 1;
	  while ( (htot*div_index(ix) > nmax) & (div_index(ix) > 1) )
		ix = ix+1;
	  end;
	div = div_index(ix);

  else
	while (htot/step > nmax)
		step = step + 1;
	end;
  end;

  hh_buffer = start_h:step:end_h;
	for i=1:length(hh_buffer)
	  hh(div*(i-1)+1:div*i) = ones(1,div)*hh_buffer(i);
	    for j=0:div-1
	      min_sh(div*(i-1)+1+j) = 60*j/div;
	    end;
	end;
 
 index = hh;
 Label = zeros(5,length(hh));
 hh = mod(hh,24);
 Label(:) = sprintf('%0.2d:%0.2d',[hh;min_sh]);
 Label=Label';
 sec = zeros(1,length(hh));
 for i=1:length(hh)
   sec(i) = toepoch([is_time(1) is_time(2) is_time(3)+fix(index(i)/24) hh(i) min_sh(i) 0]);
 end;
 
 % Now , we will add a Tick at each half hour
 % mm is 0 or 30 from start hour to end hour
 % mm = round((ut - hh)*60);
 
 set(ax,'XTickMode','manual');
 set(ax,'XTick',sec);
 set(ax,'XTickLabel',Label);




