Fortran 90 - Attempt to read past end of file

此生再无相见时 提交于 2021-02-20 16:56:07

问题


I am having a read issue in Fortran 90. I am attempting to read 31488 rows of data. I am using the Portland Group Fortran 90 compiler.

My error message is this:

PGFIO-F-217/list-directed read/unit=14/attempt to read past end of file. File name = /import/c/w/username/WRFV3/SKILLSETS/Overestimations.txt formatted, sequential access record = 31489

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

I have attached the relevant part of the code... I have searched high and low through this part of the code, I have investigated the text file to see if the number of rows match. I absolutely do not see where the problem lies.

The compiler states that the error is located in the read statement... at read(14,*), that line of the code, within the do statements.

Please help. Thank you very much.

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)

回答1:


Generally, we would need to see the data file in order to determine why you get the error. List-directed input is very susceptible to mistakes made a long ways away from where the error is detected. For example, the error is reported at record 31489, but maybe record 7233 had one too few values on the line - with list-directed, it would automatically read the next record to pick up the missing value and then discard the rest of that new line. Then when it gets to the last record, it wants one more and.. error!

I am fairly confident that the problem is in the data file, not the program source. You should add some verification to make sure it is actually reading the values you want. Depending on how your data file is formatted, I might recommend using formatted input with G format rather than list-directed. I have seen far too many programmers led astray by list-directed input (and output).




回答2:


As a fix you can use the Fortran equivalent to reading to the end of a 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 in Fortran there are some constructs I've yet to find a way around using one.



来源:https://stackoverflow.com/questions/18003610/fortran-90-attempt-to-read-past-end-of-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!