-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to add date and step info when writing grib file with to_grib
#156
Comments
In general you should use arrays of |
Any news regarding this issue ? I'm stuck too ! Since datetime-like coordinates are always converted to |
This has been fixed in current xarray master, but it is not release yet. The best fix ATM is still to downgrade pandas to version 1.0.5 |
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 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 |
With xarray 0.16.2, I still have the same error.
Yes indeed. |
I just tested myself and the fix is not there yet. I'm surprised :( |
Still the same problem with pandas 1.3: $ conda list | grep -E 'python |pandas|cfgrib|xarray'
cfgrib 0.9.9.0 pyhd8ed1ab_1 conda-forge
pandas 1.3.0 py39hde0f152_0 conda-forge
python 3.9.6 h49503c6_1_cpython conda-forge
xarray 0.18.2 pyhd8ed1ab_0 conda-forge
The source of the problem seems to be: cfgrib/cfgrib/xarray_to_grib.py Line 222 in 843bf91
The call to However, I have no idea what further impacts this fix would have on other use cases. |
Well, reading grib2 files (NCEP GFS) with |
Thank you for your suggestion @honnorat . I have tried it with my local installation, but the tests fail, so we cannot use your change as is. I tried a few further modifications, but with no success so far. |
This issue should be fixed by #272. |
I followed the example in the README for writing a GRIB file from a simple Dataset
That was successful.
Now, I would like to specify the datetime to the grib2 file, but when I add the coordinate
time
,valid_time
, orstep
I get an error.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!
The text was updated successfully, but these errors were encountered: