Silverfrost Forums

Welcome to our forums

Multifle file handling with different sequences

29 Apr 2011 5:32 #8151
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!

29 Apr 2011 8:21 #8152

Like this, you mean?

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
2 May 2011 3:54 #8163

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)

 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
Please login to reply.