Several ascii files, read, write, average?

  • Thread starter Thread starter Taiseer
  • Start date Start date
  • Tags Tags
    Average files
AI Thread Summary
The discussion revolves around a user seeking assistance with a programming task involving the averaging of data from multiple ASCII files. The user has 744 files, each containing a header and a column of 275,648 values. The goal is to create 248 new files, where each new file contains the average of three corresponding old files, maintaining the same header and number of rows.The user provided a trial program written in Fortran, which attempts to read the input files and write the output files but lacks the complete logic for averaging the values. Suggestions include creating a batch file for Windows to automate the process and using a function to list files in a directory. There is a request for a more detailed code example to implement these suggestions effectively, indicating a need for clearer programming guidance.
Taiseer
Messages
3
Reaction score
0
Hi,

I have the following problem, can anyone 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
 
Technology news on Phys.org
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.
 
I do not understand your point. May you explain please, I am not that expert in programming
 
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.
 
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.
 
I am sorry, but could you please write the code which do your suggestions?
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
12
Views
3K
Replies
5
Views
5K
Replies
33
Views
5K
Replies
75
Views
6K
Replies
3
Views
3K
Replies
5
Views
2K
Replies
1
Views
1K
Back
Top