NetCDF Fortran showing a wrong dimension of a Metgrid file

Miscellaneous or general questions regarding the WPS
Post Reply
lkugler
Posts: 16
Joined: Fri Aug 17, 2018 11:30 am
Location: Vienna

NetCDF Fortran showing a wrong dimension of a Metgrid file

Post by lkugler » Thu Nov 22, 2018 6:45 pm

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 [ucar.edu][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 https://stackoverflow.com/questions/470 ... sion-bound, 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.

Ncdump:

Code: Select all

    netcdf met_em.d01.2018-05-14_18\:00\:00 {
    dimensions:
            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 ;
    variables:
            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-4.2.1.1/lib -I/usr/local/netcdf-4.2.1.1/include/ -lnetcdff -lnetcdf

And it returned:

Code: Select all

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

Code: Select all

    ! http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
    ! Full documentation of the netCDF Fortran 90 API can be found at:
    ! http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90

    program simple_xy_rd
      use netcdf
      implicit none
    
      ! This is the name of the data file we will read.
      character (len = 34) :: FILE_NAME="met_em.d01.2018-11-01_12:00:00.nc"
    
      ! 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
    
      ALLOCATE(name(numDims),nx(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, "! "
    
    contains
      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
[1]: https://www.unidata.ucar.edu/software/n ... /programs/
Lukas Kugler, PhD student / University of Vienna

Ming Chen
Posts: 1577
Joined: Mon Apr 23, 2018 9:42 pm

Re: NetCDF Fortran showing a wrong dimension of a Metgrid file

Post by Ming Chen » Mon Nov 26, 2018 7:16 pm

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.
WRF Help Desk

Post Reply

Return to “Miscellaneous WPS”