python – 如何在ecmwf文件上读取日期和时间

我在netcdf文件中有全局数据集.数据文件的时间信息是:

<type 'netCDF4._netCDF4.Variable'>
int32 time(time)
    units: hours since 1900-01-01 00:00:0.0
    long_name: time
    calendar: gregorian
unlimited dimensions: time
current shape = (5875,)
filling off

当我从文件中提取时间时,我得到了这个数组:

array([ 876600,  876624,  876648, ..., 1017528, 1017552, 1017576], dtype=int32) 

我的问题是如何将此数组转换为正确的日期格式?
[注意:这是每日数据集,数组中的数字对应于1900-01-01的小时数]

最佳答案 这样做的理想方法是使用
netCDF4 num2date

import netCDF4

ncfile = netCDF4.Dataset('./foo.nc', 'r')
time = ncfile.variables['time']
dates = netCDF4.num2date(time[:], time.units, time.calendar)
点赞