Project

General

Profile

convert netcdf to grib2 error:

Added by Micheal Simpson about 2 months ago

I have a netcdf file with the following information when I run 'cdo sinfov file.netcdf'

File format : NetCDF4 classic
-1 : Institut Source T Steptype Levels Num Points Num Dtype : Parameter name
1 : unknown unknown c instant 1 1 1 1 F64 : ul_lat
2 : unknown unknown c instant 1 1 1 1 F64 : ul_lon
3 : unknown unknown c instant 1 1 1 1 F64 : lr_lat
4 : unknown unknown c instant 1 1 1 1 F64 : lr_lon
5 : unknown unknown c instant 1 1 1 1 F64 : fixed_angle
6 : unknown unknown c instant 1 1 1 1 F64 : grid_spacing
7 : unknown unknown c instant 1 1 24500000 2 F64 : CONUS_Mosaic_QPE
Grid coordinates :
1 : generic : points=1
2 : generic : points=24500000 (7000x3500)
Vertical coordinates :
1 : surface : levels=1
cdo sinfon: Processed 7 variables [0.01s 36MB].

I have seen several other forums with this issue, but mine is a little unique because I do not keep the exact coordinates in the netcdf file for every point in the 7000x3500 grid. Instead, I define the bounds of the grid, and then the 'grid_spacing', which has a value of 0.01 (crs = 4326).

When I try to convert this netcdf to a grib2, I get the error:
Error (gribapiDefGrid): Unsupported grid type: generic

Is there an easy way to be able to identify the bounds and the spacing of the coordinates to be able to output to a grib2 file?


Replies (7)

RE: convert netcdf to grib2 error: - Added by Estanislao Gavilan about 2 months ago

Hi Micheal,

you can set the horizontal grid with horizontal grid setgrid like this

cdo setgrid,grid.txt input.nc output.nc

the text file should be something like this #
  1. gridID 1 #
    gridtype = lonlat or projection etcc
    gridsize = 24500000
    xsize = 7000
    ysize = 3500
    xname = lon
    xlongname = "longitude"
    xunits = "degrees_east"
    yname = lat
    ylongname = "latitude"
    yunits = "degrees_north"
    xfirst = 137.671
    xinc = 0.01
    yfirst = 36.726
    yinc = 0.01

RE: convert netcdf to grib2 error: - Added by Micheal Simpson about 2 months ago

Thanks for the prompt response:

I seem to be getting an error with this:

Namelist error: Invalid contents in grid.txt!

cdo setgrid (Abort): Namelist error!

Below are the contents of grid.txt:

And the command I use is below:
cdo setgrid,grid.txt in.nc out.nc

RE: convert netcdf to grib2 error: - Added by Estanislao Gavilan about 2 months ago

Hi Michael

you need to choose lonlat or projection. That depends on your source grid

Kind regards,

Estanislao

RE: convert netcdf to grib2 error: - Added by Micheal Simpson about 2 months ago

Ah, yes.

I noticed I also needed to remove the first line 'gridID 1'

I then ran the grib2 command:
cdo -f grb2 copy out.nc out.grb2

with a few errors, likely because of non-standard naming fields:

ECCODES ERROR : concept: no match for shortName=ul_lat
ECCODES ERROR : concept: input handle edition=2, centre=ecmf
ECCODES ERROR : Please check the Parameter Database 'https://apps.ecmwf.int/codes/grib/param-db/'
!!! grib_set_string( grib_handle* h, "shortName", "ul_lat") !!!
Warning (gribapiDefParam): grib_api: No match for shortName=ul_lat
ECCODES ERROR : concept: no match for shortName=ul_lon
ECCODES ERROR : concept: input handle edition=2, centre=ecmf
ECCODES ERROR : Please check the Parameter Database 'https://apps.ecmwf.int/codes/grib/param-db/'
!!! grib_set_string( grib_handle* h, "shortName", "ul_lon") !!!
Warning (gribapiDefParam): grib_api: No match for shortName=ul_lon
ECCODES ERROR : concept: no match for shortName=lr_lat
ECCODES ERROR : concept: input handle edition=2, centre=ecmf
ECCODES ERROR : Please check the Parameter Database 'https://apps.ecmwf.int/codes/grib/param-db/'
!!! grib_set_string( grib_handle* h, "shortName", "lr_lat") !!!
Warning (gribapiDefParam): grib_api: No match for shortName=lr_lat
ECCODES ERROR : concept: no match for shortName=lr_lon
ECCODES ERROR : concept: input handle edition=2, centre=ecmf
ECCODES ERROR : Please check the Parameter Database 'https://apps.ecmwf.int/codes/grib/param-db/'
!!! grib_set_string( grib_handle* h, "shortName", "lr_lon") !!!
Warning (gribapiDefParam): grib_api: No match for shortName=lr_lon
ECCODES ERROR : concept: no match for shortName=fixed_angle
ECCODES ERROR : concept: input handle edition=2, centre=ecmf
ECCODES ERROR : Please check the Parameter Database 'https://apps.ecmwf.int/codes/grib/param-db/'
!!! grib_set_string( grib_handle* h, "shortName", "fixed_angle") !!!
Warning (gribapiDefParam): grib_api: No match for shortName=fixed_angle
ECCODES ERROR : concept: no match for shortName=grid_spacing
ECCODES ERROR : concept: input handle edition=2, centre=ecmf
ECCODES ERROR : Please check the Parameter Database 'https://apps.ecmwf.int/codes/grib/param-db/'
!!! grib_set_string( grib_handle* h, "shortName", "grid_spacing") !!!
Warning (gribapiDefParam): grib_api: No match for shortName=grid_spacing
ECCODES ERROR : concept: no match for shortName=Accum_60_minCONUS_Mosaic_QPE_latlon
ECCODES ERROR : concept: input handle edition=2, centre=ecmf
ECCODES ERROR : Please check the Parameter Database 'https://apps.ecmwf.int/codes/grib/param-db/'
!!! grib_set_string( grib_handle* h, "shortName", "Accum_60_minCONUS_Mosaic_QPE_latlon") !!!
Warning (gribapiDefParam): grib_api: No match for shortName=Accum_60_minCONUS_Mosaic_QPE_latlon
cdo copy: Processed 24500006 values from 7 variables over 1 timestep [0.72s 651MB].

But I still get a .grib2 file with multiple 'numberOfPoints' that can cause errors in some downstream applications. There are workarounds, but this seems to be a great start, and appreciate the help.

RE: convert netcdf to grib2 error: - Added by Estanislao Gavilan about 2 months ago

Hi Micheal,

yes your file uses a very specific format. That will cause problems with cdo. The best is to edit the file to match the cf-convection. You could try to select the variables that your are interested. After all, you are going to specify the grid so you do not need those ulon ulat variables...

RE: convert netcdf to grib2 error: - Added by Ralf Mueller about 2 months ago

hi!

the problems is, that in GRIB1 and GRIB2 field identifiers (like shortnames) are NOT free to choose like in netcdf. GRIB is not only a file format but also a meta-data standard like the CF-convention for netcdf.

Hence a conversion from netcdf to grib involves a lookup for shortnames or related CF-attributes within the allowed names defined in the GRIB2 standard. If this cannot by done (like for lr_lon) the conversion cannot be done.

I regard this more a problem of input data. Any reasonable conversion to GRIB2 should fail with this input, sry.

cheers
ralf

RE: convert netcdf to grib2 error: - Added by Ralf Mueller about 2 months ago

GRIB never saves the coordinate values with the data. instead it used grid description with which tools can re-create the coordinates if needed. u have to check the grib2 standard, if your grid is supported. Maybe you have to interpolate into another grid before converting to GRIB.

which leads to the question: Why do you want to convert to grib?

    (1-7/7)