function mod_rot(modindex,rotations)

% function mod_rot(modindex,rotations)
%
% This function is used in conjunction with FD3.  It rotates the module
% designated by FD3MODULES(:,modindex) by 'rotations' 90 degree rotations.
%
% ACP

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

global FD3MODULES

% Get x/ydata for PRIMARY OBJECT if present

xdata=get(FD3MODULES(1,modindex),'Xdata');
ydata=get(FD3MODULES(1,modindex),'Ydata');

% Get x/ydata for SECONDARY OBJECT if present

if FD3MODULES(3,modindex)
   xdata2=get(FD3MODULES(3,modindex),'Xdata');
   ydata2=get(FD3MODULES(3,modindex),'Ydata');
end

for i=1:sign(rotations):rotations

   % The rotations are carried out one at a time.  First rotate the PRIMARY
   % OBJECT.

   newxdata=FD3MODULES(4,modindex)-(ydata-FD3MODULES(5,modindex));
   newydata=FD3MODULES(5,modindex)+(xdata-FD3MODULES(4,modindex));
   xdata=newxdata;
   ydata=newydata;

   % Next, rotate the the SECONDARY OBJECT if present.

   if FD3MODULES(3,modindex)~=0
      newxdata2=FD3MODULES(4,modindex)-(ydata2-FD3MODULES(5,modindex));
      newydata2=FD3MODULES(5,modindex)+(xdata2-FD3MODULES(4,modindex));
      xdata2=newxdata2;
      ydata2=newydata2;
   end

   % Adjust the thetas by 90 degrees and then shift the theta vector over
   % by one so the angles are in the right posistions.

   FD3MODULES(10:13,modindex)=rem(FD3MODULES(10:13,modindex)+pi/2,2*pi);
   FD3MODULES(10:13,modindex)=FD3MODULES([13 10:12],modindex);

   % swap x scaling factor and y scaling factor

   FD3MODULES(14:15,modindex)=FD3MODULES([15 14],modindex);

end

% Set SECONDARY OBJECT if present

if FD3MODULES(3,modindex)
   set(FD3MODULES(3,modindex),'Xdata',xdata2,'Ydata',ydata2);
end

% Set PRIMARY OBJECT

set(FD3MODULES(1,modindex),'Xdata',xdata,'Ydata',ydata);

% Adjust number of rotations

FD3MODULES(9,modindex)=rem(FD3MODULES(9,modindex)+rotations,4);
