Reading more data files FORTRAN90

477 views Asked by At

I have a question about f90 as title says. I have many .dat files (say 15 files each with 2 columns and 2000 rows) that i need to work with. I need to save all the data in a unique matrix (that will have 30 columns and 2000 rows). But when I use the instruction READ(21,*) MATRIX during execution it says

   Fortran runtime error: End of file

I think it's because the dimension of the matrix is different from the dimension of the single data file. Is there a way to make it work? And is it possible to open all the data files maybe in a DO cycle w/o repeating the OPEN statement a large number of times?

Here is the code

program expected

REAL*8, ALLOCATABLE, DIMENSION ( :, :) :: dati


OPEN (unit=20, status='OLD', file='PRINT_07.CSV', form='FORMATTED')
OPEN (unit=21, status='OLD', file='PRINT_08.CSV', form='FORMATTED')
OPEN (unit=22, status='OLD', file='PRINT_09.CSV', form='FORMATTED')
OPEN (unit=23, status='OLD', file='PRINT_10.CSV', form='FORMATTED')
OPEN (unit=24, status='OLD', file='PRINT_11.CSV', form='FORMATTED')
OPEN (unit=25, status='OLD', file='PRINT_12.CSV', form='FORMATTED')
OPEN (unit=26, status='OLD', file='PRINT_13.CSV', form='FORMATTED')
OPEN (unit=27, status='OLD', file='PRINT_14.CSV', form='FORMATTED')
OPEN (unit=28, status='OLD', file='PRINT_15.CSV', form='FORMATTED')
OPEN (unit=29, status='OLD', file='PRINT_16.CSV', form='FORMATTED')
OPEN (unit=30, status='OLD', file='PRINT_17.CSV', form='FORMATTED')
OPEN (unit=31, status='OLD', file='PRINT_18.CSV', form='FORMATTED')
OPEN (unit=32, status='OLD', file='PRINT_19.CSV', form='FORMATTED')
OPEN (unit=33, status='OLD', file='PRINT_20.CSV', form='FORMATTED')
OPEN (unit=34, status='OLD', file='PRINT_21.CSV', form='FORMATTED')


ALLOCATE ( dati(30,2000) )

READ(20,*) dati


end program    

In a simpler program i've solved the data importing in this way. But i still have the problem of opening multiple data files at once.

program mats

REAL, ALLOCATABLE, DIMENSION (:,:) :: mat


OPEN (UNIT=21, STATUS='OLD', file='mat1.dat', form='FORMATTED')
OPEN (UNIT=22, STATUS='OLD', file='mat2.dat', form='FORMATTED')

OPEN (UNIT=23, STATUS='UNKNOWN', file='mat.dat', form='FORMATTED')


DO i=1,2
OPEN (UNIT=20+i, STATUS='OLD', file= 'mat'i'.dat', form='FORMATTED')
END DO


ALLOCATE ( mat(5,2) )

mat=0

PRINT*, mat

  READ(21,*) mat(1:3,1:2)
  READ(22,*) mat(4:5,1:2)

PRINT*, mat

end program
1

There are 1 answers

0
M. S. B. On

Here's a sketch to give you a concept on how to deal with multiple files in a more elegant manner. It's not exactly a solution to your problem nor tested -- it shows some concepts.

character (len=80) :: filename
   do i=7, 20
      write (filename, '( "PRINT_", I2.2, ".CSV" )' )  i
      open ( unit=55,file=filename, status="old", action="read", form="formattted" )
      read (55, datai (i,:) )
      close (unit=55)
    end do