	subroutine looptime(npas,RESTART)
c       reads RC-Clock time, and loop untill TIME(6) [s] = wanted s
c       and starts then RC.
c-----------------------------------------------------------------------
c       enielsen        August, 1995
c       Revision:
c                       en dec 1995: for RC clock: min increment 5 ms after sec=0
c                       en feb 1996: set CPU clock once every 24 hours
c                       en march 1996: enable/disable RC, rckick /Saeger
c                       en april 1996: 'ld_adc adc_1.out' /Perplies
c                       en may 1996: shift between radio- and cpu clock
c                       en august 1996: include Watchdog /Saeger
c                       en january 1997: include 2 ADC cards/ Saeger,Perplies
c                       en december 1999: y2k
c-----------------------------------------------------------------------
c
	INTEGER*4
     #  IT,NT,RESTART 

	INTEGER*4  HDR(100)
	INTEGER*2  TIME(6),NTIME(6)
     
	COMMON/BLK_MES/HDR
	COMMON/BLK_TIM/TIME,NTIME
	COMMON/BLK_PRM/idecode,itpds,ip,iclock,idspl

	REAL year,month,tday,nday

	LOGICAL IFTODO

C        c_external PRINTOUT
C        Character text1*18,text2*9,text3*4
C        data text2/'time < '/
C        data text3/'cput'/

	RESTART=0

	call gettime
c
c       check radio clock is OK after re-boot
	if((iclock.eq.2).and.(npas.eq.0)) then
	is1=TIME(6)
	 WTIME=1.0
C         call sleep@(WTIME)
	 call gettim(ih,im,isold,i100)
98        call gettim(ih,im,is,i100)
	  if(is.eq.isold) goto 98
99        call gettim(ih,im,isn,i100)
	  if(is.eq.isn) goto 99
	call gettime
	is2=TIME(6)
	  if(((TIME(2).ge.1).and.(TIME(2).le.12)).and. 
     #     (is1.ne.is2)) then                        !if MONTH OK
	  write(6,*) 'RADIO CLOCK MONTH = ',TIME(2)
	  else
	  write(6,*) 'RADIO CLOCK MONTH = ',TIME(2)
	       iclock=1  !change to read CPU clock
	       call gettime
	       write(6,*) 'CPU CLOCK MONTH = ',TIME(2)
	       HDR(28)=1
	       open(10,file='header.par',status='old')
	       do i=1,100
	       write(10,11) i,HDR(i)
	       enddo
11             format(1x,I6,I8)
	       close(10)
	  endif
	endif
c
c       if reading CPU clock then check every 4 hours to see if radio clock OK
cc        do i=1,24,4
c        i=10
c        if((TIME(4).eq.i).and.(TIME(5).eq.20).and.(iclock.eq.1))then
c        iclock=2
c        call gettime
c        is1=TIME(6)
c         WTIME=1.0
c         call sleep@(WTIME)
c        call gettime
c        is2=TIME(6)
c          if(((TIME(2).ge.1).and.(TIME(2).le.12)).and. 
c     #     (is1.ne.is2)) then                        !if MONTH OK
c               iclock=2  !change to read radio clock
c               HDR(28)=2
c               open(10,file='header.par',status='old')
c               do ii=1,100
c               write(10,11) ii,HDR(ii)
c               enddo
c               close(10)
c               goto 44
c          else
c                iclock=1 !continue to read cpu clock
c                call gettime
c                goto 44
c          endif
c        endif
cc        enddo
c44      continue
c
c       if reading radio clock then set CPU clock at 1100 UT:
	if(iclock.eq.2) then
	IF((time(4).eq.11).and.(time(5).eq.0))THEN
c        open(3,file='cput',status='old')
c        write(3,10) (time(i),i=4,6)
c        close(3)
c10      format(I2.2,':',I2.2,':',I2.2)
c        text1=text2//text3
c        call cissue(text1,istst)
	call SETTIM(time(4),time(5),time(6),0)
	call SETDAT(time(1),time(2),time(3))
	write(6,*) 'cpu time set'
	ELSE
	ENDIF
	endif
c
	IF(npas.ne.0) THEN

	year=float(TIME(1))
	month=float(TIME(2))
	tday=float(TIME(3))
	call sdateday(year,month,tday)
	td=ifix(tday)
	year=float(NTIME(1))
	month=float(NTIME(2))
	nday=float(NTIME(3))
	call sdateday(year,month,nday)
	nd=ifix(nday)
	
	IT=((TIME(1)-1995)*365*86400+(td-1)*86400+
     #   TIME(4)*3600+TIME(5)*60+TIME(6))
	NT=((NTIME(1)-1995)*365*86400+(nd-1)*86400+
     #   NTIME(4)*3600+NTIME(5)*60+NTIME(6))

	IF((IT-NT).gt.0) goto 40        !if TIME>NTIME
	goto 41

40      continue
	write(6,*) 'TIME-NTIME = ',IT-NT,'; TIME: ',(TIME(i),i=4,6) 
	write(6,*) 'TIME TOO LATE; EXPERIMENT RESTARTING'

	RESTART=1

	return

	ELSE
	ENDIF

41      write(6,*) '>> Waiting for Start of Integration Time........'
	if( NTIME(6).eq.0
C     .          .and. NTIME(5).eq.59           ! check once per hour
     .    ) then
		inquire(FILE='P:\UPDATE\DO.BAT',EXIST=IFTODO)
		if(IFTODO) STOP 'STOP - for SW update'
	    endif

	IF(npas.eq.0)then
42         call gettime
	 it=time(5)+1                 !
	 if(time(6).ge.55) it=it+1    !
	 if(it.ge.60) goto 42
	  write(6,*) 'Start on minute = ',it
	 DO WHILE (Time(5).ne.it)     !((NTIME(6)-TIME(6)).ne.0)
	  call gettime
	 ENDDO
	ELSE
	 DO WHILE ((NTIME(6)-TIME(6)).ne.0)
	  call gettime
	 ENDDO
	ENDIF

c       NTIME=TIME=start of next int.tim; start RC, and update header:

	ircst=1               !to start RC
	call rcstrstp(ircst)  ! =1 start RC; =0 stop RC
c
	return
	end
