      SUBROUTINE VARYMOD
C Get parameters to vary

      INCLUDE 'BINFIT.INC'
      CHARACTER*1 BLANK, PSTR(8)
      INCLUDE 'MODEL.INC'
      DATA BLANK /' '/
      DATA PSTR/'S','A','D','R','M','C','f','F'/

      NVPARTOT = 0
  100 WRITE (OUTC,1140)     ! Return here to change parm.s to vary
      CALL WRITEHD(OUTC,BLANK)
      WRITE (OUTC,1145)
      DO IC = 1, NCOMP
          DO IG = 1, NFILT
              NVPAR(IG) = 0
              CALL CWRITEMD(OUTC,' ',PARM,IC,IG)
              DO J = 1, 8
                  IVARY(J) = 0
                  VARY(IC,IG,J) = .FALSE.
              END DO
              READ (INC,'(8I1)') IVARY
C             WRITE(OUTC,'(1X,8(1X,I2))') (IVARY(I),I=1,8)
              DO J = 1, 8
                  IF (IC.EQ.1 .AND. IG.EQ.1) THEN
                      IF (IVARY(J).EQ.1 .OR. IVARY(J).EQ.2 .OR.
     1                    IVARY(J).EQ.3 .OR. IVARY(J).EQ.8) THEN
                          NVPAR(IG) = NVPAR(IG)+1
                          NVPARTOT  = NVPARTOT+1
                          VARY(IC,IG,IVARY(J)) = .TRUE.
CC                        LISTA(NVPARTOT) = (IVARY(J)-1)*NCOMP*NFILT
CC   1                                              + (IG-1)*NCOMP + IC
C                         WRITE(OUTC,'(A,I3,A,I3)')
C    1                        ' LISTA(',NVPARTOT,') = ',LISTA(NVPARTOT)
                      ELSE IF (IVARY(J).EQ.4 .OR. IVARY(J).EQ.5 .OR.
     1                    IVARY(J).EQ.6 .OR. IVARY(J).EQ.7) THEN
                          WRITE(OUTC,'(A,A,A)') ' Can''t vary ',
     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 (IVARY(J).EQ.4 .OR. IVARY(J).EQ.8) THEN
                          NVPAR(IG) = NVPAR(IG)+1
                          NVPARTOT  = NVPARTOT+1
                          VARY(IC,IG,IVARY(J)) = .TRUE.
CC                        LISTA(NVPARTOT) = (IVARY(J)-1)*NCOMP*NFILT
CC   1                                              + (IG-1)*NCOMP + IC
C                         WRITE(OUTC,'(A,I3,A,I3)')
C    1                        ' LISTA(',NVPARTOT,') = ',LISTA(NVPARTOT)
                      ELSE IF (IVARY(J).NE.4 .AND. IVARY(J).NE.8 .AND.
     1                    IVARY(J).NE.0) THEN
                          WRITE(OUTC,'(A,A,A)') ' Can''t vary ',
     1                        'SE, PA, DI, MD, CO, or FL for ',
     2                        'component 1, filter > 1.'
                      END IF
                  ELSE IF (IC.NE.1 .AND. IG.EQ.1) THEN
                      IF (IVARY(J).EQ.1 .OR. IVARY(J).EQ.2 .OR.
     1                    IVARY(J).EQ.3 .OR. IVARY(J).EQ.5) THEN
                          NVPAR(IG) = NVPAR(IG)+1
                          NVPARTOT  = NVPARTOT+1
                          VARY(IC,IG,IVARY(J)) = .TRUE.
CC                        LISTA(NVPARTOT) = (IVARY(J)-1)*NCOMP*NFILT
CC   1                                              + (IG-1)*NCOMP + IC
C                         WRITE(OUTC,'(A,I3,A,I3)')
C    1                        ' LISTA(',NVPARTOT,') = ',LISTA(NVPARTOT)
                      ELSE IF (IVARY(J).EQ.4 .OR. IVARY(J).EQ.6 .OR.
     1                    IVARY(J).EQ.7 .OR. IVARY(J).EQ.8) THEN
                          WRITE(OUTC,'(A,A,A)') ' Can''t vary ',
     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 (IVARY(J).EQ.4 .OR. IVARY(J).EQ.6) THEN
                          NVPAR(IG) = NVPAR(IG)+1
                          NVPARTOT  = NVPARTOT+1
                          VARY(IC,IG,IVARY(J)) = .TRUE.
CC                        LISTA(NVPARTOT) = (IVARY(J)-1)*NCOMP*NFILT
CC   1                                              + (IG-1)*NCOMP + IC
C                         WRITE(OUTC,'(A,I3,A,I3)')
C    1                        ' LISTA(',NVPARTOT,') = ',LISTA(NVPARTOT)
                      ELSE IF (IVARY(J).NE.4 .AND. IVARY(J).NE.6 .AND.
     1                    IVARY(J).NE.0) THEN
                          WRITE(OUTC,'(A,A,A)') ' Can''t vary ',
     1                        'SE, PA, DI, MD, FL, or FT for ',
     2                        'component > 1, filter = 1.'
                      END IF
                  END IF
              END DO
C             Adjust NVPAR(IG): Reduced chi-squared for individual
C             filters will be calculated as if Sep, P.A., Diameter
C             vary independently for each filter.
              IF (IG.NE.1 .AND .VARY(IC,1,1)) NVPAR(IG) = NVPAR(IG) + 1
              IF (IG.NE.1 .AND. VARY(IC,1,2)) NVPAR(IG) = NVPAR(IG) + 1
              IF (IG.GE.2 .AND. VARY(IC,1,3) .AND. (.NOT.VARY(IC,IG,4)))
     1        THEN
                  NVPAR(IG) = NVPAR(IG) + 1
              END IF
              IF (IG.GE.2 .AND. VARY(IC,1,5) .AND. (.NOT.VARY(IC,IG,6)))
     1        THEN
                  NVPAR(IG) = NVPAR(IG) + 1
              END IF
          END DO
      END DO
C Check that arrays VARY and VARY1D are indeed equivalent
D     DO I=1,NCOMP
D         DO J=1,NFILT
D             DO K=1,8
D                 L=(K-1)*MXMOD*MXFILT+(J-1)*MXMOD+I
D                 IF(VARY(I,J,K)) THEN
D                     IF(VARY1D(L)) THEN
D                         WRITE(OUTC,'(A,3(I1,A),I3,A)')
D    1                    ' VARY(',I,',',J,',',K,') true;  VARY1D(',
D    2                    L,') true' 
D                     ELSE
D                         WRITE(OUTC,'(A,3(I1,A),I3,A)')
D    1                    ' VARY(',I,',',J,',',K,') true;  VARY1D(',
D    2                    L,') false' 
D                     END IF
D                 ELSE
D                     IF(VARY1D(L)) THEN
D                         WRITE(OUTC,'(A,3(I1,A),I3,A)')
D    1                    ' VARY(',I,',',J,',',K,') false; VARY1D(',
D    2                    L,') true' 
D                     ELSE
D                         WRITE(OUTC,'(A,3(I1,A),I3,A)')
D    1                    ' VARY(',I,',',J,',',K,') false; VARY1D(',
D    2                    L,') false' 
D                     END IF
D                 END IF
D             END DO
D         END DO
D     END DO
C Fill LISTA, the list of parameters to vary
      J=0
      DO I=1,MXPARM
          IF(VARY1D(I)) THEN
              J=J+1
              LISTA(J)=I
D             WRITE(OUTC,'(A,I3,A,I3)') ' LISTA(',J,') = ',LISTA(J)
          END IF
      END DO
C Assign labels to the parameters to vary (uses "J" from the loop
C immediately above
      DO I=1,J
          K = 1 + ( (LISTA(I)-1)/(MXMOD*MXFILT) )   ! Parameter no.
          IG = MOD(LISTA(I),(MXMOD*MXFILT))
          IF(IG.EQ.0) IG = IG + MXMOD*MXFILT        
          IG = 1 + (IG-1) / MXMOD                   ! Filter no.
          IC = MOD(LISTA(I),MXMOD)
          IF(IC.EQ.0) IC = IC + MXMOD               ! Component no.
          WRITE(PARMNAME(LISTA(I)),'(A,I1,I1)') PSTR(K),IC,IG
D         WRITE(OUTC,'(A,A)') ' PARMNAME: ',PARMNAME(LISTA(I))
      END DO

      RETURN
 1140 FORMAT (//' Enter the numbers of the parameters to be varied:',
     + /' (e.g. 125 varies Separation, Position angle, and Mag. diff.)')
 1145 FORMAT ('             1         2       3     4        5      ',
     1       ' 6       (7)      8')
      END

