%
% An example setparms.m is:
%
% dbpath = ['/home/moli4/kaimi/ka9705/adcpdb/'];
% dbname=['a9705']; %used to name the output
% ctddb = [''];
% year_base = 1997;
%

if ~exist('./setparms.m','file')
 help websect
 error('setparms.m must exist in the current directory.')
end

setparms

uvfile = sprintf('%s_uv.mat',dbname);
xyfile = sprintf('%s_xy.mat',dbname);

if ( ~exist(uvfile) & ~exist(xyfile) )
 command=sprintf('!my_as.prl %s%s %d %s\n',dbpath,dbname,year_base,ctddb)
 eval(command);
end

load(uvfile)
load(xyfile)

x=xyt(1,:)'-360;
y=xyt(2,:)';
date=xyt(3,:)';
n=length(uv(1,:));
u=uv(:,1:2:n)';
v=uv(:,2:2:n)';
badi=find((isnan(date)) | (isnan(x)) | isnan(y));
date(badi)=[];
x(badi)=[];
y(badi)=[];
u(badi,:)=[];
v(badi,:)=[];


while(1)
	
	figure(1)
	clf
	plot(x,y,'g')
	hold on
	plot(x,y,'b.')
	zoom on
	grid on
	disp('zoom is ON')
	disp('use zoom to focus on the region you want, then press return')
	paused
	
	disp('now click on two points of the cruisetrack to delimit a section')
	hold on
	[xpt,ypt]=ginput(2);
	%get the closest indices
    dist1=sqrt((xpt(1)-x).^2 + (ypt(1)-y).^2);
    i1=find(dist1==min(dist1));
    dist2=sqrt((xpt(2)-x).^2 + (ypt(2)-y).^2);
    i2=find(dist2==min(dist2));

	disp('you  must choose a section name')
	sec_name=input('enter section name here:  ','s');

	ii=[min(i1,i2):max(i1,i2)];
	h=plot(x(ii),y(ii),'r.');set(h,'markersize',20')
	orient portrait
	eval(['print -dpsc ',sec_name,'map'])

	xyt=[x(ii)'+360; y(ii)'; date(ii)'];
	uv=[u(ii,:)' u(ii,:)']; %for space
		uv(:,1:2:2*length(ii))=u(ii,:)';
  		uv(:,2:2:2*length(ii))=v(ii,:)';
        com = sprintf('save6 sect%s uv xyt zc',sec_name);
        eval(com)
        title_str = sprintf(', Section %s, %s',sec_name,...
                    strtok( to_date(year_base,mean(xyt(3,:))) ));
	title(title_str)

        llnum = input('lat (2) or long (1)? ');
        if llnum==2,
	 ll=[min(y(ii)):.1:max(y(ii))];
        else
	 ll=[min(x(ii)+360):.1:max(x(ii)+360)];
        end
	webplot(ll,xyt,llnum,zc,uv,title_str);

	orient tall
	eval(['print -dpsc sect',sec_name])

	disp(['saving to ',sec_name,'_cont.ps'])
	
	disp('this loop will repeat indefinitely until you type ^C to get out')
	disp('otherwise, press <return> to continue')

end
