      SUBROUTINE READDATA
C-----------------------------------------------------------------
C Reads data file of binary star separations and position angles
C for program ELLIPSE.  By J.T. Armstrong
C     25 Apr 1991     JTA:   Modified for reading error ellipse parameters
C-----------------------------------------------------------------
C READ DATA FILE 
C DATA NEEDED ARE:  STARNAME AND PERIOD (FIRST RECORD); THEN 
C     SEPARATION, POSITION ANGLE, TIME FOR EACH POINT, UNCERTAINTIES
C     IN "RA" AND "DEC", AND POSITION ANGLE OF THE "RA" AXIS OF THE 
C     UNCERTAINTY ELLIPSE.
C-----------------------------------------------------------------
C INPUT DATA FORMAT IS:
C RECORD
C   1:  STARNAME, FK5, PERIOD, ORBSENSE (pro- or retrograde)
C   2:  ROBS(mas), THETAOBS(deg), DRA(mas), DDEC(mas), PHI_ERR(deg), 
C             TSTRING(date: YYMMDD), UT(sec)
C   3:  ROBS, etc.
C-----------------------------------------------------------------
      IMPLICIT UNDEFINED (A-Z)
      INCLUDE 'PARAMETR.INC'
      INCLUDE 'ELLIPSE.INC'    ! Variable declarations, common blocks
      REAL*8       TYR, TMON, TDAY
      REAL*8       DUMMY
      INTEGER*4    I, J, IER, INDEX, YEAR, MONTH, DAY
      CHARACTER*2  TWOSTRING

      OPEN (UNIT=INDATA, FILE=DATAFILE,   
     1           FORM='FORMATTED', STATUS='OLD', ERR=1900)

      CALL GETPT(INDEX)
      CLOSE (UNIT=INDATA)

      NPTS        = INDEX - 2
D     WRITE(PR,*) 'READDATA:  NPTS = ',NPTS
      WRITE(OUTC,506) STARNAME, FK5, PERIOD, ORBSENSE
      WRITE(PR,506) STARNAME, FK5, PERIOD, ORBSENSE

C INTERPRET TSTRING AND SAVE FIRSTYR

      DO I = 1, NPTS

          TWOSTRING = TSTRING(I)(1:2)
          CALL CHARNUM(TWOSTRING,TYR,IER)
          TYR     = TYR + 1900.
          YEAR    = INT(TYR) 

          TWOSTRING = TSTRING(I)(3:4)
          CALL CHARNUM(TWOSTRING,TMON,IER)
          MONTH   = INT(TMON)

          TWOSTRING = TSTRING(I)(5:6)
          CALL CHARNUM(TWOSTRING,TDAY,IER)
          DAY     = INT(TDAY)

          CALL JULDA(YEAR,MONTH,DAY,JULDAY(I),GMST(I))

      END DO

C      WRITE(OUTC,*) ' DONE WITH TSTRING INTERPRETATION'

C SORT THE DATA ACCORDING TO DATE (NUMERICAL RECIPES, P. 227)

C      DO J = 2, NPTS
C          DUMMY   = JULDAY(J)
C          DO I = J-1, 1, -1
C              IF(JULDAY(I).LE.DUMMY) GO TO 10
C              JULDAY(I+1) = JULDAY(I)
C          END DO
C          I = 0
C   10     JULDAY(I+1) = DUMMY
C      END DO

      WRITE(OUTC,*) ' DONE SORTING THE DATA'

C ADD THE U.T. OF THE OBSERVATION

      DO I = 1, NPTS
          TOBS(I) = JULDAY(I) + UT(I)/86400.
      END DO

      TOBSREF     = TOBS(1)

C CONVERT ANGLES TO RADIANS

      DO I = 1, NPTS
          THETAOBS(I)  = RPDEG*THETAOBS(I)         
          PHI_ERR(I)   = RPDEG*PHI_ERR(I)
C          WRITE(OUTC,*) ' THETAOBS=',THETAOBS(I)
C          WRITE(OUTC,*) ' PHI_ERR=',PHI_ERR(I)
      END DO

      RETURN


 1900 WRITE(OUTC,501) (' Problem opening datafile')
      RETURN

  501 FORMAT(A)
  502 FORMAT(1X,'I=',I4,'ROBS=',F8.4,' THETAOBS=',F8.4,' TOBS=',F20.10) 
  503 FORMAT(1X,'YEAR ',I4,' MONTH ',I2,' DAY ',I2,
     1          'JULIAN DAY ',I4,2X,'=',F10.2)
  504 FORMAT(' I     R    DR   THETA DTHETA TSTRING')
  505 FORMAT(1X,I3,1X,F6.2,1X,F4.2,1X,F6.2,1X,F5.2,2X,A6)
  506 FORMAT(1X,'STAR ',A30,'; FK5 ',I4,'; PERIOD ',F8.3,' DAYS; ',
     1           A10,' ORBIT')
  507 FORMAT(1X, 'TYR  = ',F8.4)
  508 FORMAT(1X, 'TMON = ',F8.4)
  509 FORMAT(1X, 'TDAY = ',F8.4)
  510 FORMAT(1X, 'TOBS(',I3,') = ',F20.5)
  511 FORMAT(1X, 'JULDAY(',I3,') = ',F20.5)
  512 FORMAT(1X, 'FRSJULDAY = ',F20.5)

      END

