如何将cftime转换为unix时间戳?

问题描述 投票:0回答:1

我正在寻找一些方便的方法来将 netcdf 文件中可用的 cftime 格式的时间转换为 unix 时间戳(毫秒),什么是一种合适的方法来做到这一点,而不需要几个

for
循环,提取日期和时间字符串,然后是
datetime 
python 中的类型转换,以及最终获得 unix 时间戳(毫秒)的其他几个步骤。在处理时间值数组时,使用多个
for
循环来完成看似简单的操作确实令人畏惧。

如果

cftime
datetime
模块中有任何可用的库或功能,我将非常感激。

这是我的初始数据的输出,格式为

cftime

<xarray.DataArray 'time' (time: 227)>
array([ 107. ,  129.5,  227.5, ..., 7928. , 7958.5, 7989. ], dtype=float32)
Coordinates:
  * time     (time) float32 107.0 129.5 227.5 ... 7.928e+03 7.958e+03 7.989e+03
Attributes:
bounds:         time_bounds
calendar:       gregorian
axis:           T
standard_name:  Time
long_name:      Time
Units:          days since 2002-01-01T00:00:00

当我使用

xarray.decode_cf(dataset)
时,显示了数组,但它仍然是随机的,我无法弄清楚这些数字的含义。

这是

xarray.decode_cf()
操作后的示例数组:

[107.   129.5  227.5  258.   288.5  319.   349.5  380.5  410.   439.5 470.   495.5  561.5  592.5]

python datetime timestamp python-xarray netcdf4
1个回答
0
投票

说明我的评论,

import xarray as xr

# note that "units" must not me title-case:
attrs = {"units": "days since 2002-01-01T00:00:00"}
ds = xr.Dataset({"time": ("time", [107. ,  129.5,  227.5, 7928. , 7958.5, 7989.0], attrs)})

# now we have datetime:
xr.decode_cf(ds)
<xarray.Dataset>
Dimensions:  (time: 6)
Coordinates:
  * time     (time) datetime64[ns] 2002-04-18 2002-05-10T12:00:00 ... 2023-11-16
Data variables:
    *empty*

xr.decode_cf(ds).time
<xarray.DataArray 'time' (time: 6)>
array(['2002-04-18T00:00:00.000000000', '2002-05-10T12:00:00.000000000',
       '2002-08-16T12:00:00.000000000', '2023-09-16T00:00:00.000000000',
       '2023-10-16T12:00:00.000000000', '2023-11-16T00:00:00.000000000'],
      dtype='datetime64[ns]')
Coordinates:
  * time     (time) datetime64[ns] 2002-04-18 2002-05-10T12:00:00 ... 2023-11-16

# to get Unix time in ms, we can convert to integer, giving us Unix time in 
# nanoseconds, then divide by 1e6 to get milliseconds:
xr.decode_cf(ds).time.astype(int)//1_000_000
<xarray.DataArray 'time' (time: 6)>
array([1019088000000, 1021032000000, 1029499200000, 1694822400000,
       1697457600000, 1700092800000])
Coordinates:
  * time     (time) datetime64[ns] 2002-04-18 2002-05-10T12:00:00 ... 2023-11-16
© www.soinside.com 2019 - 2024. All rights reserved.