3
votes

I have written a code in Fortran to read a NetCDF file that has 4-d data [time, level,longitude,latitude]. However, my code yields an error

NetCDF: Start+count exceeds dimension bound

on any 4-d NetCDF file I am using. For example, the file at http://people.sc.fsu.edu/~jburkardt/f_src/netcdf/pres_temp_4D.nc has pressure and temperature. I paste my code below. Please suggest what is going wrong.

PROGRAM rw_nc4d_main

  USE rw_nc4d,   ONLY: read_nc4

  IMPLICIT NONE

  CHARACTER(LEN=50) :: ncfn
  CHARACTER(LEN=15) :: vname

  ncfn = 'pres_temp_4D.nc'
  vname = 'pressure'

  CALL read_nc4(ncfn, vname)    

END PROGRAM rw_nc4d_main
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
MODULE rw_nc4d

USE netcdf

IMPLICIT NONE

CONTAINS

  SUBROUTINE read_nc4(fname,vin_name)   

    IMPLICIT NONE

    CHARACTER(LEN=*), INTENT(IN)          :: fname
    CHARACTER(LEN=*), INTENT(IN)          :: vin_name

    ! Local variables
    INTEGER                  :: ncid, var_id, ndim, nvar, nattr, unlim_id
    CHARACTER(LEN=15)        :: dname
    INTEGER                  :: dlength
    INTEGER                  :: ii, status, lx, ly, lz, lt, lzp1 
    REAL                     :: sf, ofs
    REAL, DIMENSION(:,:,:,:), ALLOCATABLE :: vin

    CALL nc_check(nf90_open(fname, nf90_nowrite, ncid))
    CALL nc_check(nf90_inquire(ncid,ndim,nvar))

    DO ii = 1, ndim
      CALL nc_check(nf90_inquire_dimension(ncid,ii,dname,len=dlength))
          SELECT CASE(TRIM(dname))
          CASE('lon', 'LON', 'longitude')
            lx = dlength
          CASE('lat', 'LAT', 'latitude' )
            ly = dlength
          CASE('lev', 'LEV',  'level'  )
            lz = dlength
          CASE('time', 'TIME'           )
            lt = dlength
          CASE('ilev', 'ILEV')
            lzp1 = dlength
          CASE DEFAULT
            WRITE(*,*)'ERROR: nc_check for dimensions!'; STOP
        END SELECT
    END DO

    ALLOCATE(vin(lt,lz,ly,lx))

    CALL nc_check(nf90_inq_varid(ncid,TRIM(vin_name),var_id))
    CALL nc_check(nf90_get_var(ncid,var_id,vin,start=(/1,1,1,1/),count=(/lt,lz,ly,lx/)),fname=TRIM(fname))

 END SUBROUTINE read_nc4

  SUBROUTINE nc_check(status,fname)

    INTEGER, INTENT(IN) :: status
    CHARACTER(LEN=*), OPTIONAL :: fname

    IF (status /= nf90_noerr) THEN
      IF (PRESENT(fname)) THEN
        WRITE(*,*)'FATAL ERROR in ',TRIM(fname),' ',TRIM(nf90_strerror(status))
      ELSE
        WRITE(*,*)'FATAL ERROR: ',TRIM(nf90_strerror(status))
      END IF
      STOP
    END IF

  END SUBROUTINE nc_check

END MODULE rw_nc4d
1
Please use tag fortran for all Fortran questions to get more attention. Use the version tags (like fortran90) for version specific questions (likely not this one). - Vladimir F

1 Answers

6
votes

You have the dimensions back to front. I also suspect that your variable has the longitude and latitude in the reverse order to which you have posted. A variable with shape [time, level,latitude,longitude] should be declared as var(longitude, latitude, level, time) in Fortran.