|
forums.silverfrost.com Welcome to the Silverfrost forums
|
View previous topic :: View next topic |
Author |
Message |
pban92
Joined: 23 Nov 2009 Posts: 38
|
Posted: Thu Nov 25, 2010 3:30 pm Post subject: Writing output results into one file |
|
|
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,
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! |
|
Back to top |
|
|
JohnCampbell
Joined: 16 Feb 2006 Posts: 2554 Location: Sydney
|
Posted: Fri Nov 26, 2010 1:34 am Post subject: |
|
|
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:
integer*4, parameter :: max_values_per_set = 1000
integer*4, parameter :: max_sets_per_file = 200
integer*4, parameter :: max_files = 100
Real*8 values(max_values_per_set,max_sets_per_file,max_files)
Real*8 averages(max_sets_per_file,max_files)
Integer*4 num_values(max_sets_per_file,max_files)
character*128 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. |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|