 |
forums.silverfrost.com Welcome to the Silverfrost forums
|
View previous topic :: View next topic |
Author |
Message |
pban92
Joined: 23 Nov 2009 Posts: 38
|
Posted: Tue May 31, 2011 11:10 pm Post subject: Multiple columns in a single output file? |
|
|
I have 16 txt files each containing 10,000 data. Now I am trying to calculate average value of each file and put the results in one single file. The result file should have 3 columns and 8 rows. The first column with a predefined values (0.25,0.5 to 3.0) and two columns (2nd and 3rd) with results.
Following codes are just showing errors. (I did not attempt to add the predefined column to keep it simple for me). So any suggestion would be highly appreciated. Many thanks!
Code: | PROGRAM test
IMPLICIT NONE
INTEGER::n,seqnum,ierr
DOUBLE PRECISION,DIMENSION(11000000)::y
CHARACTER (LEN=11)::seqfilein
ierr = 0
seqfilein = 'caseXXX.txt'
DO seqnum = 1,16
WRITE(seqfilein(5:7),'(I3.3)')seqnum
WRITE(*,*)seqfilein
OPEN(UNIT=10,FILE=seqfilein,STATUS='OLD',ACTION='READ',IOSTAT=ierr)
CALL READDATA(y,n,seqfilein)
CALL MEAN(y,n,seqfilein)
ENDDO
CLOSE (UNIT = 10)
END
!&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
SUBROUTINE READDATA(y,n,seqfname)
IMPLICIT NONE
INTEGER::i,ierr
INTEGER,INTENT(out)::n
DOUBLE PRECISION,INTENT(INOUT),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, size(y)
READ(3,*,IOSTAT=ierr)y(i)
if (ierr /=0) exit
END DO
n=i-1
CLOSE (UNIT = 3)
RETURN
END SUBROUTINE
!$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
SUBROUTINE MEAN(y,n,seqfileout)
IMPLICIT NONE
INTEGER::i,j,ierr
DOUBLE PRECISION::sum1
INTEGER,INTENT(IN)::n
DOUBLE PRECISION,INTENT(IN),DIMENSION(n)::y
DOUBLE PRECISION,DIMENSION(96)::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 + y(i)
avg(i) = sum1/REAL(n)
END DO
DO i=1,16
DO j=1,8
OPEN(UNIT=9,FILE='s1c.avg',STATUS='REPLACE',ACTION='WRITE',IOSTAT=ierr)
WRITE(9,'(F5.2)')avg(j)
CLOSE(UNIT=9)
ENDDO
ENDDO
RETURN
END SUBROUTINE |
|
|
Back to top |
|
 |
JohnCampbell
Joined: 16 Feb 2006 Posts: 2615 Location: Sydney
|
Posted: Tue May 31, 2011 11:50 pm Post subject: |
|
|
What are you trying to do ?
Your calculation in MEAN does not look correct to me.
I'm not sure of your use of i and j, such as avg(i).
The average calculation could look like:
sum1 = 0
DO i = 1, n
sum1 = sum1 + y(i)
END DO
avg(seqnum) = sum1 / dble(n)
or even
avg(seqnum) = sum (y(1:n)) / dble(n)
This is the average of the "n" values you have read.
I then get confused by what your "avg(j)" is that you are writing. It appears to be the average of the first j values. Is this what you meant to do?
This has been around for a while, so if you could explain what it is you are trying to do I could help.
You appear to have 16 files of numbers.
You could calculate the average of the numbers in each file,
I thought you wanted to report these 16 averages to another file.
Your program does not do that.
A simpler approach could be.
0) get the list of file names
1) read all 16 files (or how many you have), retaining file name, number of values and the value list.
2) process the data, using the relationships you require.
3) finally, write out the results you want to the file or files you want.
By the indications you have given previously, the total amount of data is not excessive (at the moment). If it grows, then you could only retain sufficient info in step 1 that is required for steps 2 and 3.
It appears to me you are getting confused in the mean calculation and sepetrating out the file writing may make your tasks a bit clearer.
My apologies if I have misunderstood your problem.
John |
|
Back to top |
|
 |
pban92
Joined: 23 Nov 2009 Posts: 38
|
Posted: Wed Jun 01, 2011 12:43 am Post subject: |
|
|
Quote: | ....Your calculation in MEAN does not look correct to me. I'm not sure of your use of i and j, such as avg(i)..... |
I sure did not mean it that way. So fixed it as follows,
Code: | DO i = 1, n
sum1 = sum1 + y(i)
avg = sum1/REAL(n)
END DO |
Code: | ...I thought you wanted to report these 16 averages to another file.... |
An absolute yes. That 16 averages should go in two columns each containing 8 rows. I intend to have an extra column with predefined values like this (Col1 is predefined while Col2 and 3 are the calculated),
Col1 Col2 Col3
0.25 0.46 0.68
0.50 0.39 0.82
0.75 0.65 0.47
1.00 0.10 0.00
1.25 0.86 0.54
1.50 0.97 0.35
1.75 0.52 0.80
2.00 0.09 0.51
2.25 0.30 0.43
2.50 0.09 0.43
2.75 0.01 0.26
3.00 0.71 0.53
Quote: | ....I then get confused by what your "avg(j)" is that you are writing.... |
Here goes the bigger problem for me. I was just throwing stone into the dark actually. By j, I was trying to see if that corresponds to the "row number".
Quote: | ...By the indications you have given previously, the total amount of data is not excessive (at the moment).... |
Indeed true! I have millions of data and hundreds of data files to calculate average on. It has been getting too much to open hundreds of output files with a single calculated avg value. I just tried to start with a simpler problem.
Now I can calculate average for as many files I want at one go (each data file and write the result in an output file). But the number of output files have been equivalent to the number of data files. Opening each output file and transferring all values in a new single txt file are becoming too much of manual works! I hope the problem is better stated this time.
Finally, thanks a lot John! |
|
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
|