尝试访问Xarray变量时的HDF错误

发布于 2025-01-23 05:58:24 字数 14491 浏览 2 评论 0原文

我已经从模型下载了温度数据。由于我选择了整个时间范围,因此数据分为不同的NETCDF文件。但是,当我试图访问这些数据时,我会发现一个错误,但我不理解。首先,我已经导入以下软件包(并正确安装),

import netCDF4 as nc
import h5py
import xarray as xr
import numpy as np
import matplotlib.pyplot as plt
#first read the first file of the same model
c1 = xr.open_dataset(path+'tas_day_CMCC-ESM2_historical_r1i1p1f1_gn_18500101-18741231_v20210114.nc')

#then read the second file
c2 = xr.open_dataset(path+'tas_day_CMCC-ESM2_historical_r1i1p1f1_gn_18750101-18991231_v20210114.nc')


#then take only the longitude and latitude values which I am interested
cc1 = c1.sel(lon = 35.86,lat = 14.375,method='nearest')
cc2 = c2.sel(lon = 35.86,lat = 14.375,method='nearest')

但是当我尝试访问第二个文件的温度变量时,我会收到错误。例如,让S打印第一个Xarray

#for the first Xarray

time ,  (time) , object,  ,1850-01-01 12:00:00 ... 1874-12-...
lat,    ()     ,float64,  ,14.61
lon,    ()     ,float64.  ,36.25
height, ().    ,float64.  ,2.0
Data variables:
time_bnds.  , (time, bnds).  , object.   , ...
lat_bnds.   , (bnds).        , float64.  , ...
lon_bnds.   , (bnds).        , float64.  , ...
tas.        ,(time).         , float32.  , 297.9 298.0 297.9 ... 297.8 298.2

,但是当我尝试打印第二个Xarray时,

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
File ~/miniforge3/lib/python3.9/site-packages/IPython/core/formatters.py:343, in BaseFormatter.__call__(self, obj)
    341     method = get_real_method(obj, self.print_method)
    342     if method is not None:
--> 343         return method()
    344     return None
    345 else:

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/dataset.py:2058, in Dataset._repr_html_(self)
   2056 if OPTIONS["display_style"] == "text":
   2057     return f"<pre>{escape(repr(self))}</pre>"
-> 2058 return formatting_html.dataset_repr(self)

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/formatting_html.py:297, in dataset_repr(ds)
    290 obj_type = f"xarray.{type(ds).__name__}"
    292 header_components = [f"<div class='xr-obj-type'>{escape(obj_type)}</div>"]
    294 sections = [
    295     dim_section(ds),
    296     coord_section(ds.coords),
--> 297     datavar_section(ds.data_vars),
    298     attr_section(ds.attrs),
    299 ]
    301 return _obj_repr(ds, header_components, sections)

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/formatting_html.py:179, in _mapping_section(mapping, name, details_func, max_items_collapse, expand_option_name, enabled)
    172 expanded = _get_boolean_with_default(
    173     expand_option_name, n_items < max_items_collapse
    174 )
    175 collapsed = not expanded
    177 return collapsible_section(
    178     name,
--> 179     details=details_func(mapping),
    180     n_items=n_items,
    181     enabled=enabled,
    182     collapsed=collapsed,
    183 )

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/formatting_html.py:138, in summarize_vars(variables)
    137 def summarize_vars(variables):
--> 138     vars_li = "".join(
    139         f"<li class='xr-var-item'>{summarize_variable(k, v)}</li>"
    140         for k, v in variables.items()
    141     )
    143     return f"<ul class='xr-var-list'>{vars_li}</ul>"

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/formatting_html.py:139, in <genexpr>(.0)
    137 def summarize_vars(variables):
    138     vars_li = "".join(
--> 139         f"<li class='xr-var-item'>{summarize_variable(k, v)}</li>"
    140         for k, v in variables.items()
    141     )
    143     return f"<ul class='xr-var-list'>{vars_li}</ul>"

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/formatting_html.py:115, in summarize_variable(name, var, is_index, dtype, preview)
    113 preview = preview or escape(inline_variable_array_repr(variable, 35))
    114 attrs_ul = summarize_attrs(var.attrs)
--> 115 data_repr = short_data_repr_html(variable)
    117 attrs_icon = _icon("icon-file-text2")
    118 data_icon = _icon("icon-database")

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/formatting_html.py:30, in short_data_repr_html(array)
     28 if hasattr(internal_data, "_repr_html_"):
     29     return internal_data._repr_html_()
---> 30 text = escape(short_data_repr(array))
     31 return f"<pre>{text}</pre>"

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/formatting.py:572, in short_data_repr(array)
    570     return limit_lines(repr(array.data), limit=40)
    571 elif array._in_memory or array.size < 1e5:
--> 572     return short_numpy_repr(array)
    573 else:
    574     # internal xarray array type
    575     return f"[{array.size} values with dtype={array.dtype}]"

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/formatting.py:548, in short_numpy_repr(array)
    547 def short_numpy_repr(array):
--> 548     array = np.asarray(array)
    550     # default to lower precision so a full (abbreviated) line can fit on
    551     # one line with the default display_width
    552     options = {"precision": 6, "linewidth": OPTIONS["display_width"], "threshold": 200}

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/common.py:143, in AbstractArray.__array__(self, dtype)
    142 def __array__(self: Any, dtype: DTypeLike = None) -> np.ndarray:
--> 143     return np.asarray(self.values, dtype=dtype)

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/variable.py:512, in Variable.values(self)
    509 @property
    510 def values(self):
    511     """The variable's data as a numpy.ndarray"""
--> 512     return _as_array_or_item(self._data)

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/variable.py:252, in _as_array_or_item(data)
    238 def _as_array_or_item(data):
    239     """Return the given values as a numpy array, or as an individual item if
    240     it's a 0d datetime64 or timedelta64 array.
    241 
   (...)
    250     TODO: remove this (replace with np.asarray) once these issues are fixed
    251     """
--> 252     data = np.asarray(data)
    253     if data.ndim == 0:
    254         if data.dtype.kind == "M":

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/indexing.py:552, in MemoryCachedArray.__array__(self, dtype)
    551 def __array__(self, dtype=None):
--> 552     self._ensure_cached()
    553     return np.asarray(self.array, dtype=dtype)

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/indexing.py:549, in MemoryCachedArray._ensure_cached(self)
    547 def _ensure_cached(self):
    548     if not isinstance(self.array, NumpyIndexingAdapter):
--> 549         self.array = NumpyIndexingAdapter(np.asarray(self.array))

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/indexing.py:522, in CopyOnWriteArray.__array__(self, dtype)
    521 def __array__(self, dtype=None):
--> 522     return np.asarray(self.array, dtype=dtype)

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/indexing.py:423, in LazilyIndexedArray.__array__(self, dtype)
    421 def __array__(self, dtype=None):
    422     array = as_indexable(self.array)
--> 423     return np.asarray(array[self.key], dtype=None)

File ~/miniforge3/lib/python3.9/site-packages/xarray/coding/variables.py:70, in _ElementwiseFunctionArray.__array__(self, dtype)
     69 def __array__(self, dtype=None):
---> 70     return self.func(self.array)

File ~/miniforge3/lib/python3.9/site-packages/xarray/coding/variables.py:137, in _apply_mask(data, encoded_fill_values, decoded_fill_value, dtype)
    133 def _apply_mask(
    134     data: np.ndarray, encoded_fill_values: list, decoded_fill_value: Any, dtype: Any
    135 ) -> np.ndarray:
    136     """Mask all matching values in a NumPy arrays."""
--> 137     data = np.asarray(data, dtype=dtype)
    138     condition = False
    139     for fv in encoded_fill_values:

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/indexing.py:423, in LazilyIndexedArray.__array__(self, dtype)
    421 def __array__(self, dtype=None):
    422     array = as_indexable(self.array)
--> 423     return np.asarray(array[self.key], dtype=None)

File ~/miniforge3/lib/python3.9/site-packages/xarray/backends/netCDF4_.py:93, in NetCDF4ArrayWrapper.__getitem__(self, key)
     92 def __getitem__(self, key):
---> 93     return indexing.explicit_indexing_adapter(
     94         key, self.shape, indexing.IndexingSupport.OUTER, self._getitem
     95     )

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/indexing.py:712, in explicit_indexing_adapter(key, shape, indexing_support, raw_indexing_method)
    690 """Support explicit indexing by delegating to a raw indexing method.
    691 
    692 Outer and/or vectorized indexers are supported by indexing a second time
   (...)
    709 Indexing result, in the form of a duck numpy-array.
    710 """
    711 raw_key, numpy_indices = decompose_indexer(key, shape, indexing_support)
--> 712 result = raw_indexing_method(raw_key.tuple)
    713 if numpy_indices.tuple:
    714     # index the loaded np.ndarray
    715     result = NumpyIndexingAdapter(np.asarray(result))[numpy_indices]

File ~/miniforge3/lib/python3.9/site-packages/xarray/backends/netCDF4_.py:106, in NetCDF4ArrayWrapper._getitem(self, key)
    104     with self.datastore.lock:
    105         original_array = self.get_array(needs_lock=False)
--> 106         array = getitem(original_array, key)
    107 except IndexError:
    108     # Catch IndexError in netCDF4 and return a more informative
    109     # error message.  This is most often called when an unsorted
    110     # indexer is used before the data is loaded from disk.
    111     msg = (
    112         "The indexing operation you are attempting to perform "
    113         "is not valid on netCDF4.Variable object. Try loading "
    114         "your data into memory first by calling .load()."
    115     )

File src/netCDF4/_netCDF4.pyx:4406, in netCDF4._netCDF4.Variable.__getitem__()

File src/netCDF4/_netCDF4.pyx:5350, in netCDF4._netCDF4.Variable._get()

File src/netCDF4/_netCDF4.pyx:1927, in netCDF4._netCDF4._ensure_nc_success()

RuntimeError: NetCDF: HDF error

<xarray.Dataset>
Dimensions:    (time: 9125, bnds: 2)
Coordinates:
  * time       (time) object 1875-01-01 12:00:00 ... 1899-12-31 12:00:00
    lat        float64 14.61
    lon        float64 36.25
    height     float64 ...
Dimensions without coordinates: bnds
Data variables:
    time_bnds  (time, bnds) object ...
    lat_bnds   (bnds) float64 ...
    lon_bnds   (bnds) float64 ...
    tas        (time) float32 ...
Attributes: (12/48)
    Conventions:            CF-1.7 CMIP-6.2
    activity_id:            CMIP
    branch_method:          standard
    branch_time_in_child:   0.0
    branch_time_in_parent:  0.0
    comment:                none
    ...                     ...
    title:                  CMCC-ESM2 output prepared for CMIP6
    variable_id:            tas
    variant_label:          r1i1p1f1
    license:                CMIP6 model data produced by CMCC is licensed und...
    cmor_version:           3.6.0
    tracking_id:            hdl:21.14100/88ee8b49-0196-4c37-b1cd-a3cc6e2e598e

应该注意的是,当我尝试打印第二个Xarray而不选择较小的域(基本上是.SEL)时,它可以打印。当我使用SEL命令时,为什么它不起作用?我已经确认我想要的坐标确实位于C2(我的第二个Xarray)内部。我的所有Xarrays(带有SEL命令)的所有Xarrays均为Xarray.core.dataset.dataset类型。 为了识别问题,我更改了.SEL命令。首先,我想查看Xarray数据集中可用的坐标。对于LON来说,最近的是35,而对于LAT的关闭为14.60733。首先,我尝试。只有lon。以下是命令

d1 = cc2.sel(lon = 35.)

d1

,我得到的结果是

time,    (time),    object,     1875-01-01 12:00:00 ... 1899-12-...
lat,     (lat),     float64,   -90.0 -89.06 -88.12 ... 89.06 90.0
lon,     (),        float64,    35.0
height,  (),        float64,    2.0
Data variables:
time_bnds,     (time, bnds),      object,    1875-01-01 00:00:00... 1900-01-...
lat_bnds
(lat, bnds)
float64
-90.0 -89.53 -89.53 ... 89.53 90.0
lon_bnds,     (bnds),           float64,     ...
tas,          (time, lat),      float32,     ...

在此数据集中,我看到了lat = 14.60732984的坐标 可以在索引111中找到,我通过使用以下命令打印来发现,

cc2['lat'][111]. 
which gives the following outcome. 
array(14.60732984)
lat,     (),    float64,    14.61
lon,     (),    float64,    35.0
height,  (),    float64,    2.0

现在我尝试了相同的lat步骤,均以14.60732984和14.61进行

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/coordinates.py:421, in remap_label_indexers(obj, indexers, method, tolerance, **indexers_kwargs)
    414 indexers = either_dict_or_kwargs(indexers, indexers_kwargs, "remap_label_indexers")
    416 v_indexers = {
    417     k: v.variable.data if isinstance(v, DataArray) else v
    418     for k, v in indexers.items()
    419 }
--> 421 pos_indexers, new_indexes = indexing.remap_label_indexers(
    422     obj, v_indexers, method=method, tolerance=tolerance
    423 )
    424 # attach indexer's coordinate to pos_indexers
    425 for k, v in indexers.items():

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/indexing.py:121, in remap_label_indexers(data_obj, indexers, method, tolerance)
    119 for dim, index in indexes.items():
    120     labels = grouped_indexers[dim]
--> 121     idxr, new_idx = index.query(labels, method=method, tolerance=tolerance)
    122     pos_indexers[dim] = idxr
    123     if new_idx is not None:

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/indexes.py:241, in PandasIndex.query(self, labels, method, tolerance)
    237                 raise KeyError(
    238                     f"not all values found in index {coord_name!r}"
    239                 )
    240         else:
--> 241             indexer = self.index.get_loc(label_value)
    242 elif label.dtype.kind == "b":
    243     indexer = label

File ~/miniforge3/lib/python3.9/site-packages/pandas/core/indexes/base.py:3623, in Index.get_loc(self, key, method, tolerance)
   3621     return self._engine.get_loc(casted_key)
   3622 except KeyError as err:
-> 3623     raise KeyError(key) from err
   3624 except TypeError:
   3625     # If we have a listlike key, _check_indexing_error will raise
   3626     #  InvalidIndexError. Otherwise we fall through and re-raise
   3627     #  the TypeError.
   3628     self._check_indexing_error(key)

KeyError: 14.60732984

I have downloaded temperature data from a model. Because I chose the whole temporal range the data was split into different netcdf files. However, when I am trying to access these data for some I get an error which I do not understand. First of all I have imported the following packages (and installed correctly)

import netCDF4 as nc
import h5py
import xarray as xr
import numpy as np
import matplotlib.pyplot as plt
#first read the first file of the same model
c1 = xr.open_dataset(path+'tas_day_CMCC-ESM2_historical_r1i1p1f1_gn_18500101-18741231_v20210114.nc')

#then read the second file
c2 = xr.open_dataset(path+'tas_day_CMCC-ESM2_historical_r1i1p1f1_gn_18750101-18991231_v20210114.nc')


#then take only the longitude and latitude values which I am interested
cc1 = c1.sel(lon = 35.86,lat = 14.375,method='nearest')
cc2 = c2.sel(lon = 35.86,lat = 14.375,method='nearest')

However when I am trying to access the temperature variable for the second file I get an error. For example let s print the first xarray

#for the first Xarray

time ,  (time) , object,  ,1850-01-01 12:00:00 ... 1874-12-...
lat,    ()     ,float64,  ,14.61
lon,    ()     ,float64.  ,36.25
height, ().    ,float64.  ,2.0
Data variables:
time_bnds.  , (time, bnds).  , object.   , ...
lat_bnds.   , (bnds).        , float64.  , ...
lon_bnds.   , (bnds).        , float64.  , ...
tas.        ,(time).         , float32.  , 297.9 298.0 297.9 ... 297.8 298.2

but when I try to print the second one

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
File ~/miniforge3/lib/python3.9/site-packages/IPython/core/formatters.py:343, in BaseFormatter.__call__(self, obj)
    341     method = get_real_method(obj, self.print_method)
    342     if method is not None:
--> 343         return method()
    344     return None
    345 else:

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/dataset.py:2058, in Dataset._repr_html_(self)
   2056 if OPTIONS["display_style"] == "text":
   2057     return f"<pre>{escape(repr(self))}</pre>"
-> 2058 return formatting_html.dataset_repr(self)

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/formatting_html.py:297, in dataset_repr(ds)
    290 obj_type = f"xarray.{type(ds).__name__}"
    292 header_components = [f"<div class='xr-obj-type'>{escape(obj_type)}</div>"]
    294 sections = [
    295     dim_section(ds),
    296     coord_section(ds.coords),
--> 297     datavar_section(ds.data_vars),
    298     attr_section(ds.attrs),
    299 ]
    301 return _obj_repr(ds, header_components, sections)

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/formatting_html.py:179, in _mapping_section(mapping, name, details_func, max_items_collapse, expand_option_name, enabled)
    172 expanded = _get_boolean_with_default(
    173     expand_option_name, n_items < max_items_collapse
    174 )
    175 collapsed = not expanded
    177 return collapsible_section(
    178     name,
--> 179     details=details_func(mapping),
    180     n_items=n_items,
    181     enabled=enabled,
    182     collapsed=collapsed,
    183 )

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/formatting_html.py:138, in summarize_vars(variables)
    137 def summarize_vars(variables):
--> 138     vars_li = "".join(
    139         f"<li class='xr-var-item'>{summarize_variable(k, v)}</li>"
    140         for k, v in variables.items()
    141     )
    143     return f"<ul class='xr-var-list'>{vars_li}</ul>"

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/formatting_html.py:139, in <genexpr>(.0)
    137 def summarize_vars(variables):
    138     vars_li = "".join(
--> 139         f"<li class='xr-var-item'>{summarize_variable(k, v)}</li>"
    140         for k, v in variables.items()
    141     )
    143     return f"<ul class='xr-var-list'>{vars_li}</ul>"

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/formatting_html.py:115, in summarize_variable(name, var, is_index, dtype, preview)
    113 preview = preview or escape(inline_variable_array_repr(variable, 35))
    114 attrs_ul = summarize_attrs(var.attrs)
--> 115 data_repr = short_data_repr_html(variable)
    117 attrs_icon = _icon("icon-file-text2")
    118 data_icon = _icon("icon-database")

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/formatting_html.py:30, in short_data_repr_html(array)
     28 if hasattr(internal_data, "_repr_html_"):
     29     return internal_data._repr_html_()
---> 30 text = escape(short_data_repr(array))
     31 return f"<pre>{text}</pre>"

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/formatting.py:572, in short_data_repr(array)
    570     return limit_lines(repr(array.data), limit=40)
    571 elif array._in_memory or array.size < 1e5:
--> 572     return short_numpy_repr(array)
    573 else:
    574     # internal xarray array type
    575     return f"[{array.size} values with dtype={array.dtype}]"

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/formatting.py:548, in short_numpy_repr(array)
    547 def short_numpy_repr(array):
--> 548     array = np.asarray(array)
    550     # default to lower precision so a full (abbreviated) line can fit on
    551     # one line with the default display_width
    552     options = {"precision": 6, "linewidth": OPTIONS["display_width"], "threshold": 200}

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/common.py:143, in AbstractArray.__array__(self, dtype)
    142 def __array__(self: Any, dtype: DTypeLike = None) -> np.ndarray:
--> 143     return np.asarray(self.values, dtype=dtype)

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/variable.py:512, in Variable.values(self)
    509 @property
    510 def values(self):
    511     """The variable's data as a numpy.ndarray"""
--> 512     return _as_array_or_item(self._data)

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/variable.py:252, in _as_array_or_item(data)
    238 def _as_array_or_item(data):
    239     """Return the given values as a numpy array, or as an individual item if
    240     it's a 0d datetime64 or timedelta64 array.
    241 
   (...)
    250     TODO: remove this (replace with np.asarray) once these issues are fixed
    251     """
--> 252     data = np.asarray(data)
    253     if data.ndim == 0:
    254         if data.dtype.kind == "M":

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/indexing.py:552, in MemoryCachedArray.__array__(self, dtype)
    551 def __array__(self, dtype=None):
--> 552     self._ensure_cached()
    553     return np.asarray(self.array, dtype=dtype)

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/indexing.py:549, in MemoryCachedArray._ensure_cached(self)
    547 def _ensure_cached(self):
    548     if not isinstance(self.array, NumpyIndexingAdapter):
--> 549         self.array = NumpyIndexingAdapter(np.asarray(self.array))

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/indexing.py:522, in CopyOnWriteArray.__array__(self, dtype)
    521 def __array__(self, dtype=None):
--> 522     return np.asarray(self.array, dtype=dtype)

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/indexing.py:423, in LazilyIndexedArray.__array__(self, dtype)
    421 def __array__(self, dtype=None):
    422     array = as_indexable(self.array)
--> 423     return np.asarray(array[self.key], dtype=None)

File ~/miniforge3/lib/python3.9/site-packages/xarray/coding/variables.py:70, in _ElementwiseFunctionArray.__array__(self, dtype)
     69 def __array__(self, dtype=None):
---> 70     return self.func(self.array)

File ~/miniforge3/lib/python3.9/site-packages/xarray/coding/variables.py:137, in _apply_mask(data, encoded_fill_values, decoded_fill_value, dtype)
    133 def _apply_mask(
    134     data: np.ndarray, encoded_fill_values: list, decoded_fill_value: Any, dtype: Any
    135 ) -> np.ndarray:
    136     """Mask all matching values in a NumPy arrays."""
--> 137     data = np.asarray(data, dtype=dtype)
    138     condition = False
    139     for fv in encoded_fill_values:

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/indexing.py:423, in LazilyIndexedArray.__array__(self, dtype)
    421 def __array__(self, dtype=None):
    422     array = as_indexable(self.array)
--> 423     return np.asarray(array[self.key], dtype=None)

File ~/miniforge3/lib/python3.9/site-packages/xarray/backends/netCDF4_.py:93, in NetCDF4ArrayWrapper.__getitem__(self, key)
     92 def __getitem__(self, key):
---> 93     return indexing.explicit_indexing_adapter(
     94         key, self.shape, indexing.IndexingSupport.OUTER, self._getitem
     95     )

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/indexing.py:712, in explicit_indexing_adapter(key, shape, indexing_support, raw_indexing_method)
    690 """Support explicit indexing by delegating to a raw indexing method.
    691 
    692 Outer and/or vectorized indexers are supported by indexing a second time
   (...)
    709 Indexing result, in the form of a duck numpy-array.
    710 """
    711 raw_key, numpy_indices = decompose_indexer(key, shape, indexing_support)
--> 712 result = raw_indexing_method(raw_key.tuple)
    713 if numpy_indices.tuple:
    714     # index the loaded np.ndarray
    715     result = NumpyIndexingAdapter(np.asarray(result))[numpy_indices]

File ~/miniforge3/lib/python3.9/site-packages/xarray/backends/netCDF4_.py:106, in NetCDF4ArrayWrapper._getitem(self, key)
    104     with self.datastore.lock:
    105         original_array = self.get_array(needs_lock=False)
--> 106         array = getitem(original_array, key)
    107 except IndexError:
    108     # Catch IndexError in netCDF4 and return a more informative
    109     # error message.  This is most often called when an unsorted
    110     # indexer is used before the data is loaded from disk.
    111     msg = (
    112         "The indexing operation you are attempting to perform "
    113         "is not valid on netCDF4.Variable object. Try loading "
    114         "your data into memory first by calling .load()."
    115     )

File src/netCDF4/_netCDF4.pyx:4406, in netCDF4._netCDF4.Variable.__getitem__()

File src/netCDF4/_netCDF4.pyx:5350, in netCDF4._netCDF4.Variable._get()

File src/netCDF4/_netCDF4.pyx:1927, in netCDF4._netCDF4._ensure_nc_success()

RuntimeError: NetCDF: HDF error

<xarray.Dataset>
Dimensions:    (time: 9125, bnds: 2)
Coordinates:
  * time       (time) object 1875-01-01 12:00:00 ... 1899-12-31 12:00:00
    lat        float64 14.61
    lon        float64 36.25
    height     float64 ...
Dimensions without coordinates: bnds
Data variables:
    time_bnds  (time, bnds) object ...
    lat_bnds   (bnds) float64 ...
    lon_bnds   (bnds) float64 ...
    tas        (time) float32 ...
Attributes: (12/48)
    Conventions:            CF-1.7 CMIP-6.2
    activity_id:            CMIP
    branch_method:          standard
    branch_time_in_child:   0.0
    branch_time_in_parent:  0.0
    comment:                none
    ...                     ...
    title:                  CMCC-ESM2 output prepared for CMIP6
    variable_id:            tas
    variant_label:          r1i1p1f1
    license:                CMIP6 model data produced by CMCC is licensed und...
    cmor_version:           3.6.0
    tracking_id:            hdl:21.14100/88ee8b49-0196-4c37-b1cd-a3cc6e2e598e

It should be noted that when I try to print the second Xarray without selecting a smaller domain (basically the .sel) it can be printed. Why does it not work when I use the sel command? I have confirmed that the coordinates I want are indeed inside c2 (my second Xarray). Also all my xarrays, with or without the sel command, are of type xarray.core.dataset.Dataset.
In order to identify the problem, I changed the .sel command. First I wanted to see the coordinates that are available in my Xarray dataset. For Lon the nearest was 35 and for lat the closes was 14.60733. First I tried to .sel only the Lon. The following is the command

d1 = cc2.sel(lon = 35.)

d1

and the result I get is

time,    (time),    object,     1875-01-01 12:00:00 ... 1899-12-...
lat,     (lat),     float64,   -90.0 -89.06 -88.12 ... 89.06 90.0
lon,     (),        float64,    35.0
height,  (),        float64,    2.0
Data variables:
time_bnds,     (time, bnds),      object,    1875-01-01 00:00:00... 1900-01-...
lat_bnds
(lat, bnds)
float64
-90.0 -89.53 -89.53 ... 89.53 90.0
lon_bnds,     (bnds),           float64,     ...
tas,          (time, lat),      float32,     ...

In this dataset I saw that the coordinate for the lat = 14.60732984
can be found in the index 111 which I found by trial and error by printing using the following command

cc2['lat'][111]. 
which gives the following outcome. 
array(14.60732984)
lat,     (),    float64,    14.61
lon,     (),    float64,    35.0
height,  (),    float64,    2.0

Now I tried the same procedure for lat both for values 14.60732984 and 14.61 but I get the following error

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/coordinates.py:421, in remap_label_indexers(obj, indexers, method, tolerance, **indexers_kwargs)
    414 indexers = either_dict_or_kwargs(indexers, indexers_kwargs, "remap_label_indexers")
    416 v_indexers = {
    417     k: v.variable.data if isinstance(v, DataArray) else v
    418     for k, v in indexers.items()
    419 }
--> 421 pos_indexers, new_indexes = indexing.remap_label_indexers(
    422     obj, v_indexers, method=method, tolerance=tolerance
    423 )
    424 # attach indexer's coordinate to pos_indexers
    425 for k, v in indexers.items():

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/indexing.py:121, in remap_label_indexers(data_obj, indexers, method, tolerance)
    119 for dim, index in indexes.items():
    120     labels = grouped_indexers[dim]
--> 121     idxr, new_idx = index.query(labels, method=method, tolerance=tolerance)
    122     pos_indexers[dim] = idxr
    123     if new_idx is not None:

File ~/miniforge3/lib/python3.9/site-packages/xarray/core/indexes.py:241, in PandasIndex.query(self, labels, method, tolerance)
    237                 raise KeyError(
    238                     f"not all values found in index {coord_name!r}"
    239                 )
    240         else:
--> 241             indexer = self.index.get_loc(label_value)
    242 elif label.dtype.kind == "b":
    243     indexer = label

File ~/miniforge3/lib/python3.9/site-packages/pandas/core/indexes/base.py:3623, in Index.get_loc(self, key, method, tolerance)
   3621     return self._engine.get_loc(casted_key)
   3622 except KeyError as err:
-> 3623     raise KeyError(key) from err
   3624 except TypeError:
   3625     # If we have a listlike key, _check_indexing_error will raise
   3626     #  InvalidIndexError. Otherwise we fall through and re-raise
   3627     #  the TypeError.
   3628     self._check_indexing_error(key)

KeyError: 14.60732984

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文