
         subroutine get_offsetsbol1(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
c          if (idatum.le.20010615) then
c            print *,' NOT BOL1 OFFSETS '
c	    stop
c          endif

	do i=1,50
	  print *,'ich dx dy ',i,dzeps(i,2),dyeps(i,2)
	enddo
       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
        call setname
        fnaam=fbase//'/cali_angles_0808.txt'
        if (dircalset.ne.'bolxx') 
     +               fnaam=dircalset(1:idir)//'/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=0
         txplate_cal(-1)=20.2
         lxplate=0.
         tsens(1)=6 ! wall
         tsens(2)=7 ! airo
         tsens(3)=7 ! airt
         tsens(4)=-1

	 itower=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)=2
         tsens_gra(2)=8
         tsens_gra(3)=9
         tsens_gra(4)=1
	 tgracal=tgrascal
* get T_Fra_S
         tsens_fra(1)=10
         tsens_fra(2)=5
         tsens_fra(3)=10
         tsens_fra(4)=5
         tfracal=tfrascal
        elseif (side.eq.1) then
* get T_Gra_N
         tsens_gra(1)=1
         tsens_gra(2)=4
         tsens_gra(3)=12
         tsens_gra(4)=0
	 tgracal=tgrancal
* get T_Fra_N
         tsens_fra(1)=3
         tsens_fra(2)=13
         tsens_fra(3)=3
         tsens_fra(4)=13
         tfracal=tfrancal
	endif


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

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

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

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




	if (name(1:1).eq.'A' .or. name(1:1).eq.'C') then 
	 if (name(2:4).eq.'+NW') rott(5)=aznw
	 if (name(2:4).eq.'+NE') rott(5)=azne
	 if (name(2:4).eq.'+SE') rott(5)=azse
	 if (name(2:4).eq.'+SW') rott(5)=azsw

         if (name(2:4).eq.'-NW') rott(5)=-azsw
         if (name(2:4).eq.'-NE') rott(5)=-azse
         if (name(2:4).eq.'-SE') rott(5)=-azne
         if (name(2:4).eq.'-SW') rott(5)=-aznw
        endif


* commented out for real C chambers
*	 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
C for new C 
	if (name(1:2).eq.'C-') igch=2
	if (name(1:2).eq.'C+') igch=1

         if (name.eq.'A+NW' .or. name.eq.'C+NW') then
              id=1
              rott(1)=-1. ! SWAP X
              rott(2)=-1. ! swap y
              rott(3)=1.  ! swap rotz
         elseif (name.eq.'A+NE' .or. name.eq.'C+NE') then
              id=2
              rott(1)=1.
              rott(2)=1.
              rott(3)=1.
         elseif (name.eq.'A+SW' .or. name.eq.'C+SW') then
              id=3
              rott(1)=-1.
              rott(2)=-1.
              rott(3)=-1.
         elseif (name.eq.'A+SE' .or. name.eq. 'C+SE') then
              id=4
              rott(1)=1.
              rott(2)=1.
              rott(3)=-1.
         elseif (name.eq.'A-NW' .or. name.eq.'C-NW') then
              id=5
              rott(1)=-1. ! SWAP X
              rott(2)=1. ! swap y
              rott(3)=1.  ! swap rotz
         elseif (name.eq.'A-NE' .or. name.eq.'C-NE') then
              id=6
              rott(1)=1.
              rott(2)=-1.
              rott(3)=1.
         elseif (name.eq.'A-SW' .or. name.eq.'C-SW') then
              id=7
              rott(1)=-1.
              rott(2)=1.
              rott(3)=-1.
         elseif (name.eq.'A-SE' .or. name.eq.'C-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
              rott(1)=1.
              rott(2)=-1.
              rott(3)=1.
              rott(4)=17.37
         elseif (name.eq.'B-SW') then
              id=15
              rott(1)=-1.
              rott(2)=-1.
              rott(3)=-1.
              rott(4)=6.28
         elseif (name.eq.'B-SE') then
              id=16
              rott(1)=1.
              rott(2)=1.
              rott(3)=-1.
              rott(4)=28.96
         elseif (name.eq.'B+NW') then
              id=17
              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
              rott(1)=1.
              rott(2)=-1.
              rott(3)=1.
              rott(4)=17.37
         elseif (name.eq.'B+SW') then
              id=19
              rott(1)=-1.
              rott(2)=-1.
              rott(3)=-1.
              rott(4)=6.28
         elseif (name.eq.'B+SE') then
              id=20
              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
         elseif (name.eq.'D_NW_PW') then
              id=26
         elseif (name.eq.'B_SW-PW') then
              id=27
         elseif (name.eq.'D_SW_PW') then
              id=28
         elseif (name.eq.'B_NE_QW') then
              id=29
         elseif (name.eq.'D_NE_QW') then
              id=30
         elseif (name.eq.'B_SE_QW') then
              id=31
         elseif (name.eq.'D_SE_QW') then
              id=32
         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
	      itower=5
        elseif (name(3:8).eq.'InPl_1') then
              id=38
              ipass=0
	      itower=6
        elseif (name(3:8).eq.'InPl_2') then
              id=39
              ipass=0
	      itower=7
        elseif (name(3:8).eq.'InPl_3') then
              id=40
              ipass=0
	      itower=8
        elseif (name(3:6).eq.'ROSH') then
              id=41
              ipass=0
        elseif (name(3:6).eq.'ROLO') then
              id=42
              if (name(1:1).eq.'C') rott(1)=-1
              if (name(1:1).eq.'C') rott(2)=-1
              ipass=0
        elseif (name(3:6).eq.'HVSH') then
              id=43
              ipass=0
        elseif (name(3:6).eq.'HVLO') then
              id=44
              rott(1)=-1.
              if (name(1:1).eq.'C') rott(1)=1
              if (name(1:1).eq.'C') rott(2)=-1
              ipass=0
        elseif (name(1:4).eq.'MILO') then
              id=45
              rott(2)=-1.
              ipass=0
        elseif (name(1:4).eq.'T-M0') then
              id=46
              ipass=0
        elseif (name(1:4).eq.'T-M1') then
              id=47
              ipass=0
        elseif (name(1:4).eq.'T-M2') then
              id=48
              ipass=0
        elseif (name(1:4).eq.'T-M3') then
              id=49
              ipass=0
        elseif (name(3:10).eq.'0_InPl_0') then
              id=50
              ipass=0
              itower=5
        elseif (name(3:10).eq.'0_InPl_1') then
              id=51
              ipass=0
              itower=6
        elseif (name(3:10).eq.'0_InPl_2') then
              id=52
              ipass=0
              itower=7
        elseif (name(3:10).eq.'0_InPl_3') then
              id=53
              ipass=0
              itower=8
        elseif (name(3:10).eq.'1_InPl_0') then
              id=54
              ipass=0
              itower=5
        elseif (name(3:10).eq.'1_InPl_1') then
              id=55
              ipass=0
              itower=6
        elseif (name(3:10).eq.'1_InPl_2') then
              id=56
              ipass=0
              itower=7
        elseif (name(3:10).eq.'1_InPl_3') then
              id=57
              ipass=0
              itower=8
        elseif (name(3:10).eq.'2_InPl_0') then
              id=58
              ipass=0
              itower=5
        elseif (name(3:10).eq.'2_InPl_1') then
              id=59
              ipass=0
              itower=6
        elseif (name(3:10).eq.'2_InPl_2') then
              id=60
              ipass=0
              itower=7
        elseif (name(3:10).eq.'2_InPl_3') then
              id=61
              ipass=0
              itower=8
        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.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='/group/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)
	return
 70	print *,'Error openign parameter file'
	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 ((idch.le.12) .or.
     +     (idch.ge.21 .and. idch.le.24) ) then
	   if (y.lt.-10.) il=1
	   if (y.gt.10.) il=3
	endif
  	y=y-dyfin(il)
  	x=x-dxfin(il)
        end

	include 'rasfile.f'
	include 'temp.f'
	include 'BOLtemp.f'
	include 'force.f'
	include 'forcefile.f'


        subroutine readslopes
C-- read calibration files
         include 'Online.cmm'
         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
        call setname
        fnaam=fbase//'/cali_slopes.txt'
        if (dircalset.ne.'bolxx') 
     +               fnaam=dircalset(1:idir)//'/cali_slopes.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
	print *,'id off slope ',id,yoff,yslope
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
	include 'Online.cmm'
C-- read calibration files
        include 'getcor.cmm'
        character*80 fnaam
C-- some initial stuff
        do i=1,100
           do j=1,3
	      dyeps_save(i,j)=0.
	      dzeps_save(i,j)=0.
	      counts_save(i,j)=0.
           enddo
           rotzplot(i)=0.
           rotzplot_save(i)=0.
        enddo
C-- do the actual work
        call setname
        fnaam=fbase//'/cali_eps.txt'
        if (dircalset.ne.'bolxx') 
     +              fnaam=dircalset(1:idir)//'/cali_eps.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.lt.0 .or. id.gt.100) then
           print *,'unknown id 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
        
        if (id.gt.0) then
        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
	endif

        
        goto 10
 99     continue
        close(38)
	end



	subroutine writeeps
C-- write calibration files
        include 'Online.cmm'
        include 'getcor.cmm'
        character*80 fnaam
C-- do the actual work
        call setname
        fnaam=fbase//'/cali_eps_write.txt'


        if (dircalset.ne.'bolxx') 
     +               fnaam=dircalset(1:idir)//'/cali_eps_write.txt'
        open(38,file=fnaam)
        write(*,*)'Y and Z EPSILONS from to file ',fnaam
	do id=1,99
        dy1=dyeps(id,1)+dyeps_save(id,1)/(counts_save(id,1)+0.00001)
        dy2=dyeps(id,2)+dyeps_save(id,2)/(counts_save(id,2)+0.00001)
        dy3=dyeps(id,3)+dyeps_save(id,3)/(counts_save(id,3)+0.00001)
        dz1=dzeps(id,1)+dzeps_save(id,1)/(counts_save(id,1)+0.00001)
        dz2=dzeps(id,2)+dzeps_save(id,2)/(counts_save(id,2)+0.00001)
        dz3=dzeps(id,3)+dzeps_save(id,3)/(counts_save(id,3)+0.00001)
        rot=rotzplot(id)+rotzplot_save(id)/
     +      (counts_save(id,1)+
     +       counts_save(id,2)+
     +       counts_save(id,3)+0.00001)
        write(38,3800)
     +          id,dy1,dy2,dy3,dz1,dz2,dz3,rot
	enddo
      close(38)
 3800 format(I2.2,1x,7(F12.7,1x))

	end


	subroutine readseqs	
        include 'Online.cmm'
C-- write calibration files
        include 'getcor.cmm'
        character*80 fnaam
	  integer iuse
	  iuse=0
	  n_use=0
C-- do the actual work
        fnaam='bol1/use_seqs_ids.txt'
	call setname
        fnaam=fbase//'/use_seqs_ids.txt'
        if (dircalset.ne.'bolxx') 
     +              fnaam=dircalset(1:idir)//'/use_seqs_ids.txt'


C examplre
* 20010620
* 12 1 3
*
*
        open(38,file=fnaam)
        read(38,*,ERR=98,END=98)idatum_use
10     continue
	  iuse=iuse+1
11	continue
        read(38,*,ERR=11,END=99) 
     +       icyc_use(iuse),id_use(iuse),il_use(iuse)
	  n_use=n_use+1
C-- offer some protection against stupidities
        if (id_use(iuse).le.0 .or. id_use(iuse).gt.100) then
           print *,'unknown ID use_seqs_ids ',id
*     goto 10 ! or stop
           stop
        endif        
        goto 10
 99     continue
      close(38)
	goto 97
 98   continue
	stop
 97   continue
	end

        subroutine initseqs
        include 'Online.cmm'
C-- init calibration files
        include 'getcor.cmm'
        n_save=0
	end



        subroutine exeseqs
        include 'Online.cmm'
        include 'getcor.cmm'
C--
C use the ids read from use_seqs as template and find now
C the best corresponding sequences.
C--
        character*80 fnaam
C-- do the actual work
        fnaam='use_seqs_ids.write'
        call setname
        fnaam=fbase//'/use_seqs_ids.write'
        if (dircalset.ne.'bolxx')
     +              fnaam=dircalset(1:idir)//'/use_seqs_ids.write'

	print *,' exeseqs called with n_use n_save', n_use,n_save
        open(38,file=fnaam)
        write(38,*)idatum
 	do i=1,n_use
	idd=id_use(i)
	ill=il_use(i)
	if (idd.lt.30) then ! only for RASASS
	  jbest=-1
	  dybest=99999.
 	  do j=1,n_save
	    jdd=id_save(j)
	    jll=il_save(j)
	    if (idd.eq.jdd .and. ill.eq.jll) then
		dy=dyeps(idd,ill)-dy_save(j)
		dy=dy*dy
	        if (dy.lt.dybest) then
		   dybest=dy
		   jbest=j
	        endif
	     endif
	   enddo ! j
	  if (jbest.lt.1) then
	    write(38,*)'* No Sequence found for id il ',idd,ill
	  else
	    if (sqrt(dybest).gt.0.100) then
	      write(38,*)'* Large Deviation dz dy ',idd,ill,
     +        dzeps(idd,ill)-dz_save(jbest),
     +        dyeps(idd,ill)-dy_save(jbest)
	    endif
            write(38,*)icyc_save(jbest),id_save(jbest),il_save(jbest)
	  endif
	endif ! RASASS only
	enddo

C
C--
C now InPlane and XP
        do i=1,n_use
        idd=id_use(i) ! always L2
        if (idd.gt.30) then ! InPlane+ XP
	iwrite=0
	 do j=n_save,1,-1
	  if (il_save(j).eq.2 .and. pm_save(j).eq.-1 .and. 
     +        id_save(j).eq.idd) then
	    do k=n_save,1,-1
	      if (il_save(k).eq.2 .and. pm_save(k).eq.1 .and.
     +            id_save(k).eq.idd) then
                dy=abs(dy_save(k)-dy_save(j))
                dz=abs(dz_save(k)-dz_save(j))
                if (idd.eq.42 .or. idd.eq.44) then ! swap z,y
		   ds=dy
		   dy=dz
		   dz=ds
	        endif
	        if (dy.gt.0.02 .and. dy.lt.0.600 .and.
     +              dz.lt.1) then ! take it
	          if (iwrite.lt.1) then ! take only first (in fact last)
                   write(38,*)icyc_save(j),id_save(j),il_save(j)
                   write(38,*)icyc_save(k),id_save(k),il_save(k)
		   iwrite=1
		  endif
                endif ! candidates physically ok
               endif ! L2 +
	    enddo ! search id L2+
	  endif ! L2 -
	enddo ! search id L2 -
        if (iwrite.lt.1) then
          write(38,*)'* No Sequence found for id il ',idd
	endif
	endif ! only InPlane + XP
	enddo ! template id 	
	          
	close(38)
        end



	subroutine setname
        include 'getcor.cmm'
        include 'Online.cmm'
        character*5 fnaam
	fnaam='bol1/'
        if (idatum.gt.20010716) fnaam='bol2/'
        if (idatum.ge.20010824) fnaam='bol3/'
        if (idatum.ge.20010926) fnaam='bol4/'
        if (idatum.ge.20011023) fnaam='bol5/'
        if (idatum.ge.20011110) fnaam='bol6/'
        if (idatum.ge.20011130) fnaam='bol7/'
        if (idatum.ge.20011213) fnaam='bol8/'
        if (idatum.ge.20020107) fnaam='bol9/'
        if (idatum.ge.20020121) fnaam='bol10'
        if (idatum.ge.20020201) fnaam='bol11'
        if (idatum.ge.20020214) fnaam='bol12'
        if (idatum.ge.20020227) fnaam='bol13'
        if (idatum.ge.20020311) fnaam='bol14'
        if (idatum.ge.20020321) fnaam='bol15'
        if (idatum.ge.20020407) fnaam='bol16'
        if (idatum.ge.20020418) fnaam='bol17'
        if (idatum.ge.20020501) fnaam='bol18'
        if (idatum.ge.20020516) fnaam='bol19'
        if (idatum.ge.20020529) fnaam='bol20'
        if (idatum.ge.20020610) fnaam='bol21'
        if (idatum.ge.20020621) fnaam='bol22'
        if (idatum.ge.20020703) fnaam='bol23'
        if (idatum.ge.20020715) fnaam='bol24'
        if (idatum.ge.20020726) fnaam='bol25'
        if (idatum.ge.20020808) fnaam='bol26'
        if (idatum.ge.20020821) fnaam='bol27' 
        if (idatum.ge.20020902) fnaam='bol28' 
        if (idatum.ge.20020911) fnaam='bol29' 
        if (idatum.ge.20020920) fnaam='bol30' 
        if (idatum.ge.20021002) fnaam='bol31' 
        if (idatum.ge.20021011) fnaam='bol32' 
        if (idatum.ge.20021022) fnaam='b_133' 
        if (idatum.ge.20021024) fnaam='b_134' 
        if (idatum.ge.20021026) fnaam='bol33' 
        if (idatum.ge.20021107) fnaam='bol34' 
	fbase=fnaam
	if (idatum.gt.60000000 .or. idatum.lt.18000000) then
	  print *,'datum is nonsense in setname ',idatum
	  stop
	endif
	end

	subroutine check_eps(snaam,id,il,ez,ey,rz)
	character*16 snaam
	real ez,ey,rz
	real eps_y,eps_z
	integer ok,zok,yok,rok
	eps_z=ez
	eps_y=ey
	rotz=rz
	ok=1
	rok=1
	zok=1
	yok=1
	if (id.lt.30) then ! otherwise no check possbble
	print *,'calcheck in ',snaam,id,il,eps_z,eps_y,rotz
C correct Y
        isi=1
        if (id.eq.2 .or. id.eq.3) isi=-1
        if (id.eq.6 .or. id.eq.7) isi=-1
        if (id.le.8) then
           ycor=100.*(rotz)/1000.
		 eps_y=eps_y+isi*ycor
	  endif
	   


C first Z
	  if (abs(eps_z).gt.0.75) zok=-1
	    if (snaam(4:4).eq.'W') then !ref side
		   if (abs(eps_z).gt.0.04) zok=-1
	    endif
C now Y
	  if (abs(eps_y).gt.0.04) yok=-1
C rotz
	  if (abs(rotz).gt.1) rok=-1

	print *,'calcheck out ',snaam,id,il,eps_z,eps_y,rotz
C now Rotz
	  ok=1+zok+yok+rok-3
	   if (ok.lt.1) then
	  	open(89,file='calcheck.txt')
	      write(89,1000)snaam(1:4),eps_z,eps_y,rotz
1000        format('Calibratie van ',a4, ' ziet er verdacht uit: z=',
     +           F7.4,' y=',F7.4,' rotz=',F7.4)
	       if (rok.lt.1)write(89,*)'Probleem zit oa in Rotz '
	       if (yok.lt.1) write(89,*)'Problem zit oa in Y'
	       if (zok.lt.1)write(89,*)'Problem zit oa in Z '
	       write(89,*)'Herhaal zonodig spacer opleggen en meting '
	    close(89)
	    open(89,file='calcheck.bat')
		 write(89,*)'type calcheck.txt'
		 write(89,*)'pause '
	    close(89)
	   endif ! ok
	endif ! id<30
	if (ok.eq.1) then
		open(89,file='calcheck.bat')
		 write(89,*)' '
	    close(89)
	endif
	end
