Problem with reopening nc file (fortran)

General scientific issues regarding ROMS

Moderators: arango, robertson

Post Reply
Message
Author
llc

Problem with reopening nc file (fortran)

#1 Unread post by llc »

I opened a nc file and closed it and read the data successfully with fortran. when I tried to reopen the same nc file and then the program crashed. Here is the code:

Code: Select all

	    status=nf_open('dysim.nc',nf_nowrite,ncid)
		if ( status/=nf_noerr ) write (*,*) nf_strerror(status) 
	   status=nf_inq(ncid,ndims,nvars,ngatts,recdim)  !recdim=id of unlimdimid
       if ( status/=nf_noerr ) write (*,*) nf_strerror(status)
	   
      do i=1,ndims 
         dimid=i
	      status=nf_inq_dim(ncid,dimid,dimnam(i),dimsiz(i))
	      if ( status/=nf_noerr ) write (*,*) nf_strerror(status)
         if (recdim.ne.-1) numrecs=dimsiz(recdim)
      end do
              do i=1,nvars 
         varid=i
         do j=1,nvatts(i)
	status=nf_inq_attname(ncid,varid,j,attnam(i,j))
	if ( status/=nf_noerr ) write (*,*) nf_strerror(status) 
	status=nf_inq_atttype(ncid,varid,attnam(i,j),attype(i,j))
	if ( status/=nf_noerr ) write (*,*) nf_strerror(status) 
	status=nf_inq_attlen(ncid,varid,attnam(i,j),attlen(i,j))
	if ( status/=nf_noerr ) write (*,*) nf_strerror(status) 
        end do
      end do 
           do i=1,nvars 
         varid=i
	status=nf_inq_var(ncid,varid,varnam(i),vartyp(i),nvdims(i),vdims(I),nvatts(I))
	if ( status/=nf_noerr ) write (*,*) nf_strerror(status)
	
         if (nvdims(i).ne.0) then 
            do k=1,nvdims(i)
               vvdims(i,k)=vdims(k) 
            end do
        endif  

      end do
            do i=1,nvars
         varid=i
         WRITE(6,*)vARID,NVARS,vartyp(varid)
         avt(i)=avartyp(vartyp(varid)) 
		 avtgetvar(i)=getvartyp(vartyp(varid))
      end do
   
        STATUS=NF_CLOSE(NCID)
  	    status=nf_open('dysim.nc',nf_nowrite,ncid)
		if ( status/=nf_noerr ) write (*,*) nf_strerror(status)
 
the problem is probably from " status=nf_inq_var(ncid,varid,varnam(i),vartyp(i),nvdims(i),vdims(I),nvatts(I))" because it was fine to reopen the nc file when I removed this code. Does any one have idea about the problem?

Thanks,

Liancong email: lluo@waikato.ac.nz

User avatar
kate
Posts: 4091
Joined: Wed Jul 02, 2003 5:29 pm
Location: CFOS/UAF, USA

Re: Problem with reopening nc file (fortran)

#2 Unread post by kate »

The code posted here should be changed to have the call to nf_inq_var before the j loop on nvatts(i) so that you know the value of nvatts(i). You don't show your variable declarations - hopefully arrays like nvatts is big enough for nvars. I didn't check the arguments to your nf_ function calls either, something you could double check.

Have you considered learning to use a debugger?

Post Reply