我正在尝试从 NetCDF 文件绘制一周的时间序列数据,并遇到一些问题。
我正在使用以下软件包:
import netCDF4
from matplotlib import pyplot as plt
import numpy as np
import xarray as xr
import dask
首先,我导入两个.nc文件:
ds1 = xr.open_dataset('ERA5_forecast_100V_247_2008.nc')
ds2 = xr.open_dataset('ERA5_analysis_100V_247_2008.nc')
然后我使用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')
然后我绘制两个时间序列:
dsloc1['v100'].plot.line('b-',figsize=(15,10))
dsloc2['v100'].plot.line('y-')
达到我的期望的
但是,当我尝试选择日期范围时,出现了一些错误...
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')
我确定这可能是语法问题,但是我花了比想解决的时间更长的时间。.感激收到任何建议!
[edit]这是回溯:
---------------------------------------------------------------------------
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
答案 0 :(得分:1)
似乎不支持将时间片与sel
结合使用的method='nearest'
:
如果任何索引器是切片对象,则不能使用
method
参数
从某种意义上讲,为切片选择nearest
似乎有些奇怪。
您可以通过分两步执行sel
来解决此问题,即首先选择时间片,然后从该时间片中选择一个位置(或相反)。我不确定这是否是最好的解决方案,但至少可以奏效。
带有一些ERA5数据的简单示例:
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')
结果如下:
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 ...