I'm new to Python and I'm using xarray. My netcdf file contains data with time given in 'days since 0001-01-01 00:00:00', calendar type is Julian. Does anybody know an easy way to convert the time to standard calendar?
我是新手,我使用的是XARRAY。我的netcdf文件包含时间为‘天数自0001-01-01 00:00:00’的数据,日历类型为Julian。有谁知道将时间转换成标准日历的简单方法吗?
Thanks in advance :)
提前感谢:)
xarray.Dataset.convert_calendar()
didn't work for me
对我来说不管用
datetime.datetime.strptime(Dataset.time, "%Y %m %d %H %M")
and similar also did not work.
而类似的做法也没有奏效。
更多回答
优秀答案推荐
You can convert Julian dates to a standard calendar (usually Gregorian) in xarray using cftime
library. Here's a minimal example:
您可以使用cftime库将儒略日期转换为标准日历(通常为公历)。下面是一个最小的例子:
- Install
cftime
: pip install cftime
- Convert time coordinates.
import xarray as xr
import cftime
# Load your dataset
ds = xr.open_dataset('your_file.nc')
# Convert Julian to standard calendar (e.g., Gregorian)
ds['time'] = xr.coding.times.decode_cf_datetime(ds['time'], calendar='gregorian')
# Now ds['time'] is in Gregorian
Note: Replace 'your_file.nc'
with your NetCDF file path.
注意:将‘you_file.nc’替换为您的NetCDF文件路径。
Hope that helps!
希望这能有所帮助!
更多回答
我是一名优秀的程序员,十分优秀!