PDA

View Full Version : openning new files and write data into them


vjramana
Nov7-10, 01:29 AM
Hi,

I am trying to write a loop to open four main-data-files. At each open of main-data-file, I want the loop to open another four sub-files and split the data from mail-data-file according to the creteria and dump into the four files. And the process goes on. That means I will have total of 16 sub files for the four main-data-files.

I have tried to write the code as below but it doesnt seems to work.

************************************************** *************************************************
program final
implicit none
!
integer :: i,j, k, m, jj, kk
integer, parameter :: dist_lines=256
real, DIMENSION(dist_lines) :: dist

! main-data-files
open(unit=100,status="old",file="selected_MAXdistance_only_malto_THERMO_1st.dat")
open(unit=101,status="old",file="selected_MAXdistance_only_malto_THERMO_2nd.dat")
open(unit=102,status="old",file="selected_MAXdistance_only_malto_THERMO_3rd.dat")
open(unit=103,status="old",file="selected_MAXdistance_only_malto_THERMO_4th.dat")

do kk = 100,103

! sub-files
open(unit=(10+kk), file="maximum_dist_001_064.dat")
open(unit=(11+kk), file="maximum_dist_065_128.dat")
open(unit=(12+kk), file="maximum_dist_129_192.dat")
open(unit=(13+kk), file="maximum_dist_193_256.dat")

read(kk,'(F8.3)') (dist(i), i = 1,dist_lines)

do k = 1, 256

if (k < 65 ) then
write ((kk+10),'(F8.3)'), dist(k)
elseif ( k >=65 .and. k < 129 ) then
write( (kk+11),'(F8.3)'), dist(k)
elseif ( k >=129 .and. k < 193 ) then
write ((kk+12),'(F8.3)'), dist(k)
elseif ( k >=193 .and. k < 257 ) then
write ((kk+13),'(F8.3)'), dist(k)
end if


end do

end do

stop
end

Could anyone help ?
Thanks in advance.
Vijay

Mark44
Nov7-10, 01:34 PM
When you say it doesn't work, can you be more specific?

Does it fail to compile?

If it compiles, does the program give incorrect results?

vjramana
Nov7-10, 11:59 PM
It compiles. There is no problem. But when I execute it stops says error. The error indicates that the files (maximum_dist_001_064.dat) open ready.

That means in the first call of the main-data-file ( UNIT=100) it opens four files. Thay are

maximum_dist_001_064.dat
maximum_dist_065_128.dat
maximum_dist_129_192.dat
maximum_dist_193_256.dat

When it calles the second file (UNIT=101), it tries to open another four files in the same name. So it stopes. I dont know how to make the four files to generate new names for each main-data-calling.

For example,

When it called file UNIT=100, the new four files might look like,

maximum_dist_001_064_100.dat
maximum_dist_065_128_100.dat
maximum_dist_129_192_100.dat
maximum_dist_193_256_100.dat

and when it called the second main-data-file, it might name as below

maximum_dist_001_064_101.dat
maximum_dist_065_128_101.dat
maximum_dist_129_192_101.dat
maximum_dist_193_256_101.dat

This allow me to know that the file generated are from calling UNIT=100, UNIT=101 and so on..

Hope this explain correctly how this code suppose to work.
Thanks in advance.

minger
Nov8-10, 05:56 AM
Right now you're opening the same files over and over. Plus, since you're not CLOSE() those files, you can't reopen the same file using the same unit number.

do kk = 100,103

! sub-files
open(unit=(10+kk), file="maximum_dist_001_064.dat")


That line will open up the file maximum_dist_001_064.dat everytime through the loop. If you want to open up a different filename each time through the loop, you can WRITE to a variable such as:

DO kk=100,103
WRITE(fname1,'(A21,I3.3,A4)') 'maximum_dist_001_064_',kk,'.dat'
OPEN(21,file=fname,form='formatted')
. . . .
END DO

I'm not sure exactly what you're trying to do here, but it seems a little messed up in a few ways.