从NETCDF文件转换时间戳(秒罪1.1.1904)
即使有几篇有关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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NetCDF4 Python库有一个方法:
num2date()
。https://unidata.github.io/netcdf4-python/netcdf4-python/python/#num2date 。无需
DateTime
模块。NETCDF4变量包含元数据属性,这些属性描述了输出中的变量:
print(ds ['time'])
##in extim the Time Variableunits
属性。以上应为您提供
dtime
列表中的所有时间。一种简单的方法是:
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 variableunits
attribute.The above should give you all the times in the
dtime
list as datetime objects.A simpler way would be:
dtime = NetCDF4.num2date(ds['time'][:], ds['time].units)