function lltick(varargin)

% function lltick(h, 'x', 'lon' [, 'y', 'lat' ])
%     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 or degrees latitude.
%     The existing tick values are used; the existing
%     tick labels are discarded.
%     Ticks in the longitude range +-360 are handled correctly.
%

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});
   LatorLon = 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


   if (strcmp(LatorLon, 'lon')), 
      lon = 1; 
   elseif (strcmp(LatorLon, 'lat')) 
      lon = 0;
   else
      error('looking for lat or lon');
   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';
   else
      iplus = find(ticks > 0 & ticks <= 90);
      iminus = find( ticks < 0 & ticks >= -90 );
      EWNS(iplus) = 'N';
      EWNS(iminus) = 'S';
   end   

   % Note: we are deliberately NOT labelling 0 or 180 with a letter.

   for ii = 1:nr,
      newlabels{ii} = deblank([num2str(abs(ticks(ii))) deg EWNS(ii)]);
   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
