Skip to content

GRIB: "Error while packing" when writing small rasters with nodata #15428

Open

Description

@Dukestep

What is the bug?

Writing a small raster to GRIB2 fails with ERROR 1: Error while packing when the raster has a nodata value. We noticed it when cutting small subsets out of a bigger GRIB2 file. Bigger subsets of the same data write fine, only the small ones fail (around 10x10 pixels or less in our tests). This was first identified when trying to use rasterio to write a a subset of a GRIB file in a Python-based backend. Replicated below purely using GDAL.

Steps to reproduce the issue

cat > small.asc <<'EOF'
ncols 6
nrows 6
xllcorner -145.5
yllcorner 49
cellsize 1
NODATA_value 9999
9999 10.44 10.59 10.72 10.88 11.04
9999 10.56 10.73 10.89 11.06 11.25
9999 10.76 10.94 11.11 11.29 11.48
9999 11.02 11.17 11.33 11.50 11.69
9999 11.27 11.40 11.55 11.75 11.97
9999 11.49 11.67 11.85 12.08 12.31
EOF

gdal_translate -a_srs EPSG:4326 -ot Float64 -of GRIB small.asc out.grib2
ERROR 1: Error while packing

With -co DATA_ENCODING=SIMPLE_PACKING it works but the nodata is not preserved.

Versions and provenance

Issue is replicated with latest version of GDAL 3.13.3 on Ubuntu 22.04 (via conda-forge).

Additional context

From what I can tell the default encoding switches to COMPLEX_PACKING when there is a nodata value and that is the one that fails on small grids.

Activity

  1. self-assigned this
    on Oct 22, 2026
  2. dbaston commented on Oct 22, 2026

    @dbaston
    Member

    pytest reproducer:

    def test_grib_small_raster_nodata(tmp_vsimem):
    
        tmpfilename = tmp_vsimem / "out.grb2"
    
        ds = gdal.GetDriverByName("MEM").Create("", 10, 10, eType=gdal.GDT_Float64)
        ds.SetGeoTransform([2, 1, 0, 49, 0, -1])
        ds.SetSpatialRef(osr.SpatialReference("WGS84"))
        ds.GetRasterBand(1).SetNoDataValue(9999)
    
        data = array.array('d', [9999 if i % 10 == 0 else 10 + i/10 for i in range(100)])
        ds.WriteRaster(0, 0, 10, 10, data)
        
        gdal.Translate(tmpfilename, ds)
    

    Failure appears to propagate outward from

    Upstream version of that file is
    https://github.com/NOAA-EMC/NCEPLIBS-g2c/blob/6542c47cef1eb177c90400eacbc0e82ab2d79520/src/reduce.c

    but nothing in git history suggests substantive changes.

  3. removed their assignment
    on Oct 22, 2026
  4. jratike80 commented on Oct 22, 2026

    @jratike80
    Collaborator

    Do you know it the GRIB format supports complex packing of so small files https://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_doc/grib2_temp5-2.shtml and can the DeGRIB library handle them?
    Are 10x10 sized GRIB2 files common in some context?

  5. Dukestep commented on Oct 22, 2026

    @Dukestep
    Author

    As far as I can see the spec doesn't set a minimum grid size for templates 5.2/5.3. Small files like this are pretty normal for us since we serve GRIB2 subsets from an OGC API - Coverages endpoint where a user asking for a small bbox on a 1-degree global model (Canadian Seasonal and Interannual Prediction System [CanSIPS]) gets back a grid of only a few pixels. The idea here being that a user may subset the source data only on a specific area of interest and receive it in its native format. In this particular example, due to the rather coarse resolution of the model, sometimes those subset requests can return rather small outputs.

  6. jratike80 commented on Oct 22, 2026

    @jratike80
    Collaborator

    This talks about packing and nodata, could there be something interesting https://github.com/OSGeo/gdal/pull/1063/changes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    not for AI loversSee https://gdal.org/en/stable/community/ai_tool_policy.html

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions