         SUBROUTINE AUGSQR(NARGS,SYSFIL,NEWFIL,SEEDDM,ERROR,ERRMSG)
C
C       FUNCTION:
Cf
Cf        This routine augments the input-output structure of
Cf        a linear system to make it square, i.e., the system
Cf        output from this program has the matrices B and D
Cf        or C and D transformed into square matrices. If the
Cf        number of outputs is greater than the number of inputs
Cf        then matrices B and D are augmented. If the number of
Cf        outputs is less than the number of inputs then the
Cf        matrices C and D are augmented. If the number of inputs
Cf        equals the number of outputs, nothing is done.
Cf        The matrices are augmented by introducing rows or columns
Cf        of random numbers for B or C, and rows or columns of
Cf        zeroes for D.
C
C         BUGS:
Cb          Currently there is nothing to prevent the introduction
Cb          of non-minimum phase zeroes into the system.
C
C         USAGE:
Cu          This routine is used to square a system for application
Cu          of the Loop Transfer Recovery part of the design.
Cu
C
C         INPUTS:
Ci          The inputs to the routine are the file name of a
Ci          system container file for the input system and
Ci          the file name of a system container file for
Ci          the output. The input system container file must conform
Ci          to the format for system container files (See the
Ci          Tool Specifications). Also the value of the seed for
Ci          the random number generator must be supplied.
C
C         OUTPUTS:
Co          The output of the program is the augmented system, now
Co          square, which is stored in the file determined by the
Co          variable NEWFIL. This file will conform the the system
Co          container file format. Also output is the integer CERR
Co          which is non-zero if an error occurs in the routine.
C
C         MACHINE DEPENDENCIES:
Cm
Cm           'INCLUDE' statement is machine dependent. There
Cm           are no other dependencies.
Cm
C
C          HISTORY:
Ch           Orginally written by Bobby Bodenheimer July 1986.
CH				added dpcom: 7/16/88 jdb
C
C          ROUTINES CALLED:
Cc           INSYS, OUTSYS, RAND.
C          COMMON MEMORY USED:
CM
CM      DPCOM -- see dpcommon.f and dpcom.f
CM
C
C--------------------------------------------------------------------
C          written for:	The CASCADE Project
C			Oak Ridge National Laboratory
C			U.S. Department of Energy
C			contract number DE-AC05-840R21400
C			subcontract number ##########
C			organization:	xxxxxx
C----------------------------------------------------------------------
C	THIS SOFTWARE IS IN THE PUBLIC DOMAIN
C	NO RESTRICTIONS ON ITS USE ARE IMPLIED
C----------------------------------------------------------------------
C
        INCLUDE          'Parameter.f'
C
        INTEGER SEEDDM
        INTEGER SEED
C
        INTEGER NINPS, NOUTS, NSTATS, ERROR
C
        DOUBLE PRECISION A(SIZE,SIZE)
        DOUBLE PRECISION B(SIZE,SIZE)
        DOUBLE PRECISION C(SIZE,SIZE)
        DOUBLE PRECISION D(SIZE,SIZE)
C
        DOUBLE PRECISION RND
	DOUBLE PRECISION BNORM
	DOUBLE PRECISION BNORM2
	DOUBLE PRECISION CNORM
	DOUBLE PRECISION CNORM2
	DOUBLE PRECISION D1NRM
C
        CHARACTER*(*) SYSFIL, NEWFIL, ERRMSG
C
	INCLUDE			'dpcom.f'
C
C If SEED equals 0 then set seed to 99991 for reproducible
C results.
C
        IF (NARGS.GE.4) THEN
           IF (SEEDDM.LE.0) THEN
              SEED = 99991
           ELSE
              SEED = SEEDDM
           END IF
        ELSE
           SEED = 99991
        END IF
C
C Check that SEED is odd. If it isn't add one to it.
C
        IF (MOD(SEED,2) .NE. 1) THEN
          SEED=SEED+1
        END IF
C
        ERROR = 0
C
C Read in the System file.
C
        CALL INSYS(SYSFIL,
     1        NINPS,NOUTS,NSTATS,
     2        SIZE,SIZE,SIZE,A,B,C,D,ERROR)
C
        CLOSE(UNIT=UNIT1)
C
        IF (ERROR.NE.0) THEN
           ERRMSG = ' AUGSQR: Error from INSYS reading '//SYSFIL
           RETURN
        END IF
C
C  Do the augmentation.
C
        IF (NINPS.EQ.NOUTS) THEN
C
C     No augmentation necessary.
C
           ERROR=1
           ERRMSG = 'AUGSQR: Number of inputs = Number of outputs.'//
     1             ' No augmentation necessary.'
           RETURN
        ELSE IF (NINPS.LT.NOUTS) THEN
C
C     Augment B
C
             BNORM = D1NRM(SIZE,NSTATS,NINPS,B)
	     IF (BNORM .EQ. 0.0D0) BNORM = 1.0D0
	     BNORM2 = 2.0D0 * BNORM
             DO 10 I=1,NOUTS-NINPS
                DO 10 J=1,NSTATS
                   CALL RAND(SEED,SEED,RND)
                   B(J,NINPS+I) = BNORM2 * RND - BNORM
                   D(J,NINPS+I) = 0.0D0
 10          CONTINUE
             CALL OUTSYS(NEWFIL,
     1       NOUTS,NOUTS,NSTATS,SIZE,SIZE,SIZE,A,B,C,D,ERROR)
        ELSE
C
C    Augment C
C
             CNORM = D1NRM(SIZE,NOUTS,NSTATS,C)
	     IF (CNORM .EQ. 0.0D0) CNORM = 1.0D0
	     CNORM2 = 2.0D0 * CNORM
             DO 20 I=1,NINPS-NOUTS
                DO 20 J=1,NSTATS
                   CALL RAND(SEED,SEED,RND)
                   C(NOUTS+I,J) = CNORM2 * RND - CNORM
                   D(NOUTS+I,J) = 0.0D0
 20          CONTINUE
             CALL OUTSYS(NEWFIL,
     1       NINPS,NINPS,NSTATS,SIZE,SIZE,SIZE,A,B,C,D,ERROR)
        END IF
C
        IF (ERROR.NE.0) THEN
           ERRMSG = 'AUGSQR: Error from OUTSYS writing  '//NEWFIL
        END IF
        RETURN
        END