C$Procedure      SAVREC ( MPFPEF, saves EK record data to buffers   )
 
      SUBROUTINE SAVREC(   TIME,   CMD,    PARNAM, PARIDX,
     .                     CHRVAL, ENTRID, MAXREC, RECIDX,
     .                     MAXCHA, CHAIDX, MAXLLN, CHRLLN,
     .                     LNVFLG, FLTIME, FLCMD,  FLPAR,
     .                     FLCHV,  FLCHF,  FLDPV,  FLDPF,
     .                     FLINV,  FLINF,  FLCHAR, FLCHAE, 
     .                     FLCHAF, FLFLG,  FLENTR, FLPRID,
     .                     ENTSZS   )
     
C$ Abstract
C
C     This subroutine saves data for a single EK data record into 
C     program buffers.
C
C$ Copyright
C
C     Copyright (1995), California Institute of Technology.
C     U.S. Government sponsorship acknowledged.
C
C$ Required_Reading
C
C     EK.REQ
C
C$ Keywords
C
C     None.
C
C$ Declarations

      DOUBLE PRECISION      TIME
      CHARACTER*(*)         CMD
      CHARACTER*(*)         PARNAM
      INTEGER               PARIDX
      CHARACTER*(*)         CHRVAL
      CHARACTER*(*)         ENTRID
      INTEGER               MAXREC
      INTEGER               RECIDX
      INTEGER               MAXCHA
      INTEGER               CHAIDX
      INTEGER               MAXLLN
      INTEGER               CHRLLN
      LOGICAL               LNVFLG

      DOUBLE PRECISION      FLTIME  ( * ) 
      CHARACTER*(*)         FLCMD   ( * )
      CHARACTER*(*)         FLPAR   ( * )
      CHARACTER*(*)         FLCHV   ( * )
      LOGICAL               FLCHF   ( * )
      DOUBLE PRECISION      FLDPV   ( * )
      LOGICAL               FLDPF   ( * )
      INTEGER               FLINV   ( * )
      LOGICAL               FLINF   ( * )
      CHARACTER*(*)         FLCHAR  ( * )
      INTEGER               FLCHAE  ( * ) 
      LOGICAL               FLCHAF  ( * ) 
      CHARACTER*(*)         FLFLG   ( * )
      CHARACTER*(*)         FLENTR  ( * )
      INTEGER               FLPRID  ( * )
      INTEGER               ENTSZS  ( * )
      
C$ Brief_I/O
C
C     Variable  I/O  Description
C     --------  ---  --------------------------------------------------
C     TIME       I   Time (ET, ephemeris seconds past J2000)
C     CMD        I   Command/subsystem name.
C     PARNAM     I   Parameter name.
C     PARIDX     I   Parameter index.
C     CHRVAL     I   Parameter value as character string
C     ENTRID     I   Entry ID string.
C     MAXREC     I   Maximum number of records in the buffer.
C     RECIDX    I/O  Previous/current record number
C     MAXCHA     I   Maximum number of value in character array buffer.
C     CHAIDX    I/O  Character array value previous/current index.
C     MAXLLN     I   Maximum length that fits into chr value buffer.
C     CHRLLN     I   Maximum length that fits into array buffer.
C     LNVFLG     I   Flag allowing long values.
C     FLTIME    I/O  Times buffer
C     FLCMD     I/O  Command/subsystem names buffer.
C     FLCHV     I/O  Character values buffer.
C     FLCHF     I/O  Character value NULL flags buffer.
C     FLPAR     I/O  Parameter/attribute names buffer.
C     FLDPV     I/O  DP values buffer.
C     FLDPF     I/O  DP values null flags buffer.
C     FLINV     I/O  INT values buffer.
C     FLINF     I/O  INT value NULL flags buffer.
C     FLCHAR    I/O  Character array values buffer.
C     FLCHAE    I/O  Character array value entry sizes buffer.
C     FLCHAF    I/O  Character array value NULL flags buffer.
C     FLFLG     I/O  Available value flags buffer.
C     FLENTR    I/O  Entry IDs buffer.
C     FLPRID    I/O  Parameter indexes buffer.
C     ENTSZS    I/O  Entity sizes buffer.
C
C$ Detailed_Input
C
C     Not described yet.
C
C$ Detailed_Output
C
C     Not described yet.
C
C$ Parameters
C
C     None.
C
C$ Exceptions
C
C     1)  If there is no space in main buffer then 
C         an error SPICE(BUFFEROVERRUN) is reported.
C
C     2)  If there is no space in character array value buffer then 
C         an error SPICE(BUFFEROVERRUN) is reported.
C
C     3)  If character value string is too long to fit into main
C         buffer and long value are nt allowed then 
C         an error SPICE(TOOSMALLSPACE) is reported.
C
C$ Files
C
C     Saves data for a single EK into the program buffers.
C
C$ Particulars
C
C     None.
C
C$ Examples
C
C     None. (See MPFPEF main program code)
C
C$ Restrictions
C
C     Not described yet.
C
C$ Literature_References
C
C     None.
C
C$ Author_and_Institution
C
C     B.V. Semenov   (JPL)
C
C$ Version
C
C-    Beta Version 1.0.0, 07-OCT-1996 (NVS)
C
C-&
 
C
C     SPICELIB functions
C
      LOGICAL               RETURN
      LOGICAL               BENUM
      LOGICAL               BEINT
      INTEGER               NBLEN

C
C     Local variables
C
      CHARACTER*(256)       ERRMSG
      INTEGER               PNTER

C
C     Standard SPICE error handling.
C
      IF ( RETURN () ) THEN
         RETURN
      ELSE
         CALL CHKIN ( 'SAVREC' )
      END IF
 
C
C     Check whether we can increment RECIDX     
C
      IF ( RECIDX + 1 .LE. MAXREC ) THEN

         RECIDX = RECIDX + 1

      ELSE

C
C        Signal about main buffer overrun.
C
         CALL DLTEEK 
         CALL SETMSG ( 'Main EK record buffer overrun.')
         CALL SIGERR ( 'SPICE(BUFFEROVERRUN)'      )
                  
      END IF
      
C
C     Store ET.
C
      FLTIME( RECIDX ) = TIME

C
C     Store entry name.
C
      FLCMD( RECIDX ) = CMD

C
C     Store parameter.
C
      FLPAR( RECIDX ) = PARNAM
                                                         
C
C     We left-justify this character value and 
C     try to parse it as DP or INT 
C     number. If we are successful, we save DP 
C     and character values and set value flag
C
      CALL LJUST( CHRVAL, CHRVAL )
      FLFLG( RECIDX ) = 'C'
                     
      IF ( BENUM( CHRVAL ) ) THEN
         CALL NPARSD(CHRVAL, FLDPV( RECIDX ), ERRMSG, PNTER)
         FLFLG( RECIDX ) = 'D'
      END IF
                     
      IF ( BEINT( CHRVAL ) ) THEN
         CALL NPARSI(CHRVAL, FLINV( RECIDX ), ERRMSG,PNTER)
         FLFLG( RECIDX ) = 'I'
      END IF
                     
C
C     Now we save character, DP, and integer
C     value and set correct NULL flags
C     depending on value available.
C
      FLCHV( RECIDX )  = CHRVAL
      FLCHF( RECIDX )  = .FALSE.
      
      IF ( FLFLG( RECIDX ) .NE. 'C' ) THEN
         FLDPF( RECIDX )  = .FALSE.
      ELSE
         FLDPF( RECIDX )  = .TRUE.
      END IF

      IF ( FLFLG( RECIDX ) .NE. 'I' ) THEN
         FLINF( RECIDX )  = .TRUE.
      ELSE
         FLINF( RECIDX )  = .FALSE.
      END IF
      
C
C     Check whether long values are allowed.
C
      IF ( LNVFLG ) THEN

C
C        Yes, they are allowed. So we check whether line is too long 
C        to get saved into scalar cell.
C
         IF ( NBLEN( CHRVAL ) .GT. MAXLLN ) THEN

C
C           Set value flag to identify array value and all NULL flags 
C           for all values to TRUE.
C
            FLFLG ( RECIDX ) = 'A'
            FLCHF ( RECIDX ) = .TRUE.
            FLDPF ( RECIDX ) = .TRUE.
            FLINF ( RECIDX ) = .TRUE.
            FLCHAF( RECIDX ) = .FALSE.

C
C           Now loop through the line, cut it into a pieces and store
C           in array column buffer.       
C
            FLCHAE( RECIDX ) = 0
         
            DO WHILE ( NBLEN( CHRVAL ) .NE. 0 )
         
C
C              Increase entry size for current record.
C
               FLCHAE( RECIDX ) = FLCHAE( RECIDX ) + 1

C
C              Check whether there is s space available in array value
C              buffer and increase buffer index if so.
C           
               IF ( CHAIDX + 1 .LE. MAXCHA ) THEN

                  CHAIDX = CHAIDX + 1

               ELSE
C
C                 Signal about array value buffer overrun.
C  
                  CALL DLTEEK
                  CALL SETMSG ( 'EK character array column buffer ' //
     .                          'overrun.'                           )
                  CALL SIGERR ( 'SPICE(BUFFEROVERRUN)'               )
                  
               END IF
            
C
C              Save CHRLLN or less characters piece in the current 
C              buffer element.
C
               IF ( NBLEN( CHRVAL ) .GT. CHRLLN ) THEN

                  FLCHAR ( CHAIDX ) = CHRVAL (:CHRLLN)
                  CHRVAL (:CHRLLN) = ' '
                  CALL LJUST( CHRVAL, CHRVAL )

               ELSE

                  FLCHAR ( CHAIDX ) = CHRVAL (:NBLEN( CHRVAL ))
                  CHRVAL (:NBLEN( CHRVAL )) = ' '
                  CALL LJUST( CHRVAL, CHRVAL )

               END IF
         
            END DO
         
         ELSE
      
C
C           Value is short to fit into scalar cell. Set NULL for 
C           array column to TRUE and entity size to 0.
C
            FLCHAF( RECIDX ) = .TRUE.
            FLCHAE( RECIDX ) = 0

         END IF
         
      ELSE
      
C
C        No, long values are not allowed.
C
         IF ( NBLEN( CHRVAL ) .GT. MAXLLN ) THEN

C
C           And the string is too long to fit into scalar cell space.
C           So we report an error in this case.
C
            CALL DLTEEK
            CALL SETMSG ( 'Character value is too long to fit into ' //
     .                    'character value column. Value is # '      )
            CALL ERRCH  ( '#', CHRVAL(:NBLEN( CHRVAL ))               )
            CALL SIGERR ( 'SPICE(TOOSMALLSPACE)'                      )
                  
         ELSE

C
C           Value is short to fit into scalar cell. Set NULL for 
C           array column to TRUE and entity size to 0.
C
            FLCHAF( RECIDX ) = .TRUE.
            FLCHAE( RECIDX ) = 0

         END IF

      END IF
      
C
C     Now we save entry size, entry ID and
C     index of current parameter.
C
      ENTSZS( RECIDX ) = 1

      FLENTR( RECIDX )  = ENTRID

      FLPRID( RECIDX ) = PARIDX
      
C
C     That's it. Check out.
C
      CALL CHKOUT ( 'SAVREC' )
      
      RETURN
      END
