【发布时间】:2018-07-22 07:09:52
【问题描述】:
我想读取指定时间段的 netCDF 数据。
我尝试读取的ncfile被命名为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 ;
这是我读取这个 ncfile 的脚本。
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
我想在检测开始日期和结束日期的特定时间段之间进行阅读。该怎么做呢?
【问题讨论】:
-
我强烈建议为此使用 xarray;参见例如这个问题/答案展示了使用 xarray 选择时间段是多么容易:stackoverflow.com/questions/51323528/…
-
它可以归结为像
import xarray as xr; nc = xr.open_dataset(file.nc, 'r'); p = nc.sel(time=slice('2015-07-20 00:00', '2015-07-24 23:00'))这样简单的东西 -
@Bart 感谢您的回复。顺便说一句,
type(p)是<xarray.Dataset>。你知道如何提取数据(温度数据)并转换为numpy吗? -
p['temperature'].values这样的东西应该会给你一个普通的 Numpy 数组。 -
@Bart 我明白了!非常感谢。