       SUBROUTINE SYNC1B(INSTIM,SCTSC,LCM,SYNCTR,SYNTIM,N)
       IMPLICIT NONE
C **********************************************************************
C
C   NAME- SYNC1B
C
C   LANGUAGE- FORTRAN 77      TYPE- SUBROUTINE    COMPUTER- CRAY    
C
C   VERSION- 1.0       DATE- 12/11/90   PROGRAMMER- CARLA NEIGH(SMSRC)
C   VERSION- 3.0       DATE- 05/28/96   PROGRAMMER- ALEX POZDNYAK (FDC)
C      MODIFIED TO RUN ON THE CRAY.  REPLACED I*4 WITH INT.
C   VERSION- 3.1       DATE- 06/15/05   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C      RETURNED THE FIRST SCAN FOUND FOR EACH INSTRUMENT INSTEAD OF
C      TRYING TO SYNCH THE INSTRUMENTS BASED ON START TIME. SYNCHRONIZATION
C      IS DONE IN INHIRS, INAMA, AND INAMB WITHOUT REFERENCE TO
C      THE ATTEMPT AT SYNCHRONIZATION DONE HERE.
C
C   FUNCTION-  SYNC1B FINDS THE SYNCHRONIZATION POINT FOR TWO OR MORE
C              INSTRUMENTS.  THE SYNCHRONIZATION POINT IS DEFINED AS
C              THE SCAN LINES FOR EACH INSTRUMENT WHICH HAVE THE
C              SMALLEST DIFFERENCE BETWEEN THE LARGEST AND SMALLEST
C              START OF SCAN TIMES.  THE INSTRUMENT SCAN TIMES HAVE
C              A REPEATING PATTERN WITH A LENGTH IN TIME EQUAL TO
C              THE LEAST COMMON MULTIPLE OF THE INSTRUMENT SCAN-TO-SCAN
C              TIMES.  THUS, WHEN FINDING THE SYNCH POINT, A TIME
C              WINDOW EQUAL TO THE LEAST COMMON MULTIPLE MUST BE
C              EXAMINED.
C
C   CALLING SEQUENCE PARAMETERS-
C       VARIABLE  TYPE  I/O  DESCRIPTION
C       --------  ----  ---  -----------
C       INSTIM     INT   I   ARRAY 2 BY N OF INST. START TIMES
C                            (ELEM. 1 IS CENT DAY;2 IS MILLISEC OF DAY)
C       SCTSC      INT   I   ARRAY DIM N OF INST. SCAN-TO-SCAN TIMES
C                            (IN MILLISECONDS)
C       LCM        INT   I   LEAST COMMON MULTIPLE OF INSTRUMENT SCAN-
C                            TO-SCAN TIMES (IN MILLISECONDS)
C       SYNCTR     INT   O   ARRAY OF SYNCH POINT DUMMY LINE COUNTERS
C       SYNTIM     INT   O   ARRAY 2 BY N OF SYNCH POINT TIMES
C                            (SAME FORMAT AS INSTIM)
C       N          INT   I   NUMBER OF INSTRUMENTS TO BE SYNCHRONIZED
C
C   LOCAL VARIABLES-
C       VARIABLE    TYPE   DESCRIPTION
C       --------    ----   -----------
C       NMAX        INT    ARRAY INDEX OF LATEST INSTRUMENT TIME
C       NMIN        INT    ARRAY INDEX OF EARLIEST INSTRUMENT TIME
C       MXSTRT      INT    LATEST (MAX) INSTRUMENT START TIME
C       MNSTRT      INT    EARLIEST (MIN) INSTRUMENT START TIME
C       MAXTIM      INT    LATEST (MAX) INSTRUMENT SCAN TIME
C       MINDIF      INT    MINIMUM TIME DIFFERENCE ENCOUNTERED
C                          BETWEEN EARLIEST AND LATEST INSTRUMENT
C                          TIMES (IN MILLISECONDS)
C       MINTIM      INT    EARLIEST (MIN) INSTRUMENT SCAN TIME
C       INSCTR      INT    INSTRUMENT "DUMMY" COUNTERS
C       TIMDIF      INT    DIFFERENCE BETWEEN EARLIEST AND LATEST TIMES
C       DUMTIM      INT    TIME DUMMIED BACK FROM EARLIEST START TIME
C       I           INT    DO LOOP COUNTER
C
C   SUBPROGRAMS CALLED-  MAXOFN, MINOFN
C
C***********************************************************************
C
      INTEGER N, NMAX, NMIN, INSTIM(2,N), SYNTIM(2,N)
      INTEGER MXSTRT(2), MNSTRT(2), MAXTIM(2), MINTIM(2), MINDIF
      INTEGER SCTSC(N), SYNCTR(N), INSCTR(20), TIMDIF, DUMTIM, LCM, I
C
      CALL MAXOFN(INSTIM,N,NMAX)
      CALL MINOFN(INSTIM,N,NMIN)
C
      MXSTRT(1) = INSTIM(1,NMAX)
      MXSTRT(2) = INSTIM(2,NMAX)
      MNSTRT(1) = INSTIM(1,NMIN)
      MNSTRT(2) = INSTIM(2,NMIN)
C
C  THE TIME DIFFERENCE IN MILLISECONDS MUST BE OBTAINED BETWEEN THE
C  EARLIEST AND LATEST START TIMES.  FROM CHECKING THE 1B DATASET
C  HEADER INFORMATION, WE KNOW THE 1B DATASETS MUST OVERLAP EACH OTHER.
C  WE ALSO KNOW THAT A 1B DATASET IS NOT LONGER THAN 115 MINUTES, THUS
C  WE CAN ASSUME THE TIME DIFFERENCE WOULD BE LESS THAN 24 HOURS
C  (86400000 MILLISECONDS).
C
      IF (MXSTRT(2) .LT. MNSTRT(2)) THEN
         TIMDIF = 86400000 - MNSTRT(2) + MXSTRT(2)
      ELSE
         TIMDIF = MXSTRT(2) - MNSTRT(2)
      ENDIF
C
      DO 10 I = 1,N
         SYNCTR(I) = 0
         INSCTR(I) = 0
         SYNTIM(1,I) = INSTIM(1,I)
         SYNTIM(2,I) = INSTIM(2,I)
   10 CONTINUE
ccft
C     BYPASS THE REST OF THIS ROUTINE
      IF (SYNCTR(1).EQ.INSCTR(1)) GO TO 90
C
      MINDIF = TIMDIF
      MAXTIM(1) = MXSTRT(1)
      MAXTIM(2) = MXSTRT(2)
      MINTIM(1) = MNSTRT(1)
      MINTIM(2) = MNSTRT(2)
      DUMTIM = 0
C
C  THE LATEST INSTRUMENT SCAN TIME IS "DUMMIED BACK" ONE SCAN LINE,
C  I.E. THE INSTRUMENT SCAN-TO-SCAN TIME IS SUBTRACTED FROM THE CURRENT
C  INSTRUMENT SCAN TIME TO GET A NEW INSTRUMENT SCAN TIME, AND THE
C  INSTRUMENT "DUMMY" COUNTER IS INCREMENTED BY 1.
C
   20 IF (MAXTIM(2) .LT. SCTSC(NMAX)) THEN
        INSTIM(2,NMAX) = 86400000 - SCTSC(NMAX) + MAXTIM(2)
        INSTIM(1,NMAX) = INSTIM(1,NMAX) - 1
      ELSE
        INSTIM(2,NMAX) = MAXTIM(2) - SCTSC(NMAX)
      ENDIF
      INSCTR(NMAX) = INSCTR(NMAX) + 1
C
C  CALL MAXOFN AND MINOFN TO COMPUTE THE NEW MAX AND MIN INSTRUMENT
C  TIMES.
C
      CALL MAXOFN(INSTIM,N,NMAX)
      CALL MINOFN(INSTIM,N,NMIN)
C
      MAXTIM(1) = INSTIM(1,NMAX)
      MAXTIM(2) = INSTIM(2,NMAX)
      MINTIM(1) = INSTIM(1,NMIN)
      MINTIM(2) = INSTIM(2,NMIN)
C
C  COMPUTE THE NEW TIME DIFFERENCE BETWEEN THE MAX AND MIN SCAN TIMES.
C
      IF (MAXTIM(2) .LT. MINTIM(2)) THEN
         TIMDIF = 86400000 - MINTIM(2) + MAXTIM(2)
      ELSE
         TIMDIF = MAXTIM(2) - MINTIM(2)
      ENDIF
C
C  THE DIFFERENCE BETWEEN THE EARLIEST AND LATEST INSTRUMENT TIMES IS
C  CHECKED AGAINST THE MINIMUM DIFFERENCE ENCOUNTERED.  IF IT IS LESS,
C  THEN THE TIME DIFFERENCE BECOMES THE NEW MINIMUM AND THE CURRENT
C  DUMMY COUNTERS AND INSTRUMENT TIMES BECOME THE NEW SYNCH POINT
C  COUNTERS AND TIMES.
C
      IF (TIMDIF .LT. MINDIF) THEN
         MINDIF = TIMDIF
ccft     DO 30 I = 1,N
ccft        SYNCTR(I) = INSCTR(I)
c           SYNTIM(1,I) = INSTIM(1,I)
c           SYNTIM(2,I) = INSTIM(2,I)
c  30    CONTINUE
      ENDIF
C
C  NOW COMPUTE THE TIME (IN MSEC) 'DUMMIED BACK' FROM THE EARLIEST
C  ORIGINAL START TIME.  THIS TIME WILL BE COMPUTED FROM THE LATEST
C  INSTRUMENT TIME, SO THAT ALL INSTRUMENTS WILL HAVE BEEN DUMMIED
C  BACK AT LEAST THAT MUCH.  IF THE LATEST INSTRUMENT TIME IS LATER
C  IN TIME THAN THE MINIMUM START TIME, THE TIME DUMMIED BACK WILL BE
C  NEGATIVE.
C
      IF (MAXTIM(1) .EQ. MNSTRT(1)) THEN
        DUMTIM = MNSTRT(2) - MAXTIM(2)
      ELSEIF (MAXTIM(1) .GT. MNSTRT(1)) THEN
        DUMTIM = -1
      ELSE
        DUMTIM = 86400000 - MAXTIM(2) + MNSTRT(2)
      ENDIF
C
C  CHECK TO SEE IF TIME DUMMIED BACK IS AT LEAST THE LEAST COMMON
C  MULTIPLE.  IF NOT, KEEP CHECKING FOR THE SYNCH POINT.
C
      IF (DUMTIM .LT. LCM) GOTO 20
C
   90 CONTINUE
      RETURN
      END
