0

When I plan to calculate temperature advection from array dataset, an error came out:

TypeError: invalid indexer array, does not have integer dtype: array(None, dtype=object)

example: I read WRF output and the xarray dataarray is:


xarray.DataArray'ua' (Time: 365south_north: 156west_east: 108)

Coordinates:

XLONG (south_north, west_east) float32 ...

XLAT (south_north, west_east) float32 ...

interp_level () float64 850.0

Time (Time) object 1997-01-01 00:00:00 ...


I run

lats=slp_yr_ds.XLAT.data
lons=slp_yr_ds.XLONG.data
dx,dy=mpcalc.lat_lon_grid_deltas(lons,lats)

tadv_850=mpcalc.advection(ta, (ua,va),(dx,dy),dim_order='yx')

The error message are:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-52-23d8c16fd3c0> in <module>
      1 #tadv_850=mpcalc.advection(ta[0,0,:,:], (ua[0,0,:,:],va[0,0,:,:]),(dx,dy),dim_order='yx')
----> 2 tadv_850=mpcalc.advection(ta, (ua,va),(dx,dy))

./software/anaconda3/envs/work/lib/python3.7/site-packages/metpy/xarray.py in wrapper(*args, **kwargs)
    675         kwargs = {name: (v.metpy.unit_array if isinstance(v, xr.DataArray) else v)
    676                   for name, v in kwargs.items()}
--> 677         return func(*args, **kwargs)
    678     return wrapper
    679 

./software/anaconda3/envs/work/lib/python3.7/site-packages/metpy/calc/kinematics.py in wrapper(dim_order, *args, **kwargs)
     56                 kwargs[k] = _check_and_flip(v)
     57 
---> 58         ret = func(*args, **kwargs)
     59 
     60         # If we flipped on the way in, need to flip on the way out so that output array(s)

./software/anaconda3/envs/work/lib/python3.7/site-packages/metpy/calc/kinematics.py in advection(scalar, wind, deltas)
    315     """
    316     # This allows passing in a list of wind components or an array.
--> 317     wind = _stack(wind)
    318 
    319     # If we have more than one component, we need to reverse the order along the first

./software/anaconda3/envs/work/lib/python3.7/site-packages/metpy/calc/kinematics.py in _stack(arrs)
     20 
     21 def _stack(arrs):
---> 22     return concatenate([a[np.newaxis] if iterable(a) else a for a in arrs], axis=0)
     23 
     24 

./software/anaconda3/envs/work/lib/python3.7/site-packages/metpy/calc/kinematics.py in <listcomp>(.0)
     20 
     21 def _stack(arrs):
---> 22     return concatenate([a[np.newaxis] if iterable(a) else a for a in arrs], axis=0)
     23 
     24 

./software/anaconda3/envs/work/lib/python3.7/site-packages/xarray/core/dataarray.py in __getitem__(self, key)
    629         else:
    630             # xarray-style array indexing
--> 631             return self.isel(indexers=self._item_key_to_dict(key))
    632 
    633     def __setitem__(self, key: Any, value: Any) -> None:

./software/anaconda3/envs/work/lib/python3.7/site-packages/xarray/core/dataarray.py in isel(self, indexers, drop, **indexers_kwargs)
   1004         indexers = either_dict_or_kwargs(indexers, indexers_kwargs, "isel")
   1005         if any(is_fancy_indexer(idx) for idx in indexers.values()):
-> 1006             ds = self._to_temp_dataset()._isel_fancy(indexers, drop=drop)
   1007             return self._from_temp_dataset(ds)
   1008 

./software/anaconda3/envs/work/lib/python3.7/site-packages/xarray/core/dataset.py in _isel_fancy(self, indexers, drop)
   1972             if name in self.indexes:
   1973                 new_var, new_index = isel_variable_and_index(
-> 1974                     name, var, self.indexes[name], var_indexers
   1975                 )
   1976                 if new_index is not None:

/./software/anaconda3/envs/work/lib/python3.7/site-packages/xarray/core/indexes.py in isel_variable_and_index(name, variable, index, indexers)
     99         )
    100 
--> 101     new_variable = variable.isel(indexers)
    102 
    103     if new_variable.dims != (name,):

./software/anaconda3/envs/work/lib/python3.7/site-packages/xarray/core/variable.py in isel(self, indexers, **indexers_kwargs)
   1058 
   1059         key = tuple(indexers.get(dim, slice(None)) for dim in self.dims)
-> 1060         return self[key]
   1061 
   1062     def squeeze(self, dim=None):

./software/anaconda3/envs/work/lib/python3.7/site-packages/xarray/core/variable.py in __getitem__(self, key)
    701         array `x.values` directly.
    702         """
--> 703         dims, indexer, new_order = self._broadcast_indexes(key)
    704         data = as_indexable(self._data)[indexer]
    705         if new_order:

./software/anaconda3/envs/work/lib/python3.7/site-packages/xarray/core/variable.py in _broadcast_indexes(self, key)
    547         # key can be mapped as an OuterIndexer.
    548         if all(not isinstance(k, Variable) for k in key):
--> 549             return self._broadcast_indexes_outer(key)
    550 
    551         # If all key is 1-dimensional and there are no duplicate labels,

./software/anaconda3/envs/work/lib/python3.7/site-packages/xarray/core/variable.py in _broadcast_indexes_outer(self, key)
    623             new_key.append(k)
    624 
--> 625         return dims, OuterIndexer(tuple(new_key)), None
    626 
    627     def _nonzero(self):

./software/anaconda3/envs/work/lib/python3.7/site-packages/xarray/core/indexing.py in __init__(self, key)
    403                 if not np.issubdtype(k.dtype, np.integer):
    404                     raise TypeError(
--> 405                         f"invalid indexer array, does not have integer dtype: {k!r}"
    406                     )
    407                 if k.ndim != 1:

TypeError: invalid indexer array, does not have integer dtype: array(None, dtype=object)


Thnak you

linyenheng
  • 33
  • 3
  • Based on the brief error message I'm guessing the problem is in `[uu2,vv2]` or `(dx, dy)`, but I'm not sure what's going on exactly. Can you edit the question to include the full traceback you get and not just the one line error message? – DopplerShift Apr 16 '20 at 04:56
  • It would be good to see more of your code, so we can see the actual library(ies) you are using for this calculation as well as what the data you are passing in actually looks like. Based on the code, it looks like you are using Python and perhaps using the MetPy.calc library, have you checked the documentation for that library (https://unidata.github.io/MetPy/latest/api/generated/metpy.calc.html). Also, have you used your IDE to set a break point and explore what is being passed into that method when the error happens. All of this will really help us help you. – Schleichermann Apr 16 '20 at 22:55
  • Can you add to the question what the shapes are of `ta`, `ua`, `va`, `dx, `dy`? – DopplerShift Aug 02 '20 at 01:45

0 Answers0