Several ascii files, read, write, average?

  • Thread starter Taiseer
  • Start date
  • #1
3
0
Hi,

I have the following problem, can any one help?

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), and so on.
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
 

Answers and Replies

  • #2
rcgldr
Homework Helper
8,756
555
You can deal with the list of file names within the program, or you can write a simple averaging program and deal with the list of files with a batch (or script) file.
 
  • #3
3
0
I do not understand your point. May you explain please, I am not that expert in programming
 
  • #4
rcgldr
Homework Helper
8,756
555
If you're running this on some version of windows, you can create and edit a batch file to run the program 248 times to average the files. You could use the "dir /b" command to create the batchfile, then edit that file.
 
  • #5
chiro
Science Advisor
4,790
132
Create a function that gets a list of files in a directory and puts it in a string table.

Then open the files in a loop and do what you need to do.
 
  • #6
3
0
I am sorry, but could you please write the code which do your suggestions?
 

Related Threads on Several ascii files, read, write, average?

Replies
20
Views
33K
  • Last Post
Replies
2
Views
3K
  • Last Post
Replies
1
Views
2K
Replies
4
Views
12K
  • Last Post
Replies
1
Views
2K
Replies
2
Views
196
Replies
3
Views
2K
Top