admin管理员组

文章数量:1405392

I have a grib file containing reanalysis (an), ensemble mean (em), and ensemble spread (es) data. The dataset I access is the analysis data,

import xarray as xr
import cfgrib
file = 'C:/Users/Downloads/04292020_rean_ensmean_ensspread.grib'
ds = xr.open_dataset(file,engine = "cfgrib",filter_by_keys={'dataType': 'an'}) # an accesses analysis data

time = 5;
res = 2; #2 = half degree
[phi,theta] = np.meshgrid(ds.variables['longitude'][::res].values,ds.variables['latitude'][::res].values)
divergence = ds.variables['d'][time,:,::res,::res].values

I'm able to access longitude and latitude values just fine, but if I try to access one of the field variable's values like divergence, it returns this error every time. Here's the head data assuming it's useful:

# <bound method Dataset.head of <xarray.Dataset> Size: 20GB
# Dimensions:        (time: 16, isobaricInhPa: 23, latitude: 721, longitude: 1440)
# Coordinates:
#     number         int32 4B ...
#   * time           (time) datetime64[ns] 128B 2020-04-29 ... 2020-04-29T23:00:00
#     step           timedelta64[ns] 8B ...
#   * isobaricInhPa  (isobaricInhPa) float64 184B 1e+03 975.0 ... 225.0 200.0
#   * latitude       (latitude) float64 6kB 90.0 89.75 89.5 ... -89.5 -89.75 -90.0
#   * longitude      (longitude) float64 12kB 0.0 0.25 0.5 ... 359.2 359.5 359.8
#     valid_time     (time) datetime64[ns] 128B ...
# Data variables: (12/13)
#     d              (time, isobaricInhPa, latitude, longitude) float32 2GB ...
#     cc             (time, isobaricInhPa, latitude, longitude) float32 2GB ...
#     z              (time, isobaricInhPa, latitude, longitude) float32 2GB ...
#     pv             (time, isobaricInhPa, latitude, longitude) float32 2GB ...
#     r              (time, isobaricInhPa, latitude, longitude) float32 2GB ...
#     clwc           (time, isobaricInhPa, latitude, longitude) float32 2GB ...
#             ...
#     crwc           (time, isobaricInhPa, latitude, longitude) float32 2GB ...
#     t              (time, isobaricInhPa, latitude, longitude) float32 2GB ...
#     u              (time, isobaricInhPa, latitude, longitude) float32 2GB ...
#     v              (time, isobaricInhPa, latitude, longitude) float32 2GB ...
#     w              (time, isobaricInhPa, latitude, longitude) float32 2GB ...
#     vo             (time, isobaricInhPa, latitude, longitude) float32 2GB ...
# Attributes:
#     GRIB_edition:            1
#     GRIB_centre:             ecmf
#     GRIB_centreDescription:  European Centre for Medium-Range Weather Forecasts
#     GRIB_subCentre:          0
#     Conventions:             CF-1.7
#     institution:             European Centre for Medium-Range Weather Forecasts
#     history:                 2025-03-23T18:13 GRIB to CDM+CF via cfgrib-0.9.1...>

EDIT: It actually works for time indices 0, 1, and 2. Anything past 2, and it fails with [Errno 22]. I've downloaded this data twice now on suspicion that the other dataset was corrupted, but it's still returning the same error.

EDIT: I went to check again today, and all of the sudden times 0-12 work now. Last night it was only times 0-2. Times 13-48 are still returning this error. I have no idea how, I didn't do anything to the data since last night. More of the data just randomly became accessible today.


Here's the full traceback:

divergence = ds.isel(time=13).d.data
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
Cell In[30], line 1
----> 1 divergence = ds.isel(time=13).d.data

File ~\anaconda3\Lib\site-packages\xarray\core\dataarray.py:795, in DataArray.data(self)
    783 @property
    784 def data(self) -> Any:
    785     """
    786     The DataArray's data as an array. The underlying array type
    787     (e.g. dask, sparse, pint) is preserved.
   (...)
    793     DataArray.values
    794     """
--> 795     return self.variable.data

File ~\anaconda3\Lib\site-packages\xarray\core\variable.py:474, in Variable.data(self)
    472     return self._data
    473 elif isinstance(self._data, indexing.ExplicitlyIndexed):
--> 474     return self._data.get_duck_array()
    475 else:
    476     return self.values

File ~\anaconda3\Lib\site-packages\xarray\core\indexing.py:840, in MemoryCachedArray.get_duck_array(self)
    839 def get_duck_array(self):
--> 840     self._ensure_cached()
    841     return self.array.get_duck_array()

File ~\anaconda3\Lib\site-packages\xarray\core\indexing.py:837, in MemoryCachedArray._ensure_cached(self)
    836 def _ensure_cached(self):
--> 837     self.array = as_indexable(self.array.get_duck_array())

File ~\anaconda3\Lib\site-packages\xarray\core\indexing.py:794, in CopyOnWriteArray.get_duck_array(self)
    793 def get_duck_array(self):
--> 794     return self.array.get_duck_array()

File ~\anaconda3\Lib\site-packages\xarray\core\indexing.py:657, in LazilyIndexedArray.get_duck_array(self)
    653     array = apply_indexer(self.array, self.key)
    654 else:
    655     # If the array is not an ExplicitlyIndexedNDArrayMixin,
    656     # it may wrap a BackendArray so use its __getitem__
--> 657     array = self.array[self.key]
    659 # self.array[self.key] is now a numpy array when
    660 # self.array is a BackendArray subclass
    661 # and self.key is BasicIndexer((slice(None, None, None),))
    662 # so we need the explicit check for ExplicitlyIndexed
    663 if isinstance(array, ExplicitlyIndexed):

File ~\anaconda3\Lib\site-packages\cfgrib\xarray_plugin.py:163, in CfGribArrayWrapper.__getitem__(self, key)
    159 def __getitem__(
    160     self,
    161     key: xr.core.indexing.ExplicitIndexer,
    162 ) -> np.ndarray:
--> 163     return xr.core.indexing.explicit_indexing_adapter(
    164         key, self.shape, xr.core.indexing.IndexingSupport.BASIC, self._getitem
    165     )

File ~\anaconda3\Lib\site-packages\xarray\core\indexing.py:1018, in explicit_indexing_adapter(key, shape, indexing_support, raw_indexing_method)
    996 """Support explicit indexing by delegating to a raw indexing method.
    997 
    998 Outer and/or vectorized indexers are supported by indexing a second time
   (...)
   1015 Indexing result, in the form of a duck numpy-array.
   1016 """
   1017 raw_key, numpy_indices = decompose_indexer(key, shape, indexing_support)
-> 1018 result = raw_indexing_method(raw_key.tuple)
   1019 if numpy_indices.tuple:
   1020     # index the loaded np.ndarray
   1021     indexable = NumpyIndexingAdapter(result)

File ~\anaconda3\Lib\site-packages\cfgrib\xarray_plugin.py:172, in CfGribArrayWrapper._getitem(self, key)
    167 def _getitem(
    168     self,
    169     key: T.Tuple[T.Any, ...],
    170 ) -> np.ndarray:
    171     with self.datastore.lock:
--> 172         return self.array[key]

File ~\anaconda3\Lib\site-packages\cfgrib\dataset.py:373, in OnDiskArray.__getitem__(self, item)
    371     continue
    372 # NOTE: fill a single field as found in the message
--> 373 message = self.index.get_field(message_ids[0])  # type: ignore
    374 values = get_values_in_order(message, array_field[tuple(array_field_indexes)].shape)
    375 array_field.__getitem__(tuple(array_field_indexes)).flat[:] = values

File ~\anaconda3\Lib\site-packages\cfgrib\messages.py:488, in FieldsetIndex.get_field(self, message_id)
    487 def get_field(self, message_id: T.Any) -> abc.Field:
--> 488     return ComputedKeysAdapter(self.fieldset[message_id], selfputed_keys)

File ~\anaconda3\Lib\site-packages\cfgrib\messages.py:345, in FileStream.__getitem__(self, item)
    343 def __getitem__(self, item: T.Optional[OffsetType]) -> Message:
    344     with open(self.path, "rb") as file:
--> 345         return self.message_from_file(file, offset=item)

File ~\anaconda3\Lib\site-packages\cfgrib\messages.py:341, in FileStream.message_from_file(self, file, offset, **kwargs)
    339 def message_from_file(self, file, offset=None, **kwargs):
    340     # type: (T.IO[bytes], T.Optional[OffsetType], T.Any) -> Message
--> 341     return Message.from_file(file, offset, **kwargs)

File ~\anaconda3\Lib\site-packages\cfgrib\messages.py:94, in Message.from_file(cls, file, offset, **kwargs)
     92     offset, field_in_message = offset
     93 if offset is not None:
---> 94     file.seek(offset)
     95 codes_id = None
     96 if field_in_message == 0:

OSError: [Errno 22] Invalid argument

I have a grib file containing reanalysis (an), ensemble mean (em), and ensemble spread (es) data. The dataset I access is the analysis data,

import xarray as xr
import cfgrib
file = 'C:/Users/Downloads/04292020_rean_ensmean_ensspread.grib'
ds = xr.open_dataset(file,engine = "cfgrib",filter_by_keys={'dataType': 'an'}) # an accesses analysis data

time = 5;
res = 2; #2 = half degree
[phi,theta] = np.meshgrid(ds.variables['longitude'][::res].values,ds.variables['latitude'][::res].values)
divergence = ds.variables['d'][time,:,::res,::res].values

I'm able to access longitude and latitude values just fine, but if I try to access one of the field variable's values like divergence, it returns this error every time. Here's the head data assuming it's useful:

# <bound method Dataset.head of <xarray.Dataset> Size: 20GB
# Dimensions:        (time: 16, isobaricInhPa: 23, latitude: 721, longitude: 1440)
# Coordinates:
#     number         int32 4B ...
#   * time           (time) datetime64[ns] 128B 2020-04-29 ... 2020-04-29T23:00:00
#     step           timedelta64[ns] 8B ...
#   * isobaricInhPa  (isobaricInhPa) float64 184B 1e+03 975.0 ... 225.0 200.0
#   * latitude       (latitude) float64 6kB 90.0 89.75 89.5 ... -89.5 -89.75 -90.0
#   * longitude      (longitude) float64 12kB 0.0 0.25 0.5 ... 359.2 359.5 359.8
#     valid_time     (time) datetime64[ns] 128B ...
# Data variables: (12/13)
#     d              (time, isobaricInhPa, latitude, longitude) float32 2GB ...
#     cc             (time, isobaricInhPa, latitude, longitude) float32 2GB ...
#     z              (time, isobaricInhPa, latitude, longitude) float32 2GB ...
#     pv             (time, isobaricInhPa, latitude, longitude) float32 2GB ...
#     r              (time, isobaricInhPa, latitude, longitude) float32 2GB ...
#     clwc           (time, isobaricInhPa, latitude, longitude) float32 2GB ...
#             ...
#     crwc           (time, isobaricInhPa, latitude, longitude) float32 2GB ...
#     t              (time, isobaricInhPa, latitude, longitude) float32 2GB ...
#     u              (time, isobaricInhPa, latitude, longitude) float32 2GB ...
#     v              (time, isobaricInhPa, latitude, longitude) float32 2GB ...
#     w              (time, isobaricInhPa, latitude, longitude) float32 2GB ...
#     vo             (time, isobaricInhPa, latitude, longitude) float32 2GB ...
# Attributes:
#     GRIB_edition:            1
#     GRIB_centre:             ecmf
#     GRIB_centreDescription:  European Centre for Medium-Range Weather Forecasts
#     GRIB_subCentre:          0
#     Conventions:             CF-1.7
#     institution:             European Centre for Medium-Range Weather Forecasts
#     history:                 2025-03-23T18:13 GRIB to CDM+CF via cfgrib-0.9.1...>

EDIT: It actually works for time indices 0, 1, and 2. Anything past 2, and it fails with [Errno 22]. I've downloaded this data twice now on suspicion that the other dataset was corrupted, but it's still returning the same error.

EDIT: I went to check again today, and all of the sudden times 0-12 work now. Last night it was only times 0-2. Times 13-48 are still returning this error. I have no idea how, I didn't do anything to the data since last night. More of the data just randomly became accessible today.


Here's the full traceback:

divergence = ds.isel(time=13).d.data
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
Cell In[30], line 1
----> 1 divergence = ds.isel(time=13).d.data

File ~\anaconda3\Lib\site-packages\xarray\core\dataarray.py:795, in DataArray.data(self)
    783 @property
    784 def data(self) -> Any:
    785     """
    786     The DataArray's data as an array. The underlying array type
    787     (e.g. dask, sparse, pint) is preserved.
   (...)
    793     DataArray.values
    794     """
--> 795     return self.variable.data

File ~\anaconda3\Lib\site-packages\xarray\core\variable.py:474, in Variable.data(self)
    472     return self._data
    473 elif isinstance(self._data, indexing.ExplicitlyIndexed):
--> 474     return self._data.get_duck_array()
    475 else:
    476     return self.values

File ~\anaconda3\Lib\site-packages\xarray\core\indexing.py:840, in MemoryCachedArray.get_duck_array(self)
    839 def get_duck_array(self):
--> 840     self._ensure_cached()
    841     return self.array.get_duck_array()

File ~\anaconda3\Lib\site-packages\xarray\core\indexing.py:837, in MemoryCachedArray._ensure_cached(self)
    836 def _ensure_cached(self):
--> 837     self.array = as_indexable(self.array.get_duck_array())

File ~\anaconda3\Lib\site-packages\xarray\core\indexing.py:794, in CopyOnWriteArray.get_duck_array(self)
    793 def get_duck_array(self):
--> 794     return self.array.get_duck_array()

File ~\anaconda3\Lib\site-packages\xarray\core\indexing.py:657, in LazilyIndexedArray.get_duck_array(self)
    653     array = apply_indexer(self.array, self.key)
    654 else:
    655     # If the array is not an ExplicitlyIndexedNDArrayMixin,
    656     # it may wrap a BackendArray so use its __getitem__
--> 657     array = self.array[self.key]
    659 # self.array[self.key] is now a numpy array when
    660 # self.array is a BackendArray subclass
    661 # and self.key is BasicIndexer((slice(None, None, None),))
    662 # so we need the explicit check for ExplicitlyIndexed
    663 if isinstance(array, ExplicitlyIndexed):

File ~\anaconda3\Lib\site-packages\cfgrib\xarray_plugin.py:163, in CfGribArrayWrapper.__getitem__(self, key)
    159 def __getitem__(
    160     self,
    161     key: xr.core.indexing.ExplicitIndexer,
    162 ) -> np.ndarray:
--> 163     return xr.core.indexing.explicit_indexing_adapter(
    164         key, self.shape, xr.core.indexing.IndexingSupport.BASIC, self._getitem
    165     )

File ~\anaconda3\Lib\site-packages\xarray\core\indexing.py:1018, in explicit_indexing_adapter(key, shape, indexing_support, raw_indexing_method)
    996 """Support explicit indexing by delegating to a raw indexing method.
    997 
    998 Outer and/or vectorized indexers are supported by indexing a second time
   (...)
   1015 Indexing result, in the form of a duck numpy-array.
   1016 """
   1017 raw_key, numpy_indices = decompose_indexer(key, shape, indexing_support)
-> 1018 result = raw_indexing_method(raw_key.tuple)
   1019 if numpy_indices.tuple:
   1020     # index the loaded np.ndarray
   1021     indexable = NumpyIndexingAdapter(result)

File ~\anaconda3\Lib\site-packages\cfgrib\xarray_plugin.py:172, in CfGribArrayWrapper._getitem(self, key)
    167 def _getitem(
    168     self,
    169     key: T.Tuple[T.Any, ...],
    170 ) -> np.ndarray:
    171     with self.datastore.lock:
--> 172         return self.array[key]

File ~\anaconda3\Lib\site-packages\cfgrib\dataset.py:373, in OnDiskArray.__getitem__(self, item)
    371     continue
    372 # NOTE: fill a single field as found in the message
--> 373 message = self.index.get_field(message_ids[0])  # type: ignore
    374 values = get_values_in_order(message, array_field[tuple(array_field_indexes)].shape)
    375 array_field.__getitem__(tuple(array_field_indexes)).flat[:] = values

File ~\anaconda3\Lib\site-packages\cfgrib\messages.py:488, in FieldsetIndex.get_field(self, message_id)
    487 def get_field(self, message_id: T.Any) -> abc.Field:
--> 488     return ComputedKeysAdapter(self.fieldset[message_id], selfputed_keys)

File ~\anaconda3\Lib\site-packages\cfgrib\messages.py:345, in FileStream.__getitem__(self, item)
    343 def __getitem__(self, item: T.Optional[OffsetType]) -> Message:
    344     with open(self.path, "rb") as file:
--> 345         return self.message_from_file(file, offset=item)

File ~\anaconda3\Lib\site-packages\cfgrib\messages.py:341, in FileStream.message_from_file(self, file, offset, **kwargs)
    339 def message_from_file(self, file, offset=None, **kwargs):
    340     # type: (T.IO[bytes], T.Optional[OffsetType], T.Any) -> Message
--> 341     return Message.from_file(file, offset, **kwargs)

File ~\anaconda3\Lib\site-packages\cfgrib\messages.py:94, in Message.from_file(cls, file, offset, **kwargs)
     92     offset, field_in_message = offset
     93 if offset is not None:
---> 94     file.seek(offset)
     95 codes_id = None
     96 if field_in_message == 0:

OSError: [Errno 22] Invalid argument
Share Improve this question edited Mar 24 at 22:01 Researcher R asked Mar 24 at 0:24 Researcher RResearcher R 1475 bronze badges 2
  • Don't make us guess where the error happens. Please edit your question and add the full error message traceback. – John Gordon Commented Mar 24 at 21:46
  • @JohnGordon Just added it – Researcher R Commented Mar 24 at 22:01
Add a comment  | 

1 Answer 1

Reset to default 0

Can you explain what you are trying to do? Why using the np.meshgrid?

Where are you obtaining the error? When calculating [phi, theta] or when obtainting the divergence ?

Are you trying to change the resolution from 0.25 to 0.5 and obtain the divergence (d variable) at a specific time?

I think should be better to access the data at the time=time with the function .isel as:

divergence = ds.isel(time=time).d.data

But still I'm not sure what you are doing, probably there is some `xarray` function that will do what you're expecting.

本文标签: pythonGetting Errno 22 Invalid argument when trying to access data variablesStack Overflow