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.

How to add date and step info when writing grib file with `to_grib`

See original GitHub issue

I followed the example in the README for writing a GRIB file from a simple Dataset

import xarray as xr
import cfgrib

ds2 = xr.Dataset({'skin_temperature': (('latitude', 'longitude'), np.zeros((5, 6)) + 300.)})
ds2.coords['latitude'] = np.linspace(90., -90., 5)
ds2.coords['longitude'] = np.linspace(0., 360., 6, endpoint=False)
ds2.skin_temperature.attrs['GRIB_shortName'] = 'skt'

cfgrib.to_grib(ds2, 'out.grib2', grib_keys={'edition': 2})

That was successful.

Now, I would like to specify the datetime to the grib2 file, but when I add the coordinate time, valid_time, or step I get an error.

import xarray as xr
import cfgrib
import pandas as pd

ds2 = xr.Dataset({'skin_temperature': (('latitude', 'longitude'), np.zeros((5, 6)) + 300.)})
ds2.coords['latitude'] = np.linspace(90., -90., 5)
ds2.coords['longitude'] = np.linspace(0., 360., 6, endpoint=False)
ds2.coords['time'] = pd.to_datetime('2017-10-01')            ### <----- Added this line
ds2.skin_temperature.attrs['GRIB_shortName'] = 'skt'
cfgrib.to_grib(ds2, 'out.grib2', grib_keys={'edition': 2})
KeyError: 1506816000000000000

I realize this is an Alpha capability, but can you please provide any additional information on passing time data when creating a simple grib file. Thanks!

Note: I have cfgrib 0.9.8.3

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
honnoratcommented, Nov 30, 2020

Here is my test case:

import cfgrib
import numpy as np
import xarray as xr

ds = xr.DataArray(
    np.zeros((1, 6, 6)), name="foo",
    dims=("time", "latitude", "longitude"),
    coords={
        "time": np.asarray(["2000-01-01"], dtype="datetime64[s]"),
        "latitude": np.linspace(0., 1., 6),
        "longitude": np.linspace(0., 1., 6)
    },
).to_dataset()
cfgrib.to_grib(ds.squeeze(), "test.grib2")

Works like a charm with pandas=1.0.5. With pandas>=1.1.0, I get:

KeyError                                  Traceback (most recent call last)
<ipython-input-3-1ce018954770> in <module>
----> 1 cfgrib.to_grib(ds.squeeze(), "test.grib2")

~/usr/conda/envs/dp_11/lib/python3.8/site-packages/cfgrib/xarray_to_grib.py in to_grib(*args, **kwargs)
    267 
    268 def to_grib(*args, **kwargs):
--> 269     return canonical_dataset_to_grib(*args, **kwargs)

~/usr/conda/envs/dp_11/lib/python3.8/site-packages/cfgrib/xarray_to_grib.py in canonical_dataset_to_grib(dataset, path, mode, no_warn, grib_keys, **kwargs)
    263     with open(path, mode=mode) as file:
    264         for data_var in dataset.data_vars.values():
--> 265             canonical_dataarray_to_grib(data_var, file, grib_keys=real_grib_keys, **kwargs)
    266 
    267 

~/usr/conda/envs/dp_11/lib/python3.8/site-packages/cfgrib/xarray_to_grib.py in canonical_dataarray_to_grib(data_var, file, grib_keys, default_grib_keys, **kwargs)
    222     for items in itertools.product(*header_coords_values):
    223         select = {n: v for n, v in zip(coords_names, items)}
--> 224         field_values = data_var.sel(**select).values.flat[:]
    225 
    226         # Missing values handling

~/usr/conda/envs/dp_11/lib/python3.8/site-packages/xarray/core/dataarray.py in sel(self, indexers, method, tolerance, drop, **indexers_kwargs)
   1141 
   1142         """
-> 1143         ds = self._to_temp_dataset().sel(
   1144             indexers=indexers,
   1145             drop=drop,

~/usr/conda/envs/dp_11/lib/python3.8/site-packages/xarray/core/dataset.py in sel(self, indexers, method, tolerance, drop, **indexers_kwargs)
   2103         """
   2104         indexers = either_dict_or_kwargs(indexers, indexers_kwargs, "sel")
-> 2105         pos_indexers, new_indexes = remap_label_indexers(
   2106             self, indexers=indexers, method=method, tolerance=tolerance
   2107         )

~/usr/conda/envs/dp_11/lib/python3.8/site-packages/xarray/core/coordinates.py in remap_label_indexers(obj, indexers, method, tolerance, **indexers_kwargs)
    395     }
    396 
--> 397     pos_indexers, new_indexes = indexing.remap_label_indexers(
    398         obj, v_indexers, method=method, tolerance=tolerance
    399     )

~/usr/conda/envs/dp_11/lib/python3.8/site-packages/xarray/core/indexing.py in remap_label_indexers(data_obj, indexers, method, tolerance)
    273             coords_dtype = data_obj.coords[dim].dtype
    274             label = maybe_cast_to_coords_dtype(label, coords_dtype)
--> 275             idxr, new_idx = convert_label_indexer(index, label, dim, method, tolerance)
    276             pos_indexers[dim] = idxr
    277             if new_idx is not None:

~/usr/conda/envs/dp_11/lib/python3.8/site-packages/xarray/core/indexing.py in convert_label_indexer(index, label, index_name, method, tolerance)
    194                 indexer = index.get_loc(label_value)
    195             else:
--> 196                 indexer = index.get_loc(label_value, method=method, tolerance=tolerance)
    197         elif label.dtype.kind == "b":
    198             indexer = label

~/usr/conda/envs/dp_11/lib/python3.8/site-packages/pandas/core/indexes/datetimes.py in get_loc(self, key, method, tolerance)
    620         else:
    621             # unrecognized type
--> 622             raise KeyError(key)
    623 
    624         try:

KeyError: 946684800000000000
1reaction
alexamicicommented, Jan 28, 2022

This issue should be fixed by #272.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tricks for wgrib2
3) Use the import_text option to read in new data values and use the -grib_out to write the values to a grib file....
Read more >
GRIB Files in the CDM | netCDF-Java Developer Guide
For simple cases, you can create the ncx4 file based on a collection spec using ToolsUI: IOSP/GRIB1(2)/GribCollection . Enter the collection ...
Read more >
Handling GRIB in GrADS - COLA/GMU
The next step is to create a descriptor file which describes the complete grid and the list of variables. The final step is...
Read more >
GRIB decoding - ECMWF Confluence Wiki
Staged migration of remaining GRIB 1 fields to GRIB 2 will follow ... Identify the parameter, date, time, forecast step and the grid...
Read more >
Coding or converting grib files - GIS Stack Exchange
There is so much library out there, that it is really a matter of preferences. The most complete, documented and up to date...
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