      SUBROUTINE ADDFLD(RET,TAG,DATA,LENGTH)                            
C                                                                       
C     THIS SUBROUTINE ADDS THE VALUES OF A TAG AND DATA TO THE RECORD   
C     BEING FORMATTED AND CREATES A CORRESPONDING DIRECTORY ENTRY FOR   
C     SUCH PAIR.                                                        
C                                                                       
C     PARAMETERS ARE AS FOLLOWS:                                        
C     RET     =RETURN FLAG, VALUES ARE:                                 
C               0 - NO ERRORS.                                          
C              -1 - NEGATIVE OR ZERO CHARACTER COUNT.                   
C              -2 - PROGRAM CAPACITY EXCEEDED.                          
C     TAG     =VALUE OF TAG, TL LEFT JUSTIFIED CHARACTERS (SSC)         
C     DATA    =ARRAY CONTAINING VALUE STRING, LEFT JUSTIFIED CHARACTERS 
C              (CSC)                                                    
C     LENGTH  =LENGTH OF VALUE STRING IN CHARACTERS, INTEGER (FCC)      
C                                                                       
C     BRIEF PROCEEDURE DESCRIPTION:                                     
C     THE RECORD IS ASSEMBLED IN THREE AREAS:                           
C     1. TAG VALUES ARE ASSEMBLED IN ARRAY ITG, ONE PER REAL*8 WORD     
C        AS SUPPLIED BY THE USER.  COUNT OF NUMBER OF TAGS IS KEPT IN   
C        LOCATION NTG.                                                  
C     2. ARRAY DIR CONTAINS CORRESPONDING LENGTH OF DATA FIELD IN       
C        CHARACTERS (STORED IN LOCATIONS 1-5,N) AND STARTING CHARACTER  
C        POSITION OF DATA IN DATA ARRAY (STORED IN LOCATIONS 6-10,N),   
C        IN DISPLAY CODE.                                               
C     3. DATA VALUE IS CONVERTED TO ASCII CODE, FIELD TERMINATOR IS ADDE
C        AND IMAGE IS INCORPORATED INTO ARRAY A1.                       
C        IF ON ENTRY THE CHARACTER COUNT IS ZERO NO ACTION IS TAKEN.    
C        RETURN TO USER WITH NO ERROR INDICATION.                       
C                                                                       
C     CURRENT LIMITS ARE AS FOLLOWS.                                    
C     MAXIMUM OF 50 TAGS PER RECORD.                                    
C     MAXIMUM OF 2046  CHARACTERS OF DATA/RECORD.                       
C     IF EITHER OF THE TWO LIMITS IS EXCEEDED THE JOB IS ABORTED WITH   
C     AN ERROR MESSAGE.                                                 
C                                                                       
      INTEGER RET                                                       
      INCLUDE 'SY:DDFCOM.FTN'                                              
      INCLUDE 'SY:FLGCM.FTN'
      REAL*8 TAG
      LOGICAL*1 DIR2(10,50)
      EQUIVALENCE (DIR(1),DIR2(1,1))
C     CHECK IF ZERO OR NEGATIVE DATA LENGTH                             
      RET=0                                                             
      IF (LENGTH.GT.0) GOTO 500                                         
      RET=-1                                                            
      RETURN                                                            
C     CHECK IF ENTRY FULL                                               
  500 CONTINUE
      IF(DEBUG) TYPE 1000,NCHA,MAXDT
      IF(NCHA.GT.MAXDT) GO TO 8
C     PUT TAG INTO TAG ARRAY                                            
      NTG=NTG+1                                                         
       IF(DEBUG) TYPE 1001,NTG,MAXTG
      IF(NTG .GT. MAXTG)GOTO 8                                          
      ITG(NTG)=TAG                                                      
      IF(DEBUG) TYPE 1003,TAG
 1003 FORMAT(' TAG = ',A8)
C                                                                       
C     STARTING CHARACTER POSITION                                       
C                                                                       
      NCHN=NCHA-1
      IF(NTG.NE.1) NCHN=NCHN+LFT                                        
C                                                                       
C     LENGTH                                                            
C     DECODE(2,2000,FCC) FCCINT
 2000 FORMAT(I2)
      N=LENGTH+LFT                                                         
      IF(N.GT.NCHM)NCHM=N                                               
      ENCODE(10,501,DIR2(1,NTG))N,NCHN                                   
      CALL LEADZE(DIR2(1,NTG),10)                                        
  501 FORMAT(2I5)                                                       
      IF(DEBUG) TYPE 502, (DIR2(NX,NTG),NX=1,10)
  502 FORMAT(' DIR2 = ',10A1)
 1000 FORMAT('  NCHA,MAXDT',2I10)
 1001 FORMAT(' NTG,MAXTG',2I10)
C                                                                       
C     MOVE DATA VALUE TO DATA ARRAY                                     
C     ADD TERMINATOR CHARACTER TO PREVIOUS DATA IF ANY                  
C                                                                       
      IF(NTG.EQ.1) GO TO 504                                            
      CALL STRMOV(FT,A1(NCHA),LFT)                                      
      NCHA=NCHA+LFT
  504 CONTINUE                                                          
C                                                                       
C     IS NEW CHARACTER ADDRESS GREATER THAN THE DIMENSION OF A1>        
      IF(DEBUG) TYPE 1000,NCHA,MAXDT
C                                                                       
      IF(NCHA.GT.MAXDT) GO TO 8                                         
      CALL STRMOV(DATA,A1(NCHA),LENGTH)                                     
      NCHA=NCHA+LENGTH
      RETURN                                                            
C                                                                       
C     PROGRAM CAPACITY EXCEEDED                                         
C                                                                       
    8 TYPE 100                                                         
  100 FORMAT(//1X,'-----PROGRAM CAPACITY EXCEEDED (TOO MANY TAGS OR DATA
     $ TOO LONG). JOB ABORTED.-----')                                    
      RET=-2                                                            
      RETURN                                                            
      END                                                               
