* Last processed by NICE on 12-Jun-2000 15:53:00
* Customized for :  IEEE, LINUX, UNIX, MOTIF, F77
      SUBROUTINE NIC_CHECK(LINE,ERROR)
C ----------------------------------------------------------------------
C 	Support routine for command
C	CHECK 	no input parameter required
C
C Arguments :
C	LINE	C*(*)	Command line			Input
C	ERROR	L	Logical error flag		Output
C
C ----------------------------------------------------------------------
      CHARACTER*(*) LINE
      LOGICAL ERROR
*
      INCLUDE 'nic.inc'
      INCLUDE 'parameter.inc'
      INCLUDE 'par.inc'
* Local
*
*
* Verify this is possible now
*
      IF (NRECORD.EQ.0) THEN
         CALL MESSAGE(2,1,'CHECK','No input file opened')
         RETURN
      ENDIF
      IF (SCAN_TYPE.EQ.6.OR.SCAN_TYPE.EQ.3.OR.SCAN_TYPE.EQ.8) THEN
c        CALL MESSAGE(2,1,'CHECK','Scan type not supported')
         RETURN
      ENDIF
*
* data consistency check
*
      IF (TELESCOPE(1:3).EQ.'CSO'.OR.TELESCOPE(1:3).EQ.'HHT') THEN
         RETURN
      ENDIF
      CALL DATA_CONSISTENCY_CHECK (NRECORD,MEMORY(IP_AZ),
     $MEMORY(IP_EL),MEMORY(IP_LST),
     $MEMORY(IPSUBSCAN+NSUBSCAN),
     $MEMORY(IPSUBSCAN+2*NSUBSCAN),
     $NSUBSCAN,MEMORY(IP_SIGNAL))
*
      RETURN
      END
*
      SUBROUTINE DATA_CONSISTENCY_CHECK (NRECORD1,AZ,EL,LST,
     $SUBSCAN_EPOCH,
     $SUBSCAN_TIME,NSUBSCAN1,SIG)
      INCLUDE 'nic.inc'
      INCLUDE 'parameter.inc'
      INCLUDE 'par.inc'
*
*
      LOGICAL NEXT1, CONSISTENT, SIC_PRESENT
      INTEGER NRECORD1, NSUBSCAN1, I, J, K, L, START
      REAL*4  SUBSCAN_EPOCH(1),SUBSCAN_TIME(1), SIG(1)
      INTEGER OFF_SUBSCAN, OFF_RECORD, IB, IC
      REAL*4 AZ(NRECORD1), EL(NRECORD1), LST(NRECORD1), LST_LAST
      REAL*4 SHOULD_LST, AZ_GAP, EL_GAP, LST_RECORD, LST_SUBSCAN
      REAL*4 LST_OFFSET
      CHARACTER STRING*80, MESS*39
*
      MESS = ' Data inconsistency detected at record '
      CONSISTENT = .TRUE.
      START = NRECORD1
      OFF_SUBSCAN = NSUBSCAN1+1
      OFF_RECORD = NRECORD1+1
      J = 1
      IB = 0
      IC = 0
      IF (SIC_PRESENT(1,0)) THEN
         DO I = 1,NRECORD1-1
            IF (LST(I+1).LT.LST(I)) THEN
               LST(I+1) = LST(I)+WOBBLER_PERIOD*0.01
               DO L = 1,NCHAN
                  SIG(I+1+(L-1)*NRECORD1) = BLANKING
               ENDDO
               CONSISTENT = .FALSE.
               IB = IB+1
               IC = IC+1
            ENDIF
         ENDDO
      ENDIF
      DO WHILE (OFF_SUBSCAN.EQ.NSUBSCAN1+1.AND.J.LT.NSUBSCAN1)
         IF (SUBSCAN_TIME(J).GT.86300.AND.SUBSCAN_TIME(J+1).LE.100)
     $   OFF_SUBSCAN = J+1
         J = J+1
      ENDDO
      J = 1
      DO WHILE (OFF_RECORD.EQ.NRECORD1+1.AND.J.LT.NRECORD1)
         IF (LST(J).GT.86380.AND.LST(J+1).LE.20) OFF_RECORD = J+1
         J = J+1
      ENDDO
      DO I = NSUBSCAN1,1,-1
         LST_SUBSCAN = SUBSCAN_TIME(I)
         IF (I.GE.OFF_SUBSCAN) LST_SUBSCAN = LST_SUBSCAN+86400.0
         NEXT1 = .TRUE.
         DO J = 1,START
            LST_RECORD = LST(J)
            LST_OFFSET = 0
            IF (J+1.GE.OFF_RECORD) LST_OFFSET = 86400.0
            IF (J.GE.OFF_RECORD) LST_RECORD = LST_RECORD+86400.0
            IF (LST_RECORD.GE.LST_SUBSCAN.AND.NEXT1) THEN
               DO K = J+1,START
                  AZ_GAP  = AZ(K)-AZ(K-1)
                  EL_GAP  = EL(K)-EL(K-1)
                  SHOULD_LST = LST(J+1)+(K-(J+1))*WOBBLER_PERIOD
     $            +LST_OFFSET
                  IF (K.GE.OFF_RECORD) THEN
                     LST_LAST = LST(K)+86400.0
                  ELSE
                     LST_LAST = LST(K)
                  ENDIF
                  IF (ABS(AZ_GAP).GE.0.1) THEN
                     WRITE (STRING,'(A,I4,'' (AZIMUTH)'')') MESS,K
                     CONSISTENT = .FALSE.
                     IF (.NOT.SIC_PRESENT(1,0))
     $               CALL MESSAGE (2,1,'CHECK',STRING)
                     IB = IB+1
                  ENDIF
                  IF (ABS(EL_GAP).GE.0.1) THEN
                     WRITE (STRING,'(a,I4,'' (ELEVATION)'')') MESS,K
                     CONSISTENT = .FALSE.
                     IF (.NOT.SIC_PRESENT(1,0))
     $               CALL MESSAGE (2,1,'CHECK',STRING)
                     IB = IB+1
                  ENDIF
                  IF (ABS(SHOULD_LST-LST_LAST).GE.
     $            WOBBLER_PERIOD*0.8) THEN
                     WRITE (STRING,'(a,I4,'' (TIME)'')') MESS,K
                     CONSISTENT = .FALSE.
                     IF (.NOT.SIC_PRESENT(1,0))
     $               CALL MESSAGE (2,1,'CHECK',STRING)
                     IB = IB+1
                  ENDIF
                  IF (K.LT.START) THEN
                     IF (LST(K).GE.LST(K+1)) THEN
                        IF (SIC_PRESENT(1,0)) THEN
                           LST(K+1) = LST(K)+WOBBLER_PERIOD*0.01
                           DO L = 1,NCHAN
                              SIG(K+1+(L-1)*NRECORD1) = BLANKING
                           ENDDO
                           CONSISTENT = .FALSE.
                           IB = IB+1
                           IC = IC+1
                        ENDIF
                     ENDIF
                  ENDIF
               ENDDO
               NEXT1 = .FALSE.
               START = J-1
            ENDIF
         ENDDO
      ENDDO
      IF (SIC_PRESENT(1,0)) THEN
         IF (CONSISTENT) THEN
            CALL MESSAGE(2,1,'CHECK','Consistent dataset')
         ELSE
            WRITE (STRING,'(A,I4)')    'Found inconsistencies:   ',IB
            CALL MESSAGE (2,1,'CHECK',STRING)
            IF (IC.NE.0) THEN
               WRITE (STRING,'(A,I4)') 'Inconsistencies removed: ',IC
               CALL MESSAGE (2,1,'CHECK',STRING)
               CALL RECORDS_IN_SUBSCAN (MEMORY(IPSUBREC),
     $         MEMORY(IPSUBSCAN),MEMORY(IPSUBSCAN+2*NSUBSCAN),
     $         NSUBSCAN,MEMORY(IP_LST),MEMORY(IP_SCAN_COORD))
            ELSE
               WRITE (STRING,'(A,I4)') 'Nothing to repair...'
               CALL MESSAGE (2,1,'CHECK',STRING)
            ENDIF
         ENDIF
      ENDIF
      RETURN
      END
