[netcdfgroup] add/delete variables in an existing NetCDF file

Hey, 

I tried to add a new variable into an existing using a Fortran code (using the 
existing dimensions defined in the netcdf file). 
My fortran program was compiled and ran without errors, but the new variable 
doesn't exist in the NetCDF file (there is no changes of the netcdf file). 
Attached is my Fortran code. Can somebody send me an example Fortran code to 
add/delete variables into an existing NetCDF file.
Thank you very much for your help.

AJ

C xlf reformat_fvcom.f -I/usrx/local/bin/include -L/usrx/local/bin/lib -lnetcdf 
-o reformat_fvcom.x      include 'netcdf.inc'
      character*120 OFS,OCEAN_MODEL*10,COLD_START*10
      character*120 FIN,FOUT,GRIDFILE,FIXnos,netcdf_file
      character*120 BUFFER,CMD*132,VNAME,ANAME
      character*120 START_TIME, END_TIME
      CHARACTER globalstr(9)*120
      integer dimids(5),COUNT(5),DIMS(5),STATUS
      integer INTVAL(4),CORNER(4)
      LOGICAL FEXIST,CHANGE_TIME
      real*8 jday_start,jdaye,jbase_date,JULIAN,yearb,monthb,dayb,hourb
      real*8 jday,jday0
! temporary arrays
      real, allocatable :: tmp1d  (:)
      real, allocatable :: tmp2d  (:,:)
      real, allocatable :: tmp3d  (:,:,:)
      real, allocatable :: tmp4d  (:,:,:,:)
      real, allocatable :: tmp5d  (:,:,:,:,:)


      FIN='nGOM_0001.nc'
      STATUS = NF_OPEN(trim(FIN),NF_WRITE, NCID)
      VNAME='u'
      STATUS = NF_INQ_VARID(NCID,TRIM(VNAME),IDVAR)
      if (status .ne. NF_NOERR)then
             print *,'status=',status
             print *, nf_strerror(status)
             stop
      endif
         
      STATUS = NF_INQ_VARNDIMS(NCID,IDVAR,NDIMS)
      if (status .ne. NF_NOERR)then
             print *,'status=',status
             print *, nf_strerror(status)
             stop
      endif
      status =NF_INQ_VARDIMID(NCID,IDVAR,dimids)
      if (status .ne. NF_NOERR)then
             print *,'status=',status
             print *, nf_strerror(status)
             stop
      endif
      do i=1,NDIMS
            STATUS = NF_INQ_DIMLEN(NCID,dimids(i),DIMS(i))
            write(*,*) TRIM(VNAME),' dim ',i,' = ',DIMS(i)
      enddo
      INTVAL(1)=DIMS(1)
      INTVAL(2)=DIMS(3)
      
      STATUS = NF_REDEF(NCID)
      STATUS = NF_DEF_VAR(NCID,'u_surface',NF_REAL,2,INTVAL,IUID)
!        call check_err(status)
        status=nf_put_att_text(ncid, IUID,'long_name',40,
     1  'surface eastern u-momentum')
!        call check_err(status)
        status=nf_put_att_text(ncid, IUID,'units',14,
     1  'meter second-1')
!        call check_err(status)
        status=nf_put_att_text(ncid, IUID,'field',25,
     1  'u_sigma0, scalar, series')
!        call check_err(status)

      STATUS = NF_DEF_VAR(NCID,'v_surface',NF_REAL,2,INTVAL,IVID)
!        call check_err(status)
        status=nf_put_att_text(ncid, IVID,'long_name',40,
     1  'surface northern v-momentum')
!        call check_err(status)
        status=nf_put_att_text(ncid, IVID,'units',14,
     1  'meter second-1')
!        call check_err(status)
        status=nf_put_att_text(ncid, IVID,'field',25,
     1  'v_sigma0, scalar, series')
!        call check_err(status)
      STATUS = NF_ENDDEF(NCID)
      print *,'redine'
      IF (ALLOCATED(tmp2d)) DEALLOCATE(tmp2d)
      ALLOCATE(tmp2d(DIMS(1),DIMS(3)) )
      IF (ALLOCATED(tmp3d)) DEALLOCATE(tmp3d)
      ALLOCATE(tmp3d(DIMS(1),DIMS(2),DIMS(3)) )
      STATUS = NF_GET_VAR_REAL(NCID,IDVAR,tmp3d)
      Do I=1,DIMS(1)
      Do J=1,DIMS(3)
        tmp2d(I,J)=tmp3d(I,1,J)
      ENDDO
      ENDDO
      CORNER(1) = 1
      CORNER(2) = 1
      COUNT(1)=INTVAL(1)
      COUNT(2)=INTVAL(2)
      status=nf_put_vara_real(ncid,IUID,CORNER,COUNT,tmp2d)
        
      STATUS = NF_CLOSE(NCID)
      END 
      subroutine check_err(status)
      integer status
      include 'netcdf.inc'
      if (status .ne. NF_NOERR) then
      print *,'status=',status
      print *, nf_strerror(status)
      stop
      endif
      return
      end
     
          
  • 2010 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdfgroup archives: