gpt4 book ai didi

Easy way to convert Julian calendar to standard with Python xarray?(简单的方法来转换儒略历到标准的Python xarray?)

转载 作者:bug小助手 更新时间:2023-10-25 19:46:00 33 4
gpt4 key购买 nike



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库将儒略日期转换为标准日历(通常为公历)。下面是一个最小的例子:



  1. Install cftime: pip install cftime

  2. 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!

希望这能有所帮助!


更多回答

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com