使用Python 3的NetCDF时间序列切片 [英] NetCDF Time series slice with Python 3
问题描述
我正在尝试从 NetCDF 文件绘制一周的时间序列数据,并遇到一些问题.
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
首先,我导入两个.nc文件:
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')
然后我使用xarray选择时间和网格位置:
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-')
哪个能达到我的期望:
但是,当我尝试选择日期范围时,出现了一些错误...
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]这是回溯:
[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
推荐答案
似乎不支持将时间片与method='nearest'
结合使用的sel
:
It seems that a sel
using a time slice combined with method='nearest'
is simply not supported:
如果任何索引器是切片对象,则不能使用
method
参数
这在某种程度上是有道理的,因为为切片选择nearest
似乎有点奇怪.
This somehow makes sense, as selecting the nearest
for a slice seems a bit strange.
您可以通过分两个步骤执行sel
来解决此问题,即首先选择时间片,然后从该时间片中选择一个位置(或相反).我不确定这是否是最好的解决方案,但至少可以奏效.
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.
带有一些ERA5数据的简单示例:
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')
结果如下:
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 ...
这篇关于使用Python 3的NetCDF时间序列切片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!