Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

FORTRAN, open several files and read it

  1. Sep 29, 2009 #1
    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 (Text):

          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
     
     
  2. jcsd
  3. Sep 29, 2009 #2

    Mark44

    Staff: Mentor

    In this code
    Code (Text):

    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.
     
  4. Sep 29, 2009 #3

    minger

    User Avatar
    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 (Text):

    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.
     
  5. Sep 30, 2009 #4
    Thanks for your replied but I need to do the same steps for 400 files so How can I do it, for instance
    Code (Text):

    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
     
  6. Sep 30, 2009 #5

    minger

    User Avatar
    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 (Text):

          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.
     
  7. Sep 30, 2009 #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 (Text):

          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
     
  8. Sep 30, 2009 #7

    minger

    User Avatar
    Science Advisor

    I can't really understand what you're trying to do. Can you try to explain a little better?
     
  9. Sep 30, 2009 #8

    rcgldr

    User Avatar
    Homework Helper

    400 files takes 3 digits, so that format should be

    10 format('iso',i3.3,'.dat')
     
  10. Oct 1, 2009 #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
     
  11. Oct 1, 2009 #10

    minger

    User Avatar
    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 (Text):

          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
     
  12. Oct 1, 2009 #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 (Text):

          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
     
  13. Oct 5, 2009 #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 (Text):

    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
     
  14. Oct 5, 2009 #13

    minger

    User Avatar
    Science Advisor

    Rather than having your write statement inside your second DO loop, just put it after, e.g.
    Code (Text):

    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.
     
  15. Oct 7, 2009 #14
    Hi it works I got to write my vectors Y1 and Z1,Z2,Z3... with its elements, this is a result
    Code (Text):

      -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?
     
  16. Oct 7, 2009 #15

    minger

    User Avatar
    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.
     
  17. Oct 7, 2009 #16
    Yeah I did what you suggested me but I got this
    Code (Text):

      -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
     
  18. Jun 17, 2010 #17
    Thanks for your discussion which was really helpful to my work.... AND THANKS TO PHYSICS FORUMS
     
  19. Jul 5, 2010 #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
     
  20. Aug 11, 2010 #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')
     
  21. May 24, 2011 #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?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: FORTRAN, open several files and read it
  1. Read file in fortran (Replies: 2)

Loading...