PROGRAM EPATHTOTRVAL
IMPLICIT NONE
INTEGER LUNIT, IOSTATUS, NMIN, NCOL, MINID, J1, NDUMMY, PUNIT, PUNIT2
INTEGER, ALLOCATABLE :: ORDER(:)
DOUBLE PRECISION EDUMMY, DUMMY

OPEN(NEWUNIT=LUNIT,FILE='min.data')
NMIN=0
DO 
! note that IOSTAT=IOSTATUS sets the variable IOSTATUS to the returned value of IOSTAT
  READ(LUNIT,*,IOSTAT=IOSTATUS) DUMMY
  IF (IOSTATUS.NE.0) EXIT
  NMIN=NMIN+1
ENDDO
CLOSE(LUNIT)
PRINT '(A,I10)','Number of minima in min.data=',NMIN
ALLOCATE(ORDER(NMIN))

ORDER(1:NMIN)=-1
NCOL=0
OPEN(NEWUNIT=LUNIT,FILE='Epath')
DO 
   READ(LUNIT,*,IOSTAT=IOSTATUS) NDUMMY, EDUMMY, MINID
   IF (IOSTATUS.NE.0) THEN
      PRINT '(A,I8,A,I8)','*** ERROR, no min line after minimum ',NCOL,' id ',MINID
      STOP
   ENDIF
   NCOL=NCOL+1
   ORDER(MINID)=NCOL
   READ(LUNIT,*,IOSTAT=IOSTATUS) NDUMMY ! this should be the ts line
   IF (IOSTATUS.NE.0) EXIT ! end of file
ENDDO
CLOSE(LUNIT)

OPEN(NEWUNIT=LUNIT,FILE='colourfile')
OPEN(NEWUNIT=PUNIT,FILE='pickfile.remove')
OPEN(NEWUNIT=PUNIT2,FILE='pickfile')
DO J1=1,NMIN
   IF (ORDER(J1).EQ.-1) THEN
      WRITE(LUNIT,'(F20.10)') -10.0D0
      WRITE(PUNIT,'(I8)') -J1
   ELSE
      WRITE(LUNIT,'(G20.10)') 1.0D0*ORDER(J1)/NCOL
      WRITE(PUNIT,'(I8)') J1
      WRITE(PUNIT2,'(I8)') J1
   ENDIF
ENDDO
CLOSE(LUNIT)
CLOSE(PUNIT)
CLOSE(PUNIT2)

DEALLOCATE(ORDER)

END PROGRAM EPATHTOTRVAL
