        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'
	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
