      SUBROUTINE MINMAX
      INCLUDE 'SIZES'
**************************************************
*  MINIMUM AND MAXIMUM VALUES OF X-Y COORDINATES STORED IN ARRAY CO
*  ARE CALCULATED.
*    ATOMS AND ATOM TYPES NOT DISPLAYED ARE NOT CONSIDERED
**************************************************
      IMPLICIT REAL (A-H, O-Z)
      REAL ATMRAD
      INTEGER*2 ATBOND
      CHARACTER*80 DUMMY
      LOGICAL FIRST
      LOGICAL DEBUG, DEBUGL, DEBUGO, DEBUGN, DEBUGP, DEBUGI
      COMMON /DEBCOM/ DEBUG, DEBUGL, DEBUGN, DEBUGO, DEBUGP, DEBUGI
      COMMON /PLOTS/ XMIN,XMAX,YMIN,YMAX,ZMIN,ZMAX,CMIN,CMAX,SCALE
      COMMON /ATOMS/ CO(3, NUMATM),IE( NUMATM),NATOMS, ATCHG( NUMATM)
      COMMON /DISPLY/ IREM(200), BSCALE, ATBOND( NUMATM, NUMATM), 
     .                ISTYPE, LATYPE, IMASK( NUMATM), ISCOLO
      COMMON /ATMRAD/ ATMRAD(200)
      DATA FIRST /.TRUE./

      IF( FIRST ) THEN
        FIRST = .FALSE.
        XMIN = -100.0
        XMAX = 100.0
      ENDIF
      IF ( DEBUG ) THEN
         WRITE ( DUMMY, '('' IN MINMAX: NATOMS='',I4)') NATOMS
         CALL DEBUGR( DUMMY )
         WRITE ( DUMMY, '(''MINMAX:  X:'',E10.4,'','',E10.4,
     .        '' Y:'',E10.4,'','',E10.4,'' SCALE='',E10.4)')
     .        XMIN, XMAX, YMIN, YMAX, SCALE
         CALL DEBUGR( DUMMY( :78) )
      ENDIF
* IF WE WISH TO KEEP THE CURRENT SCALE SIZE FOR THE NEXT PICTURE
*   WE WILL NEGATE THE MIN AND MAX
      IF( (XMAX-XMIN .LT. -0.10) .AND. YMAX-YMIN .GT. 0.1) RETURN
*
      XMIN = 100.0
      XMAX = -100.0
      YMIN = 100.0
      YMAX = -100.0
      ZMIN = 100.0
      ZMAX = -100.0
      MINXAT = 1
      MAXXAT = 1
      MINYAT = 1
      MAXYAT = 1

      DO 10 IA= 1, NATOMS
         IF ( IREM(IE(IA)) .NE. 0 ) GO TO 10
         IF ( IMASK(IA) .NE. 0 ) GO TO 10
         IF (CO(1,IA).LT.XMIN)  THEN
            XMIN=CO(1,IA)
            MINXAT = IA
         ENDIF
         IF(CO(1,IA).GT.XMAX)  THEN
            XMAX=CO(1,IA)
            MAXXAT = IA
         ENDIF
         IF (CO(2,IA).LT.YMIN)  THEN
            YMIN=CO(2,IA)
            MINYAT = IA
         ENDIF
         IF (CO(2,IA).GT.YMAX)  THEN
            YMAX=CO(2,IA)
            MAXYAT = IA
         ENDIF
         IF (CO(3,IA).LT.ZMIN) THEN
            ZMIN=CO(3,IA)
         ENDIF
         IF (CO(3,IA).GT.ZMAX) THEN
            ZMAX=CO(3,IA)
         ENDIF
   10 CONTINUE
      CMIN=XMIN
      IF(XMIN.GT.YMIN) CMIN=YMIN
      SCALE=XMAX-XMIN
      IF(YMAX-YMIN.GT.SCALE) SCALE=YMAX-YMIN
      IF ( NATOMS .GT. 0) THEN
         XMIN = XMIN - MAX( ATMRAD( IE( MINXAT) ), 1.0)
         XMAX = XMAX + MAX( ATMRAD( IE( MAXXAT) ), 1.0)
         YMIN = YMIN - MAX( ATMRAD( IE( MINYAT) ), 1.0)
         YMAX = YMAX + MAX( ATMRAD( IE( MAXYAT) ), 1.0)
      ENDIF
      CMIN=XMIN
      IF(XMIN.GT.YMIN) CMIN=YMIN
      SCALE=XMAX-XMIN
      IF(YMAX-YMIN.GT.SCALE) SCALE=YMAX-YMIN
      SCALE=SCALE+1.4
      IF(SCALE.LT.4.4) SCALE=4.4
      IF ( XMIN.EQ. XMAX) THEN
         XMIN=XMIN-SCALE/2
         XMAX=XMAX+SCALE/2
      ENDIF
      IF ( YMIN.EQ. YMAX) THEN
         YMIN=YMIN-SCALE/2
         YMAX=YMAX+SCALE/2
      ENDIF
      IF ( ZMIN.EQ. ZMAX) THEN
         ZMIN=ZMIN-SCALE/2
         ZMAX=ZMAX+SCALE/2
      ENDIF
      IF ( DEBUG ) THEN
         WRITE ( DUMMY, '(''MINMAX:  X:'',E10.4,'','',E10.4,
     .        '' Y:'',E10.4,'','',E10.4,'' SCALE='',E10.4)')
     .        XMIN, XMAX, YMIN, YMAX, SCALE
         CALL DEBUGR( DUMMY( :78) )
         WRITE ( DUMMY, '(''MINMAX: MIN X-ATOM:'',I4,'' MAX X-ATOM:'',
     .        I4,'' MIN Y-ATOM:'',I4,''MAX Y-ATOM:'',I4)')
     .        MINXAT, MAXXAT, MINYAT, MAXYAT
         CALL DEBUGR( DUMMY( :78) )
      ENDIF
      RETURN
      END
