      SUBROUTINE CHNGMOD
C Changes to model

      INCLUDE 'BINFIT.INC'
      INCLUDE 'MODEL.INC'

      WRITE (OUTC,1100) DMAGMAX
      WRITE (OUTC,1051)
      DO IC = 1, NCOMP
          DO IG = 1, NFILT
              DO J = 1, 8
                  VALUES(J) = -1000.
              END DO
              CALL CWRITEMD(OUTC,' ',PARM,IC,IG)
              MODE = 2
              CALL KEYIN(NAMES,VALUES,8,ENDMRK,MODE,INC,OUTC)
              IF (IC.EQ.1 .AND. IG.EQ.1) THEN
                  IF (VALUES(1).NE.-1000.) THEN
                      SEP    (IC,IG) = VALUES(1)
                  END IF
                  IF (VALUES(2).NE.-1000.) THEN
                      POSANG (IC,IG) = VALUES(2)
                  END IF
                  IF (VALUES(3).NE.-1000.) THEN
                      DIAM   (IC,IG) = VALUES(3)
                  END IF
                  IF (VALUES(8).NE.-1000.) THEN
                      FLUXTOT(IC,IG) = VALUES(8)
                  END IF
                  IF (VALUES(4).NE.-1000. .OR. 
     1                VALUES(5).NE.-1000. .OR.
     2                VALUES(6).NE.-1000. .OR.
     3                VALUES(7).NE.-1000.) THEN
                      WRITE(OUTC,'(A,A,A)') ' Can''t change ',
     1                  'DR, MD, CO, or FL for ',
     2                  'component 1, filter 1.'
                  END IF
              ELSE IF (IC.EQ.1 .AND. IG.NE.1) THEN
                  IF (VALUES(4).NE.-1000.) THEN
                      DRATIO (IC,IG) = VALUES(4)
                  END IF
                  IF (VALUES(6).NE.-1000.) THEN
                      COLOR  (IC,IG) = VALUES(6)
                  END IF
                  IF (VALUES(8).NE.-1000.) THEN
                      FLUXTOT(IC,IG) = VALUES(8)
                  END IF
                  IF (VALUES(1).NE.-1000. .OR. 
     1                VALUES(2).NE.-1000. .OR.
     2                VALUES(3).NE.-1000.) THEN
                      WRITE(OUTC,'(A,A,A)') ' Can''t change ',
     1                  'SE, PA, DI, MD, or FL for ',
     2                  'component 1, filter > 1.'
                  END IF
                  IF (VALUES(5).NE.-1000. .OR.
     2                VALUES(7).NE.-1000.) THEN
                      WRITE(OUTC,'(A,A,A)') ' Can''t change ',
     1                  'SE, PA, DI, MD, or FL for ',
     2                  'component 1, filter > 1.'
                  END IF
              ELSE IF (IC.NE.1 .AND. IG.EQ.1) THEN
                  IF (VALUES(1).NE.-1000.) THEN
                      SEP    (IC,IG) = VALUES(1)
                  END IF
                  IF (VALUES(2).NE.-1000.) THEN
                      POSANG (IC,IG) = VALUES(2)
                  END IF
                  IF (VALUES(3).NE.-1000.) THEN
                      DIAM   (IC,IG) = VALUES(3)
                  END IF
                  IF (VALUES(5).NE.-1000.) THEN
                      DELMAG (IC,IG) = VALUES(5)
                  END IF
                  IF (VALUES(4).NE.-1000. .OR.
     1                VALUES(6).NE.-1000. .OR.
     2                VALUES(7).NE.-1000. .OR.
     3                VALUES(8).NE.-1000.) THEN
                      WRITE(OUTC,'(A,A,A)') ' Can''t change ',
     1                  'DR, CO, FL, or FT for ',
     2                  'component > 1, filter = 1.'
                  END IF
              ELSE IF (IC.NE.1 .AND. IG.NE.1) THEN
                  IF (VALUES(4).NE.-1000.) THEN
                      DRATIO (IC,IG) = VALUES(4)
                  END IF
                  IF (VALUES(6).NE.-1000.) THEN
                      COLOR  (IC,IG) = VALUES(6)
                  END IF
                  IF (VALUES(1).NE.-1000. .OR.
     1                VALUES(2).NE.-1000. .OR.
     2                VALUES(3).NE.-1000.) THEN
                      WRITE(OUTC,'(A,A,A)') ' Can''t change ',
     1                  'SE, PA, DI, MD, FL, or FT for ',
     2                  'component > 1, filter = 1.'
                  END IF
                  IF (VALUES(5).NE.-1000. .OR.
     1                VALUES(7).NE.-1000. .OR.
     2                VALUES(8).NE.-1000.) THEN
                      WRITE(OUTC,'(A,A,A)') ' Can''t change ',
     1                  'SE, PA, DI, MD, FL, or FT for ',
     2                  'component > 1, filter = 1.'
                  END IF
              END IF
D             CALL CWRITEMD(OUTC,' ',PARM,IC,IG)
          END DO
      END DO

C             Remove components with DELMAG > DMAGMAX from model.
C             First fix things up if the first component is to be
C             deleted; make the next brightest component # 1:

      TOOFAINT = .FALSE.
      DO IG = 1, NFILT
          IF(DELMAG(1,IG).GE.DMAGMAX) TOOFAINT = .TRUE.
      END DO
      IF (TOOFAINT) THEN
          DDMAG = DMAGMAX      ! Find next brightest component
          DO IC = 1, NCOMP
              DO IG = 1, NFILT
                  IF(DELMAG(IC,IG) .LT. DDMAG) THEN
                      DDMAG = DELMAG(IC,IG)
                      IDMAGMIN = IC
                  END IF
              END DO
          END DO
          DO IG = 1, NFILT
              DDDMAG(IG) = DELMAG(IDMAGMIN,IG)
              DELMAG(IDMAGMIN,IG) = DMAGMAX
              SEP   (1,IG) = SEP   (IDMAGMIN,IG)
              POSANG(1,IG) = POSANG(IDMAGMIN,IG)
              DIAM  (1,IG) = DIAM  (IDMAGMIN,IG)
              DRATIO(1,IG) = DRATIO(IDMAGMIN,IG)
              DELMAG(1,IG) = 0.0
              COLOR (1,IG) = COLOR (IDMAGMIN,IG)
              FLUX  (1,IG) = FLUX  (IDMAGMIN,IG)  ! FLUXTOT(1,IG) is kept
          END DO
          DO IC = 2, NCOMP
              DO IG = 1, NFILT
                  DELMAG(IC,IG) = DELMAG(IC,IG) - DDDMAG(IG)
              END DO
          END DO
          TOOFAINT = .FALSE.
      END IF
C             Continue for the rest of the components
   81 JSTOP = NCOMP-1  ! Return here if a component is deleted.
      DO IC = 1, JSTOP
          DO IG = 1, NFILT
              IF (DELMAG(IC,IG) .GE. DMAGMAX) THEN
                  DO K = IC, NCOMP-1
                      DO L = 1, NFILT
                          SEP    (K,L) = SEP    (K+1,L)
                          POSANG (K,L) = POSANG (K+1,L)
                          DIAM   (K,L) = DIAM   (K+1,L)
                          DRATIO (K,L) = DRATIO (K+1,L)
                          DELMAG (K,L) = DELMAG (K+1,L)
                          COLOR  (K,L) = COLOR  (K+1,L)
                          FLUX   (K,L) = FLUX   (K+1,L)
                          FLUXTOT(K,L) = FLUXTOT(K+1,L)
                      END DO
                  END DO
                  NCOMP = NCOMP-1
                  GO TO 81     ! Leave DO's, IF, and start over.
              END IF
          END DO
      END DO
      DO IG = 1, NFILT
          IF (DELMAG(NCOMP,IG) .GE. 10.) THEN
              NCOMP = NCOMP-1                      ! Takes care of
              GO TO 82                             ! last one.
          END IF           
      END DO
   82 CONTINUE
      RETURN
 1051 FORMAT(' # Lambda   SE        PA      DI    DR       MD      ',
     1       'CO      (FL)     FT')
 1100 FORMAT (//' Enter free format changes, one component at a time',
     +          ' (e.g. "PA=33.1  DM=1.13").'
     +          /' To delete a component, change DMag to ',F5.2,'.'
     +          /' To go to next component, hit RETURN key.')
      END
