Fortran 90 - attempt to read past end of file

I have a reading problem in Fortran 90. I am trying to read 31488 rows of data. I am using the Portland Group Fortran 90 compiler.

My error message:

PGFIO-F-217 / list-direct read / unit = 14 / try to read the last end of the file. File name = / import / c / w / username / WRFV3 / SKILLSETS / Overestimations.txt in the format, sequential access record = 31489

The Fortran program thinks I have an extra line. I do not know where this is indicated in the code.

I attached the corresponding part of the code ... I searched high and low through this part of the code, I examined the text file to find out if the number of lines matches. I absolutely do not see where the problem is.

The compiler claims that the error is in the read statement ... when reading (14, *) this line of code in the do statement.

Please, help. Many thanks.

Program skillruss ! Purpose: to calculate skill scores implicit none integer :: i,j,nsite,ntime,iref,jj,csite ! nsite = number of observation sites, csites = number of chemical sites, ntime = number of hours parameter(nsite=32,csite=1,ntime=984) real :: Tob(nsite,ntime),RHo(nsite,ntime),diro(nsite,ntime) real :: raino(nsite,ntime),swo(nsite,ntime),po(nsite,ntime) real :: Tdo(nsite,ntime),vo(nsite,ntime) real :: Ts(nsite,ntime),RHs(nsite,ntime),dirs(nsite,ntime) real :: rains(nsite,ntime),sws(nsite,ntime),ps(nsite,ntime) real :: Tds(nsite,ntime),vs(nsite,ntime) real :: PMo(csite,ntime),PMs(csite,ntime) real :: pers(csite,ntime) real :: bias,rmse,sde,r,x,y,sx,sy,dw,isig real :: countn real :: nrmse,fac2,nstdev,mg,fb,nmse real :: biast(ntime),rmset(ntime),sdet(ntime) real :: rt(ntime),xt(ntime),yt(ntime) real :: sxt(ntime),syt(ntime),isigt(ntime),countt(ntime),dt(ntime) ! Open file to read the observational data open(14,file=& "/import/c/w/username/WRFV3/SKILLSETS/Overestimations.txt",& form="formatted",status="old") Tob= -999. RHo= -999. vo= -999. diro= -999. raino= -999. swo= -999. po= -999. Tdo= -999. do i=1,nsite do j=1,ntime read(14,*) Tob(i,j),RHo(i,j),vo(i,j),diro(i,j),raino(i,j),swo(i,j),& po(i,j),Tdo(i,j) if(vo(i,j) <=0.)diro(i,j)=-999. end do end do close(14) 
+4
source share
2 answers

Typically, we will need to see a data file to determine why you are getting an error. List-oriented entry is very susceptible to errors made away from where the error was found. For example, an error is reported on record 31489, but perhaps record 7233 had too few values ​​in a row - with a list, it automatically reads the next record to get the missing value, and then discard the rest of this new line. Then, when he gets to the last record, he wants one more and ... mistake!

I am pretty sure that the problem is in the data file and not in the source of the program. You need to add some validation to make sure that it really reads the desired values. Depending on how the data file is formatted, I might recommend using formatted input with a G-format rather than a list. I saw that too many programmers were confused with list-oriented input (and output).

+7
source

As a fix, you can use the Fortran equivalent to read to the end of the file.

 do i=1,nsite do j=1,ntime read(14,*, end=10)Tob(i,j),RHo(i,j),vo(i,j),diro(i,j),& raino(i,j),swo(i,j),po(i,j),Tdo(i,j) if(vo(i,j) <=0.)diro(i,j)=-999. end do end do 10 continue 

As a rule, I generally try to avoid goto statements, but there are some constructs in Fortran that I have yet to find using one of them.

0
source

Source: https://habr.com/ru/post/1494822/


All Articles