*
*   Online RASNIK 'post'-analysis 
*
*   GM - version derived from Marcel's inplane.f etc.
*  14/5 MV include FTP system
*  14/5 MV include Temperature file
*
      program Online
*     ------- ------

      include 'Online.cmm'

      character*12 ras
      character*10 chtime
      character*8 datum
      integer system

	do i=1,10
		ierror(i)=0
	enddo
	do i=1,100
	   namech(i)='empty'
	enddo

cmg	open(33,file='steer')
cmg          read *,fname
cmg          read *,tcut
cmg        close(33)
cmg


      print *,' .res filename = ?'
      read(*,'(a)')rname
      print *,rname
      print *,' temp filename = ?'
      read(*,'(a)')tname
      print *,tname
      print *,' Start-time = ?'
      read(*,'(a)')chtime
      print *,chtime
      read(chtime,'(i2.2,1x,i2.2)')nhour,nminu
      tcut=nhour+float(nminu)/60.
      print *,' sysname= ?'
      read(*,'(a)')sysname



      ifile=2
      call readtemp(ifile)
      close(2)
*     ----
      call read
*     ----

      print *
      print *,' # of succesful cycles : ',ncyc,
     +        ' starting from ',chtime  
	
      end

      subroutine read
*     ---------- ----

      include 'Online.cmm'

      character*125 chline
      character*11 kname

      character*59 comm,comm_prev
      character*2  tprob,pprob
      character*3  resu
      character*16 sname

      character*80 aline
      character*1  devch(5)

      dimension value(9)

* ---------

      ipass=0

      print *,'rname = ',rname         
      print *,'Start Time = ',tcut
      ifile=1
      call rasfile(ifile,rname)

      write(*,*)'file opened ',rname
      read(1,'(a)')aline
      print *,'first line=',aline
      rewind 1

      open(33,file='raw.dat')

      open(34,file='correction.dat')
      open(35,file='ntuple.ascii')
      open(36,file='tower')

      print *,
     +   'SYSTEM: TIME h:m(.fh)     X:        Y:',
     +   '         SCALE:      ROTZ:  '

      nch=1
      ns=1

      ncyc=1
      time_prev = -1.0
      comm_prev = 'NO COMMENT'
      mcomm     = 1
*
*  Read Loop
*  ---------

    3  read(1,'(a)',END=99) chline
       
*
* Select and decode  'st' line
*
         if(chline(1:2) .eq. 'st') then

           sname = chline(31:46)
           comm  = chline(67:125)

           if (sname.eq.sysname) then


              read(chline,1002)nhour,nminu,nsec
 1002 format(21x,i2.2,1x,i2.2,1x,i2.2)

              time=nhour+float(nminu)/60.+float(nsec)/3600.

*
* Check Time
*
              if (time.gt.tcut) then

                ipass=1
*
* Read and decode following 'ud' line
*
                read(1,'(a)') chline
                if( chline(1:2) .eq. 'ud' ) then

                  read(chline,1001) value

 1001 format(9x,3e14.3,2e12.3,2e14.3,2e11.3)

                  call get_offsets(sname)
               
*              ----

                  x=value(1)
                  y=value(2)
	          scale=value(3)
	          rotz=1000.*value(6)

c               print *,' value',value


*   Check if Comment has changed and add mcomm to  .dat file  

                  if (comm .ne. comm_prev ) mcomm = -mcomm

*
*  write 'raw' values to 'ntpl'-file
*
                  write(33,*) sname, time, x,y,scale,rotz,mcomm


                  call get_correction(x,y,scale,rotz)
*                 ----
                  call get_txplate(time,txplate)
*                 ----
	          x=x-
     +             (23.*lxplate/1000.*
     +             (txplate-txplate_cal(xplate)))/1000.

      write(*,*)' lxplate ',lxplate
      write(*,*)' dtxpl   ',txplate-txplate_cal(xplate)
      write(*,*)' tcal   ',txplate_cal(xplate)
*              x=x+(23[um/m] * Length[m] * delta_temp ) [mm]
*
*  write corrected values to 'ntpl'-file
*
         write(34,2002) sname, time,txplate-txplate_cal(xplate),
     +               x,y,scale,rotz
 2002	format(a8,1x,F9.4,1x,F9.4,1x,F9.4,1x,F9.4,1x,F9.4,
     +                 1x,F9.4,1x)

          write(35,2003)idch,time,txplate-txplate_cal(xplate),
     +               x,y,scale,rotz,ierror(1),ierror(2)
 2003   format(I3,1x,F9.4,1x,F9.4,1x,F9.4,1x,F9.4,1x,F9.4,
     +                 1x,F9.4,1x,I2,1x,I2)

                write(36,*)time,x,y,scale,rotz



*     print comment only if 
*        . it has changed or
*        . more the 5 seconds have passed between measmnts

               if ( time-time_prev .gt. 5./3600.
     +             .or. comm .ne. comm_prev ) then
                  print *
                  print *,comm
               endif
               time_prev = time
               comm_prev = comm

               dx=abs(x)
               dd=abs(abs(x)-15.017)
               if (dd.lt.dx) dx=dd

               dy=abs(y)
               dd=abs(abs(y)-26.011)
               if (dd.lt.dy) dy=dd
               dd=abs(abs(y)-2.*26.011)
               if (dd.lt.dy) dy=dd
                 
               resu='RED'
               tprob='++'
               pprob='++'
               if (dx.le.0.010 .and. dy.le.0.010) resu='BLU'
               if (dx.le.0.006 .and. dy.le.0.006) resu='GRE'
               if (ierror(1).gt.0) tprob=':T'
               if (ierror(2).gt.0) pprob=':P'

*
*     Tag deviating values
*
               do i=1,5
                 devch(i) = ' '
               enddo

               if ( dx .gt. 0.006 ) then
                  devch(1) = '*'
               endif
               if ( dy .gt. 0.006 ) then
                  devch(2) = '*'
               endif
*
*     Show corrected results
*
               dectime = float(nminu)/60.+float(nsec)/3600.

               write(*,2001) sname, nhour,nminu,dectime,
     +         txplate-txplate_cal(xplate),
     +         x,devch(1),y,devch(2),scale,devch(3),
     +              rotz,devch(4),resu,tprob,pprob
C               write(*,*) xpar,ypar,rott

 2001 format(a8,i3,':',i2.2,'(',f3.2,')', ' dT=',F5.2,
     +                       ' x=',F7.3,a,' y=',F7.3,a,
     +                       ' s=',F5.3,a,' Az=',F7.3,a,1x,a,a,a)


c	        write(6,*)value

                ncyc=ncyc+1

	      else 

                print *,' ud line missing for system ',sname,
     +                  ' at time ',nhour,':',nminu

* ud-part
              endif
* time-cut  
            endif
            endif ! sysname
* st-part
	    endif
* Read Loop
          goto 3

*
*  End of story
*
   99 close(1)

      close(33)
      close(34)
      close(35)
      close(36)
	open(34,file='channels.dat')
	do i=1,100
          if (namech(i).ne.'empty') then
            write(kname,2004)i+99
 2004       format('id',I3,'.kumac')
	    open(35,file=kname)
	    write(35,*)'text 0 1.1 ',namech(i),' 0.3'
            close(35)
            write(34,*)i
          endif
	enddo
        close(34)
	open(34,file='rastit.kumac')
        write(34,*)'title \'',rname(5:12),
     +             ' RASNIK and Temp vs Time\''
        close(34)
        open(34,file='temptit.kumac')
        write(34,*)'title \'',tname(8:15),
     +        ' Temperature vs Time\''
        close(34)
        open(34,file='rasmove.kumac')
        write(34,*)'sh mv plot.ps plots/rasnik_',rname(5:12),'.ps'
        close(34)
        open(34,file='tempmove.kumac')
        write(34,*)'sh mv plot.ps plots/temp_',rname(5:12),'.ps'
        close(34)



        

cgm updating of 'steer'-file : NOT YET checked

      if (ipass.eq.1) then
        if (tcut.ge.0) tcut=time ! auto-update-mode
      else
        print *,'NO DATA FOUND - CHECK FILE steer '
        print *,'DATA ignored before time ',tcut
      endif
	
CMV      open(33,file='Online.steer')
CMV        write(33,'(a)')fname
CMV        write(33,*)tcut
CMV      close(33)

      end

	include 'getcor.f'
