	INCLUDE 'C:\FORTRAN\INCLUDE\FLIB.FI'

	Program ConvertPHA

	IMPLICIT NONE

	INCLUDE 'C:\FORTRAN\INCLUDE\FLIB.FD'

	INTEGER*4 NumArgs, Data

	INTEGER*2 InFileNameLen, OutFileNameLen, Status, Dot
	INTEGER*2 StartChan, N, NData, DataLen

	CHARACTER*80 InFileName, OutFileName, InString

	LOGICAL Exists

	NumArgs = NARGS() - 1

	IF ( NumArgs .GE. 1 ) THEN
	 CALL GETARG(1, InFileName, InFileNameLen )
	ELSE
	 InFileNameLen = 0
	 DO WHILE ( InFileNameLen .LE. 0 )
	  WRITE (*,'(1X,A\)') 'Input file: '
	  READ ( *, '(A)', IOSTAT=Status ) InFileName
	  InFileNameLen = LEN_TRIM( InFileName )
	 END DO
	END IF

	INQUIRE ( FILE = InFileName(1:InFileNameLen), EXIST=Exists )

	IF ( .NOT. Exists ) THEN
	 WRITE (*,'(1X,A\)') InFileName(1:InFileNameLen)//' not found'
	 STOP
	END IF

	OPEN (UNIT=1, FILE = InFileName(1:InFileNameLen), IOSTAT=Status )

	IF ( NumArgs .GE. 2 ) THEN
	 CALL GETARG(2, OutFileName, OutFileNameLen )
	ELSE
	 Dot = Index( InFileName, '.' )
	 IF (Dot .GT. 0) THEN
	  OutFileName = InFileName(1:Dot)//'TXT'
	 ELSE
	  OutFileName = InFileName(1:InFileNameLen)//'.TXT'
	 END IF
	 OutFileNameLen = LEN_TRIM(OutFileName)
	END IF

	INQUIRE ( FILE = OutFileName(1:OutFileNameLen), EXIST=Exists )

	IF ( Exists ) THEN
	 WRITE (*,'(1X,A\)') OutFileName(1:OutFileNameLen) // 
     +    ' exists. Overwrite [Y]? '
	 READ ( *, '(A)', IOSTAT=Status ) InString
	 IF ( LEN_TRIM(InString) .GT. 0 ) THEN
	  IF ( ( InString(1:1) .EQ. 'N' ) .OR. 
     +	   ( InString(1:1) .EQ. 'n' ) ) THEN
	   STOP 'Try Again.'
	  END IF
	 END IF
	END IF

	OPEN (2, FILE = OutFileName(1:OutFileNameLen), IOSTAT=Status )
	DO WHILE (Status .EQ. 0)
	 READ ( 1, '(A)', IOSTAT=Status ) InString
	 IF ( Index( InString, ':' ) .EQ. 6 ) THEN
	  DataLen = LEN_TRIM( InString )
	  READ( InString(1:5), '(I5)' ) StartChan
	  READ( InString(7:18), '(I12)' ) Data
	  WRITE (2,'(I5,1X,I12)') StartChan, Data
	  NData = 1 + (DataLen-8)/13
	  DO N=2,NData
	   READ( InString(N*13-7:N*13+5), '(I12)' ) Data
	   WRITE (2,'(I5,1X,I12)') StartChan+(N-1), Data
	  END DO
	 END IF
	END DO

	CLOSE( 1 )
	CLOSE( 2 )

	STOP 'Conversion successful.'
	END
