'Read a selected a line in and input file

I have a formatted file, and I would like recursively randomly select one row and read it. Due to memory issues, it is not possible to read all the data, save it on vectors, and later select one line at time.

I solved in this way (only relevant code is reported), but it is quite slow and I'm wondering if someone could help me to find a fastest way to do it (I'm not fortran expert)

Edit: Yes I would like to use this routine several (~1kk) times, I'm defining the starting parameters for further analysis

  PARAMETER(NLINES=10000000)
  REAL ID,E,X,Y,Z,COSX,COSY
  SAVE LOO
  DATA LFIRST / .TRUE. /
  
  IF ( LFIRST ) THEN
  LFIRST = .FALSE.
  OPEN(UNIT=88,FILE="../../../gene_rid.txt",STATUS="OLD")
  END IF

  XI = FLRNDM(XDUMMY)
  LINE = INT(XI * DBLE(NLINES)) + 1

  DO LOO=1,LINE
     READ(88,*,IOSTAT=iostat) ID
  END DO
  READ(88,*,IOSTAT=iostat) ID,E,
 &        COSX, COSY, X, Y, Z
  REWIND(88)

This is how the input file is formatted

head gene_rid.txt 
  7  0.933549E-03  -.162537E+00  0.136150E-01   -.4791E+01   0.3356E+00   0.2900E+02
  7  0.203748E-02  -.115359E+00  -.217682E+00   -.3453E+01   -.6606E+01   0.2900E+02
  7  0.289498E-02  0.159572E+00  -.954033E-01   0.4767E+01   -.2730E+01   0.2900E+02


Solution 1:[1]

It might help to read the file once, and write it to lots of one-line files, e.g.

integer, parameter :: nlines = 1e7
character(100) :: line
character(100) :: filename

integer :: i

open(unit=88, file="../../../gene_rid.txt", status="old")
do i=1,nlines
  read(88,'(a)') line
  write(filename,'(a,i0)') "gene_rid", i
  open(unit=89, file=filename, status="new")
  write(89,*) line
  close(89)
enddo
close(88)

Then in your main program you only have to open the right one-line file and read one line in each time, rather than scrolling through the entire large file.

If writing 1e7 one-line files is too much for your file system, it might instead be worth writing 1e4 thousand-line files or somesuch.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1