	subroutine calc
	
	include 'inplane.cmm'

c	First load the proper Z (CCS) values:

	do nz=1,3
	  Z(nz)=geoz(nch,nz)
	enddo

c	offset correction:

	do ns=0,3
	  do npar=1,4
	    dat(nch,ns,npar,ncyc)=
     &      dat(nch,ns,npar,ncyc)-offset(nch,ns,npar)
	  enddo
	enddo
	
	do ns=0,3
	  xras(ns)=dat(nch,ns,1,ncyc)
	  yras(ns)=dat(nch,ns,2,ncyc)
	  zras(ns)=dat(nch,ns,3,ncyc)
	  rotZras(ns)=dat(nch,ns,4,ncyc)/1000.
	enddo
	
c	Now all relevant in-plane data sits in xras, yras,
c	zras and rotZras. Now it really starts!

c	Torsion of chamber, expressed in relative rotation
c	around chamber X axis of the outer crossplates

	sum=0.
	do ns=0,3
	  sum=sum+rotZras(ns)
	enddo
	torque(nch,ncyc)=1000.*sum/4.
	
c	sag of MSK cross plate
	alf1=rotZras(0)+rotZras(1)-rotZras(2)-rotZras(3)
	c1=(z(1)+Z(2))/Z(3)
	alfmsk=-alf1/(2.*c1)
	sagmsk(nch,ncyc)=z(3)*alfmsk*2.
	
c	sag of CCD cross plates
	c1=z(1)/z(2)
	alf1=c1*rotZras(0)-rotZras(1)+rotZras(2)-c1*rotZras(3)
	c2=(z(2)/z(3)+(z(1)*z(2))/(z(2)*z(3)))
	alfccd=alf1/(2.*c2)
	sagccd(nch,ncyc)=z(3)*alfccd*2.
	
c	sag of LEN cross plate
	y1=yras(0)-yras(1)-yras(2)+yras(3)
	c1=Z(1)**2-Z(2)**2
	c2=2.*c1/(Z(3)**2)*sagmsk(nch,ncyc)
	c3=Z(2)**2-((Z(1)-Z(2))/2)**2
	c4=4.*c3/(Z(3)**2)
	saglen(nch,ncyc)=(y1-c2)/c4
	
c	chamber sag
	sum=0.
	do ns=0,3
	  sum=sum+yras(ns)
	enddo
	chasag(nch,ncyc)=sum/8.
	
c	chamber sag in Z direction (CCS):
	dx=xras(0)+xras(1)+xras(2)+xras(3)
	chasgz(nch,ncyc)=dx/8.

c	Tmsk - Tlen:
	temmsk(nch,ncyc)=
     &  -(xras(0)+xras(1)-xras(2)-xras(3))/
     &  (2.*tec*(Z(1)+Z(2)))
	
c	TCCD - Tlen
	dx=xras(0)-(Z(2)/Z(1))*(xras(1)-xras(2))-xras(3)
	den=2.*Z(2)*tec*(1.+(Z(2)/Z(1)))
	temccd(nch,ncyc)=-dx/den	

c	middle cross plate rotX (CCS):
	dy=yras(0)-yras(3)
	roxlen(nch,ncyc)=1000.*dy/(4.*Z(2))

c	torque from dy:
	dy=yras(1)-yras(2)
	c1=2.*roxlen(nch,ncyc)*(Z(2)-Z(1))/1000.
	torqdy(nch,ncyc)=-1000.*(dy+c1)/(Z(1)+Z(2))
	
	end
