New Reply

FORTRAN, open several files and read it

 
Share Thread Thread Tools
Sep29-09, 07:55 AM   #1
 

FORTRAN, open several files and read it


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
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
PhysOrg.com
PhysOrg
science news on PhysOrg.com

>> Galaxies fed by funnels of fuel
>> The better to see you with: Scientists build record-setting metamaterial flat lens
>> Google eyes emerging markets networks
Sep29-09, 01:25 PM   #2
 
Mentor
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.
Sep29-09, 02:34 PM   #3
 
Recognitions:
Science Advisor Science Advisor
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.
Sep30-09, 07:09 AM   #4
 

FORTRAN, open several files and read it


Quote by minger View Post
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
Sep30-09, 07:20 AM   #5
 
Recognitions:
Science Advisor Science Advisor
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 gotta do.
Sep30-09, 08:44 AM   #6
 
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 cant get to see :-(
any tip to help me please
RG
Sep30-09, 12:15 PM   #7
 
Recognitions:
Science Advisor Science Advisor
I can't really understand what you're trying to do. Can you try to explain a little better?
Sep30-09, 06:07 PM   #8
 
Recognitions:
Homework Helper Homework Help
400 files takes 3 digits, so that format should be

10 format('iso',i3.3,'.dat')
Oct1-09, 06:36 AM   #9
 
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
Oct1-09, 07:46 AM   #10
 
Recognitions:
Science Advisor Science Advisor
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
Oct1-09, 09:25 AM   #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
Oct5-09, 08:00 AM   #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
Oct5-09, 12:43 PM   #13
 
Recognitions:
Science Advisor Science Advisor
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.
Oct7-09, 03:32 PM   #14
 
Quote by minger View Post
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?
Oct7-09, 03:59 PM   #15
 
Recognitions:
Science Advisor Science Advisor
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.
Oct7-09, 04:32 PM   #16
 
Quote by minger View Post
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
Jun17-10, 11:45 PM   #17
 
Thanks for your discussion which was really helpful to my work.... AND THANKS TO PHYSICS FORUMS
New Reply
Thread Tools


Similar Threads for: FORTRAN, open several files and read it
Thread Forum Replies
Python: open dat-files, read data Engineering, Comp Sci, & Technology Homework 5
FORTRAN Open Arbitrary number of files in Do Loop Programming & Comp Sci 8
Loading 2D array files to Fortran variables Programming & Comp Sci 12
Fortran 90 (need to read in binary data files into arrays) Math & Science Software 1
Fortran 90 question about reading files with text Programming & Comp Sci 1