C$Procedure NEXTQ ( read next pointing record from input file )
C
      SUBROUTINE NEXTQ ( DATTYP, DATFIL, IVEL, AXNUM, PRCYCL, HANDLE,
     .                                  DESCR,  NREC, RECORD, EOFQTR )

C$ Abstract
C          NEXTQ sequentally reads pointing records from input file.
C          If EOF is reached then EOFQTR = .TRUE. is returned
C

C$ Declarations

      IMPLICIT                NONE
      
      INCLUDE                 'ckparam.inc'

      INTEGER                 DATTYP
      CHARACTER*(*)           DATFIL
      INTEGER                 IVEL
      INTEGER                 AXNUM   ( 3 )
      INTEGER                 PRCYCL
      INTEGER                 HANDLE
      DOUBLE PRECISION        DESCR   ( 5 )
      INTEGER                 NREC
      DOUBLE PRECISION        RECORD  ( 8 )
      LOGICAL                 EOFQTR


C$ Copyright
C
C      Copyright (1997-1998), Jet Propulsion Laboratory
C
C$ Brief_I/O
C
C     Variable  I/O  Description
C     --------  ---  --------------------------------------------------
C     DATTYP     I   Input data type
C     DATFIL     I   Input data file name
C     IVEL       I   Angular velocity flag
C     AXNUM      I   3 axis codes of euler angles rotation sequence
C     PRCYCL     I   number of current record
C     HANDLE     I   Handle for input CK DAF file
C     DESCR      I   Double precision components of segment descriptor
C                    (only for CK input files).
C     NREC       I   Number of records in the input file
C                    (only for CK input files)
C     RECORD     O   Pointing record
C     EOFQTR     O   End of file flag (no more records)
C
C
C$ Detailed_Input
C
C     DATTYP     Input data type for a file which is defined in setup
C                file of MAKECK4 programme. The type can be:
C                CK, raw quaternions array, Euler angles array
C
C     DATFIL     Input data file name which is provided by INPUT_FILE
C                parameter in setup file of MAKECK4 programme
C
C     IVEL       Angular velocities presence flag for input file records
C                ( IVEL = 1 - present
C                  IVEL = 0 - not present )
C
C     AXNUM      3 axis codes of euler angles rotation sequence
C
C     PRCYCL     Number of current record which is red form input file
C
C     HANDLE     Handle of input DAF file. This parameter has sense
C                only for CK type of input data
C
C     DESCR      An array to store double precision components of
C                segment descriptor from input CK file
C
C     NREC       Number of pointing records in the input CK file.
C
C$ Detailed_Otput
C
C     RECORD     Pointing data record
C                RECORD( 1 ) - SCLKDP
C                RECORD( 2 ) - Q0
C                RECORD( 3 ) - Q1
C                RECORD( 4 ) - Q2
C                RECORD( 5 ) - Q3
C                RECORD( 6 ) - AV1 \
C                RECORD( 7 ) - AV2  > optionaly
C                RECORD( 8 ) - AV3 /
C
C
C     EOFQTR     End of file falg which is set to .TRUE. if
C                no more records there are in the input file
C



      INTEGER                 KI
      INTEGER                 PTR
      INTEGER                 LOC
      INTEGER                 CK1TYP
      INTEGER                 CK3TYP
      INTEGER                 QTYP
      INTEGER                 EULTYP
      INTEGER                 PSIZ

      DOUBLE PRECISION        EULER   ( 3 )
      DOUBLE PRECISION        MAT     ( 3, 3 )
      DOUBLE PRECISION        QUAT    ( 4 )
      DOUBLE PRECISION        RPD


      CHARACTER*200           RECSTR
      CHARACTER*80            ERROR
      CHARACTER*30            WORD


      SAVE

      DATA CK1TYP  /  1      /,
     .     CK3TYP  /  3      /,
     .     QTYP    /  4      /,
     .     EULTYP  /  5      /


C
C     Standard SPICE error handling initialization
C
      CALL CHKIN ( 'NEXTQ' )

C
C     PSIZ is the number of pointing components in the record
C
      IF ( IVEL .EQ. 1 ) THEN
         PSIZ = QAVSIZ
      ELSE
         PSIZ = QSIZ
      END IF

C
C     If source data has CK type then use CKGR0x subruotines to get
C     record
C
C
C     If source data is CK 1 TYPE
C
      IF ( DATTYP .EQ. CK1TYP ) THEN

C
C        Get next record
C
         IF ( PRCYCL .GT. NREC ) THEN

            EOFQTR = .TRUE.

         END IF

         IF ( .NOT. EOFQTR ) THEN

            Call CKGR01 ( HANDLE, DESCR, PRCYCL,
     .                              RECORD )

         END IF

C
C     If source data is CK 3 TYPE
C
      ELSE IF ( DATTYP .EQ. CK3TYP ) THEN

C
C        Get next record
C
         IF ( PRCYCL .GT. NREC ) THEN

            EOFQTR = .TRUE.

         END IF

         IF ( .NOT. EOFQTR ) THEN

            Call CKGR03 ( HANDLE, DESCR, PRCYCL,
     .                              RECORD )
C            WRITE(*,*) PRCYCL, RECORD(1)

         END IF

C
C     If source data is QUATERNIONS
C
      ELSE IF ( DATTYP .EQ. QTYP ) THEN

C
C        Get next record from input text file
C
         CALL RDNBL ( DATFIL, RECSTR, EOFQTR )

         IF ( .NOT. EOFQTR ) THEN

            DO KI = 1, PSIZ + 1

               CALL NTHWD ( RECSTR, KI, WORD, LOC )

               CALL NPARSD ( WORD, RECORD( KI ), ERROR, PTR )

               IF ( PTR .NE. 0 ) THEN

                  CALL SETMSG ( '#th value on the QUATERNIONS '//
     .                     'input line "#" is not a DP number.' )
                  CALL ERRINT ( '#', KI                         )
                  CALL ERRCH  ( '#', RECSTR                     )
                  CALL SIGERR ( 'SPICE(BADINPUTVALUE)'          )

               END IF

            END DO

         END IF

C
C     If source data is EULER ANGLES
C
      ELSE IF ( DATTYP .EQ. EULTYP ) THEN

C
C        Get next record from input text file
C
         CALL RDNBL ( DATFIL, RECSTR, EOFQTR )

         IF ( .NOT. EOFQTR ) THEN

            DO KI = 1, PSIZ

               CALL NTHWD ( RECSTR, KI, WORD, LOC )

               CALL NPARSD ( WORD, RECORD( KI ), ERROR, PTR )

               IF ( PTR .NE. 0 ) THEN

                  CALL SETMSG ( '#th value on the EULER input '//
     .                          'line "#" is not a DP number.'  )
                  CALL ERRINT ( '#', KI                         )
                  CALL ERRCH  ( '#', RECSTR                     )
                  CALL SIGERR ( 'SPICE(BADINPUTVALUE)'          )

               END IF

            END DO

            EULER( 1 ) = RECORD( 2 )
            EULER( 2 ) = RECORD( 3 )
            EULER( 3 ) = RECORD( 4 )

            RECORD( 8 ) = RECORD( 7 )
            RECORD( 7 ) = RECORD( 6 )
            RECORD( 6 ) = RECORD( 5 )


            DO KI = 1, 3

               EULER( KI ) = RPD () * EULER( KI )
               RECORD( KI + 5 ) = RPD () * RECORD( KI + 5 )

            END DO

C
C           Transform Euler angles to quaternions
C
            CALL EUL2M (  EULER( 3 ), EULER( 2 ), EULER( 1 ),
     .                         AXNUM(3),  AXNUM(2),  AXNUM(1),  MAT  )
            CALL M2Q ( MAT, QUAT )

            DO KI = 1, 4

               RECORD( KI + 1 ) = QUAT( KI )

            END DO

         END IF

      END IF

      CALL CHKOUT ( 'NEXTQ' )
      RETURN
      END
