Silverfrost Forums

Welcome to our forums

Writing output results into one file

25 Nov 2010 2:30 #7156

Hello all,

Here is my program which is taking multiple input files and producing same number of output files. For example, I have 50 input files, the program is calculating the mean of the data the input file has and giving me 50 output files. This is the code,

PROGRAM test
IMPLICIT NONE

INTEGER::n,seqnum,ierr
DOUBLE PRECISION::avg
DOUBLE PRECISION,DIMENSION(1048576)::vdata

CHARACTER (LEN=11)::seqfilein

n = 1048576							

ierr = 0														
seqfilein = 'caseXXX.txt'					

DO seqnum = 1,50							
WRITE(seqfilein(5:7),'(I3.3)') seqnum	
WRITE(*,*) seqfilein 
OPEN (UNIT=10,FILE=seqfilein, STATUS='OLD', ACTION='READ', IOSTAT=ierr) 

CALL READDATA(vdata,n,seqfilein)
CALL MEAN(vdata,avg,n,seqfilein)

ENDDO
STOP
END 
!&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
SUBROUTINE READDATA(y,n,seqfname)
	IMPLICIT NONE
	INTEGER::i,ierr
    REAL::xdummy
	INTEGER,INTENT(IN)::n 
	DOUBLE PRECISION,INTENT(OUT),DIMENSION(n)::y
	CHARACTER(LEN=11),INTENT(IN)::seqfname
    
	ierr = 0
	OPEN (UNIT=3, FILE=seqfname, STATUS='OLD', ACTION='READ', IOSTAT=ierr) 
		DO i = 1, n
			READ (3,*,IOSTAT=ierr) xdummy, y(i)  
		END DO
	CLOSE (UNIT = 3)
    RETURN
END SUBROUTINE

!$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
SUBROUTINE MEAN(vdata,avg,n,seqfileout)
	IMPLICIT NONE
	INTEGER :: i, ierr
	DOUBLE PRECISION :: sum1
	INTEGER, INTENT(IN) :: n
	DOUBLE PRECISION, INTENT(IN), DIMENSION(n) :: vdata
	DOUBLE PRECISION, INTENT(OUT) :: avg
    CHARACTER(LEN=11),INTENT(IN)::seqfileout
    CHARACTER (LEN=11)::seqfout   
	ierr = 0
	seqfout = seqfileout(1:7)	
	sum1 = 0

	DO i = 1, n
	sum1 = sum1 + vdata(i)
	END DO
	avg = sum1/REAL(n)
OPEN (UNIT=9, FILE=trim(seqfout)//'.avg', STATUS='REPLACE', ACTION='WRITE', IOSTAT=ierr) 
WRITE(9,'(F5.2)') avg
CLOSE (UNIT = 9)
RETURN
END SUBROUTINE
!$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

Since the mean is a single value, it is rather not efficient to open 50 files to get 50 mean values. I have been trying to group the output files. That is, my program will calculate first 10 mean values and put them in one file, then another 10 values in another file and thus I would get only 5 files rather than 50 files. (I hope I could explain about what I am trying to do).

Not sure how to proceed. Any suggestion would be appreciated.

Many thanks!

26 Nov 2010 12:34 #7160

Why not seperate the reading, averageing and writing processes entirely. then manage the writing process as a final problem to solve, based on the number of files. (why more than 1?) The following data structure could make this relatively easy to achieve:

integer4, parameter :: max_values_per_set = 1000 integer4, parameter :: max_sets_per_file = 200 integer*4, parameter :: max_files = 100

Real8 values(max_values_per_set,max_sets_per_file,max_files) Real8 averages(max_sets_per_file,max_files) Integer4 num_values(max_sets_per_file,max_files) character128 data_file_names(max_files) integer*4 num_files

call read_all_files call calculate_averages call manage_write_results

memory usage is about 160mb (1000x200x100x8) in this simple approach, which is not excessive.

Please login to reply.