You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ERA5再分析数据netCDF文件时间单位与请求不匹配的解决方法

问题描述

从ERA5再分析数据集「1940年至今逐小时单层次再分析数据」下载2024年6月的10m风场(10m_u_component_of_wind、10m_v_component_of_wind)数据后,在Anaconda Spyder中查看.nc文件时,发现valid_time变量的单位为seconds since 1970-01-01,日历为proleptic_gregorian,看起来与请求的2024年时间不符,怀疑是日历设置导致问题,寻求解决方法。

下载请求代码

import cdsapi

dataset = "reanalysis-era5-single-levels"
request = {
'product_type': ['reanalysis'],
'variable': ['10m_u_component_of_wind', '10m_v_component_of_wind'],
'year': ['2024'],
'month': ['06'],
'day': ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30'],
'time': ['00:00', '01:00', '02:00', '03:00', '04:00', '05:00', '06:00', '07:00', '08:00', '09:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00'],
'data_format': 'netcdf',
'download_format': 'unarchived',
'area': [38, 61, 23, 81]
}

client = cdsapi.Client()
client.retrieve(dataset, request).download()

查看的valid_time变量信息

<class 'netCDF4._netCDF4.Variable'>
int64 valid_time(valid_time)
long_name: time
standard_name: time
units: seconds since 1970-01-01
calendar: proleptic_gregorian
unlimited dimensions:
current shape = (720,)
filling off

valid_time.dimensions
Out[88]: ('valid_time',)

valid_time.units
Out[89]: 'seconds since 1970-01-01'
解决方法

这不是日历设置的问题,ERA5数据默认采用Unix时间戳(以1970-01-01为起始点的秒数)存储时间,proleptic_gregorian是国际标准的公历日历,和实际日期完全一致。这种存储方式是气象数据的常规操作,目的是节省存储空间并提升处理效率。

只需将数值时间转换为可读的datetime格式即可验证数据确实是2024年6月的,以下是两种常用方法:

方法1:使用netCDF4库转换

from netCDF4 import Dataset, num2date

# 替换为你的nc文件路径
nc_path = "下载的文件名.nc"
with Dataset(nc_path, 'r') as nc_file:
    valid_time_data = nc_file.variables['valid_time'][:]
    # 转换为datetime数组
    time_datetime = num2date(valid_time_data, 
                            units=nc_file.variables['valid_time'].units,
                            calendar=nc_file.variables['valid_time'].calendar)
    # 打印前5个时间点确认
    print("转换后的时间示例:")
    print(time_datetime[:5])

方法2:使用xarray库自动解析(更简便)

xarray会自动识别时间单位并转换为datetime格式,无需手动处理:

import xarray as xr

# 替换为你的nc文件路径
ds = xr.open_dataset("下载的文件名.nc")
# 直接查看转换后的时间
print("xarray自动解析的时间:")
print(ds.valid_time)

执行上述代码后,你会看到valid_time对应的是2024年6月的逐小时时间,数据完全符合你的请求。

内容的提问来源于stack exchange,提问作者Shahid Hussain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 23:18:19