问题
I'm trying to plot a week of time series data from NetCDF files and coming into some problems.
I'm using the following packages:
import netCDF4
from matplotlib import pyplot as plt
import numpy as np
import xarray as xr
import dask
First I import two .nc files:
ds1 = xr.open_dataset('ERA5_forecast_100V_247_2008.nc')
ds2 = xr.open_dataset('ERA5_analysis_100V_247_2008.nc')
Then I select time and grid location using xarray:
dsloc1 = ds1.sel(time='2008-02-10',longitude=2.2,latitude=48.7,method='nearest')
dsloc2 = ds2.sel(time='2008-02-10',longitude=2.2,latitude=48.7,method='nearest')
Then I plot the two time series:
dsloc1['v100'].plot.line('b-',figsize=(15,10))
dsloc2['v100'].plot.line('y-')
Which produces what I expect:
But, when I try and select a range of dates, I get some errors...
dsloc1 = ds1.sel(time=slice('2008-03-01','2008-03-07'),longitude=2.2,latitude=48.7,method='nearest')
dsloc2 = ds2.sel(time=slice('2008-03-01','2008-03-07'),longitude=2.2,latitude=48.7,method='nearest')
I'm sure it's probably a syntax thing, but I've spent longer than I want trying to work it out.. Any suggestions gratefully received!
[edit] Here is the Traceback:
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<ipython-input-308-c3385fa732ab> in <module>()
1 # select time and grid location (Feb 10th, 2008, near Paris)
----> 2 dsloc1 = ds1.sel(time=slice('2008-03-01','2008-03-07'),longitude=2.2,latitude=48.7,method='nearest')
3 dsloc2 = ds2.sel(time=slice('2008-03-01','2008-03-07'),longitude=2.2,latitude=48.7,method='nearest')
/usr/local/lib/python3.6/site-packages/xarray/core/dataset.py in sel(self, indexers, method, tolerance, drop, **indexers_kwargs)
1507 indexers = either_dict_or_kwargs(indexers, indexers_kwargs, 'sel')
1508 pos_indexers, new_indexes = remap_label_indexers(
-> 1509 self, indexers=indexers, method=method, tolerance=tolerance)
1510 result = self.isel(indexers=pos_indexers, drop=drop)
1511 return result._replace_indexes(new_indexes)
/usr/local/lib/python3.6/site-packages/xarray/core/coordinates.py in remap_label_indexers(obj, indexers, method, tolerance, **indexers_kwargs)
353
354 pos_indexers, new_indexes = indexing.remap_label_indexers(
--> 355 obj, v_indexers, method=method, tolerance=tolerance
356 )
357 # attach indexer's coordinate to pos_indexers
/usr/local/lib/python3.6/site-packages/xarray/core/indexing.py in remap_label_indexers(data_obj, indexers, method, tolerance)
248 else:
249 idxr, new_idx = convert_label_indexer(index, label,
--> 250 dim, method, tolerance)
251 pos_indexers[dim] = idxr
252 if new_idx is not None:
/usr/local/lib/python3.6/site-packages/xarray/core/indexing.py in convert_label_indexer(index, label, index_name, method, tolerance)
132 if method is not None or tolerance is not None:
133 raise NotImplementedError(
--> 134 'cannot use ``method`` argument if any indexers are '
135 'slice objects')
136 indexer = index.slice_indexer(_sanitize_slice_element(label.start),
NotImplementedError: cannot use ``method`` argument if any indexers are slice objects
回答1:
It seems that a sel
using a time slice combined with method='nearest'
is simply not supported:
cannot use
method
argument if any indexers are slice objects
This somehow makes sense, as selecting the nearest
for a slice seems a bit strange.
You can work around this by doing the sel
in two steps, i.e. first select the time slice, and from that time slice select a location (or the other way around). I'm not sure if this is the best solution, but at least it works.
Quick example with some ERA5 data:
import xarray as xr
ds1 = xr.open_dataset('20160502_cabauw_model_fc.nc')
# Works:
dsloc1 = ds1.sel(time='2016-05-02 10:00', longitude=4.9, latitude=51.2, method='nearest')
# Doesn't work:
#dsloc2 = ds1.sel(time=slice('2016-05-02 10:00', '2016-05-02 12:00'), longitude=4.9, latitude=51.2, method='nearest')
# Works:
tmp = ds1.sel(time=slice('2016-05-02 10:00', '2016-05-02 12:00'))
dsloc2 = tmp.sel(longitude=4.9, latitude=51.2, method='nearest')
This results in something like:
In [23]: dsloc2
Out[23]:
<xarray.Dataset>
Dimensions: (level: 137, time: 3)
Coordinates:
longitude float32 4.8
latitude float32 51.3
* level (level) int32 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
* time (time) datetime64[ns] 2016-05-02T10:00:00 2016-05-02T11:00:00 ...
Data variables:
z (time, level) float32 ...
来源:https://stackoverflow.com/questions/51323528/netcdf-time-series-slice-with-python-3