      SUBROUTINE GRIDCHI
C DOES A GRID SEARCH AROUND A GIVEN POINT OF PARAMETER SPACE, AND
C SAVES THE ELEMENTS CORRESPONDING TO THE MINIMUM
      IMPLICIT UNDEFINED (A-Z)
      INCLUDE 'PARAMETR.INC'
      INCLUDE 'ELLIPSE.INC'

      CHARACTER*1  CHOICE
      CHARACTER*80 INS

      INTEGER*4    I, J, K, L, M, N, P, Q, R, NDATA, NSTEPS(7)
      INTEGER*4    MINI, MINJ, MINK, MINL, MINM, MINN, MINP 

      REAL*8     RMOD, THMOD, RAMOD, DECMOD, NCHISQ
      REAL*8     A(7), DELTA_A(7), ACHECK(7), DYDA(7,2)

      CHARACTER*35 QUES(7)
      CHARACTER*49 BLANKOUT

      LOGICAL*1    NEWMIN

      DATA QUES(1)/'SEMIMAJOR AXIS:      NSTEPS, size: '/      ! 35 characters
      DATA QUES(2)/'ELLIPTICITY:         NSTEPS, size: '/      ! 35 characters
      DATA QUES(3)/'PERIOD:              NSTEPS, size: '/      ! 35 characters
      DATA QUES(4)/'EPOCH:               NSTEPS, size: '/      ! 35 characters
      DATA QUES(5)/'INCLINATION:         NSTEPS, size: '/      ! 35 characters
      DATA QUES(6)/'ASCENDING NODE:      NSTEPS, size: '/      ! 35 characters
      DATA QUES(7)/'ARG. OF PERIASTRON:  NSTEPS, size: '/      ! 35 characters
      DATA BLANKOUT/'                                                 '/

      NDATA           = 2 * (NPTS - DEL_COUNT)

C READ PARAMETERS INTO ARRAY

      A(1)            = A_TRUE
      A(2)            = E_TRUE
      A(3)            = PERIOD
      A(4)            = EPOCH 
      A(5)            = INCLIN
      A(6)            = ASC_NODE
      A(7)            = ARG_PERI

C ASK FOR THE INCREMENTS FOR THE ELEMENTS

      CALL LOCATE(0,2)
      CALL EGA_RESTORE_DEFAULT
      CALL WRITE_STRING(' Calc. CHISQ on grid from -NSTEPS to +NSTEPS')
      CALL LOCATE(0,3)
      CALL EGA_RESTORE_DEFAULT
      CALL WRITE_STRING( 'NSTEPS = 0  =>  don''t vary parameter' )
      DO I=1,7
          CALL LOCATE(0,4)
          CALL EGA_RESTORE_DEFAULT
C          CALL WRITE_STRING( BLANKOUT )
C          CALL EGA_RESTORE_DEFAULT
          CALL WRITE_STRING( QUES(I) )
          READ (INC,*) NSTEPS(I),DELTA_A(I)   
          IF(I.GE.5) THEN
              DELTA_A(I) = DELTA_A(I)*RPDEG
          END IF
      END DO

C GET THE PREDICTED PLACES FOR TOBS(I) AND ADD UP CHISQ

      CHISQ           = 0.
      NCHISQ          = 0.

      DO I=1,NPTS
          CALL TRUE2APP(TOBS(I),A,RMOD,THMOD,RAMOD,DECMOD,DYDA,PR)
          R_EST(I)    = RMOD
          THETA_EST(I)= THMOD
      END DO
      CALL CHISQR(R_EST,ROBS,THETA_EST,THETAOBS,DRA,DDEC,PHI_ERR,
     1            NPTS,DELETE,CHISQ,DELRA,DELDEC) 
      CALL LOCATE(0,16)
      WRITE(INS,'(A,F10.3)') 'CHISQ AT START:  ',CHISQ
      CALL WRITE_STRING( INS )

C MAKE THE ARRAY OF PARAMETERS USED TO CHECK CHISQ

      DO I=1,7
          ACHECK(I)=A(I)
      END DO

C SEARCH A GRID IN PARAMETER SPACE FOR THE SMALLEST CHISQ

      DO I=-NSTEPS(1),NSTEPS(1)
        ACHECK(1) = A(1)+I*DELTA_A(1)
        CALL LOCATE(0,9)
        WRITE(INS,'(A,I3,A,F9.4)') 'A_TRUE  (',I,') = ',ACHECK(1)
        CALL WRITE_STRING( INS )
        DO J=-NSTEPS(2),NSTEPS(2)
          ACHECK(2) = A(2)+J*DELTA_A(2)
          CALL LOCATE(0,10)
          WRITE(INS,'(A,I3,A,F9.4)') 'E_TRUE  (',J,') = ',ACHECK(2)
          CALL WRITE_STRING( INS )
          DO K=-NSTEPS(3),NSTEPS(3)
            ACHECK(3) = A(3)+K*DELTA_A(3)
            CALL LOCATE(0,11)
            WRITE(INS,'(A,I3,A,F9.4)') 'PERIOD  (',K,') = ',ACHECK(3)
            CALL WRITE_STRING( INS )
            DO L=-NSTEPS(4),NSTEPS(4)
              ACHECK(4) = A(4)+L*DELTA_A(4)
              CALL LOCATE(0,12)
              WRITE(INS,'(A,I3,A,F11.3)') 'EPOCH   (',L,') = ',ACHECK(4)
              CALL WRITE_STRING( INS )
              DO M=-NSTEPS(5),NSTEPS(5)
                ACHECK(5) = A(5)+M*DELTA_A(5)
                CALL LOCATE(0,13)
                WRITE(INS,'(A,I3,A,F9.4)') 
     1              'INCLIN  (',M,') = ',ACHECK(5)/RPDEG
                CALL WRITE_STRING( INS )
                DO N=-NSTEPS(6),NSTEPS(6)
                  ACHECK(6) = A(6)+N*DELTA_A(6)
                  CALL LOCATE(0,14)
                  WRITE(INS,'(A,I3,A,F9.4)')
     1                'ASC_NODE(',N,') = ',ACHECK(6)/RPDEG
                  CALL WRITE_STRING( INS )
                  DO P=-NSTEPS(7),NSTEPS(7)       
                    ACHECK(7) = A(7)+P*DELTA_A(7)
                    CALL LOCATE(0,15)
                    WRITE(INS,'(A,I3,A,F9.4)')
     1                  'ARG_PERI(',P,') = ',ACHECK(7)/RPDEG
                    CALL WRITE_STRING( INS )
                    NCHISQ        = 0.
                    DO Q=1,NPTS
D                     CALL LOCATE(0,20)
D                     WRITE(INS,'(A,F11.3,1X,F10.3,1X,F10.3)')
D    1                  'TRUE2APP IN: TOBS, SEP, P.A.: ',TOBS(Q),
D    2                  ROBS(Q), THETAOBS(Q)
D                     CALL WRITE_STRING( INS )
D                     CALL LOCATE(0,21)
D                     WRITE(INS,'(3(F9.4,1X),F11.3,3(1X,F9.4))')
D    1                  (ACHECK(R),R=1,4),(ACHECK(R)/RPDEG,R=5,7)
D                     CALL WRITE_STRING( INS )
D                     READ(INC,'(A)') ANSWER
                      CALL TRUE2APP(TOBS(Q),ACHECK,RMOD,THMOD,RAMOD,
     1                              DECMOD,DYDA,PR)
                      R_EST(Q)    = RMOD
                      THETA_EST(Q)= THMOD
                    END DO  
                    CALL CHISQR(R_EST,ROBS,THETA_EST,THETAOBS,DRA,DDEC,
     1                          PHI_ERR,NPTS,DELETE,NCHISQ,DELRA,DELDEC)
                    IF(NCHISQ.LT.CHISQ) THEN
                      CHISQ     = NCHISQ
                      MINI      = I
                      MINJ      = J
                      MINK      = K
                      MINL      = L
                      MINM      = M
                      MINN      = N
                      MINP      = P
                      NEWMIN    = .TRUE.
                      CALL LOCATE(0,16)
                      WRITE(INS,'(A,F10.3)') 'NEW CHISQ(MIN):  ',CHISQ
                      CALL WRITE_STRING( INS )
                    END IF
                  END DO
                END DO
              END DO
            END DO
          END DO
        END DO
      END DO

      IF(NEWMIN) THEN
          A(1)        = A(1)+MINI*DELTA_A(1)
          A(2)        = A(2)+MINJ*DELTA_A(2)
          A(3)        = A(3)+MINK*DELTA_A(3)
          A(4)        = A(4)+MINL*DELTA_A(4)
          A(5)        = A(5)+MINM*DELTA_A(5)
          A(6)        = A(6)+MINN*DELTA_A(6)
          A(7)        = A(7)+MINP*DELTA_A(7)
          A_TRUE      = A(1)
          E_TRUE      = A(2)
          PERIOD      = A(3)
          EPOCH       = A(4)
          INCLIN      = A(5)
          ASC_NODE    = A(6)
          ARG_PERI    = A(7)
          WRITE(PR,'(A)') ' Minimum in chi^2 found for new elements:'
          WRITE(INS,'(A)') 'Minimum in chi^2 found for new elements:'
          CALL LOCATE(0,5)
          CALL WRITE_STRING( INS )
      ELSE
          WRITE(PR,'(A)') ' Minimum in chi^2 at old values of elements:'
          WRITE(INS,'(A)') 'Minimum in chi^2 at old values of elements:'
          CALL LOCATE(0,5)
          CALL WRITE_STRING( INS )
      END IF

      WRITE(PR,'(1X,A,/,F9.4,1X,F9.4,1X,F9.4,1X,F11.3,1X,F9.4,1X,F9.4,
     1           1X,F9.4,/,A,F10.3)')
     2           'GRIDCHI: A, E, P, T, i, OMEGA, omega = ',
     3            A(1),A(2),A(3),A(4),A(5)/RPDEG,A(6)/RPDEG,
     4            A(7)/RPDEG,'CHISQ = ',CHISQ

      WRITE(INS,'(A)') 'A, E, P, T, i, OMEGA, omega = '
      CALL LOCATE(0,6)
      CALL WRITE_STRING ( INS )
      WRITE(INS,'(F9.4,1X,F9.4,1X,F9.4,1X,F11.3,1X,F9.4,1X,F9.4,
     1            1X,F9.4)')
     2            A(1),A(2),A(3),A(4),A(5)/RPDEG,A(6)/RPDEG,
     3            A(7)/RPDEG
      CALL LOCATE(0,7)
      CALL WRITE_STRING ( INS )
      WRITE(INS,'(A,F10.3)') 'CHISQ = ',CHISQ
      CALL LOCATE(0,8) 
      CALL WRITE_STRING ( INS )

      CALL PGASK(.TRUE.)
      CALL PGADVANCE
      CALL PGASK(.FALSE.)

      RETURN

      END

