Issue
I am trying to slice
data by longtitude using xarray
.
The data is in a netcdf file I created from measurements I made.
The xarray.Dataset
has the following attributes:
Dimensions:
(lat: 1321, lon: 1321)
Data variables:
- (lon) float64 '8.413 8.411 8.409 ... 4.904 4.905'
- (lat) float64 '47.4 47.4 47.41 ... 52.37 52.37'
- (data) float64 ... #dimension: 1321
my code is:
import xarray as xr
obs = xr.open_dataset('data.nc')
obs=obs['data'].sel(lon=slice(4.905, 8.413))
The error I get is TypeError: 'float' object cannot be interpreted as an integer
I could not find out whether it is an error in my code, or an error in xarray. I would expect such an error using isel
instead of sel
. Could not find any solution on here or over at the xarray documentation.
Full error message:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-434-5b37e4c5d0c6> in <module>
----> 1 obs=obs['data'].sel(lon=slice(4.905, 8.413))
~/opt/anaconda3/lib/python3.7/site-packages/xarray/core/dataarray.py in sel(self, indexers, method, tolerance, drop, **indexers_kwargs)
1059 method=method,
1060 tolerance=tolerance,
-> 1061 **indexers_kwargs,
1062 )
1063 return self._from_temp_dataset(ds)
~/opt/anaconda3/lib/python3.7/site-packages/xarray/core/dataset.py in sel(self, indexers, method, tolerance, drop, **indexers_kwargs)
2066 self, indexers=indexers, method=method, tolerance=tolerance
2067 )
-> 2068 result = self.isel(indexers=pos_indexers, drop=drop)
2069 return result._overwrite_indexes(new_indexes)
2070
~/opt/anaconda3/lib/python3.7/site-packages/xarray/core/dataset.py in isel(self, indexers, drop, **indexers_kwargs)
1933 var_indexers = {k: v for k, v in indexers.items() if k in var_value.dims}
1934 if var_indexers:
-> 1935 var_value = var_value.isel(var_indexers)
1936 if drop and var_value.ndim == 0 and var_name in coord_names:
1937 coord_names.remove(var_name)
~/opt/anaconda3/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):
~/opt/anaconda3/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:
~/opt/anaconda3/lib/python3.7/site-packages/xarray/core/variable.py in _broadcast_indexes(self, key)
540
541 if all(isinstance(k, BASIC_INDEXING_TYPES) for k in key):
--> 542 return self._broadcast_indexes_basic(key)
543
544 self._validate_indexers(key)
~/opt/anaconda3/lib/python3.7/site-packages/xarray/core/variable.py in _broadcast_indexes_basic(self, key)
568 dim for k, dim in zip(key, self.dims) if not isinstance(k, integer_types)
569 )
--> 570 return dims, BasicIndexer(key), None
571
572 def _validate_indexers(self, key):
~/opt/anaconda3/lib/python3.7/site-packages/xarray/core/indexing.py in __init__(self, key)
369 k = int(k)
370 elif isinstance(k, slice):
--> 371 k = as_integer_slice(k)
372 else:
373 raise TypeError(
~/opt/anaconda3/lib/python3.7/site-packages/xarray/core/indexing.py in as_integer_slice(value)
344
345 def as_integer_slice(value):
--> 346 start = as_integer_or_none(value.start)
347 stop = as_integer_or_none(value.stop)
348 step = as_integer_or_none(value.step)
~/opt/anaconda3/lib/python3.7/site-packages/xarray/core/indexing.py in as_integer_or_none(value)
340
341 def as_integer_or_none(value):
--> 342 return None if value is None else operator.index(value)
343
344
I want to select the entire data, because eventually I want to subtract the entire array from a bigger data base with a wider grid. This bigger data base is a NETCDF file as well. And for that one, I managed to slice the longitude with the exact same code I am trying on this smaller data set where I get the error. The only difference is, that the bigger NETCDF uses a float32 format. I don't suspect this could cause the error.
Any help is appreciated. Thank you.
Solution
I think I found the problem.
When I created the netcdf file for the observation, I made a mistake in the createDimension
part, when I named the lon and lat data. Because of this, lat and lon showed up under 'Data variables' in the netcdf file, where they should show up under 'Coordinates'
wrong was something like:
#Specifying dimensions#
f.createDimension('longitude', len(lon_list))
f.createDimension('latitude', len(lat_list))
#Building variables
longitude = f.createVariable('lon', float, ('lon',), zlib=True)
latitude = f.createVariable('lat', float, ('lat',), zlib=True)
data = f.createVariable('data', float, ('lat','lon'), zlib=True)
correct was:
#Specifying dimensions#
f.createDimension('lon', len(lon_list))
f.createDimension('lat', len(lat_list))
#Building variables
longitude = f.createVariable('lon', float, ('lon',), zlib=True)
latitude = f.createVariable('lat', float, ('lat',), zlib=True)
data = f.createVariable('data', float, ('lat','lon'), zlib=True)
Answered By - pwi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.