Several ascii files, read, write, average?

In summary, the programmer wants to create 248 simple ascii files, each of which has the following format: One line header and a column of values. The column has 275648 values (rows). The target is to create a file with the average of the old files in the directory.
  • #1
Taiseer
3
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
  • #2
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
I do not understand your point. May you explain please, I am not that expert in programming
 
  • #4
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
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
I am sorry, but could you please write the code which do your suggestions?
 

FAQ: Several ascii files, read, write, average?

1. What is an ascii file?

An ascii file is a text file that contains only characters from the American Standard Code for Information Interchange (ASCII) character set. These files can be easily read and written by computers.

2. How do I read an ascii file?

To read an ascii file, you can use a programming language such as Python, Java, or C++, which have built-in functions for reading text files. These functions allow you to read the contents of the file and store them in a variable for further processing.

3. How do I write to an ascii file?

Similar to reading, you can use programming languages to write to an ascii file. These languages have functions that allow you to open a file for writing and specify the contents to be written. You can also use specialized software such as Microsoft Excel or Notepad to write to an ascii file.

4. What does it mean to average ascii files?

Averaging ascii files means taking the numerical data from multiple ascii files and finding the average value for each data point. This can be useful for analyzing data from different sources or performing statistical analysis.

5. Why use ascii files instead of other file types?

Ascii files are preferred for data storage and manipulation because they are human-readable, lightweight, and widely supported by different programming languages and software. They are also simple to edit and can be easily shared between different systems.

Similar threads

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