C Copyright(c) 1997, Space Science and Engineering Center, UW-Madison
C Refer to "McIDAS Software Acquisition and Distribution Policies"
C in the file  mcidas/data/license.txt

C *** $Id: resync.f,v 1.9 1997/10/10 20:18:45 dglo Exp $ ***

C     RESYNC - TRY TO FIND THE SYNC PATTERN AFTER LOSING IT       (RCD)
C           STATUS - 0=OK  -1=DID NOT FIND

      SUBROUTINE RESYNC(STATUS)
      IMPLICIT INTEGER(A-Z)

      DIMENSION PATTRN(8)
      COMMON /RTCOMX/ RTC
      DATA MAXCHK/4000/,PATTRN/00,255,00,255,00,255,00,255/

      STATUS=-1
      I=0

1     I=I+1
      IF(I.GT.MAXCHK) RETURN
      pat = 0
      CALL GRAB(1,PAT,RETBYT)
      call fbyte4(pat, 1)
      call swbyt4(pat, 1)
      IF(RTC.EQ.1) RETURN

2     IF(PAT.NE.PATTRN(1)) GOTO 1

      DO 10 J=2,8
      I=I+1
      IF(I.GT.MAXCHK) RETURN
      pat = 0
      CALL GRAB(1,PAT,RETBYT)
      IF(RTC.EQ.1) RETURN
      call fbyte4(pat, 1)
      call swbyt4(pat, 1)
      IF(PAT.NE.PATTRN(J)) GOTO 2
10    CONTINUE

      STATUS=0
      RETURN
      END
