从NETCDF文件转换时间戳(秒罪1.1.1904)

发布于 2025-01-27 02:34:27 字数 1479 浏览 4 评论 0原文

即使有几篇有关NetCDF文件和时间戳转换的帖子,我今天还是空白了。

我 在NetCDF数据集(版本3)中阅读,然后在调用变量信息之后:

# Load required Python packages
import netCDF4 as nc
import matplotlib.pyplot as plt
from datetime import datetime, timedelta
import pandas as pd

#read in a NetCDF data set
ds = nc.Dataset(fn)

# call time variable information
print(ds['time'])

作为答案,我得到:

<class 'netCDF4._netCDF4.Variable'>
float64 time(time)
    units: seconds since 1904-01-01 00:00:00.000 00:00
    long_name: time UTC
    axis: T
unlimited dimensions: time
current shape = (5760,)
filling on, default _FillValue of 9.969209968386869e+36 used

现在我想将自1.1.1904时邮票以来的秒转换为dd.mm.m.yyyy HH:MM:SS。 SSS格式。 (顺便说一句:为什么在时间戳记之后包含第二个00:00信息?)

(1)我尝试了:

t = ds['time'][:]
dtime = []
dtime = (pd.to_datetime(t, format='%d.%m.%Y %H:%M:%S.micros') - datetime(1904, 1, 1)).total_seconds()

我得到了错误: pandas_libs \ tslibs \ strptime.pyx in pandas._libs.tslibs.strptime.array_strptime() 时间数据“ 3730320000”不匹配格式'%d。%m。%y%h:%m:%s'(匹配)

(2)我尝试过:

d = datetime.strptime("01-01-1904", "%m-%d-%Y")
dt = d + timedelta(seconds=(t))

我得到了 TypeError:未支撑的类型为Quitsedelta秒组件:MaskEdarray

(3)我尝试了

d = datetime.strptime("%m-%d-%Y", "01-01-1904")
dt = d + timedelta(seconds=(ds['time']))

,我得到了答案: timedelta秒的不支持类型组件:netcdf4._netcdf4.varaible

对解决方案的看法比我目前更清晰吗?

谢谢, 斯瓦瓦

even there are several posts concerning NetCDF files and timestamp conversion I draw a blank today.

I
read in a NetCDF data set (version 3), and after I call variables information:

# Load required Python packages
import netCDF4 as nc
import matplotlib.pyplot as plt
from datetime import datetime, timedelta
import pandas as pd

#read in a NetCDF data set
ds = nc.Dataset(fn)

# call time variable information
print(ds['time'])

As answer I get:

<class 'netCDF4._netCDF4.Variable'>
float64 time(time)
    units: seconds since 1904-01-01 00:00:00.000 00:00
    long_name: time UTC
    axis: T
unlimited dimensions: time
current shape = (5760,)
filling on, default _FillValue of 9.969209968386869e+36 used

Now I would like to transform the seconds since 1.1.1904 time stamp into a DD.MM.YYYY HH:MM:SS.sss format. (by the way: why is there a second 00:00 information included after the time stamp?)

(1) I tried:

t = ds['time'][:]
dtime = []
dtime = (pd.to_datetime(t, format='%d.%m.%Y %H:%M:%S.micros') - datetime(1904, 1, 1)).total_seconds()

And I get the error:
pandas_libs\tslibs\strptime.pyx in pandas._libs.tslibs.strptime.array_strptime()
time data '3730320000' does not match format '%d.%m.%Y %H:%M:%S' (match)

(2) I tried:

d = datetime.strptime("01-01-1904", "%m-%d-%Y")
dt = d + timedelta(seconds=(t))

I get the
TypeError: unsupported type for timedelta seconds component: MaskedArray

(3) I tried

d = datetime.strptime("%m-%d-%Y", "01-01-1904")
dt = d + timedelta(seconds=(ds['time']))

And I get the answer:
unsupported type for timedelta seconds component: netCDF4._netCDF4.Variable

Has somebody a clearer view on the solution than I have at the moment?

Thanks,
Swawa

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

琉璃繁缕 2025-02-03 02:34:27

NetCDF4 Python库有一个方法:num2date()
https://unidata.github.io/netcdf4-python/netcdf4-python/python/#num2date 。无需DateTime模块。
NETCDF4变量包含元数据属性,这些属性描述了输出中的变量:

print(ds ['time'])##in extim the Time Variable units属性。

# t contains just the numeric values of the time in `seconds since 1904-01-01 00:00:00.000 00:00`

t = ds['time'][:]
dtime = []
# t_var is the NetCDF4 variable which has the `units` attribute.
t_var = ds.['time']
#dtime = (pd.to_datetime(t, format='%d.%m.%Y %H:%M:%S.micros') - datetime(1904, 1, 1)).total_seconds()
dtime = NetCDF4.num2date(t, t_var.units)

以上应为您提供dtime列表中的所有时间。

print(dtime[0].isoformat())
print(dtime[-1].isoformat())

一种简单的方法是:

dtime = netcdf4.num2date(ds ['time'] [:],ds ['time] .units)

The NetCDF4 python library has a method for this: num2date().
https://unidata.github.io/netcdf4-python/#num2date. No need for datetime module.
NetCDF4 variables contain metadata attributes which describe the variable as seen in the output to your print:

print(ds['time']) #In particular the time variable units attribute.

# t contains just the numeric values of the time in `seconds since 1904-01-01 00:00:00.000 00:00`

t = ds['time'][:]
dtime = []
# t_var is the NetCDF4 variable which has the `units` attribute.
t_var = ds.['time']
#dtime = (pd.to_datetime(t, format='%d.%m.%Y %H:%M:%S.micros') - datetime(1904, 1, 1)).total_seconds()
dtime = NetCDF4.num2date(t, t_var.units)

The above should give you all the times in the dtime list as datetime objects.

print(dtime[0].isoformat())
print(dtime[-1].isoformat())

A simpler way would be:

dtime = NetCDF4.num2date(ds['time'][:], ds['time].units)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文