question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support to write a `xarray.Dataset` to a GRIB file.

See original GitHub issue

Due to the fact that the NetCDF data model is much more free and extensible than the GRIB one, it is not possible to write a generic xarray.Dataset to a GRIB file. The aim for cfgrib is to implement write support for a subset of carefully crafted datasets that fit the GRIB data model.

In particular the only coordinates that we target at the moment are the one returned by opening a GRIB with the cfgrib flavour of cfgrib.open_dataset, namely:

number, time, step, a vertical coordinate (isobaricInhPa, heightAboveGround, surface, etc), and the horizontal coordinates (for example latitude and longitude for a regular_ll grid type).

Note that all passed GRIB_ attributes are used to set keys in the output file, it is left to the user to ensure coherence among them.

Some of the keys are autodetected from the coordinates, namely:

Horizontal coordinates gridTypes:

  • regular: regular_ll and regular_gg
  • not target: projected: lambert, etc (can be controlled with GRIB_ attributes)
  • not target: reduced: reduced_ll and reduced_gg (can be controlled with GRIB_ attributes)

Vertical coordinates typeOfLevel:

  • single level: surface, meanSea, etc.
  • pressure: isobaricInhPa and isobaricInPa
  • other: hybrid

GRIB edition:

  • GRIB2
  • GRIB1

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
iainrussellcommented, Sep 21, 2018

@alexamici - thanks for the very quick response! I can confirm that the code in the master branch works perfectly for this file now, and I plotted the fields with metview to check that they look identical (they do)!

0reactions
zxdawncommented, Dec 8, 2022

Another error:

AttributeError: 'Dataset' object has no attribute 'to_grib'

Update

We should import to_grib using the newest name.

from cfgrib.xarray_to_grib import to_grib
to_grib(ds, 'out.grib')
Read more comments on GitHub >

github_iconTop Results From Across the Web

GRIB Data Example - Xarray
GRIB format is commonly used to disseminate atmospheric model data. With xarray and the cfgrib engine, GRIB data can easily be analyzed and...
Read more >
cfgrib: easy and efficient grib file access in xarray - ecmwf
Pros: xarray backend, conda. Cons: partial CDM support, Python 2-only, no. PyPI, read-only. Iris-grib. Pros: xarray conversion, read-write, ...
Read more >
Xarray (from grib file) to dataset - python - Stack Overflow
I need to have those data in a dataset format with 6 column : longitude, latitude, ID of the cell/point in the grib...
Read more >
cfgrib · PyPI
supports writing the index of a GRIB file to disk, to save a full-file scan on ... import xarray as xr >>> ds...
Read more >
Introduction to Xarray - Pythia Foundations
The most common way to create a Dataset object is to load data from a file (see below). Here, instead, we will create...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found