C-----------------------------------------------------------------------
C  UFSCAD  ADD/DELETE/MODIFY SCALARS IN A UFILES FILE
C
      SUBROUTINE UFSCAD(ILUN,ISHOT,NSCMAX,NSC,SCVAL,SCLAB,
     >    IORDER,ILUNC)
C
      LOGICAL, EXTERNAL :: LCHAR,LXOUT,LYES,LYESC
C
      REAL SCVAL(NSCMAX)    ! SCALAR VALUES
      INTEGER IORDER(NSCMAX)   ! ORDERING WORKSPACE FOR UFSCLK
      CHARACTER*10 SCLAB(3,NSCMAX)  ! SCALAR LABELS
C
      CHARACTER*10 ZKEY,ZLBL,ZUNS
C
C  SEE SUBROUTINE UFSCLK -- WHERE MOST OF THESE ARGUMENTS ARE USED
C  AND EXPLAINED
C
C  NSCMAX = MAX NO. OF SCALARS
C  NSC = CURRENT ACTUAL NO. OF SCALARS
C
C  MENU
C
 10   CONTINUE
      CALL NEUPAG
 11   CONTINUE
      IF(LXOUT(0)) WRITE(LNOUT(0),1001) NSC
 1001 FORMAT(/' UFSCAD OPTIONS:'/
     >  ' A -- ADD A SCALAR (VALUE, LABEL WILL BE REQUESTED)'/
     >  ' D -- DELETE A SCALAR   [CURRENT NO. OF SCALARS = ',I3,' ]'/
     >  ' M -- DISPLAY AND/OR MODIFY VALUES OF SCALARS'/
     >  ' Q -- QUIT (EXIT THIS ROUTINE)'/)
C
      CALL UREAD('UFSCAD:  ENTER OPTION CODE:$$')
C
      IF(LCHAR('A')) THEN
C  ADD A SCALAR
C
        IF(NSC.EQ.NSCMAX) THEN
          CALL UERMSI('UFSCAD:  SCALAR LIST IS FULL',NSCMAX,0)
          GO TO 11
        ENDIF
C
        CALL UFGKEY(ZKEY)
        ISC=0
        IF(NSC.GT.0) ISC=IUFKEY(ZKEY,SCLAB,NSC)
        IF(ISC.GT.0) THEN
          CALL UERMSG('UFSCAD:  SCALAR ALREADY EXISTS',ZKEY,0)
          GO TO 11
        ENDIF
C
        ZVAL=0.0
        CALL UREADF('UFSCAD:  ENTER VALUE OF NEW SCALAR:$$',ZVAL)
        ZLBL='DUMMY'
        CALL UREADL('UFSCAD:  ENTER 10 CHAR LABEL FOR SCALAR:$$',ZLBL)
        ZUNS='NONE'
        CALL UREADW('UFSCAD:  ENTER 10 CHAR UNITS FOR SCALAR:$$',ZUNS)
C
        NSC=NSC+1
        SCVAL(NSC)=ZVAL
        SCLAB(1,NSC)=ZKEY
        SCLAB(2,NSC)=ZLBL
        SCLAB(3,NSC)=ZUNS
C
        WRITE(ILUNC,7000) SCLAB(1,NSC),ZVAL
 7000   FORMAT(1X,'*** SCALAR "',A10,'" = ',1PE11.4,' ADDED TO LIST')
C
      ELSE IF(LCHAR('D')) THEN
C  DELETE A SCALAR
C
        IF(NSC.GT.0) THEN
          CALL UFGKEY(ZKEY)
          ISC=IUFKEY(ZKEY,SCLAB,NSC)
          IF(ISC.EQ.0) THEN
            CALL UERMSG('UFSCAD:  NO SUCH SCALAR',ZKEY,0)
            GO TO 11
          ELSE
C
            WRITE(ILUNC,7001) SCLAB(1,ISC)
 7001       FORMAT(1X,'*** SCALAR "',A10,'" DELETED FROM LIST')
            DO 50 I=ISC+1,NSC
              IM1=I-1
      	SCVAL(IM1)=SCVAL(I)
      	SCLAB(1,IM1)=SCLAB(1,I)
      	SCLAB(2,IM1)=SCLAB(2,I)
      	SCLAB(3,IM1)=SCLAB(3,I)
 50         CONTINUE
            NSC=NSC-1
          ENDIF
        ELSE
          CALL UERMSG('UFSCAD:  THE SCALAR LIST IS EMPTY',' ',0)
          GO TO 11
        ENDIF
C
      ELSE IF(LCHAR('M')) THEN
C  DISPLAY/MODIFY SCALARS
C
        IF(NSC.GT.0) THEN
          CALL UFSCLK(ILUN,ISHOT,NSC,SCVAL,SCLAB,IORDER,1,ILUNC)
        ELSE
          CALL UERMSG('UFSCAD:  THE SCALAR LIST IS EMPTY',' ',0)
          GO TO 11
        ENDIF
C
      ELSE IF(LCHAR('Q')) THEN
C  EXIT
        RETURN
C
      ELSE
        CALL UERMSG('UFSCAD:  ILLEGAL OPCODE',' ',0)
        GO TO 11
      ENDIF
C
      GO TO 10
C
      END
