	program calibration
C------------------------------------------------------------------
        character*8 fbase
        character*80 fnaam
        character*180 line
        real dyeps(100,3), rotzplot(100)
        real dzeps(100,3)
        real xdb(10),ydb(10),sdb(10),rotdb(10),rmdb(10)
        real xg(10),yg(10),sg(10),rg(10)
        real exg(10),eyg(10),esg(10),erg(10)
	real xcal(4),ycal(4),scal(4),rcal(4),rmcal(4)
C-- do the actual work
        fbase='../bol88'
        fnaam=fbase//'/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)


        print *,'---EPSILONS-----------------------------'
        id=37
        print *,dzeps(id,2),dyeps(id,2),'1',rotzplot(id)
        id=38
        print *,dzeps(id,2),dyeps(id,2),'1',rotzplot(id)
        id=39
        print *,dzeps(id,2),dyeps(id,2),'1',rotzplot(id)
        id=40
        print *,dzeps(id,2),dyeps(id,2),'1',rotzplot(id)



C	'InPl_0,1,2,3' id=37,38,39,40


C------------------------------------------------------------------
C-- Now read existing db.dat
        print *,'---------DB.DAT------------------------------'
        fnaam=fbase//'/db.dat'        
        open(34,file=fnaam)
        i=1
 210    continue
        read(34,'(a)',ERR=230,END=230)line
        if (i.ge.100) then
           print *,'FATAL 1 ' 
           goto 230
        endif
        ks=10
        do k=3,10
           if (line(k:k).eq.' ') ks=k
	enddo
           
        read(line(ks:180),*,ERR=210,END=210)
     +       xdb(i),ydb(i),sdb(i),rotdb(i),rmdb(i)

        print *, xdb(i),ydb(i),sdb(i),rotdb(i),rmdb(i)
        i=i+1
        if (i.eq.5) goto 230
        goto 210
 230    continue
        if (i.ne.5) print *,'FATAL 2 '
	close(34)

C------------------------------------------------------------------
C-- Now read Grazianoos offsets
        fnaam='inpcal88'        
        open(34,file=fnaam)
        i=1
 310    continue
        read(34,'(a)',ERR=330,END=330)line
        
        if (line(1:3).eq.'BOL') then
           if (line(4:5).ne.fbase(7:8)) then
              print *,'FATAL BOLnr ',line, fbase
           endif
        endif
        if (i.ge.100) then
           print *,'FATAL 3 ' 
           goto 330
        endif

        if (line(1:4).eq.'InpX') then
           read(34,'(a)',ERR=330,END=330)line
           read(line(8:180),*,ERR=310,END=310)xg(1),xg(2),xg(3),xg(4)
           read(34,'(a)',ERR=330,END=330)line
           read(line(8:180),*,ERR=310,END=310)
     +             exg(1),exg(2),exg(3),exg(4)
           do k=1,4
              xg(k)=xg(k)/1000.
              exg(k)=exg(k)/1000.
           enddo

        endif

        if (line(1:4).eq.'InpY') then
           read(34,'(a)',ERR=330,END=330)line
           read(line(8:180),*,ERR=310,END=310)yg(1),yg(2),yg(3),yg(4)
           read(34,'(a)',ERR=330,END=330)line
           read(line(8:180),*,ERR=310,END=310)
     +                eyg(1),eyg(2),eyg(3),eyg(4)
           do k=1,4
              yg(k)=yg(k)/1000.
              eyg(k)=eyg(k)/1000.
           enddo

        endif

        if (line(1:4).eq.'RotZ') then
           read(34,'(a)',ERR=330,END=330)line
           read(line(8:180),*,ERR=310,END=310)rg(1),rg(2),rg(3),rg(4)
           read(34,'(a)',ERR=330,END=330)line
           read(line(8:180),*,ERR=310,END=310)
     +                   erg(1),erg(2),erg(3),erg(4)
           do k=1,4
              rg(k)=rg(k)/1000.
              erg(k)=erg(k)/1000.
           enddo
        endif

        if ( (line(1:4).eq.'(Sca') .or. (line(1:4).eq.'(1-S') ) then
           read(34,'(a)',ERR=330,END=330)line
           read(line(8:180),*,ERR=310,END=310)sg(1),sg(2),sg(3),sg(4)
           read(34,'(a)',ERR=330,END=330)line
           read(line(8:180),*,ERR=310,END=310)
     +                     esg(1),esg(2),esg(3),esg(4)
           do k=1,4
              sg(k)=(sg(k)/10000.+1)
              esg(k)=esg(k)/10000.
           enddo
        endif
        i=i+1
        goto 310
 330    continue
	close(34)
        if (i.lt.5) print *,'FATAL 4 ',i

        print *,'-------GRAZIANO---------------------'
        do k=1,4
           print *, xg(k),yg(k),sg(k),rg(k)
        enddo




        print *,'-------NEW CALIBRATION------------------'

	do i=1,4
	   xcal(i)=dzeps(36+i,2)+xg(i)
	   ycal(i)=dyeps(36+i,2)+yg(i)
	   scal(i)=sg(i)
	   rcal(i)=rotzplot(36+i)+rg(i)
	   rmcal(i)=rmdb(i)

	   dx=abs(xcal(i)-xdb(i))
	   dy=abs(ycal(i)-ydb(i))
	   ds=abs(scal(i)-sdb(i))
	   dr=abs(rcal(i)-rotdb(i))
	
	   print *,'for i= ',i
	   print *,xcal(i),ycal(i),scal(i),rcal(i)
	   if (dx.gt.0.020) print *,'BIZARRE X difference '
	   if (dy.gt.0.020) print *,'BIZARRE Y difference '
	   if (ds.gt.0.001) print *,'BIZARRE Scale difference '
	   if (dr.gt.0.050) print *,'BIZARRE Rotz difference '

	   if (exg(i).gt.0.020) print *,'BIZARRE X RMS '
	   if (eyg(i).gt.0.020) print *,'BIZARRE Y RMS '
	   if (esg(i).gt.0.001) print *,'BIZARRE Scale RMS '
	   if (erg(i).gt.0.050) print *,'BIZARRE Rotz RMS '

	enddo

	open(38,file='db.new')	
	do i=1,4
	   write(38,1000)
     +           i,xcal(i),ycal(i),scal(i),rcal(i),rmcal(i)

	   write(*,1000)
     +           i,xcal(i),ycal(i),scal(i),rcal(i),rmcal(i)
	enddo
 1000	format('InPl_',I1,4X,F10.6,2X,F10.6,3X,F10.6,3X,F10.6,3X,F10.6)
C        print *, xdb(i),ydb(i),sdb(i),rotdb(i),rmdb(i)

        close(38)

        end
