简体   繁体   English

带有Python 3的NetCDF时间序列切片

[英]NetCDF Time series slice with Python 3

I'm trying to plot a week of time series data from NetCDF files and coming into some problems. 我试图从NetCDF文件中绘制一个星期的时间序列数据,并遇到一些问题。

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: 首先,我导入两个.nc文件:

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: 然后我使用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: 哪个产生我的期望:

2008年2月10日

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

It seems that a sel using a time slice combined with method='nearest' is simply not supported: 似乎不支持使用时间片结合method='nearest'sel

cannot use method argument if any indexers are slice objects 如果任何索引器是切片对象,则无法使用method参数

This somehow makes sense, as selecting the nearest for a slice seems a bit strange. 这在某种程度上是有道理的,因为为切片选择nearest的位置似乎有些奇怪。

You can work around this by doing the sel in two steps, ie first select the time slice, and from that time slice select a location (or the other way around). 您可以通过分两步进行sel来解决此问题,即首先选择时间片,然后从该时间片中选择一个位置(或相反)。 I'm not sure if this is the best solution, but at least it works. 我不确定这是否是最好的解决方案,但至少可以奏效。

Quick example with some ERA5 data: 具有一些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')

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 ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM