如何使用netCDF4模块仅在指定期间读取数据?

时间:2018-07-20 09:44:03

标签: python netcdf netcdf4

我想读取netCDF数据一段指定的时间。 我尝试读取ncfile的名称为ItemsControl.Resourcesfile.nc的信息部分为

ncdump -c file.nc

这是我读取此ncfile的脚本。

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 ;

我想在检测开始日期和结束日期的特定时间段之间进行阅读。怎么学呢?

1 个答案:

答案 0 :(得分:3)

正如@Bart所建议的,xarray是必经之路。这是没有xarray的答案。 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