*
*
      program monitor
*     ------- ------

      include 'Online.cmm'

      character*15 tem
      character*12 ras
      character*10 chtime
      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

      istat=system(' date \'+%Y%m%d\' > datum.txt ')
      open(34,file='datum.txt')
      read(34,*)datum
      close(34)
      write(*,*)datum
      ras='ras.'//datum
      rname=ras//'.res'
      write(*,*)rname

      tem='BOLtemp'//datum
      tname=tem//'.txt'
      write(*,*)tname

      chtime='00:00'


      goto 345

      print *,' .res filename = ?'
      read(*,'(a)')rname
      print *,rname
      print *,' temp filename = ?'
      read(*,'(a)')tname
      print *,tname
      print *,' Start-time = ?'
      read(*,'(a)')chtime
      print *,chtime

 345  continue
      read(chtime,'(i2.2,1x,i2.2)')nhour,nminu
      tcut=nhour+float(nminu)/60.


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

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

      nch=1
      ns=1
      nrasnik=0
      trasnik(1)=-1
      trasnik(2)=-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)

           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.
           trasnik(2)=time
           if (trasnik(1).lt.0) trasnik(1)=time
           nrasnik=nrasnik+1

*
* 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.
*              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)




*     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

 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
* st-part
	    endif
* Read Loop
          goto 3

*
*  End of story
*
   99 close(1)

      close(33)
      close(34)
      close(35)
	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)


	call I_spy

        

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

         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)=12 ! wall
         tsens(2)=13 ! airo
         tsens(3)=14 ! 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
	   tsens(1)=19
           tsens(2)=25
           tsens(3)=26
           tsens(4)=18
           lxplate=200.
           txplate_cal(0)=20.2
           if (name(4:4).eq.'E') lxplate=2600.
	elseif (xplate.eq.1) then
	   tsens(1)=29
	   tsens(2)=27
	   tsens(3)=22
           tsens(4)=28
           lxplate=200.
           if (name(4:4).eq.'E') lxplate=2600.
           txplate_cal(1)=20.2
        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)=4
           tsens(2)=5
           tsens(3)=6
           tsens(4)=7
           lxplate=0.
	endif

C Inplane
        if (name(3:6).eq.'InPl') then
           tsens(1)=24
           tsens(2)=20
           tsens(3)=17
           tsens(4)=23
           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.
         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/'//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)

*        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 I_spy
        include 'Online.cmm'
        integer system
* get tunix
      istat=system(' date \'+%H\' > time.txt ')
      istat=system(' date \'+%M\' >> time.txt ')
      open(34,file='time.txt')
      read(34,*)ih
      read(34,*)im
      close(34)
      tunix=float(ih)+float(im)/60.
* read old stuff
        open(44,file='spy.raw')
        read(44,*)datum_old
        read(44,*)tunix_old
        read(44,*)trasnik_old(1),trasnik_old(2),nrasnik_old
        read(44,*)ttemp_old(1),ttemp_old(2),ntemp_old
        read(44,*)irasdataerror,itempdataerror
        close(44)
* write new stuff + check
        istat=system('date >> spynew.txt')
        open(44,file='spynew.txt')
	   if (abs(trasnik(2)-tunix).gt.1 .and. 
     +         abs(trasnik(2)-tunix).lt.23.) then ! no data for one hour
             write(44,*)'FATAL: no RASNIK data for one hour '
             if (irasdataerror.eq.0 .or. irasdataerror.gt.11) then
                istat=system('mail -s RASNIK_PROBLEM h73 < mailinfo')
                irasdataerror=1
	     else
	        irasdataerror=irasdataerror+1
             endif
           else
	     irasdataerror=0
           endif
           if (abs(ttemp(2)-tunix).gt.1  .and.
     +         abs(ttemp(2)-tunix).lt.23.) then ! no data for one hour
             write(44,*)'FATAL: no TEMP data for one hour '
             if (itempdataerror.eq.0 .or. itempdataerror.gt.11) then
                istat=system('mail -s TEMP_PROBLEM h73 < mailinfo')
	        itempdataerror=1
             else
	        itempdataerror=itempdataerror+1
             endif
           else
	     itempdataerror=0
           endif
	write(44,*)'datum: ',datum,' tijd:',tunix
	write(44,*)'RASNIK tijd: ',trasnik(1), ' to ',trasnik(2),
     +           ' # readings: ',nrasnik
	write(44,*)'TEMP tijd: ',ttemp(1), ' to ',ttemp(2),
     +           ' # readings: ',ntemp
        write(44,*)'********************************************'
        close(44)
        istat=system('cat spynew.txt >> spy.txt')
* 
        open(44,file='spy.raw')
        write(44,*)datum
        write(44,*)tunix
        write(44,*)trasnik(1),trasnik(2),nrasnik
        write(44,*)ttemp(1),ttemp(2),ntemp
        write(44,*)irasdataerror,itempdataerror
        close(44)
	end
