function [A,numprofiles,numpoints,closed]=addendprofile(A,numprofiles,numpoints,closed,prevplot,root);
%ADDENDPROFILE - add point at the end of a n existing profile
%
%   this script is used by expmaster as an elementary operation
%   on an ARGUS profile
%
%   Usage:
%      [A,numprofiles,numpoints,closed]=addendprofile(A,numprofiles,numpoints,closed,prevplot,root)
		
	%some checks
	if numprofiles==0
		disp('no profile present')
		return
	end	   
	if ~any(~closed)
		disp('all profiles are closed')
		return
	end	   
	%select a profile first
	if numprofiles>1
		%first step, select a profile
		isclosed=1;
		title('click on a profile, RETURN to exit','FontSize',14)
		while isclosed
			[xi,yi] = ginput(1);
			if ~isempty(xi)
				%get the closest point 
				[profsel indsel]=closestpoint(A,numprofiles,xi,yi);
				if closed(profsel)
					disp('selected profile is closed, make another selection')
				else
					isclosed=0;
				end

			else
				%RETURN -> out
				return
			end
		end
	else
		profsel=1;
	end

	%initialize x and y
	x=A(profsel).x;
	y=A(profsel).y;

	%plot the selected profile
	hold on
	if length(x)
		plot(x,y,'-rs','MarkerSize',10);
	end
	plot(x,y,'rs','MarkerSize',10);
	plot(x(end),y(end),'rs','MarkerSize',14);


	loop=1;

	while loop

		%first step, select a profile
		title('click to add point to the selected profile, RETURN to exit','FontSize',14)
		[xi,yi] = ginput(1);

		if ~isempty(xi)
			x(end+1,1)=xi;
			y(end+1,1)=yi;

			%plot everything
			undoplots(prevplot);
			if length(x)
				plot(x,y,'-rs','MarkerSize',10);
			end
			plot(x,y,'rs','MarkerSize',10);
			plot(x(end),y(end),'rs','MarkerSize',14);

		else

			%check that the profile is not empty
			if ~isempty(x)
				A(profsel).x=x; 
				A(profsel).y=y; 
				A(profsel).name=root; 
				A(profsel).density=1; 
				numpoints=numpoints+length(x);
			end

			%get out
			loop=0;
		end
	end
end
