      subroutine readtemp(ifile)
*  
*  Read Temp. Measurements (original BOL..file with tabs) ,
*  Write 'ntpl' 
*
      include 'Online.cmm'
      character*300 line

      character*80  ffname,fname
      character*10 chstime

      character*15 chdatt
      character*13 chtimt
      character*10 chdat
      character*8  chtim
      character*8 fdate
      integer jj,mm,dd

      character*7 chsens_nr(30),chsans_nr(30),srid
      character*2 chid
      logical first
*
      parameter(ntmax=30)
      real temps(ntmax)
        common /pawc/ hmem(500000)
        call hlimit(500000)


	
      do idh=1,33
       call hbook1(100+idh,'nfill',100,0.,24.,0.)
       call hbook1(200+idh,'temp',100,0.,24.,0.) 
      enddo
*
*  Prompt for file-name and start-time and #of sensors
*
*        tname='BOLtemp20000512.txt'

	ntemp=0
        ttemp(1)=-1
	ttemp(2)=-1
      if (tname(8:15).ne.rname(5:12)) then
	print *,'RASNIK file date differs form BOLtemp file!!!'
        print *,'RASNIK file ',rname
        print *,'BOLtemp file ',tname
      endif
      call tempfile(ifile,tname)
      nsens=32
** Loop
	nlines=0
  100   read(ifile,'(a)',ERR=200,END=300)line
        nlines=nlines+1
C action for dat Date with sensornumber line
        if( line(1:4) .eq. 'Date' ) then ! bingo
        nlines=nlines+11
           read(line,1000,ERR=500,END=500)
     +            chdatt,chtimt,(chsans_nr(i),i=1,nsens) 
 500	continue
           msens = 0
           do i=1,nsens
             if( chsans_nr(i)(1:2) .ne. '08' ) goto 101
             msens = msens + 1
           enddo
  101      if ( msens .eq. 0 ) then
             print *,' ZERO sensors recognised'
           endif 
           print *,msens,' sensors recognised'
           nsens=msens
           do i=1,nsens
              chsens_nr(i) = chsans_nr(i) 
           enddo
           goto 100
        else
         if (nlines.lt.10) goto 100 ! skip very fist part of file with muck
C action for dat temp-data line
           read(line,1001) chdat,chtim,(temps(i),i=1,nsens)
           read(chtim,1002) nhour,nmin,nsec
           time=nhour+float(nmin)/60.+float(nsec)/3600.
           if (time.gt.0) then ! make sure time is a valid number
		i=i
	   else
		goto 100
	   endif
           read(chdat,1003) dd,mm,jj
           if( dd .gt. 0 )  jday = jaardag(dd,mm,jj)
           do i=1,nsens
             srid=chsens_nr(i)
             chid=srid(6:7)
             read(chid,501)idh
 501	format(i2)
	     call hfill(100+idh,time,0.,1.)
	     call hfill(200+idh,time,0.,temps(i))
           enddo
           ntemp=ntemp+1
	   ttemp(2)=time
	   if (ttemp(1).lt.0) ttemp(1)=time
           goto 100
        endif

	
 2000 format(i4,1x,i2.2,1x,i2.2,1x,i4,
     >                  f8.4,30f7.3) 
 1000 format(32(a,1x))
 1001 format(a,1x,a,30(1x,f7.3))
 1002 format(i2.2,1x,i2.2,1x,i2.2)
 1003 format(i2,1x,i2,1x,i4)
  200 print *,' Read error'
  300 print *
      print 3000,ncyc,chdat,chtim
 3000 format(' Written ',i8,' cycles to /tmp/boltemp.dat',/,
     +       '     Last datime : ',a,' ',a)

        call hfinish

	xplate=0
        if (xplate.eq.0) then
           tsens(1)=4
           tsens(2)=25
           tsens(3)=26
           tsens(4)=1
        else
           tsens(1)=29
           tsens(2)=27
           tsens(3)=22
           tsens(4)=28
        endif
 	call get_txplate(16.3,temp)
	print *,'temperature ',temp


      lun  = ifile+2
      CALL HROPEN(lun,'oops','uit','N',1024,ISTAT)
      if (istat.ne.0) then
        CALL HROPEN(lun,'oops','uit','U',1024,ISTAT)
      endif
      call hrout(0,icycle,'T')
      call hrend('oops')
      close(lun)
      end

      integer function jaardag(idag,maand,jaar)

*  datum <-> jaardag ( incl schrikkeldag )  

      integer maandag(13) 
*                  jan  feb maa  apr mei juni
*                  juli aug sept okt nov dec 
      data maandag/0,   31,  59,  90, 120,151,
     >            181, 212, 243, 273, 304,334, 365/

      ischrik = 0
      if ( mod(jaar,4) .eq. 0 ) ischrik =1

      jdag = maandag(maand) + idag
      if ( maand .gt. 2 ) jdag = jdag +ischrik

      JAARDAG = JDAG
      RETURN
      END


   
	subroutine hfinish
C fills empty bins in histograms.

        nbin=100
	xl=0.
	xh=24.

       do idh=1,33
C (2046,icel)
C hi(2046,icel)
C call hxi(250,xdet ,id)
C call hix(idata,i,x)
C       call hopera(300,'/',420,425,1.,1.)
	
c       call hbook1(100+idh,'nfill',100,0.,24.,0.)
c       call hbook1(200+idh,'temp',100,0.,24.,0.)
       ifirst=0
       ilast=0
       if (ifirst.eq.0 .and. ilast.eq.0) goto 10
 20	continue
        if (ifirst.eq.1 .and. ilast.eq.nbin) then ! no info
          goto 33
        endif


        if (ifirst.eq.1) then ! first part missing
          do j=1,ilast
            call hix(idh+100,j,x1)
            call hix(idh+100,j+1,x2)
            x=(x1+x2)/2.
            call hfill(idh+100,x,0.,hi(idh+100,ilast+1))
            call hfill(idh+200,x,0.,hi(idh+200,ilast+1))
          enddo
          ilast=0
          ifirst=0
        endif

	if (ilast.eq.nbin) then ! last part missing
	  do j=ifirst,nbin
            call hix(idh+100,j,x1)
            call hix(idh+100,j-1,x2)
            x=x1+ (x1-x2)/2.
            call hfill(idh+100,x,0.,hi(idh+100,ifirst-1))
            call hfill(idh+200,x,0.,hi(idh+200,ifirst-1))
          enddo
          ilast=0
          ifirst=0
        endif

	if (ifirst.gt.0 .and. ilast.gt.0) then ! some times are missing
          do j=ifirst,ilast
            call hix(idh+100,ifirst-1,xf)
            call hix(idh+100,ilast+1,xl)
            tempf=hi(idh+200,ifirst-1)/hi(idh+100,ifirst-1)
            templ=hi(idh+200,ilast+1)/hi(idh+100,ilast+1)
            call hix(idh+100,j,x1)
            call hix(idh+100,j+1,x2)
            x=(x1+x2)/2.
            dx=x2-x1
	    xf=xf+dx/2.
            xl=xl+dx/2.
            temp=tempf+ (templ-tempf)/(xl-xf) * (x-xf)
            call hfill(idh+100,x,0.,1.)
            call hfill(idh+200,x,0.,temp)
          enddo
          ilast=0
          ifirst=0
        endif
 10	continue
           ifirst=0
           ilast=0
	do i=1,nbin
           rfill=hi(100+idh,i)
           if (rfill.lt.0.0001 .and. ifirst.eq.0)  ifirst=i
           if (rfill.gt.0.0001 .and.
     +          ifirst.ne.0 .and. ilast.ne.0) goto 20
           if (rfill.lt.0.0001 .and. 
     +         ifirst.gt.0)  ilast=i
	enddo ! i to nbin
        if (ifirst.ne.0 .and. ilast.ne.0) goto 20
 33	continue
       enddo ! idh to 33
      end

	subroutine get_txplate(time_now,txplate)
	include 'Online.cmm'
        logical hexist
	tav=0
	rav=0
        txplate=txplate_cal(-1) ! default temperature
	do i=1,4
           rcount=0
	   if (hexist(100+tsens(i))) then
	     temp=hx(200+tsens(i),time_now)
	     rcount=hx(100+tsens(i),time_now)
           endif
           if (rcount.gt.0) then
		tav=tav+temp
		rav=rav+rcount
           endif
        enddo
        if (rav.gt.0.) then
	  txplate=tav/rav
          ierror(1)=0
	else
c	  print *,'Temperature set to default '
          ierror(1)=1
	endif
	end



