Fortran FORTRAN, open several files and read it

  • Thread starter Thread starter vikincarioca
  • Start date Start date
  • Tags Tags
    files Fortran
AI Thread Summary
The discussion focuses on using Fortran to open and read multiple files with a specific naming convention (ISO1.dat to ISO400.dat) and how to handle the data within those files. Participants suggest using formatted strings to generate file names dynamically and employing loops to read data efficiently. Key points include the importance of correctly managing file openings and closings, as well as writing output in the desired format. The conversation also touches on the challenge of averaging data from numerous files and how to structure the output correctly. Overall, the thread provides insights into file handling and data processing in Fortran.
vikincarioca
Messages
8
Reaction score
0
Hi pals I'm using fortran program to open several files like iso1.dat,iso2.dat... iso9.dat withis this files are number that I can read with my program, but I don't know how can I open the files above and then read it
this is my simple code
Code:
      implicit real*8(a-h,o-z)
      parameter(nx=100)
      dimension y(nx),eta(nx),iso(nx)
      integer::nl1,i,n
      character*60 dumi
	
      nl1=9
	do i=1,8
      open(10,file='iso00.dat')

	end do
      

	
      do i=1,nl1
      read(10,fmt=*)dumi
      end do
	DO n=1,5
      read(10,fmt=*)i,y(n),eta(n),j,k,p,q,s
      write(*,'(2d14.6)')y(n),eta(n)
	END DO


      stop
	end
 
Technology news on Phys.org
In this code
Code:
do i=1,8
    open(10,file='iso00.dat')
end do
you are attempting to open the same file 8 times. Since you want to open 8 different files, I don't see any way of doing that in a loop. Instead, what you should do is open the first file, read the data you need from it, then close the file. After that, do the same with the second file, then the third, and so on.
 
There are a couple ways of opening files in a DO loops. The easiest is to just open them prior to the DO loop, and then just reading unit number of the do iterator, e.g.
Code:
OPEN(11,file...)
OPEN(12,file...)
OPEN(13,file...)
OPEN(14,file...)
...
DO i=1,4
 ii = i + 10
 READ(ii,*) ...
 ...
END DO
...

There is a better way to do it with an arbitrary number of files, but if this works, then its easiest.
 
minger said:
There are a couple ways of opening files in a DO loops. The easiest is to just open them prior to the DO loop, and then just reading unit number of the do iterator, e.g.
Code:
OPEN(11,file...)
OPEN(12,file...)
OPEN(13,file...)
OPEN(14,file...)
...
DO i=1,4
 ii = i + 10
 READ(ii,*) ...
 ...
END DO
...

There is a better way to do it with an arbitrary number of files, but if this works, then its easiest.

Thanks for your replied but I need to do the same steps for 400 files so How can I do it, for instance
Code:
DO I=1,400
OPEN(I,file=' ISO?.DAT'...) HERE IS MY PROBLEM HOW CAN I OPEN FILES AS ISO1,ISO2...ISO400
END DO

...
DO i=1,4
 ii = i + 10
 READ(ii,*) ...
 ...
END DO
...
looking forward for a replied
 
I had actually started a thread a little while back that you can see below in the similar threads. You'll basically want something like:
Code:
      CHARACTER(len=12) :: FN
      N=5 !--arbitrary number of files
      DO I=1,N
      WRITE(FN,10)I
      WRITE(6,*)FN
      OPEN(1,FILE=FN)
      CLOSE(1)
      END DO
   10 FORMAT('ISO',I2.2,'.DAT')
      STOP
      END

This should open a file according to the format statement, i.e. ISOxx.DAT...then just do whatever you got to do.
 
following your tips I compile the program and it worked, but I need to read and stored the dates in another file,well I thought It wouldn't difficult but I was wrong, so further details of my code is
Code:
      implicit real*8(a-h,o-z)
      parameter(nx=100)
      dimension y(nx),eta(nx)
      integer::nl1,i,j,k
      character(len=12)::fn
      n=5  !arbitrary number of files
      nl1=9
	
   10 format('iso',i2.2,'.dat')
	do i=2,n
	write(fn,10)i
	write(6,*)fn
	open(1,file=fn)



	do j=1,nl1
	read(1,fmt=*)dumi  THIS LINE  READ THE CHARACTERS
	end do
	do k=1,5
	read(1,fmt=*)w,y(k),eta(k),p,q,s,t,r THIS LINE SHOULD BE STORE MY DATAS IN 2 VECTORS
	end do
	close(1)
	end do


      
      stop
	end
something is wrong in this code,but I can't get to see :-(
any tip to help me please
RG
 
I can't really understand what you're trying to do. Can you try to explain a little better?
 
400 files takes 3 digits, so that format should be

10 format('iso',i3.3,'.dat')
 
Im going to explain what I need to do.
First I would like to open 400 files e.g ISO1.dat,..ISO400.dat, within this files I have datas which means numbers for instance( X,Y,Z are vectors)
ISO1.dat has X1,Y1,Z1 vectors
ISO2.dat has X1,Y2,Z2 vectors
ISO3.dat has X1,Y3,Z3 vectors
I only need to read X and Y
then write in a unique file X1,Y1,Y2,Y3...Y400
How Can I do it
Hope to be clear this time
Tks
 
  • #10
As Jeff mentioned, it'll be a lot easier if your files are ISO001, rather than just ISO1. Using that method, you can just open another file
Code:
      PROGRAM 
      IMPLICIT NONE
      INTEGER :: n
      REAL,DIMENSION(:),ALLOCATABLE :: x,y,z
      CHARACTER(len=12) :: FN

      n=400
      OPEN(11,file='output.dat',form='formatted')
      !--allocate your vectors here
      DO I=1,N
      WRITE(FN,10)I
      WRITE(6,*)FN
      OPEN(1,FILE=FN)
      READ(1,*) x,y,z
      WRITE(11,*) x,y
      CLOSE(1)
      END DO
   10 FORMAT('ISO',I3.3,'.DAT')
      DEALLOCATE( x,y,z )
      END PROGRAM
 
  • Like
Likes Lekha Kumar
  • #11
hey thanks for your replied, One more question since my vector X,Y,Z has 5 elements so what I need to do is a loop



Code:
      PROGRAM 
      IMPLICIT NONE
      INTEGER :: n
      REAL,DIMENSION(:),ALLOCATABLE :: x,y,z
      CHARACTER(len=12) :: FN

      n=400
      OPEN(11,file='output.dat',form='formatted')
      !--allocate your vectors here
      DO I=1,N
      WRITE(FN,10)I
      WRITE(6,*)FN
      OPEN(1,FILE=FN)

      DO j=1,5
      READ(1,*) x(J),y(J),z(J)   HERE I STORED MY VECTORS WITH ITS ELEMENTS
      WRITE(11,*) x(J),y(J)
      END DO

      CLOSE(1)
      END DO
   10 FORMAT('ISO',I3.3,'.DAT')
      DEALLOCATE( x,y,z )
      END PROGRAM
it is what I think
Many thanks
 
  • #12
Hi Pals I got to read the files finally :-) and the write in one file in this case output.dat
[code\]
PROGRAM eta
implicit none
integer ::n,i,j
real,dimension(:),allocatable::x,y,z,u,v,w,p,f

character(len=12)::fn

n=3 !NUMBER OF FILES
open(11,file='output.dat',form='formatted') !allocate your vectors here
allocate (x(5),y(5),z(5),u(5),v(5),w(5),p(5),f(5))

DO i=1,n !LOOP FOR THE NUMBER OF FILES
write(fn,10)i
write(6,*)fn
open(1,file=fn)
do j=1,5
read(1,*)x(j),y(j),z(j),u(j),v(j),w(j),p(j),f(j)
write(11,*)y(j),z(j)
end do

close(1)
END DO


10 format('iso',i3.3,'.dat')
deallocate(x,y,z,u,v,w,p,f)
END PROGRAM eta

Code:
but I have one problem in the output I wrote vector X1 three times and then vector Y1 follow Y2 and Y3 in one column, I would like to write it in this shape
X1 Y1 Y2 Y3...Y400 it means 400 columns
Can you suggest me something I am really close what I want, just I need a bit help please
 
  • #13
Rather than having your write statement inside your second DO loop, just put it after, e.g.
Code:
DO i=1,n !LOOP FOR THE NUMBER OF FILES
write(fn,10)i
write(6,*)fn
open(1,file=fn)
do j=1,5
read(1,*)x(j),y(j),z(j),u(j),v(j),w(j),p(j),f(j)
end do

close(1)
END DO

write(11,*)x(1),y(:)
...
After it reads in the vectors for an individual file, it will write out the first X variable, followed by the entire Y vector.
 
  • #14
minger said:
Rather than having your write statement inside your second DO loop, just put it after, e.g.
Code:
DO i=1,n !LOOP FOR THE NUMBER OF FILES
write(fn,10)i
write(6,*)fn
open(1,file=fn)
do j=1,5
read(1,*)x(j),y(j),z(j),u(j),v(j),w(j),p(j),f(j)
end do

close(1)
END DO
 
write(11,*)x(1),y(:)
...
After it reads in the vectors for an individual file, it will write out the first X variable, followed by the entire Y vector.

Hi it works I got to write my vectors Y1 and Z1,Z2,Z3... with its elements, this is a result
Code:
  -20.50000      -13.50000      -6.500000       7.500000       14.50000    this is   Y1
   1.450721       1.410950       1.247190       1.258387       1.406754     this is    Z1
  -20.50000      -13.50000      -6.500000       7.500000       14.50000    this is    Y1
   1.320300       1.294613       1.176677       1.187839       1.292512    this is     Z2
  -20.50000      -13.50000      -6.500000       7.500000       14.50000    this is    Y1
   1.228186       1.202048       1.076701       1.095856       1.203530    this is     Z3
that's good but now i have the last question I think how can write the elements of Y1,Z1,Z2,Z3 as a column and not as a above, I tried differents formats but I can't get to write as I wish. Is there any special format?
 
  • #15
You should really get a Fortran book and start from page 1. In order to write those as a column and not a row, you can loop over the vector writing out a single entry each time.

In fact, that's what you were doing in post 11. If you want, you can use a counter variable and store the entire XYZ values (all of them from each file) in a separate file, and write it out at the end...the possibilities are numerous.
 
  • #16
minger said:
You should really get a Fortran book and start from page 1. In order to write those as a column and not a row, you can loop over the vector writing out a single entry each time.

In fact, that's what you were doing in post 11. If you want, you can use a counter variable and store the entire XYZ values (all of them from each file) in a separate file, and write it out at the end...the possibilities are numerous.
Yeah I did what you suggested me but I got this
Code:
  -20.50000       1.450721    
  -13.50000       1.410950    
  -6.500000       1.247190    
   7.500000       1.258387    
   14.50000       1.406754    
  -20.50000       1.320300    
  -13.50000       1.294613    
  -6.500000       1.176677    
   7.500000       1.187839    
   14.50000       1.292512    
  -20.50000       1.228186    
  -13.50000       1.202048    
  -6.500000       1.076701    
   7.500000       1.095856    
   14.50000       1.203530    


[\code]
all together but this is what I don't wish, I want Y1,Z1,Y1,Z2,Y1,Z3 or if I could get to write it as:Y1,Z1,Z2,Z3 would be better but I dude that i can write it out in that way, anyway FORTRAN is a big complicated
 
  • #17
Thanks for your discussion which was really helpful to my work... AND THANKS TO PHYSICS FORUMS
 
  • #18
Dear all!
i have collected 1000 numbers of gridded rainfall data. The name file of that file are like 2007_01_01_01, 2007_01_01_02, ......(1000 files)
I want to read all data file and want to calculate average of each grid of that file as a output.
more details, main problem for me is how to read and write all these data( because, there is not good solution to open individually 1000 files and to write 1000 times)

so I need your guide and suggestion as soon
I tired by like

program test
implicit none

integer,parameter :: nrow = 240
integer,parameter :: ncol = 324
real rain(nrow,ncol)
real,allocatable:: rain(:,:)
real,allocatable:: Avg_rain(:,:)
integer i,j,k
integer dis, size

inquire(iolength=dis) rain(1,1)
inquire(iolength=dis) avg_rain(1,1)
size = nrow*ncol*dis


open(22,file='2007_01_01_01.bin',form='unformatted ',access='direct',recl=size)

!( problem is here how to open and read all rest 999 files)


open(33,file='avg_rain.bin',form='unformatted',acc ess='direct',recl=size)

! how to read alll those files and each grid cell to calculate average of each gird of rainfall
read(22,rec=1)((rain(i,j),i=1,nrow),j=1,ncol)
close(22)
read(33,rec=1)((Avg_rain(i,j),i=1,nrow),j=1,ncol)
close(33)

do i=1,nrow
do j=1,ncol
Avg_rain(i,j)= avg( how to write for those 1000 files)
end program test


I am hoping for good suggestion from you...


shaktipande
 
  • #19
Hi!

I have a problem i don't have any information about column of my data in separate files.
How can I allocate such column x,y ,which has ex.isooo1:(1310),is0002:(1186),... row of data.
my code:
DO I=1,200
WRITE(A,10)I
WRITE(6,*)A
OPEN(1,FILE=A,IOSTAT=OPENSTATUS,STATUS='OLD')

IF(STATUS==0) THEN
DO j=1,SIZE(X)
read(1,*,END=200)x(j),y(j)
200 write(11,*)x(j),y(j)
END DO

close(1)

ELSE
STOP "****CANNOT OPEN FILE****"
END IF


! END DO

END DO

10 FORMAT('iso',I3.3,'.dat')
 
  • #20
what about writing the output in multiple files? I have a program to read number of of data files, process them and have to write the output in multiple files. May anyone share the idea please?
 
  • #21
Minger , I want to say Thanks you for your support in this problem !. really.
 

Similar threads

Replies
8
Views
1K
Replies
12
Views
3K
Replies
5
Views
5K
Replies
5
Views
2K
Replies
3
Views
2K
Replies
8
Views
2K
Replies
3
Views
3K
Replies
6
Views
2K
Replies
16
Views
4K
Back
Top