function [xdata,ydata]=auto_str(modindex1,xy1,modindex2,xy2)

% function [xdata,ydata]=auto_str(modindex1,xy1,modindex2,xy2)
%
% This function is used in conjuction with FD3.  It generates the 'xdata' and
% 'ydata' necessary for the efficient connection of two modules,
% FD3MODULES(:,[modindex1 modindex2]), with horizontal/vertical streams.
% 'Xy1' and 'xy2' are the endpts of the stream. 
%
% ACP

% Last update:  July 8, 1993
% Author:       Adrian C. Penisson

global FD3SCALE FD3MODULES

r=1.5*FD3SCALE;

side1=get_side(modindex1,xy1);
side2=get_side(modindex2,xy2);

% x and y coordinates for both endpts

x1=xy1(1);
x2=xy2(1);
y1=xy1(2);
y2=xy2(2);

% average x and average y coordinates

xavg=(x1+x2)/2;
yavg=(y1+y2)/2;

% Maximum x and maximum y coordinates for each endpt.  If a module has a
% secondary object, its xdata/ydata is also taken into consideration when
% calculating the maximum and minimum coordinates.  It is necessary
% to remove all NaN's that may be present in the xdata/ydata when finding
% the maximums and minimums.

xdata1=get(FD3MODULES(1,modindex1),'Xdata');
xdata1=xdata1(find(~isnan(xdata1)));
xdata2=get(FD3MODULES(1,modindex2),'Xdata');
xdata2=xdata2(find(~isnan(xdata2)));

xmax1=max(xdata1);
xmax2=max(xdata2);
xmin1=min(xdata1);
xmin2=min(xdata2);

ydata1=get(FD3MODULES(1,modindex1),'Ydata');
ydata1=ydata1(find(~isnan(ydata1)));
ydata2=get(FD3MODULES(1,modindex2),'Ydata');
ydata2=ydata2(find(~isnan(ydata2)));

ymax1=max(ydata1);
ymax2=max(ydata2);
ymin1=min(ydata1);
ymin2=min(ydata2);

if FD3MODULES(3,modindex1)
   xdata1s=get(FD3MODULES(3,modindex1),'Xdata');
   xdata1s=xdata1s(find(~isnan(xdata1s)));
   ydata1s=get(FD3MODULES(3,modindex1),'Ydata');
   ydata1s=ydata1s(find(~isnan(ydata1s)));
   xmax1=max([xmax1 max(xdata1s)]);
   xmin1=min([xmin1 min(xdata1s)]);
   ymax1=max([ymax1 max(ydata1s)]);
   ymin1=min([ymin1 min(ydata1s)]);
end

if FD3MODULES(3,modindex2)
   xdata2s=get(FD3MODULES(3,modindex2),'Xdata');
   xdata2s=xdata2s(find(~isnan(xdata2s)));
   ydata2s=get(FD3MODULES(3,modindex2),'Ydata');
   ydata2s=ydata2s(find(~isnan(ydata2s)));
   xmax2=max([xmax2 max(xdata2s)]);
   xmin2=min([xmin2 min(xdata2s)]);
   ymax2=max([ymax2 max(ydata2s)]);
   ymin2=min([ymin2 min(ydata2s)]);
end

% LEFT to RIGHT

if ( side1==1 & side2==3 ) | ( side1==3 & side2==1 )

   if ((side1==1 & (xmax1+r)<x2) | (side2==1 & (xmax2+r)<x1)) | ...
      (side1==1 & xmax1<xmin2 & y1<(ymax2+r/2) & y1>(ymin2-r/2)) | ...
      (side2==1 & xmax2<xmin1 & y2<(ymax1+r/2) & y2>(ymin1-r/2))

      if y1==y2

	 xdata=[x1 x2];
	 ydata=[y1 y2];
      else

	 xdata=[x1 xavg xavg x2];
	 ydata=[y1 y1 y2 y2];
      end

   else

      if side1==1 & (xmax1+r)>=x2

	 xdata=[x1 max([xmax1 xmax2])+[r r] min([xmin2 xmin1])-[r r] x2];
      else

	 xdata=[x1 min([xmin1 xmin2])-[r r] max([xmax2 xmax1])+[r r] x2];
      end

      ydata=[y1 y1 max([ymax1 ymax2])+[r r] y2 y2];

   end

% TOP to BOTTOM

elseif ( side1==4 & side2==2 ) | ( side1==2 & side2==4 )

   if (side1==4 & (ymin1-r)>y2) | (side2==4 & (ymin2-r)>y1) | ...
      (side1==4 & ymin1>y2 & x1<(xmax2+r/2) & x1>(xmin2-r/2)) | ...
      (side2==4 & ymin2>y1 & x2<(xmax1+r/2) & x2>(xmin1-r/2))

      if x1==x2
	 xdata=[x1 x2];
	 ydata=[y1 y2];
      else
	 xdata=[x1 x1 x2 x2];
	 ydata=[y1 yavg yavg y2];
      end

   else

      xdata=[x1 x1 min([xmin1 xmin2])-[r r] x2 x2];

      if side2==4 & (ymin2-r)<=y1
	 ydata=[y1 max([ymax1 ymax2])+[r r] min([ymin2 ymin1])-[r r] y2];
      else
	 ydata=[y1 min([ymin1 ymin2])-[r r] max([ymax2 ymax1])+[r r] y2];
      end

   end

% BOTTOM/TOP to LEFT/RIGHT (simple)

elseif ((side1==4 & (ymin1-r)>=y2) | (side1==2 & (ymax1+r)<=y2)) & ...
   ((side2==3 & (xmin2-r)>=x1) | (side2==1 & x1>=(xmax2+r)))

   xdata=[x1 x1 x2];
   ydata=[y1 y2 y2];

elseif ((side2==4 & (ymin2-r)>=y1) | (side2==2 & (ymax2+r)<=y1)) & ...
   ((side1==3 & (xmin1-r)>=x2) | (side1==1 & x2>=(xmax1+r)))

   xdata=[x1 x2 x2];
   ydata=[y1 y1 y2];

% BOTTOM to RIGHT 

% basically in very center of lower half if side 4 mod is at origin

elseif side1==4 & (ymin1-r)>y2 & side2==1 & x1<(xmax2+r) & (xmin2-r)<=x1

   xdata=[x1 x1 xmax2+r xmax2+r x2];
   ydata=[y1 (ymin1+ymax2)./[2 2] y2 y2];

elseif side2==4 & (ymin2-r)>y1 & side1==1 & x2<(xmax1+r) & (xmin1-r)<=x2

   xdata=[x1 xmax1+r xmax1+r x2 x2];
   ydata=[y1 y1 (ymin2+ymax1)./[2 2] y2];

% basically right side if side 4 mod is at origin

elseif side1==4 & side2==1 & (((xmin2-r)>x1 & y1>y2)|...
   ((xmax2+r)>xmin1 & y2>=y1))

   xdata=[x1 x1 max([xmax1 xmax2])+[r r] x2];
   ydata=[y1 min([ymin1 ymin2])-[r r] y2 y2];

elseif side2==4 & side1==1 & (((xmin1-r)>x2 & y2>y1)|...
   ((xmax1+r)>xmin2 & y1>=y2))

   xdata=[x1 max([xmax1 xmax2])+[r r] x2 x2];
   ydata=[y1 y1 min([ymin1 ymin2])-[r r] y2];

% upper left quadrant if side 4 mod is at origin

elseif side1==4 & side2==1

   xdata=[x1 x1 (xmax2+xmin1)./[2 2]  x2];
   ydata=[y1 ymin1-r ymin1-r y2 y2];

elseif side2==4 & side1==1

   xdata=[x1 (xmax1+xmin2)./[2 2] x2 x2];
   ydata=[y1 y1 ymin2-r ymin2-r y2];

% BOTTOM to LEFT 

% basically in very center of lower half if side 4 mod is at origin

elseif side1==4 & (ymin1-r)>y2 & side2==3 & (xmin2-r)<x1 & (xmax2+r)>=x1

   xdata=[x1 x1 xmin2-r xmin2-r x2];
   ydata=[y1 (ymin1+ymax2)./[2 2] y2 y2];

elseif side2==4 & (ymin2-r)>y1 & side1==3 & (xmin1-r)<x2 & (xmax1+r)>=x2

   xdata=[x1 xmin1-r xmin1-r x2 x2];
   ydata=[y1 y1 (ymin2+ymax1)./[2 2] y2];

% basically left side if side 4 mod is at origin

elseif side1==4 & side2==3 & ((x1>(xmax2+r) & y1>y2)|...
   (xmin2<(xmax1+r) & y2>=y1))

   xdata=[x1 x1 min([xmin1 xmin2])-[r r] x2];
   ydata=[y1 min([ymin1 ymin2])-[r r] y2 y2];

elseif side2==4 & side1==3 & ((x2>(xmax1+r) & y2>y1)|...
   (xmin1<(xmax2+r) & y1>=y2))

   xdata=[x1 min([xmin1 xmin2])-[r r] x2 x2];
   ydata=[y1 y1 min([ymin1 ymin2])-[r r] y2];

% upper right quadrant if side 4 mod is at origin

elseif side1==4 & side2==3

   xdata=[x1 x1 (xmax2+xmin1)./[2 2]  x2];
   ydata=[y1 ymin1-r ymin1-r y2 y2];

elseif side2==4 & side1==3

   xdata=[x1 (xmax1+xmin2)./[2 2] x2 x2];
   ydata=[y1 y1 ymin2-r ymin2-r y2];

% TOP to RIGHT 

% basically in very center of upper half if side 2 mod is at origin

elseif side1==2 & (ymax1+r)<y2 & side2==1 & x1<(xmax2+r) & (xmin2-r)<=x1

   xdata=[x1 x1 xmax2+r xmax2+r x2];
   ydata=[y1 (ymax1+ymin2)./[2 2] y2 y2];

elseif side2==2 & (ymax2+r)<y1 & side1==1 & x2<(xmax1+r) & (xmin1-r)<=x2

   xdata=[x1 xmax1+r xmax1+r x2 x2];
   ydata=[y1 y1 (ymax2+ymin1)./[2 2] y2];

% basically right side if side 2 mod is at origin

elseif side1==2 & side2==1 & (((xmin2-r)>x1 & y1<y2)|...
   ((xmax2+r)>xmin1 & y2<=y1))

   xdata=[x1 x1 max([xmax1 xmax2])+[r r] x2];
   ydata=[y1 max([ymax1 ymax2])+[r r] y2 y2];

elseif side2==2 & side1==1 & (((xmin1-r)>x2 & y2<y1)|...
   ((xmax1+r)>xmin2 & y1<=y2))

   xdata=[x1 max([xmax1 xmax2])+[r r] x2 x2];
   ydata=[y1 y1 max([ymax1 ymax2])+[r r] y2];

% lower left quadrant if side 2 mod is at origin

elseif side1==2 & side2==1

   xdata=[x1 x1 (xmax2+xmin1)./[2 2]  x2];
   ydata=[y1 ymax1+r ymax1+r y2 y2];

elseif side2==2 & side1==1

   xdata=[x1 (xmax1+xmin2)./[2 2] x2 x2];
   ydata=[y1 y1 ymax2+r ymax2+r y2];

% TOP to LEFT 

% basically in very center of upper half if side 4 mod is at origin

elseif side1==2 & (ymax1+r)<y2 & side2==3 & (xmin2-r)<x1 & (xmax2+r)>=x1

   xdata=[x1 x1 xmin2-r xmin2-r x2];
   ydata=[y1 (ymax1+ymin2)./[2 2] y2 y2];

elseif side2==2 & (ymax2+r)<y1 & side1==3 & (xmin1-r)<x2 & (xmax1+r)>=x2

   xdata=[x1 xmin1-r xmin1-r x2 x2];
   ydata=[y1 y1 (ymax2+ymin1)./[2 2] y2];

% basically left side if side 2 mod is at origin

elseif side1==2 & side2==3 & ((x1>(xmax2+r) & y1<y2)|...
   (xmin2<(xmax1+r) & y2<=y1))

   xdata=[x1 x1 min([xmin1 xmin2])-[r r] x2];
   ydata=[y1 max([ymax1 ymax2])+[r r] y2 y2];

elseif side2==2 & side1==3 & ((x2>(xmax1+r) & y2<y1)|...
   (xmin1<(xmax2+r) & y1<=y2))

   xdata=[x1 min([xmin1 xmin2])-[r r] x2 x2];
   ydata=[y1 y1 max([ymax1 ymax2])+[r r] y2];

% lower right quadrant if side 2 mod is at origin

elseif side1==2 & side2==3

   xdata=[x1 x1 (xmax2+xmin1)./[2 2]  x2];
   ydata=[y1 ymax1+r ymax1+r y2 y2];

elseif side2==2 & side1==3

   xdata=[x1 (xmax1+xmin2)./[2 2] x2 x2];
   ydata=[y1 y1 ymax2+r ymax2+r y2];

% TOP to TOP

elseif side1==2 & side2==2

   if ~(((xmax1+r)>x2 & x2>(xmin1-r) & y2<ymin1) | ...
      ((xmax2+r)>x1 & x1>(xmin2-r) & y1<ymin2))


      xdata=[x1 x1 x2 x2];
      if y1>y2
	 ydata=[y1 ymax1+r ymax1+r y2];
      else
	 ydata=[y1 ymax2+r ymax2+r y2];
      end

   else

      if (xmin1-r)<=x2 & x2<=x1 & y1>y2
	 xdata=[x1 x1 xmin1-r xmin1-r x2 x2];
      elseif (xmin2-r)<=x1 & x1<=x2 & y2>y1	
	 xdata=[x1 x1 xmin2-r xmin2-r x2 x2];
      elseif (xmax1+r)>=x2 & x2>x1 & y1>y2
	 xdata=[x1 x1 xmax1+r xmax1+r x2 x2];
      elseif (xmax2+r)>=x1 & x1>x2 & y2>y1
	 xdata=[x1 x1 xmax2+r xmax2+r x2 x2];
      end

      ydata=[y1 ymax1+r ymax1+r ymax2+r ymax2+r y2];

   end


% BOTTOM to BOTTOM

elseif side1==4 & side2==4

   if ~(((xmax1+r)>x2 & x2>(xmin1-r) & y2>ymax1) | ...
      ((xmax2+r)>x1 & x1>(xmin2-r) & y1>ymax2))

      xdata=[x1 x1 x2 x2];
      if y1<y2
	 ydata=[y1 ymin1-r ymin1-r y2];
      else
	 ydata=[y1 ymin2-r ymin2-r y2];
      end

   else

      if (xmin1-r)<=x2 & x2<=x1 & y1<y2
	 xdata=[x1 x1 xmin1-r xmin1-r x2 x2];
      elseif (xmin2-r)<=x1 & x1<=x2 & y2<y1	
	 xdata=[x1 x1 xmin2-r xmin2-r x2 x2];
      elseif (xmax1+r)>=x2 & x2>x1 & y1<y2
	 xdata=[x1 x1 xmax1+r xmax1+r x2 x2];
      elseif (xmax2+r)>=x1 & x1>x2 & y2<y1
	 xdata=[x1 x1 xmax2+r xmax2+r x2 x2];
      end

      ydata=[y1 ymin1-r ymin1-r ymin2-r ymin2-r y2];

   end

% RIGHT to RIGHT 

elseif side1==1 & side2==1

   if ~(((ymax1+r)>y2 & y2>(ymin1-r) & x2<xmin1) | ...
      ((ymax2+r)>y1 & y1>(ymin2-r) & x1<xmin2))

      if x1>x2
         xdata=[x1 xmax1+r xmax1+r x2];
      else
         xdata=[x1 xmax2+r xmax2+r x2];
      end
      ydata=[y1 y1 y2 y2];

   else

      if (ymin1-r)<=y2 & y2<=y1 & x1>x2
	 ydata=[y1 y1 ymin1-r ymin1-r y2 y2];
      elseif (ymin2-r)<=y1 & y1<=y2 & x2>x1	
	 ydata=[y1 y1 ymin2-r ymin2-r y2 y2];
      elseif (ymax1+r)>=y2 & y2>y1 & x1>x2
	 ydata=[y1 y1 ymax1+r ymax1+r y2 y2];
      elseif (ymax2+r)>=y1 & y1>y2 & x2>x1
	 ydata=[y1 y1 ymax2+r ymax2+r y2 y2];
      end

      xdata=[x1 xmax1+r xmax1+r xmax2+r xmax2+r x2];

   end

% LEFT to LEFT

elseif side1==3 & side2==3

   if ~(((ymax1+r)>y2 & y2>(ymin1-r) & x2>xmax1) | ...
      ((ymax2+r)>y1 & y1>(ymin2-r) & x1>xmax2))

      if x1<x2
	 xdata=[x1 xmin1-r xmin1-r x2];
      else
	 xdata=[x1 xmin2-r xmin2-r x2];
      end
      ydata=[y1 y1 y2 y2];

   else

      if (ymin1-r)<=y2 & y2<=y1 & x1<x2
	 ydata=[y1 y1 ymin1-r ymin1-r y2 y2];
      elseif (ymin2-r)<=y1 & y1<=y2 & x2<x1	
	 ydata=[y1 y1 ymin2-r ymin2-r y2 y2];
      elseif (ymax1+r)>=y2 & y2>y1 & x1<x2
	 ydata=[y1 y1 ymax1+r ymax1+r y2 y2];
      elseif (ymax2+r)>=y1 & y1>y2 & x2<x1
	 ydata=[y1 y1 ymax2+r ymax2+r y2 y2];
      end

      xdata=[x1 xmin1-r xmin1-r xmin2-r xmin2-r x2];

   end

end



