 |
forums.silverfrost.com Welcome to the Silverfrost forums
|
View previous topic :: View next topic |
Author |
Message |
pban92
Joined: 23 Nov 2009 Posts: 38
|
Posted: Fri Apr 29, 2011 6:32 pm Post subject: Multifle file handling with different sequences |
|
|
Code: |
DO seqnum = 1,857,8
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(nvdata,avg,n,seqfilein)
ENDDO
DO seqnum = 866,942,1
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(nvdata,avg,n,seqfilein)
ENDDO |
The above code works with two different sequences, 1 to 857 and 866 to 942. Is there any efficient way to write the same code?
Many thanks! |
|
Back to top |
|
 |
sparge
Joined: 11 Apr 2005 Posts: 371
|
Posted: Fri Apr 29, 2011 9:21 pm Post subject: |
|
|
Like this, you mean?
Code: |
integer, parameter :: sets = 2
integer set, f(sets), l(sets)
f(1)=1
f(2)=866
l(1)=857
l(2)=942
do set=1, sets, 1
DO seqnum = f(set),l(set),1
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(nvdata,avg,n,seqfilein)
ENDDO
enddo
|
|
|
Back to top |
|
 |
JohnCampbell
Joined: 16 Feb 2006 Posts: 2615 Location: Sydney
|
Posted: Mon May 02, 2011 4:54 am Post subject: |
|
|
You could replace the file opening with a function, which returns an error if there is a problem with the file name etc. The following could be a more robust approach. I have made some assumptions to compile. (The write statement in the function is possibly non-standard)
Code: | integer*4 seqnum, n
character seqfilein*20
real*8 vdata(1000), avg
integer*4 open_num_file
external open_num_file
!
DO seqnum = 1,942
if (seqnum < 866 .and. mod(seqnum,8) /= 1) cycle
if ( open_num_file (seqnum,seqfilein) /= 0) cycle
CALL READDATA (vdata,n,seqfilein)
CALL MEAN (vdata,avg,n,seqfilein) ! nvdata or vdata ?
ENDDO
!
end
integer*4 function open_num_file (seqnum, seqfilein)
integer*4 seqnum
character seqfilein*(*)
!
integer*4 ierr
character message*80
!
WRITE (seqfilein(5:7),'(I3.3)') seqnum
WRITE (*,*) seqfilein
close (UNIT=10, IOSTAT=ierr)
OPEN (UNIT=10, FILE=seqfilein, STATUS='OLD', ACTION='READ', IOSTAT=ierr)
if (ierr /= 0) then
CALL FORTRAN_ERROR_MESSAGE@ (ierr, MESSAGE)
write (*,*) 'Error opening ', seqfilein,' : ', trim(message)
end if
open_num_file = ierr
return
end function open_num_file
|
|
|
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
|