Python 如何使用netCDF4模块仅读取指定时间段的数据?

Python 如何使用netCDF4模块仅读取指定时间段的数据?,python,netcdf,netcdf4,Python,Netcdf,Netcdf4,我想在指定的时间段内读取netCDF数据。 我试图读取的文件名为file.nc,而ncdump-c file.nc的部分信息是 dimensions: lat = 1 ; lon = 1 ; time = UNLIMITED ; // (744 currently) variables: float lat(lat) ; lat:units = "degrees_north" ; lat:long_name = "latitude" ; float lon(lon) ;

我想在指定的时间段内读取netCDF数据。 我试图读取的文件名为
file.nc
,而
ncdump-c file.nc
的部分信息是

dimensions:
lat = 1 ;
lon = 1 ;
time = UNLIMITED ; // (744 currently)
variables:
float lat(lat) ;
    lat:units = "degrees_north" ;
    lat:long_name = "latitude" ;
float lon(lon) ;
    lon:units = "degrees_east" ;
    lon:long_name = "longitude" ;
double time(time) ;
    time:units = "hours since 2015-07-01 01:00:00" ;
    time:long_name = "time" ;
double rain(time, lat, lon) ;
    rain:_FillValue = -999000000. ;
    rain:units = "K" ;
    rain:standard_name = "temperature" 
data:

lat = 1 ;
lon = 1 ;
time = -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, ... 
737, 738, 739, 740, 741, 742 ;
这是我用来读取这个文件的脚本

import netCDF4

nc = netCDF4.Dataset(file.nc, 'r')
data = nc.variables['temperature'][:] #I want to read between 2015-07-20 00:00 to 2015-07-24 23:00

我想通过检测开始日期和结束日期来读取特定期间之间的数据。怎么做?

正如@Bart所建议的那样,xarray是一条出路。这是没有沙雷的答案<答案是code>NetCDF4.date2index()

import netCDF4
import dateutil.parser

nc = netCDF4.Dataset(file.nc, 'r')

# all_times variable includes the time:units attribute
all_times = nci.variables['time']

sdt = dateutil.parser.parse("2015-07-20T00:00:00")
edt = dateutil.parser.parse("2015-07-24T23:00:00")

st_idx = netCDF4.date2index(sdt, all_times)
et_idx = netCDF4.date2index(edt, all_times)

data = nc.variables['temperature'][st_idx:et_idx+1,:] #I want to read between 2015-07-20 00:00 to 2015-07-24 23:00

正如@Bart所言,xarray是一条出路。这是没有沙雷的答案<答案是code>NetCDF4.date2index()

import netCDF4
import dateutil.parser

nc = netCDF4.Dataset(file.nc, 'r')

# all_times variable includes the time:units attribute
all_times = nci.variables['time']

sdt = dateutil.parser.parse("2015-07-20T00:00:00")
edt = dateutil.parser.parse("2015-07-24T23:00:00")

st_idx = netCDF4.date2index(sdt, all_times)
et_idx = netCDF4.date2index(edt, all_times)

data = nc.variables['temperature'][st_idx:et_idx+1,:] #I want to read between 2015-07-20 00:00 to 2015-07-24 23:00

我强烈建议使用xarray进行此操作;例如,请参见此问题/答案,以演示使用xarray选择时间段是多么容易:它可以归结为像导入xarray as xr这样简单的东西;nc=xr.open_数据集(file.nc,'r');p=nc.sel(time=slice('2015-07-20 00:00','2015-07-24 23:00'))@Bart谢谢您的回复。顺便说一下,
类型(p)
。你知道如何提取数据(温度数据)并转换成
numpy
?类似于
p['temperature']。值应该会给你一个正常的numpy数组。@Bart我知道了!非常感谢。我强烈建议您使用xarray;例如,请参见此问题/答案,以演示使用xarray选择时间段是多么容易:它可以归结为像导入xarray as xr这样简单的东西;nc=xr.open_数据集(file.nc,'r');p=nc.sel(time=slice('2015-07-20 00:00','2015-07-24 23:00'))
@Bart谢谢您的回复。顺便说一下,
类型(p)
。你知道如何提取数据(温度数据)并转换成
numpy
?类似于
p['temperature']。值应该会给你一个正常的numpy数组。@Bart我知道了!非常感谢。我不知道
date2index
,不错的选择。我使用NetCDF4的解决方案(我不敢发布)稍微不那么优雅;-)@Eric Bridger我也按照你的建议做了。感谢您的回复。不知道
date2index
,不错的选项。我使用NetCDF4的解决方案(我不敢发布)稍微不那么优雅;-)@Eric Bridger我也按照你的建议做了。谢谢你的回复。