Index: /issm/trunk/src/m/model/plot/applyoptions.m
===================================================================
--- /issm/trunk/src/m/model/plot/applyoptions.m	(revision 6641)
+++ /issm/trunk/src/m/model/plot/applyoptions.m	(revision 6642)
@@ -54,11 +54,4 @@
 end
 
-%latlon
-%Must be done here so that it uses the same xlim and ylim as plot_overlay
-%these are changed by axis that follows
-if ~strcmpi(getfieldvalue(options,'latlon','off'),'off')
-	latlonoverlay(options);
-end
-
 %axis
 set(gca,'FontSize',fontsize);
@@ -82,4 +75,12 @@
 	ylim(getfieldvalue(options,'ylim'));
 end
+
+%latlon
+%Must be done here (before xlim and ylim??) so that it uses the same xlim and ylim as plot_overlay
+%these are changed by axis that follows
+if ~strcmpi(getfieldvalue(options,'latlon','off'),'off')
+	latlonoverlay(md,options);
+end
+
 
 %zlim
Index: /issm/trunk/src/m/model/plot/latlonoverlay.m
===================================================================
--- /issm/trunk/src/m/model/plot/latlonoverlay.m	(revision 6641)
+++ /issm/trunk/src/m/model/plot/latlonoverlay.m	(revision 6642)
@@ -1,3 +1,3 @@
-function latlonoverlay(options)
+function latlonoverlay(md,options)
 %LATLONOVERLAY - overlay latitude and longitude lines on current figure
 %
@@ -20,148 +20,131 @@
 if ~iscell(latlon),
 	if ischar(latlon) & strcmpi(latlon,'on'),
-		latstep=3;
-		lonstep=3;
+		%defaults
+		latstep=3; lonstep=3;
 		resolution=0.1;
 		color=[1 1 1];
-	else
-		return;
-	end
+	else return; end
 else
 	if length(latlon)<2
 		error('latlonoverlay error message: at least 2 arguments are required, or use ''on'' option.');
 	end
-	if length(latlon)==2,
-		latstep=latlon{1};
-		lonstep=latlon{2};
-		resolution=0.1;
-		color=[1 1 1];
-	elseif length(latlon)==3,
-		latstep=latlon{1};
-		lonstep=latlon{2};
-		resolution=latlon{3};
-		color=[1 1 1];
-	else
-		latstep=latlon{1};
-		lonstep=latlon{2};
-		resolution=latlon{3};
-		color=latlon{4};
-	end
+	if length(latlon)>3, color=latlon{4};      else color=[1 1 1]; end
+	if length(latlon)>2, resolution=latlon{3}; else resolution=0.1;end
+	latstep=latlon{1};
+	lonstep=latlon{2};
 end
 
 %2: numbering
 if ~iscell(numbering) & isnan(numbering),
-	numbering=0;
+	numbering=false;
 else
 	if ~iscell(numbering),
 		if strcmpi(char(numbering),'on'),
-			latgap=2;
-			longap=2;
+			%defaults
+			latgap=2; longap=2;
 			colornumber=color;
-			latangle=0;
-			lonangle=0;
-			numbering=1;
+			latangle=0; lonangle=0;
+			numbering=true;
 		else
-			numbering=0;
+			numbering=false;
 		end
 	else
-		latgap=numbering{1};
-		longap=numbering{2};
+		latgap=numbering{1}; longap=numbering{2};
 		colornumber=numbering{3};
-		latangle=numbering{4};
-		lonangle=numbering{5};
-		numbering=1;
+		latangle=numbering{4}; lonangle=numbering{5};
+		numbering=true;
 	end
 end
 
 %Find lat and lon that fit within the bounds of our image.
-lat=-90:resolution:0;
-lon=0:resolution:360;
+if length(md.lat)~=md.numberofgrids,  error('model field ''lat'' is empty'); end
+if length(md.long)~=md.numberofgrids, error('model field ''lon'' is empty'); end
 
-xlimits=xlim;
-ylimits=ylim;
+%what are the x and y limits
+xlimits=getfieldvalue(options,'xlim',xlim);
+ylimits=getfieldvalue(options,'ylim',ylim);
 
-for i=1:length(lon),
-	if(rem(lon(i),lonstep)==0)
-		latitudes=lat';
-		longitudes=lon(i)*ones(length(latitudes),1);
-		[x,y]=ll2xy(latitudes,longitudes,-1);
-		pos=find(x<=xlimits(2) & x>=xlimits(1) & y<=ylimits(2) & y>=ylimits(1));
-		if length(pos)<=1,
-			continue;
-		end
-		x=x(pos);y=y(pos);
+%lat
+for lat=-90:latstep:90
+	longitudes=0:resolution:360;
+	latitudes =lat*ones(size(longitudes));
 
-		l=line(x,y,'Color',color);
-		if numbering,
-			if latlonclick
-				disp(['Specifiy where to put number for longitude ' num2str(lon(i))]);
-				[xcorner,ycorner]=ginput(1);
+	if strcmpi(md.hemisphere,'n'),
+		if lat<0, continue; end
+		[x,y]=ll2xy(latitudes,longitudes,+1,45,70);
+	elseif strcmpi(md.hemisphere,'s'),
+		if lat>0, continue; end
+		[x,y]=ll2xy(latitudes,longitudes,-1, 0,71);
+	else error('field hemisphere should either be ''n'' or ''s'''); end
 
-				%Find nearest point on this profile corresponding to (xcorner,ycorner):
-				ind=find_point(x,y,xcorner,ycorner); xcorner=x(ind); ycorner=y(ind);
-			else
-				ind=round(9*length(x)/10); xcorner=x(ind); ycorner=y(ind);
-			end
-			if length(x(1:ind))>10,
-				xcorner2=x(ind-10); ycorner2=y(ind-10);
-			else
-				xcorner2=x(ind-1); ycorner2=y(ind-1);
-			end
-			angle=mod((180)/pi*atan2((ycorner2-ycorner),(xcorner2-xcorner))+lonangle,360);
-			
-			if (xcorner>xlimits(1) & xcorner<xlimits(2) & ycorner>ylimits(1) & ycorner<ylimits(2)),
-				th=text(xcorner,ycorner,[num2str(lon(i)) '^{\circ}']);set(th,'Color',colornumber,'Rotation',angle,'FontSize',fontsize,'HorizontalAlignment','center','VerticalAlignment','middle');
-				
-				%erase line and redraw it in two parts, to leave space for latitude number
-				delete(l);
-				line(x(1:ind-longap),y(1:ind-longap),'Color',color);hold on;
-				line(x(ind+longap:end),y(ind+longap:end),'Color',color);
-			end
+	pos=find(x<=xlimits(2) & x>=xlimits(1) & y<=ylimits(2) & y>=ylimits(1));
+	if length(pos)<=1, continue; end
+	x=x(pos);y=y(pos);
+	l=line(x,y,'Color',color);
 
-		end
-	end
-end
+	if numbering,
+		ind=round(9*length(x)/10);
+		xcorner=x(ind);            ycorner=y(ind);
+		xcorner2=x(max(ind-10,1)); ycorner2=y(max(ind-10,1));
 
-for i=1:length(lat),
-	if(rem(lat(i),latstep)==0)
-		longitudes=lon';
-		latitudes=lat(i)*ones(length(longitudes),1);
-		[x,y]=ll2xy(latitudes,longitudes,-1);
-		pos=find(x<=xlimits(2) & x>=xlimits(1) & y<=ylimits(2) & y>=ylimits(1));
-		if length(pos)<=1,
-			continue;
-		end
-		x=x(pos);y=y(pos);
+		if (xcorner>xlimits(1) & xcorner<xlimits(2) & ycorner>ylimits(1) & ycorner<ylimits(2)),
+			angle=mod((180)/pi*atan2((ycorner2-ycorner),(xcorner2-xcorner))+latangle,360);
+			if lat<0, label=[num2str(abs(lat)) '^{\circ}S'];
+			else      label=[num2str(abs(lat)) '^{\circ}N']; end
+			th=text(xcorner,ycorner,label);
+			set(th,'Color',colornumber,'Rotation',angle,'FontSize',fontsize,'HorizontalAlignment','center','VerticalAlignment','middle');
 
-		l=line(x,y,'Color',color);
-		
-		if numbering,
-			if latlonclick
-				disp(['Specifiy where to put number for latitude ' num2str(lat(i))]);
-				[xcorner,ycorner]=ginput(1);
-
-				%Find nearest point on this profile corresponding to (xcorner,ycorner):
-				ind=find_point(x,y,xcorner,ycorner); xcorner=x(ind); ycorner=y(ind);
-			else
-				ind=round(9*length(x)/10); xcorner=x(ind); ycorner=y(ind);
-			end
-			if length(x(1:ind))>10,
-				xcorner2=x(ind-10); ycorner2=y(ind-10);
-			else
-				xcorner2=x(ind-1); ycorner2=y(ind-1);
-			end
-			angle=mod((180)/pi*atan2((ycorner2-ycorner),(xcorner2-xcorner))+latangle,360);
-			
-			if (xcorner>xlimits(1) & xcorner<xlimits(2) & ycorner>ylimits(1) & ycorner<ylimits(2)),
-				th=text(xcorner,ycorner,[num2str(lat(i)) '^{\circ}']);set(th,'Color',colornumber,'Rotation',angle,'FontSize',fontsize,'HorizontalAlignment','center','VerticalAlignment','middle');
-				
-				%erase line and redraw it in two parts, to leave space for latitude number
-				delete(l);
-				line(x(1:ind-latgap),y(1:ind-latgap),'Color',color);hold on;
-				line(x(ind+latgap:end),y(ind+latgap:end),'Color',color);
-			end
-
+			%erase line and redraw it in two parts, to leave space for latitude number
+			delete(l);
+			line(x(1:ind-latgap),y(1:ind-latgap),'Color',color);hold on;
+			line(x(ind+latgap:end),y(ind+latgap:end),'Color',color);
 		end
 
 	end
 end
+
+%lon
+for lon=-180:lonstep:180
+
+	if strcmpi(md.hemisphere,'n'),
+		latitudes =0:resolution:90;
+		longitudes=lon*ones(size(latitudes));
+		[x,y]=ll2xy(latitudes,longitudes,+1,45,70);
+	elseif strcmpi(md.hemisphere,'s'),
+		latitudes =-90:resolution:0;
+		longitudes=lon*ones(size(latitudes));
+		[x,y]=ll2xy(latitudes,longitudes,-1, 0,71);
+	else
+		error('field hemisphere should either be ''n'' or ''s'''); 
+	end
+
+
+	pos=find(x<=xlimits(2) & x>=xlimits(1) & y<=ylimits(2) & y>=ylimits(1));
+	if length(pos)<=1, continue; end
+	x=x(pos);y=y(pos);
+	l=line(x,y,'Color',color);
+
+	if numbering,
+		ind=round(9*length(x)/10);
+		xcorner=x(ind);            ycorner=y(ind);
+		xcorner2=x(max(ind-10,1)); ycorner2=y(max(ind-10,1));
+
+		if (xcorner>xlimits(1) & xcorner<xlimits(2) & ycorner>ylimits(1) & ycorner<ylimits(2)),
+			angle=mod((180)/pi*atan2((ycorner2-ycorner),(xcorner2-xcorner))+lonangle,360);
+			if lon<0, label=[num2str(abs(lon)) '^{\circ}W'];
+			else      label=[num2str(abs(lon)) '^{\circ}E']; end
+			th=text(xcorner,ycorner,label);
+			set(th,'Color',colornumber,'Rotation',angle,'FontSize',fontsize,'HorizontalAlignment','center','VerticalAlignment','middle');
+
+			%erase line and redraw it in two parts, to leave space for latitude number
+			delete(l);
+			line(x(1:ind-longap),y(1:ind-longap),'Color',color);hold on;
+			line(x(ind+longap:end),y(ind+longap:end),'Color',color);
+		end
+
+	end
+end
+
+%Back to original limits
+xlim(xlimits);
+ylim(ylimits);
