reading and writing several files in fortran


Taiseer
05-04-2011, 12:20 PM
Hello,

I have 300 files (simple ascii) which I want to read and make some precessing. The files have the name (temp.time.1.sa to temp.time.300.sa). Each files have a one-line header.

How can I read these files and write them to a new set of files (also sa) after doing some maths on them.

Thanks

davekw7x
05-04-2011, 05:28 PM
...
How can I ...

You can use strings constructed as "internal files" to build the file names:


program testInternalFiles

implicit none
integer i
integer ios

integer, parameter:: inFileUnit = 8
integer, parameter:: outFileUnit = 9

! Buffers to hold file names
character(80) inputFileName
character(80) outputFileName

do i = 1, 300

!Use internal files to convert integers to string values
! and embed it in the file names
write (inputFileName, '(a, I0, a)') 'temp.time.',i,'.sa'
write (outputFileName, '(a, I0, a)') 'temp.time.out',i,'.sa'


open(inFileUnit, file=inputFileName,status="old", iostat=ios)
if (ios /= 0 ) then
write(*, '("Can''t open file ", a, " for reading.")') &
trim(inputFileName)
stop
endif

write(*, '("Opened file ", a, " for reading.")') &
trim(inputFilename)

!
! It will bail out rather than overwrite an existing file.
!
open(outFileUnit, file=outputFileName,status="new", iostat=ios)
if (ios /= 0) then
write(*, '("Can''t open file ", a, " for writing.")') &
trim(outputFileName)
stop
endif

write(*, '("Opened file ", a, " for writing.")') &
trim(outputFilename)

! Here's where you read stuff from the input file
! and write stuff to the output file.

! Close files before next pass through the loop
write(*, '("Closing files.")')
close(inFileUnit)
close(outFileUnit)
write(*,*)
enddo

end program testInternalFiles


With a couple of files in the directory, the output (after compiling with GNU gfortran 4.1.2) is
__________________________________________

Opened file temp.time.1.sa for reading.
Opened file temp.time.out1.sa for writing.
Closing files.

Opened file temp.time.2.sa for reading.
Opened file temp.time.out2.sa for writing.
Closing files.

Can't open file temp.time.3.sa for reading.
__________________________________________


Regards,

Dave

michaelgmaloy
05-18-2011, 02:45 PM
thanx,
i'll use that code too

Taiseer
06-05-2011, 01:34 PM
thank you very much,

but I am still not able to make processes in between and to write the results in the new files.

my original files contains a one line header and a one columns of values (there are 250 values). I want to read these values and to average them in to have one new value (the average) of each 5 values.

thanks

Taiseer
06-09-2011, 08:00 PM
I tried with your help, but I am still not able to solve my specific problem which is:
I have 744 simple ascii files. Each of them has the following format: One line header and a column of values. The column has 275648 values (rows).

The target is to create 248 simple ascii files (744/3). Each of the new files has the following format:
The same header as in the old files and the same number of rows (275648). But the first new file contains the average of the old files (file1, file2 and file3). The second new files will contain the average of the three old files (file4, file5, and file6).

How can i do this, below is my trial.

*****************************
program testInternalFiles
implicit none
integer i
integer ios

integer, parameter:: inFileUnit = 8
integer, parameter:: outFileUnit = 9

! Buffers to hold file names
character(80) inputFileName
character(80) outputFileName
character(180) skip

do i=1,248
jj=3*i
j=3*i-2
do k=j,jj
!Use internal files to convert integers to string values
! and embed it in the file names
write (inputFileName, '(a, I0, a)') 'NLDAS.VGRD.',k,'.sa'
write (outputFileName, '(a, I0, a)') 'NLDAS.VGRD.',i,'.dat'

open(inFileUnit, file=inputFileName,status="old", iostat=ios)
if (ios /= 0 ) then
write(*, '("Can''t open file ", a, " for reading.")') &
trim(inputFileName)
stop
endif

write(*, '("Opened file ", a, " for reading.")') &
trim(inputFilename)

!
! It will bail out rather than overwrite an existing file.
!
open(outFileUnit, file=outputFileName,status="new", iostat=ios)
if (ios /= 0) then
write(*, '("Can''t open file ", a, " for writing.")') &
trim(outputFileName)
stop
endif

write(*, '("Opened file ", a, " for writing.")') &
trim(outputFilename)
! Here's where you read stuff from the input file
! and write stuff to the output file.

read(inFileUnit,*) skip
read(inFileUnit,*) x
y=
write(outFileUnit,*) skip

! Close files before next pass through the loop
write(*, '("Closing files.")')
close(inFileUnit)
close(outFileUnit)
write(*,*)
enddo

end program testInternalFiles
*****************************************

thanks