
CD-Section: Write 'KDM' (&'KSM') Data
C A routine is available to write out, with various options, the Keyword 
C Data Module (KDM) keyworded parameter data. An alternative routine will
C allow the symmetry (KSM) data to be written in addition to the KDM data.
CD-end

C LAST UPDATE: 
C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C
C****************************
C**     KWD_WRITEKDM       **
C****************************
C
CD-Routine: Output a KDM data set - KWD_WRITEKDM
C This routine is used to output a KDM keyword parameter data set. A number
C of options are available to control the amount and style of the output
C and there is also an option to output only changed KDM parameter values.
CD-end
CD-Fortran:
      SUBROUTINE KWD_WRITEKDM (KDX, IUNOUT, IOPTYP, ICHANGED, IERR)
CD-end
      INCLUDE 'implicit_none.finc'
C
C Purpose: Write KDM parameters file data
C
C Author:  John W. Campbell, July 1995
C
C Arguments:
C
      INTEGER KDX
      INTEGER IUNOUT
      INTEGER IOPTYP
      INTEGER ICHANGED
      INTEGER IERR

CD-Parameters:
C KDX       i (R)   Index to required KDM dataset
C IUNOUT    i (R)   Unit number for output (if <0 then terminal output is 
C                   required on unit -IUNOUT)
C IOPTYP    i (R)   Type of output = 1 minimum, do not o/p parameters with 
C                                      default values
C                                  = 2 standard output, do not o/p parameters
C                                      with default values except for those
C                                      which have been flagged to be printed
C                                      as standard.
C                                  = 3 full (all parameters will be included)
C                                      but with reduced output where feasible
C                                      by recombining values if all the same 
C                                      for a set etc.
C                                  = 4 all pamaeters and individual values 
C                                      given explicitly for all set/subset 
C                                      parameters.
C ICHANGED  i (R)   =0 Output all parameters
C                   1-16 Write changed parameter values since last reset of 
C                        flag for channel ICHANGED
C IERR      i (W)   Error flag  = 0  OK
C                               = 1  Invalid KDM index or invalid parameter
CD-end
C
C====== Parameter statements
C 

C
C====== GLOBALS:
C
      INCLUDE 'kwd_outstring.finc'
      EXTERNAL KWD_OPFUN
C
C====== FUNCTION TYPES:
C
      INTEGER KDMSTR
      INTEGER LENSTR
C
C====== LOCALS:
C
      INTEGER IP
      INTEGER NUMP
      INTEGER KERR
      INTEGER IREDUCE
      INTEGER NSS(2)
      CHARACTER*50 KEYWORD
      CHARACTER*2 ERRSTR

C     INTEGER IP           !Parameters loop counter
C     INTEGER NUMP         !No. of parameters in KDM data set
C     INTEGER KERR         !Error flag
C     INTEGER IREDUCE      !Flag for reduced output
C     INTEGER NSS(2)       !Dummy array of nos. of subsets/set
C     CHARACTER*50 KEYWORD !Keyword name
C     CHARACTER*2 ERRSTR   !Error string (dummy)
C
C-------------------------------------------------------------------------------
C
C====== Initialisations
C
      IERR = 0
      KUNIT = IUNOUT
      IF (ICHANGED.LT.0.OR.ICHANGED.GT.16) THEN
         IERR = 1
         RETURN
      END IF
      IF (IOPTYP.LT.1.OR.IOPTYP.GT.4) THEN
         IERR = 1
         RETURN
      END IF
      IREDUCE = 0
      IF (IOPTYP.EQ.3) IREDUCE = 1
      IF (IOPTYP.EQ.2) IREDUCE = 2
      IF (IOPTYP.EQ.1) IREDUCE = 3

      CALL KDMF_NUMPARS (KDX, NUMP, IERR)
      IF (IERR.NE.0) RETURN

      IF (NUMP.LE.0) RETURN

      DO 100 IP = 1, NUMP
         CALL KDMF_PARNAME (KDX, IP, KDMSTR(KEYWORD), 50, KERR)
         IF (KERR.NE.0) GO TO 100
         CALL KDMF_OUTPUT (KDX, KDMSTR(KEYWORD), LENSTR(KEYWORD),
     +                     ICHANGED, IREDUCE, -1, NSS, 
     +                     KDMSTR(OUTSTR), 2050,
     +                     KWD_OPFUN, KDMSTR(ERRSTR), 2, KERR)
 100  CONTINUE
      RETURN
      END
