function lltick(varargin)

% function lltick(h, 'x', 'lon' [, 'y', 'lat' [,'y', 'none'] ])
%     The first argument is an axes object handle.
%     The following arguments may be one or two pairs
%     specifying that a given axis is to be labelled
%     as degrees longitude ('lon': labels appended with 'E' or 'W' around 0),
%     degrees latitude ('lat': labels appended with 'N' or 'S' around 0),
%     or simply degrees ('none': nothing after the deg symbol (eg. heading))
%     The existing tick values are used; the existing
%     tick labels are discarded.
%     Ticks in the longitude range +-360 are handled correctly.
%

%1999/07/15 added 'none' option for other kinds of degrees  JH

if nargin ~= 3 & nargin ~= 5,
   error('lltick requires either 3 or 5 arguments.');
end

deg = setstr(176);

ha = varargin{1};

for iarg = 2:2:nargin
   XorY = lower(varargin{iarg});
   LatorLonorNone = lower(varargin{iarg+1});

   if (strcmp(XorY, 'x')),
      x = 1;
      Tick = 'XTick';
      TickLabel = 'XTickLabel';
      TickMode  = 'XTickMode';
   elseif (strcmp(XorY, 'y')),
      x = 0;
      Tick = 'YTick';
      TickLabel = 'YTickLabel';
      TickMode  = 'YTickMode';
   else
      error('looking for x or y')
   end

   lat=0;  %default is none
   lon=0;
   
   if (strcmp(LatorLonorNone, 'lon')), 
	 lon = 1; 
   elseif (strcmp(LatorLonorNone, 'lat')) 
	 lat = 1;
   else
	 if ~strcmp(LatorLonorNone, 'none')
	   error('looking for ''lat'' or ''lon'' or ''none'' ');
	 end
     %  i.e. if none, lat==0 and lon==0
   end

   ticks     = get(ha, Tick);
   nr = length(ticks);

   EWNS = ' ';
   EWNS = EWNS(ones(nr,1));

   if lon == 1,
      iwrap = find(ticks > 180 & ticks < 360);
      ticks(iwrap) = ticks(iwrap) - 360;
      iwrap = find(ticks < -180 & ticks > -360);
      ticks(iwrap) = ticks(iwrap) + 360;
      iplus = find(ticks > 0 & ticks < 180);
      iminus = find( ticks < 0 & ticks > -180);
      EWNS(iplus) = 'E';
      EWNS(iminus) = 'W';
   elseif lat==1
      iplus = find(ticks > 0 & ticks <= 90);
      iminus = find( ticks < 0 & ticks >= -90 );
      EWNS(iplus) = 'N';
      EWNS(iminus) = 'S';
	else
	  %do nothing
	end   

   % Note: we are deliberately NOT labelling 0 or 180 with a letter.
   
   if (strcmp(LatorLonorNone, 'none')), 
	 for ii = 1:nr,
	   newlabels{ii} = deblank([num2str(ticks(ii)) deg EWNS(ii)]);
	 end
   else
	 for ii = 1:nr,
	   newlabels{ii} = deblank([num2str(abs(ticks(ii))) deg EWNS(ii)]);
	 end
   end
   

   % If we don't set TickMode to manual, the ticks might be 
   % changed when the figure is resized or printed.
   set(ha, TickMode, 'manual', TickLabel, newlabels);

end
