      program dyzch

      real diy(3,8),diz(3,8),d2y(8),d2z(8),doy(3,8),doz(3,8)
      real ddy(3,8),ddz(3,8)

      open(23,file='cali.ntp')


      do ich=1,24

        read(23,*)ichn
        print *,ichn

        do l=1,8
          read(23,*)lay,(diy(j,l),j=1,3),(diz(k,l),k=1,3)
*        print *,lay,(diy(j,l),j=1,3),(diz(k,l),k=1,3)
          d2y(l)=diy(2,l)
          d2z(l)=diz(2,l)
        enddo


*        call dyzcal(1,d2y,d2z,doy,doz)
        call dyzcal(2,d2y,d2z,doy,doz)

        do l=1,8
*        print 2000,l,(1000*diy(j,l),j=1,3),(1000*diz(k,l),k=1,3)
*        print 2000,l,(1000*doy(j,l),j=1,3),(1000*doz(k,l),k=1,3)
 2000 format(i1,6f10.1)
          do j=1,3
            ddy(j,l)=1000.*(doy(j,l)-diy(j,l))
            ddz(j,l)=1000.*(doz(j,l)-diz(j,l))
          enddo
          print 2000,l,(ddy(j,l),j=1,3),(ddz(k,l),k=1,3)
        enddo

        do idum=1,8
          read(23,*)dum
        enddo

      enddo

      close(23)


      end
      

      subroutine dyzcal(mode,dy2,dz2,dy,dz)
*
* 'Predict' calibration off-sets (epsi's) 
* for layer's 1 and 3     in A+ and A-               
* from  epsis of layer 2  in A+ and A- for mode=1    
* from  epsis of layer 2  in A+ only   for mode=2

* input    dy2,dz2 
* output   dy,dz   


*           ----  A+ ----    ----  A- ----
* index :   1   2   3   4    5   6   7   8
*          NW  NE  SW  SE   NW  NE  SW  SE

      real dy2(8),dz2(8),dy(3,8),dz(3,8)

      do i=1,8
        dy(2,i) = dy2(i)
        dz(2,i) = dz2(i)
      enddo


* A+ dy     extra's are from MV 'grand fit' block deviations
* 0.83 ( 1.21) comes from Ear/Mask geometry and dz=15mm

*                            extra
      dy(1,1) = 0.83*dy(2,1) -0.004 
      dy(3,1) = 0.83*dy(2,1) +0.005 
      dy(1,2) = 1.21*dy(2,2) -0.009 
      dy(3,2) = 1.21*dy(2,2) -0.001 
      dy(1,3) = 0.83*dy(2,3) +0.000 
      dy(3,3) = 0.83*dy(2,3) +0.003 
      dy(1,4) = 1.21*dy(2,4) -0.006 
      dy(3,4) = 1.21*dy(2,4) +0.000 
*
      if ( mode .eq. 2 ) then
* A- dy2 from A+ dy2 , same Ear :
        dy(2,5) = -dy(2,3)
        dy(2,7) = -dy(2,1)
* add  misterious 0.035on Non-Ref side  ( after BOL3 )
        dy(2,6) = -dy(2,4) +0.035
        dy(2,8) = -dy(2,2) +0.030
      endif

      dy(1,5) = 0.83*dy(2,5) -0.004 
      dy(3,5) = 0.83*dy(2,5) +0.005 
      dy(1,6) = 1.21*dy(2,6) -0.009
      dy(3,6) = 1.21*dy(2,6) -0.001
      dy(1,7) = 0.83*dy(2,7) +0.000 
      dy(3,7) = 0.83*dy(2,7) +0.003 
      dy(1,8) = 1.21*dy(2,8) -0.006
      dy(3,8) = 1.21*dy(2,8) +0.000

* A+ dz     extra's are empirical
*           no correl with MV 'grand fit' block deviations
* 0.3 comes from Ear/Mask geometry and dy=26mm

*                                   extra
      dz(1,1) = dz(2,1)-0.3*dy(2,1) +0.005
      dz(3,1) = dz(2,1)+0.3*dy(2,1) +0.005 
      dz(1,2) = dz(2,2)+0.3*dy(2,2) -0.01 
      dz(3,2) = dz(2,2)-0.3*dy(2,2) +0.02  
      dz(1,3) = dz(2,3)-0.3*dy(2,3) -0.000
      dz(3,3) = dz(2,3)+0.3*dy(2,3) -0.005 
      dz(1,4) = dz(2,4)+0.3*dy(2,4) -0.01 
      dz(3,4) = dz(2,4)-0.3*dy(2,4) +0.005 

      if ( mode .eq. 1 ) then

        dz(1,5) = dz(2,5)-0.3*dy(2,5) +0.000
        dz(3,5) = dz(2,5)+0.3*dy(2,5) +0.008 
        dz(1,6) = dz(2,6)+0.3*dy(2,6) -0.01 
        dz(3,6) = dz(2,6)-0.3*dy(2,6) +0.02  
        dz(1,7) = dz(2,7)-0.3*dy(2,7) -0.005
        dz(3,7) = dz(2,7)+0.3*dy(2,7) -0.000 
        dz(1,8) = dz(2,8)+0.3*dy(2,8) -0.005
        dz(3,8) = dz(2,8)-0.3*dy(2,8) +0.005 

      else

* A- dz2 from A+ dz2 , 
*      Ref-side same Tower ( expect dz2~0 , assume dev comes from tower )
*                                          ~OK for NW , blah for SW 
        dz(2,5) = dz(2,1) -0.008
        dz(2,7) = dz(2,3) +0.010

*      Non-ref side same Ear, expect large dz from XP
*                                           OK apart from large shifts

        dz(2,6) = dz(2,4) -0.045
        dz(2,8) = dz(2,2) +0.050

        dz(1,5) = dz(2,5)+0.3*dy(2,3) +0.000
        dz(3,5) = dz(2,5)-0.3*dy(2,3) +0.008 
        dz(1,6) = dz(2,6)-0.3*dy(2,4) +0.000
        dz(3,6) = dz(2,6)+0.3*dy(2,4) +0.010 
        dz(1,7) = dz(2,7)+0.3*dy(2,1) -0.005
        dz(3,7) = dz(2,7)-0.3*dy(2,1) -0.000 
        dz(1,8) = dz(2,8)-0.3*dy(2,2) +0.005
        dz(3,8) = dz(2,8)+0.3*dy(2,2) +0.000 

       endif

       end
     
