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

         include 'Online.cmm'
         character*16 name,myname
         character*40 namex
         character*40 namey

         ipass=1
         ierror(2)=0

         rott(5)=0.
C FIX rotation rotz for the following RASASS systems
         aznw=-3.98
	 azne=-3.95
         azse=-1.48
         azsw=-0.7

         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

	if (xplate.eq.0) then ! READOUT
	   tsens(1)=0
           tsens(2)=1
           tsens(3)=2
           tsens(4)=3
           lxplate=200.
           txplate_cal(0)=19.95
           if (name(1:2).eq.'C+') txplate_cal(0)=20.1061249
           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=200.
           if (name(4:4).eq.'E') lxplate=2600.
           txplate_cal(1)=20.1
           if (name(1:2).eq.'C+') txplate_cal(0)=20.0816994
        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





         if (name.eq.'A+NW') then
              id=1
              namex='xpars.a+nw'
              namey='ypars.a+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.'A+NE') then
              id=2
              namex='xpars.a+ne'
              namey='ypars.a+ne'
              rott(1)=1.
              rott(2)=1.
              rott(3)=1.
              rott(4)=17.37
         elseif (name.eq.'A+SW') then
              id=3
              namex='xpars.a+sw'
              namey='ypars.a+sw'
              rott(1)=-1.
              rott(2)=-1.
              rott(3)=-1.
              rott(4)=6.28
         elseif (name.eq.'A+SE') then
              id=4
              namex='xpars.a+se'
              namey='ypars.a+se'
              rott(1)=1.
              rott(2)=1.
              rott(3)=-1.
              rott(4)=28.96
         elseif (name.eq.'A-NW') then
              id=5
              namex='xpars.a-nw'
              namey='ypars.a-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.'A-NE') then
              id=6
              namex='xpars.a-ne'
              namey='ypars.a-ne'
              rott(1)=1.
              rott(2)=-1.
              rott(3)=1.
              rott(4)=17.37
         elseif (name.eq.'A-SW') then
              id=7
              namex='xpars.a-sw'
              namey='ypars.a-sw'
              rott(1)=-1.
              rott(2)=1.
              rott(3)=-1.
              rott(4)=6.28
         elseif (name.eq.'A-SE') then
              id=8
              namex='xpars.a-se'
              namey='ypars.a-se'
              rott(1)=1.
              rott(2)=-1.
              rott(3)=-1.
              rott(4)=28.96
         elseif (name.eq.'C-NW') then
              id=9
              namex='xpars.c-nw'
              namey='ypars.c-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.'C-NE') then
              id=10
              namex='xpars.c-ne'
              namey='ypars.c-ne'
              rott(1)=1.
              rott(2)=1.
              rott(3)=1.
              rott(4)=17.37
         elseif (name.eq.'C-SW') then
              id=11
              namex='xpars.c-sw'
              namey='ypars.c-sw'
              rott(1)=-1.
              rott(2)=-1.
              rott(3)=-1.
              rott(4)=6.28
         elseif (name.eq.'C-SE') then
              id=12
              namex='xpars.c-se'
              namey='ypars.c-se'
              rott(1)=1.
              rott(2)=1.
              rott(3)=-1.
              rott(4)=28.96
         elseif (name.eq.'B-NW') then
              id=13
              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=14
              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
              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
              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
              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
              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
              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
              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
              namex='xpars.c+nw'
              namey='ypars.c+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.'C+NE') then
              id=22
              namex='xpars.c+ne'
              namey='ypars.c+ne'
              rott(1)=1.
              rott(2)=-1.
              rott(3)=1.
              rott(4)=17.37
         elseif (name.eq.'C+SW') then
              id=23
              namex='xpars.c+sw'
              namey='ypars.c+sw'
              rott(1)=-1.
              rott(2)=1.
              rott(3)=-1.
              rott(4)=6.28
         elseif (name.eq.'C+SE') then
              id=24
              namex='xpars.c+se'
              namey='ypars.c+se'
              rott(1)=1.
              rott(2)=-1.
              rott(3)=-1.
              rott(4)=28.96
         elseif (name.eq.'B_NW_PW') then
              id=25
              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
              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
              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
              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
              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
              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
              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
              namex='xpars.dseqw'
              namey='ypars.dseqw'
              rott(1)=1.
              rott(2)=1.
              rott(3)=1.
              rott(4)=0.
              rott(5)=0.
         elseif (name.eq.'Gra0') then
              id=33
              namex='xpars.gra0'
              namey='ypars.gra0'
              rott(1)=1.
              rott(2)=1.
              rott(3)=1.
              rott(4)=0.
              rott(5)=0.00001
         elseif (name.eq.'Gra1') then
              id=34
              namex='xpars.gra1'
              namey='ypars.gra1'
              rott(1)=1.
              rott(2)=1.
              rott(3)=1.
              rott(4)=0.
              rott(5)=0.00001
         elseif (name.eq.'Gra2') then
              id=35
              namex='xpars.gra2'
              namey='ypars.gra2'
              rott(1)=1.
              rott(2)=1.
              rott(3)=1.
              rott(4)=0.
              rott(5)=0.00001
         elseif (name.eq.'Gra3') then
              id=36
              namex='xpars.gra3'
              namey='ypars.gra3'
              rott(1)=1.
              rott(2)=1.
              rott(3)=1.
              rott(4)=0.
              rott(5)=0.00001
         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
        if (ipass.eq.1) then
	   call readoff(namex,xpar)
	   call readoff(namey,ypar)
           idch=id
	   namech(id)=name	
        else
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 ! ipass
	

        end

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

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

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

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

	print *,'file openened ',dname
	read(68,*,END=69,ERR=69)par(1)
	read(68,*,END=69,ERR=69)par(2)
	read(68,*,END=69,ERR=69)par(3)
        print *,par
 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
        if (rott(5).ne.0.) then ! fix rotz
          rotz=rott(5)
        endif
        x=xr+yr*rotz/1000.
        y=yr-xr*rotz/1000.
        end

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

           
	subroutine readcal
C-- read calibration files
	common /slopecal/offsets,slopes,tcps,tempfra,tempgra
	real yoffsets(100),yslopes(100) ! corresponding to channel ID
	real zoffsets(100),zslopes(100) ! corresponding to channel ID
	real tcps(100),tempfra(100),tempgra ! corresponding to channel ID
	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_0808.txt'
	open(35,file=fnaam)
	write(*,*)'SLOPES and TEMPS from file ',fnaam
 10	continue
	read(35,ERR=10,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
        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
	end
	

