Scheduled Downtime
On Friday 21 April 2023 @ 5pm MT, this website will be down for maintenance and expected to return online the morning of 24 April 2023 at the latest

NetCDF Fortran showing a wrong dimension of a Metgrid file

This post was from a previous version of the WRF&MPAS-A Support Forum. New replies have been disabled and if you have follow up questions related to this post, then please start a new thread from the forum home page.


New member
Hi, has anyone of you experienced similar issues?

I tried to read a netcdf file produced by Metgrid with Fortran, extending a simple example from [][1] and noticed that it does not show the correct dimension names and lengths (from ncdump) of the file.
Specifically it says DateStrLen(19) is the third dimension besides south_north, west_east and Time; where it really is num_metgrid_levels or num_sm_levels.
Is "DateStrLen" really a dimension?

As it showed the correct results for the netcdf on this question, I suspect that the issue is related to the Metgrid-file. I need to add that the metgrid-file was created on another server from which I copied the file to read it with fortran.


    netcdf met_em.d01.2018-05-14_18\:00\:00 {
            Time = UNLIMITED ; // (1 currently)
            DateStrLen = 19 ;
            west_east = 499 ;
            south_north = 429 ;
            num_metgrid_levels = 43 ;
            num_sm_levels = 4 ;
            num_st_levels = 4 ;
            south_north_stag = 430 ;
            west_east_stag = 500 ;
            z-dimension0012 = 12 ;
            z-dimension0016 = 16 ;
            z-dimension0021 = 21 ;
            char Times(Time, DateStrLen) ;
            float PRES(Time, num_metgrid_levels, south_north, west_east) ;
                    PRES:FieldType = 104 ;
                    PRES:MemoryOrder = "XYZ" ;
                    PRES:units = "" ;
                    PRES:description = "" ;
                    PRES:stagger = "M" ;
                    PRES:sr_x = 1 ;
                    PRES:sr_y = 1 ;

I compiled with:

ifort read_landmask.f90 -L/usr/local/netcdf- -I/usr/local/netcdf- -lnetcdff -lnetcdf

And it returned:

    ....:~/SWI_to_SMOIS$ ./a.out 
               7           4
     Time                                                         1
     DateStrLen                                                  19
     west_east                                                  499
     south_north                                                429
     NetCDF: Start+count exceeds dimension bound

The code is

    ! Full documentation of the netCDF Fortran 90 API can be found at:

    program simple_xy_rd
      use netcdf
      implicit none
      ! This is the name of the data file we will read.
      character (len = 34) :: FILE_NAME=""
      ! dimension names and lengths
      integer, allocatable :: nx(:)
      CHARACTER(LEN=50), allocatable :: name(:)
      integer, dimension(nf90_max_var_dims) :: dimIDs
      ! This will be the netCDF ID for the file and data variable.
      integer :: ncid, varid, status, len, i
      ! Loop indexes, and error handling.
      integer :: x, y, z, numAtts, numDims
      real, allocatable :: data_in(:,:,:,:) !(NX,NY,NZ)
      ! Open the file. NF90_NOWRITE tells netCDF we want read-only access to
      ! the file.
      call check( nf90_open(TRIM(FILE_NAME), NF90_NOWRITE, ncid) )
      ! Get the varid of the data variable, based on its name.
      call check( nf90_inq_varid(ncid, "SM", varid) )
      call check(nf90_inquire_variable(ncid, varid, ndims = numDims, natts = numAtts))
      write(*,*) numAtts, numDims
      do i = 1, numDims, 1
        CALL check(nf90_inquire_dimension(ncid,i,name(i),nx(i)))
        write(*,*) name(i),nx(i)
      end do

      ALLOCATE(data_in(nx(1), nx(2), nx(3), nx(4)))
      ! Read the data.
      call check( nf90_get_var(ncid, varid, data_in))
      call check( nf90_close(ncid) )
      print *,"*** SUCCESS reading example file ", FILE_NAME, "! "
      subroutine check(status)
        integer, intent ( in) :: status
        if(status /= nf90_noerr) then
          print *, trim(nf90_strerror(status))
          stop "Stopped"
        end if
      end subroutine check
    end program simple_xy_rd

DateStrLen is the second dimension of the variable "Times", and it defines the length of "Times". It has nothing to do with other variables in met_em files.

Please double check your Fortran code. The output of ncdump met_em looks fine.