      Program BigStar
      Implicit None
      Integer StarNum,StarIdx(22)
      Integer*2 FPos,FileNum,J
      Character*50 InFile,OutFile
      Character*44 IdxString
      Real*8 RA,Dec
      Real Mag,RAMot,DecMot,RAIdx(22),MaxMag
      Logical FileThere,FirstStar

      IdxString = '01020304050607080910111213141516171819202122'
      Write (*,10)
10    Format (' Enter name of first 28-byte star file: ')
      Read (*,20) InFile
20    Format (A)
      FPos = Index(InFile,'01')
      IF (FPos .EQ. 0) THEN
        Write (*,30)
30      Format ('+Filename must have "01" as part of name.')
        STOP
      ENDIF
      Write (*,40)
40    Format ('+Enter output file name (file will contain all',
     &   ' stars): ')
      Read (*,20) OutFile
      Open (Unit=2, File = OutFile, Status = 'New', Form=
     &    'Unformatted', Recl = 28, Access = 'Direct')
      FileNum = 1
      StarNum = 1
      Write (*,45)
45    Format ('+Enter dimmest star magnitude of interest: ')
      Read (*,*) MaxMag

50    InFile(FPos:FPos+1) = IdxString(FileNum*2-1:FileNum*2)
      Inquire (Exist = FileThere, File = InFile)
      IF (.NOT. FileThere) THEN
        Write (*,60) CHARNB(InFile)
60      Format (' Error.  File "',A,'" not found.')
        Close (2)
        STOP
      ENDIF
      Write (*,70) CHARNB(InFile)
70    Format (' Opening ',A,/)
      Open (Unit=1, File = InFile, Status = 'Old', Form =
     &    'Unformatted', Access = 'Transparent')
      FirstStar = .True.

100   Read (1,End=300) RA,Dec,Mag,RAMot,DecMot
      IF (FirstStar) THEN
        StarIdx(FileNum) = StarNum
        FirstStar = .False.
      ENDIF
      IF (Mag .GT. MaxMag) GOTO 100
      IF (StarNum/100 .EQ. Float(StarNum)/100.) Write (*,120) StarNum
120   Format ('+Star #',I6)
      Write (2,Rec=StarNum) RA,Dec,Mag,RAMot,DecMot
      StarNum = StarNum + 1
      GOTO 100

300   Write (*,120) StarNum-1
      RAIdx(FileNum) = MIN(24.,RA + 1E-6)
      Close (1)
      IF (FileNum .NE. 22) THEN
C       More star files to read:
        FileNum = FileNum + 1
        GOTO 50
      ENDIF
      Write (*,310) StarNum - 1
310   Format (' Total stars read in: ',I6,/)
      Close (2)
      Write (*,320)
320   Format (' Building index file STARS.IDX...')
      Open (1, File = 'STARS.IDX', Status = 'Unknown')
      Write (1,330) StarNum-1
330   Format ('22,',I6)
      DO 500 J = 1,22
        Write (1,340) StarIdx(J),RAIdx(J)
340     Format (I6,X,F9.6)
500   CONTINUE
      Close (1)
      Stop
      End
