如何在多个x,y和时间值上切片X数组netcdf数据集?

时间:2019-02-05 20:34:16

标签: python netcdf python-xarray

我已经创建了一个函数,可以使用X数组为单个x,y和时间选择netcdf数据,但是如何输入多个x,y,time值(即一条路线)并从netcdf返回值呢?即一系列x,y,time数组的风速。

我能想到的唯一方法是循环遍历x,y,time数组并每次都运行函数,但这似乎效率很低。关于最佳方法有什么建议吗?

我的输入数据集是:

| x y timestamp | | 0 77.49316 6.320333 2018-08-01 00:10:00+00:00 | 1 77.64450 6.287833 2018-08-01 00:44:00+00:00 | 2 77.98483 6.202500 2018-08-01 02:02:00+00:00

,我想将netcdf变量作为列返回。以下是我为单个位置选择的代码。

# Scan the folder for netcdf datasets and return variables
def listmodelvars(model):
    varlist = xr.open_mfdataset(os.path.join(model, '*.nc'))
    print(varlist.data_vars.keys())
    return varlist    

ds = listmodelvars(ncep_model)
KeysView(Data variables:
    tmp2m     (time, latitude, longitude) float64 dask.array<shape=(270, 311, 720), chunksize=(9, 311, 720)>
    rh2m      (time, latitude, longitude) float64 dask.array<shape=(270, 311, 720), chunksize=(9, 311, 720)>
    tmpsfc    (time, latitude, longitude) float64 dask.array<shape=(270, 311, 720), chunksize=(9, 311, 720)>
    pratesfc  (time, latitude, longitude) float64 dask.array<shape=(270, 311, 720), chunksize=(9, 311, 720)>
    prmslmsl  (time, latitude, longitude) float64 dask.array<shape=(270, 311, 720), chunksize=(9, 311, 720)>
    ugrd10m   (time, latitude, longitude) float64 dask.array<shape=(270, 311, 720), chunksize=(9, 311, 720)>
    vgrd10m   (time, latitude, longitude) float64 dask.array<shape=(270, 311, 720), chunksize=(9, 311, 720)>)
In [4]:

# Variables    
parameter = 'tmp2m'
start_date = '2018-08-01'
end_date = '2018-08-31'
lat = 0
lon = 0    

# Define a function to return time-series data for a single location    
def array_2d(model, parameter, start_date, end_date, lat, lon):
    data = xr.open_mfdataset(os.path.join(str(model), '*.nc'))[str(parameter)].sel(
        time=slice(str(start_date) + 'T00:00:00', str(end_date) + 'T00:00:00'), 
        latitude=lat, longitude=lon)
    return data    

data_2d = array_2d(ncep_model, parameter, start_date, end_date, lat, lon)

0 个答案:

没有答案