Several ascii files, read, write, average?

  • Thread starter Thread starter Taiseer
  • Start date Start date
  • Tags Tags
    Average files
Click For Summary

Discussion Overview

The discussion revolves around the problem of averaging multiple ASCII files in a programming context. Participants explore methods for reading, processing, and writing these files, with a focus on creating a new set of files that contain averaged data from the originals. The scope includes programming techniques and file handling, particularly in relation to Fortran and batch scripting.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • The original poster describes a need to average values from 744 ASCII files into 248 new files, detailing the format and structure of the data.
  • One participant suggests managing file names within the program or using a batch file to handle the averaging process.
  • Another participant proposes creating a batch file on Windows to run the averaging program multiple times, mentioning the use of the "dir /b" command.
  • A suggestion is made to create a function that retrieves a list of files from a directory and stores them in a string table for processing.
  • The original poster requests specific code examples to implement the suggestions provided by other participants.

Areas of Agreement / Disagreement

There is no consensus on the best approach to solve the problem, as participants offer different methods and suggestions without agreement on a single solution.

Contextual Notes

Participants have not provided detailed assumptions or constraints regarding the programming environment or specific requirements for the averaging process, leading to potential ambiguity in the suggested approaches.

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?
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 33 ·
2
Replies
33
Views
6K
  • · Replies 75 ·
3
Replies
75
Views
7K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
10
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K