
         subroutine get_offsets(name)
*        ---------- ----------------

         include 'Online.cmm'
        include 'getcor.cmm'

        character*16 name,myname
        character*40 namex
        character*40 namey,fnaam
	common/fgerewee/first
	logical first

	if (first) then
	  call readslopes
	  call readeps
          if (idatum.ge.20000810) then
              dyeps(41,1)=dyeps(41,1)+0.083
              dyeps(41,2)=dyeps(41,2)+0.083
              dyeps(41,3)=dyeps(41,3)+0.083
C              62.57463311	75.80870656	70.80870605	41.32464073
              do i=1,3
	        dyeps(37,i)=dyeps(37,i)+62.57463311/1000.
	        dyeps(38,i)=dyeps(38,i)+75.80870656/1000.
                dyeps(39,i)=dyeps(39,i)+70.80870605/1000.
                dyeps(40,i)=dyeps(40,i)+41.32464073/1000.
              enddo  
C after sag adjustment
C-7.880454583	-15.08332448	-17.58332574	-5.380453322
          if (idatum.ge.20000907) then
              do i=1,3
                dyeps(37,i)=dyeps(37,i)-7.880454583/1000. 
                dyeps(38,i)=dyeps(38,i)-15.08332448/1000.
                dyeps(39,i)=dyeps(39,i)-17.58332574/1000.
                dyeps(40,i)=dyeps(40,i)-5.380453322/1000.
              enddo
          endif
          endif
        endif

	
         ipass=0
         ierror(2)=0
C--
C default
C--
         id=100
            do i=1,3
                xpar(i)=0.
                ypar(i)=0.
            enddo
            rott(1)=1.
            rott(2)=1.
            rott(3)=1.
            rott(4)=0.
            rott(5)=0.00001 
C FIX rotation rotz for the RASASS systems
        if (first) then
	fnaam='cali_angles_0808.txt'
	write(*,*)'ANGLES CALIBRATION from file ',fnaam
	if (first) then
          open(38,file=fnaam)
          read(38,*)aznw
          read(38,*)azne
          read(38,*)azse
          read(38,*)azsw
	  close(38)
        endif
        endif
C Historic values
*         aznw=-3.98
*	 azne=-3.95
*         azse=-1.48
*         azsw=-0.7


	do i=1,3
		dyfin(i)=0.
		dxfin(i)=0.
	enddo
C---

         xplate=-1
         txplate_cal(-1)=20.2
         lxplate=0.
         tsens(1)=8+40 ! wall
         tsens(2)=7+40 ! airo
         tsens(3)=9+40 ! airt
         tsens(4)=-1

C RASASS
C    xplate=0 or 1  for RO=0 HV=1
	if (name(1:2).eq.'A+' .and.
     +         name(3:3).eq.'N') xplate=1
        if (name(1:2).eq.'A+' .and.
     +         name(3:3).eq.'S') xplate=0
        if (name(1:2).eq.'C+' .and.
     +         name(3:3).eq.'S') xplate=1
        if (name(1:2).eq.'C+' .and.
     +         name(3:3).eq.'N') xplate=0
        if (name(1:2).eq.'A-' .and.
     +         name(3:3).eq.'N') xplate=0
        if (name(1:2).eq.'A-' .and.
     +         name(3:3).eq.'S') xplate=1
        if (name(1:2).eq.'C-' .and.
     +         name(3:3).eq.'S') xplate=0
        if (name(1:2).eq.'C-' .and.
     +         name(3:3).eq.'N') xplate=1
C Xplate rasniks
        if (name(3:4).eq.'RO') xplate=0
        if (name(3:4).eq.'HV') xplate=1

	side=xplate
	if (name(3:3).eq.'N') side=1
	if (name(3:3).eq.'S') side=0

C calibrated from 0626/2000 at 14:30
         tgrancal=20.5036259
         tgrascal=20.2929993
         tfrascal=20.0769997
         tfrancal=20.3600006
	
	if (side.eq.0) then
* get T_Gra_S
         tsens_gra(1)=42
         tsens_gra(2)=48
         tsens_gra(3)=49
         tsens_gra(4)=51
	 tgracal=tgrascal
* get T_Fra_S
         tsens_fra(1)=55
         tsens_fra(2)=55
         tsens_fra(3)=50
         tsens_fra(4)=50
         tfracal=tfrascal
        elseif (side.eq.1) then
* get T_Gra_N
         tsens_gra(1)=41
         tsens_gra(2)=44
         tsens_gra(3)=52
         tsens_gra(4)=52
	 tgracal=tgrancal
* get T_Fra_N
         tsens_fra(1)=43
         tsens_fra(2)=43
         tsens_fra(3)=53
         tsens_fra(4)=53
         tfracal=tfrancal
	endif


	if (xplate.eq.0) then ! READOUT
	   tsens(1)=0
           tsens(2)=1
           tsens(3)=2
           tsens(4)=3
           lxplate=40.
           if (name(4:4).eq.'E') lxplate=2600.
	elseif (xplate.eq.1) then ! HV
	   tsens(1)=8
	   tsens(2)=9
	   tsens(3)=10
           tsens(4)=11
           lxplate=40.
           if (name(4:4).eq.'E') lxplate=2600.
        endif

C Xplate rasniks
        if (name(1:2).eq.'RO') lxplate=0.
        if (name(1:2).eq.'HV') lxplate=0.

C granite
        if (name(1:3).eq.'Gra') then
	   tsens(1)=2+40
           tsens(2)=4+40
           tsens(3)=9+40
           tsens(4)=1+40
           lxplate=0.
	endif

C Inplane
        if (name(3:6).eq.'InPl') then
           tsens(1)=4
           tsens(2)=5
           tsens(3)=6
           tsens(4)=7
           lxplate=0.
        endif


	 if (name.eq.'A+NW') rott(5)=aznw
	 if (name.eq.'A+NE') rott(5)=azne
	 if (name.eq.'A+SE') rott(5)=azse
	 if (name.eq.'A+SW') rott(5)=azsw



         if (name.eq.'C+NW') rott(5)=-azse
         if (name.eq.'C+NE') rott(5)=-azsw
         if (name.eq.'C+SE') rott(5)=-aznw
         if (name.eq.'C+SW') rott(5)=-azne


         if (name.eq.'A-NW') rott(5)=-azsw
         if (name.eq.'A-NE') rott(5)=-azse
         if (name.eq.'A-SE') rott(5)=-azne
         if (name.eq.'A-SW') rott(5)=-aznw

         if (name.eq.'C-NW') rott(5)=azne
         if (name.eq.'C-NE') rott(5)=aznw
         if (name.eq.'C-SE') rott(5)=azsw
         if (name.eq.'C-SW') rott(5)=azse

C--
C find channels
C--
	igch=5
	if (name(1:2).eq.'A+') igch=1
	if (name(1:2).eq.'A-') igch=2
	if (name(1:2).eq.'C-') igch=3
	if (name(1:2).eq.'C+') igch=4

         if (name.eq.'A+NW') then
              id=1
              rott(1)=-1. ! SWAP X
              rott(2)=-1. ! swap y
              rott(3)=1.  ! swap rotz
         elseif (name.eq.'A+NE') then
              id=2
              rott(1)=1.
              rott(2)=1.
              rott(3)=1.
         elseif (name.eq.'A+SW') then
              id=3
              rott(1)=-1.
              rott(2)=-1.
              rott(3)=-1.
         elseif (name.eq.'A+SE') then
              id=4
              rott(1)=1.
              rott(2)=1.
              rott(3)=-1.
         elseif (name.eq.'A-NW') then
              id=5
              rott(1)=-1. ! SWAP X
              rott(2)=1. ! swap y
              rott(3)=1.  ! swap rotz
         elseif (name.eq.'A-NE') then
              id=6
              rott(1)=1.
              rott(2)=-1.
              rott(3)=1.
         elseif (name.eq.'A-SW') then
              id=7
              rott(1)=-1.
              rott(2)=1.
              rott(3)=-1.
         elseif (name.eq.'A-SE') then
              id=8
              rott(1)=1.
              rott(2)=-1.
              rott(3)=-1.
         elseif (name.eq.'C-NW') then
              id=9
              rott(1)=-1. ! SWAP X
              rott(2)=-1. ! swap y
              rott(3)=1.  ! swap rotz
         elseif (name.eq.'C-NE') then
              id=10
              rott(2)=1.
              rott(3)=1.
         elseif (name.eq.'C-SW') then
              id=11
              rott(1)=-1.
              rott(2)=-1.
              rott(3)=-1.
         elseif (name.eq.'C-SE') then
              id=12
              rott(1)=1.
              rott(2)=1.
              rott(3)=-1.
         elseif (name.eq.'B-NW') then
              id=13
              rott(1)=-1. ! SWAP X
              rott(2)=1. ! swap y
              rott(3)=1.  ! swap rotz
         elseif (name.eq.'B-NE') then
              id=14
              ipass=1
              namex='xpars.b-ne'
              namey='ypars.b-ne'
              rott(1)=1.
              rott(2)=-1.
              rott(3)=1.
              rott(4)=17.37
         elseif (name.eq.'B-SW') then
              id=15
              ipass=1
              namex='xpars.b-sw'
              namey='ypars.b-sw'
              rott(1)=-1.
              rott(2)=-1.
              rott(3)=-1.
              rott(4)=6.28
         elseif (name.eq.'B-SE') then
              id=16
              ipass=1
              namex='xpars.b-se'
              namey='ypars.b-se'
              rott(1)=1.
              rott(2)=1.
              rott(3)=-1.
              rott(4)=28.96
         elseif (name.eq.'B+NW') then
              id=17
              ipass=1
              namex='xpars.b+nw'
              namey='ypars.b+nw'
              rott(1)=-1. ! SWAP X
              rott(2)=1. ! swap y
              rott(3)=1.  ! swap rotz
              rott(4)=-4.8  ! rotz tower
         elseif (name.eq.'B+NE') then
              id=18
              ipass=1
              namex='xpars.b+ne'
              namey='ypars.b+ne'
              rott(1)=1.
              rott(2)=-1.
              rott(3)=1.
              rott(4)=17.37
         elseif (name.eq.'B+SW') then
              id=19
              ipass=1
              namex='xpars.b+sw'
              namey='ypars.b+sw'
              rott(1)=-1.
              rott(2)=-1.
              rott(3)=-1.
              rott(4)=6.28
         elseif (name.eq.'B+SE') then
              id=20
              ipass=1
              namex='xpars.b+se'
              namey='ypars.b+se'
              rott(1)=1.
              rott(2)=1.
              rott(3)=-1.
              rott(4)=28.96
         elseif (name.eq.'C+NW') then
              id=21
              rott(1)=-1. ! SWAP X
              rott(2)=1. ! swap y
              rott(3)=1.  ! swap rotz
         elseif (name.eq.'C+NE') then
              id=22
              rott(1)=1.
              rott(2)=-1.
              rott(3)=1.
         elseif (name.eq.'C+SW') then
              id=23
              rott(1)=-1.
              rott(2)=1.
              rott(3)=-1.
         elseif (name.eq.'C+SE') then
              id=24
              rott(1)=1.
              rott(2)=-1.
              rott(3)=-1.
         elseif (name.eq.'B_NW_PW') then
              id=25
              ipass=1
              namex='xpars.bnwpw'
              namey='ypars.bnwpw'
	      rott(1)=1.
              rott(2)=1.
              rott(3)=1.
              rott(4)=0.
              rott(5)=0.
         elseif (name.eq.'D_NW_PW') then
              id=26
              ipass=1
              namex='xpars.dnwpw'
              namey='ypars.dnwpw'
              rott(1)=1.
              rott(2)=1.
              rott(3)=1.
              rott(4)=0.
              rott(5)=0.
         elseif (name.eq.'B_SW-PW') then
              id=27
              ipass=1
              namex='xpars.bswpw'
              namey='ypars.bswpw'
              rott(1)=1.
              rott(2)=1.
              rott(3)=1.
              rott(4)=0.
              rott(5)=0.
         elseif (name.eq.'D_SW_PW') then
              id=28
              ipass=1
              namex='xpars.dswpw'
              namey='ypars.dswpw'
              rott(1)=1.
              rott(2)=1.
              rott(3)=1.
              rott(4)=0.
              rott(5)=0.
         elseif (name.eq.'B_NE_QW') then
              id=29
              ipass=1
              namex='xpars.bneqw'
              namey='ypars.bneqw'
              rott(1)=1.
              rott(2)=1.
              rott(3)=1.
              rott(4)=0.
              rott(5)=0.
         elseif (name.eq.'D_NE_QW') then
              id=30
              ipass=1
              namex='xpars.dneqw'
              namey='ypars.dneqw'
              rott(1)=1.
              rott(2)=1.
              rott(3)=1.
              rott(4)=0.
              rott(5)=0.
         elseif (name.eq.'B_SE_QW') then
              id=31
              ipass=1
              namex='xpars.bseqw'
              namey='ypars.bseqw'
              rott(1)=1.
              rott(2)=1.
              rott(3)=1.
              rott(4)=0.
         elseif (name.eq.'D_SE_QW') then
              id=32
              ipass=1
              namex='xpars.dseqw'
              namey='ypars.dseqw'
              rott(1)=1.
              rott(2)=1.
              rott(3)=1.
              rott(4)=0.
              rott(5)=0.
         elseif (name(2:4).eq.'ra0') then
              id=33
              rott(1)=1.
              rott(2)=1.
              rott(3)=1.
         elseif (name(2:4).eq.'ra1') then
              id=34
              rott(1)=1.
              rott(2)=1.
              rott(3)=1.
         elseif (name(2:4).eq.'ra2') then
              id=35
              rott(1)=1.
              rott(2)=1.
              rott(3)=1.
         elseif (name(2:4).eq.'ra3') then
              id=36
              rott(1)=1.
              rott(2)=1.
              rott(3)=1.
	elseif (name(3:8).eq.'InPl_0') then
              id=37
              ipass=0
        elseif (name(3:8).eq.'InPl_1') then
              id=38
              ipass=0
        elseif (name(3:8).eq.'InPl_2') then
              id=39
              ipass=0
        elseif (name(3:8).eq.'InPl_3') then
              id=40
              ipass=0
        elseif (name(3:6).eq.'ROSH') then
              id=41
              ipass=0
        elseif (name(3:6).eq.'ROLO') then
              id=42
              ipass=0
        elseif (name(3:6).eq.'HVSH') then
              id=43
              ipass=0
        elseif (name(3:6).eq.'HVLO') then
              id=44
              ipass=0
        elseif (name(1:4).eq.'MILO') then
              id=45
              rott(2)=-1.
              ipass=0
        else
            id=100
            ipass=0
c	    print *,'WARNING -- no predefined offsets for ',name
            ierror(2)=1
            do i=1,3
		xpar(i)=0.
		ypar(i)=0.
	    enddo
            rott(1)=1.
            rott(2)=1.
            rott(3)=1.
            rott(4)=0.
            rott(5)=0.000001
         endif
C
	if (id.ne.100) then
           idch=id ! store the channels id
           namech(id)=name
           if (name(3:6).eq.'InPl') namech(id)=name(3:8)
           if (name(3:4).eq.'RO' .or.
     +           name(3:4).eq.'HV') namech(id)=name(3:6)
	endif

        if (ipass.eq.1) then ! for channels with parameter file
	   call readoff(namex,xpar)
	   call readoff(namey,ypar)
        elseif (ipass.eq.0 .and. id.lt.100) then
	      ypar(1)=yoffsets(id)
              ypar(2)=yslopes(id)
              xpar(1)=zoffsets(id)
              xpar(2)=zslopes(id)
              txplate_cal(xplate)=tcps(id)
              tgracal=tempgra(id)
              tfracal=tempfra(id)
              rott(4)=rotzplot(id)
              do i=1,3
                dxfin(i)=dzeps(id,i)
                dyfin(i)=dyeps(id,i)
              enddo
c	    print *,name,rott(5),rott(4)
c              print *,name,ypar(1),ypar(2),
c     +                   dyfin(1),dyfin(2),dyfin(3)
          endif

        
        if (id.eq.100) then
C make a name slot for this channel
           iname=0
           ifree=0
	   do i=50,100
	    if (namech(i).eq.name) then
		iname=i
	    elseif (ifree.eq.0 .and. namech(i).eq.'empty') then
                ifree=i
	    endif ! found name or found first free
	   enddo ! i for channels
          
           if (iname.gt.0) then
		idch=iname
	   elseif (iname.eq.0 .and. ifree.gt.0) then
		idch=ifree
		namech(idch)=name
	   endif ! iname or new name
        endif ! id=100, so no predefined channel
	

	first=.false.
        end

	
	subroutine readoff(name,par)
*       ---------- ----------------

	real par(3)
	character*40 name
	character*80 dname
        dname='/project/atlas/BOL/towers/spacer/0626/'//name

	par(1)=0.
	par(2)=0.
        par(3)=0.

        open(68,file=dname,STATUS='OLD',ERR=70)

	read(68,*,END=69,ERR=69)par(1)
	read(68,*,END=69,ERR=69)par(2)
	read(68,*,END=69,ERR=69)par(3)
 69	continue
        close(68)

*        print *,dname
*        print *,par


        return
   70 print *,'Error in Opening Parameter-file : ',dname

	end

        subroutine get_correction(x,y,scale,rotz)
*         -------  -----------------------------

        include 'Online.cmm'
        real zz
        zz=0
        if (scale.gt.0) zz=1./scale
        x= rott(1)*(x-xpar(1)-xpar(2)*zz)
        y= rott(2)*(y-ypar(1)-ypar(2)*zz)
        xr=x
        yr=y
        rotz=rotz-rott(4)
        rotz=rott(3)*rotz
	rotf=rotz ! for actual use
        rotz=rotz-rott(5) ! for plotting
        if (rott(5).ne.0.) then ! fix rotz
          rotf=rott(5)
        endif
        x=xr+yr*rotf/1000.
        y=yr-xr*rotf/1000.
C---
C final dx,dy
C---
        il=2
	if (y.lt.-10.) il=1
	if (y.gt.10.) il=3
  	y=y-dyfin(il)
  	x=x-dxfin(il)
        end

	include 'rasfile.f'
	include 'temp.f'
	include 'BOLtemp.f'


        subroutine readslopes
C-- read calibration files
         include 'getcor.cmm'

        character*80 fnaam
C-- some initial stuff
        do i=1,100
          tempfra(i)=0.
          tempgra(i)=0.
          tcps(i)=0.
          yslopes(i)=0.
          zslopes(i)=0.
          yoffsets(i)=0.
          zoffsets(i)=0.
        enddo
C-- do the actual work
        fnaam='cali_slopes_0808.txt'
        open(38,file=fnaam)
        write(*,*)'SLOPES and TEMPS from file ',fnaam
 10     continue
        read(38,*,ERR=99,END=99)
     +          id,yoff,yslope,zoff,zslope,tcp,tfra,tgra
C-- offer some protection against stupidities
        if (id.le.0 .or. id.gt.100) then
           print *,'unknown ID in cali file ',id
*     goto 10 ! or stop
           stop
        endif
c 	print *,'id yoff yslope zoff zslope '
c	print *,id,yoff,yslope,zoff,zslope,tcp
        yoffsets(id)=yoff
        yslopes(id)=yslope
        zoffsets(id)=zoff
        zslopes(id)=zslope
        tcps(id)=tcp
        tempfra(id)=tfra
        tempgra(id)=tgra
        goto 10
 99     continue
        close(38)
        end


	subroutine readeps
C-- read calibration files
        include 'getcor.cmm'
        character*80 fnaam
C-- some initial stuff
        do i=1,100
           do j=1,3
	      dyeps(i,j)=0.
	      dzeps(i,j)=0.
           enddo
           rotzplot(i)=0.
        enddo
C-- do the actual work
        fnaam='cali_eps_0808.txt'
        open(38,file=fnaam)
        write(*,*)'Y and Z EPSILONS from file ',fnaam
 10     continue
        read(38,*,ERR=99,END=99)
     +          id,dy1,dy2,dy3,dz1,dz2,dz3,rot
C-- offer some protection against stupidities
        if (id.le.0 .or. id.gt.100) then
           print *,'unknown ID in cali eps file ',id
*     goto 10 ! or stop
           stop
        endif
c        print *,'id dy dz rotplot'
c	print *,id,dy1,dy2,dy3,dz1,dz2,dz3,rot
        
        dyeps(id,1)=dy1
        dyeps(id,2)=dy2
        dyeps(id,3)=dy3
        dzeps(id,1)=dz1
        dzeps(id,2)=dz2
        dzeps(id,3)=dz3
        rotzplot(id)=rot

        
        goto 10
 99     continue
        close(38)
	end

