首页 > 解决方案 > 在python中更改netcdf变量中的单位

问题描述

我在 Python 中有一个 netCDF 文件,其中时间变量的单位是包days_since_Jan11900无法读取的xarray。它抛出错误

unable to decode time units 'days_since_Jan11900' with 'the default calendar'. Try opening your dataset with decode_times=False or installing cftime if it is not installed.

我不想使用decode_times,只是将其更改为days_since_0. 这是自 1/1/0000 以来的日子。

如何将 netCDF 中的单位更改为可读的单位?

<class 'netCDF4._netCDF4.Variable'>
float64 days(time)
    units: days_since_Jan11900
    long_name: calendar_days
unlimited dimensions: 
current shape = (87600,)
filling on, default _FillValue of 9.969209968386869e+36 used

标签: pythonspatialnetcdf

解决方案


如果您在 Unix 上,您可以尝试使用 CDO 更改日历。以下应该可以解决您的问题:

cdo -L  -setreftime,0001-01-01  -settaxis,0001-01-01,0:00:00,1day  -setcalendar,standard infile.nc outfile.nc 

我已将时间设置为第 1 年,因为我认为任何有效日历中都没有第 0 年。


推荐阅读