PDA

View Full Version : FORTRAN, open several files and read it


vikincarioca
Sep29-09, 08:55 AM
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 dont know how can I open the files above and then read it
this is my simple 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

Mark44
Sep29-09, 02:25 PM
In this 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.

minger
Sep29-09, 03:34 PM
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.

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.

vikincarioca
Sep30-09, 08:09 AM
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.

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

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

minger
Sep30-09, 08:20 AM
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:

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 gotta do.

vikincarioca
Sep30-09, 09:44 AM
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

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 cant get to see :-(
any tip to help me please
RG

minger
Sep30-09, 01:15 PM
I can't really understand what you're trying to do. Can you try to explain a little better?

Jeff Reid
Sep30-09, 07:07 PM
400 files takes 3 digits, so that format should be

10 format('iso',i3.3,'.dat')

vikincarioca
Oct1-09, 07:36 AM
Im gonna 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

minger
Oct1-09, 08:46 AM
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

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

vikincarioca
Oct1-09, 10:25 AM
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




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

vikincarioca
Oct5-09, 09:00 AM
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

minger
Oct5-09, 01:43 PM
Rather than having your write statement inside your second DO loop, just put it after, e.g.

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.

vikincarioca
Oct7-09, 04:32 PM
Rather than having your write statement inside your second DO loop, just put it after, e.g.

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

-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?

minger
Oct7-09, 04:59 PM
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.

vikincarioca
Oct7-09, 05:32 PM
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