      SUBROUTINE GETCHI
C CALCULATES CHI SQUARED FOR A GIVEN SET OF ORBITAL ELEMENTS
      IMPLICIT UNDEFINED (A-Z)
      INCLUDE 'PARAMETR.INC'
      INCLUDE 'ELLIPSE.INC'

      CHARACTER*1  CHOICE
      CHARACTER*80 INS

      INTEGER*4    I, J, K, NDATA

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

      NDATA           = 2 * (NPTS - DEL_COUNT)

C ASK FOR THE ELEMENTS

      WRITE(INS,1001) A_TRUE, VALS(53)
      CALL LOCATE(0,2)
      CALL EGA_RESTORE_DEFAULT
      CALL WRITE_STRING( INS )
      CALL PGCURSE(XR,YR,CHOICE)
      CALL CAPS(CHOICE)
      IF      (CHOICE.EQ.'C') THEN
          ACHECK(1) = A_TRUE
      ELSE IF (CHOICE.EQ.'K') THEN
          ACHECK(1) = VALS(53)
      ELSE 
          CALL LOCATE(0,2)
          CALL WRITE_STRING('                                         ')
          CALL LOCATE(0,2)
          CALL WRITE_STRING('Value for semimajor axis: ')
          READ(INC,*) ACHECK(1)
      END IF

      WRITE(INS,1002) E_TRUE, VALS(54)
      CALL LOCATE(0,2)
      CALL EGA_RESTORE_DEFAULT
      CALL WRITE_STRING( INS )
      CALL PGCURSE(XR,YR,CHOICE)
      CALL CAPS(CHOICE)
      IF      (CHOICE.EQ.'C') THEN
          ACHECK(2) = E_TRUE
      ELSE IF (CHOICE.EQ.'K') THEN
          ACHECK(2) = VALS(54)
      ELSE 
          CALL LOCATE(0,2)
          CALL WRITE_STRING('                                         ')
          CALL LOCATE(0,2)
          CALL WRITE_STRING('Value for ellipticity: ')
          READ(INC,*) ACHECK(2)
      END IF

      WRITE(INS,1003) PERIOD, VALS(55)
      CALL LOCATE(0,2)
      CALL EGA_RESTORE_DEFAULT
      CALL WRITE_STRING( INS )
      CALL PGCURSE(XR,YR,CHOICE)
      CALL CAPS(CHOICE)
      IF      (CHOICE.EQ.'C') THEN
          ACHECK(3) = PERIOD
      ELSE IF (CHOICE.EQ.'K') THEN
          ACHECK(3) = VALS(55)
      ELSE 
          CALL LOCATE(0,2)
          CALL WRITE_STRING('                                         ')
          CALL LOCATE(0,2)
          CALL WRITE_STRING('Value for period: ')
          READ(INC,*) ACHECK(3)
      END IF

      WRITE(INS,1004) EPOCH, VALS(49)
      CALL LOCATE(0,2)
      CALL EGA_RESTORE_DEFAULT
      CALL WRITE_STRING( INS )
      CALL PGCURSE(XR,YR,CHOICE)
      CALL CAPS(CHOICE)
      IF      (CHOICE.EQ.'C') THEN
          ACHECK(4) = EPOCH 
      ELSE IF (CHOICE.EQ.'K') THEN
          ACHECK(4) = VALS(49)
      ELSE 
          CALL LOCATE(0,2)
          CALL WRITE_STRING('                                         ')
          CALL LOCATE(0,2)
          CALL WRITE_STRING('Value for epoch: ')
          READ(INC,*) ACHECK(4)
      END IF

      WRITE(INS,1005) INCLIN/RPDEG, VALS(50)
      CALL LOCATE(0,2)
      CALL EGA_RESTORE_DEFAULT
      CALL WRITE_STRING( INS )
      CALL PGCURSE(XR,YR,CHOICE)
      CALL CAPS(CHOICE)
      IF      (CHOICE.EQ.'C') THEN
          ACHECK(5) = INCLIN
      ELSE IF (CHOICE.EQ.'K') THEN
          ACHECK(5) = VALS(50)*RPDEG
      ELSE 
          CALL LOCATE(0,2)
          CALL WRITE_STRING('                                         ')
          CALL LOCATE(0,2)
          CALL WRITE_STRING('Value for inclination (deg): ')
          READ(INC,*) ACHECK(5)
          ACHECK(5) = ACHECK(5)*RPDEG
      END IF

      WRITE(INS,1006) ASC_NODE/RPDEG, VALS(52)
      CALL LOCATE(0,2)
      CALL EGA_RESTORE_DEFAULT
      CALL WRITE_STRING( INS )
      CALL PGCURSE(XR,YR,CHOICE)
      CALL CAPS(CHOICE)
      IF      (CHOICE.EQ.'C') THEN
          ACHECK(6) = ASC_NODE
      ELSE IF (CHOICE.EQ.'K') THEN
          ACHECK(6) = VALS(52)*RPDEG
      ELSE 
          CALL LOCATE(0,2)
          CALL WRITE_STRING('                                         ')
          CALL LOCATE(0,2)
          CALL WRITE_STRING('Value for ascending node (deg): ')
          READ(INC,*) ACHECK(6)
          ACHECK(6) = ACHECK(6)*RPDEG
      END IF

      WRITE(INS,1007) ARG_PERI/RPDEG, VALS(51)
      CALL LOCATE(0,2)
      CALL EGA_RESTORE_DEFAULT
      CALL WRITE_STRING( INS )
      CALL PGCURSE(XR,YR,CHOICE)
      CALL CAPS(CHOICE)
      IF      (CHOICE.EQ.'C') THEN
          ACHECK(7) = ARG_PERI
      ELSE IF (CHOICE.EQ.'K') THEN
          ACHECK(7) = VALS(51)*RPDEG
      ELSE 
          CALL LOCATE(0,2)
          CALL WRITE_STRING('                                         ')
          CALL LOCATE(0,2)
          CALL WRITE_STRING('Value for arg. of periastron (deg): ')
          READ(INC,*) ACHECK(7)
          ACHECK(7) = ACHECK(7)*RPDEG
      END IF


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

      CHISQ           = 0.

      DO I=1,NPTS
          CALL TRUE2APP(TOBS(I),ACHECK,RMOD,THMOD,RAMOD,DECMOD,DYDA,PR)
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(I),
D    2                  ROBS(I), THETAOBS(I)
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(J),J=1,4),(ACHECK(J)/RPDEG,J=5,7)
D                     CALL WRITE_STRING( INS )
D                     READ(INC,'(A)') ANSWER
          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)
            
      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           'GETCHI: A, E, P, T, i, OMEGA, omega = ',
     3            ACHECK(1),ACHECK(2),ACHECK(3),
     4            ACHECK(4),ACHECK(5)/RPDEG,ACHECK(6)/RPDEG,
     5            ACHECK(7)/RPDEG,'CHISQ = ',CHISQ

      WRITE(INS,'(A)') 'A, E, P, T, i, OMEGA, omega = '
      CALL LOCATE(0,3)
      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            ACHECK(1),ACHECK(2),ACHECK(3),
     3            ACHECK(4),ACHECK(5)/RPDEG,ACHECK(6)/RPDEG,
     4            ACHECK(7)/RPDEG
      CALL LOCATE(0,4)
      CALL WRITE_STRING ( INS )
      WRITE(INS,'(A,F10.3)') 'CHISQ = ',CHISQ
      CALL LOCATE (0,5) 
      CALL WRITE_STRING ( INS )

      RETURN

 1001 FORMAT(1X,'SEMIMAJ: [C]alculated (',F9.4,'), [K]EYIN (',
     1          F9.4,') or [O]ther value? [O]  ')      ! 78 characters
 1002 FORMAT(1X,'ELLIPTICITY: [C]alc. (',F9.4,'), [K]EYIN (',
     1          F9.4,') or [O]ther value? [O]   ')     ! 78 characters
 1003 FORMAT(1X,'PERIOD: [C]alculated (',F9.4,'), [K]EYIN (',
     1          F9.4,') or [O]ther value? [O]   ')     ! 78 characters
 1004 FORMAT(1X,'EPOCH: [C]alculated (',F11.3,'), [K]EYIN (',
     1          F11.3,') or [O]ther value? [O]')       ! 78 characters
 1005 FORMAT(1X,'INCLIN: [C]alculated (',F7.2,'), [K]EYIN (',
     1          F7.2,') or [O]ther value? [O]       ') ! 78 characters
 1006 FORMAT(1X,'ASC. NODE: [C]alculated (',F7.2,'), [K]EYIN (',
     1          F7.2,') or [O]ther value? [O]    ')    ! 78 characters
 1007 FORMAT(1X,'ARG. PERI: [C]alculated (',F7.2,'), [K]EYIN (',
     1          F7.2,') or [O]ther value? [O]    ')    ! 78 characters

      END

