英文:
Time missmatch when using NetCDF4
问题
我有一个带有时间变量time
的netcdf文件。当我打印这个变量时,我得到了以下结果:
<class 'netCDF4._netCDF4.Variable'>
float32 time(time)
long_name: time
units: hours since 1900-01-01 00:00:00.0
calendar: gregorian
unlimited dimensions: time
current shape = (31,)
filling on, default _FillValue of 9.969209968386869e+36 used
我想要查看存储在time
变量中的日期。如果我键入并运行print(time[:])
,我将无法真正看到time
变量的日期。相反,我将看到以下内容:
[1004406. 1004430. 1004454. 1004478. 1004502. 1004526. 1004550. 1004574.
1004598. 1004622. 1004646. 1004670. 1004694. 1004718. 1004742. 1004766.
1004790. 1004814. 1004838. 1004862. 1004886. 1004910. 1004934. 1004958.
1004982. 1005006. 1005030. 1005054. 1005078. 1005102. 1005126.]
现在,这是我用来以日期格式查看时间所做的事情:
dtime = nc.num2date(time[:], time.units)
然而,当我打印dtime
时,这是我的结果:
[cftime.DatetimeGregorian(2014, 8, 1, 6, 0, 0, 0, has_year_zero=False)
cftime.DatetimeGregorian(2014, 8, 2, 6, 0, 0, 0, has_year_zero=False)
cftime.DatetimeGregorian(2014, 8, 3, 6, 0, 0, 0, has_year_zero=False)
cftime.DatetimeGregorian(2014, 8, 4, 6, 0, 0, 0, has_year_zero=False)
cftime.DatetimeGregorian(2014, 8, 5, 6, 0, 0, 0, has_year_zero=False)
cftime.DatetimeGregorian(2014, 8, 6, 6, 0, 0, 0, has_year_zero=False)
cftime.DatetimeGregorian(2014, 8, 7, 6, 0, 0, 0, has_year_zero=False)
cftime.DatetimeGregorian(2014, 8, 8, 6, 0, 0, 0, has_year_zero=False)
cftime.DatetimeGregorian(2014, 8, 9, 6, 0, 0, 0, has_year_zero=False)...
为什么它显示的是2014而不是1990,如何使用netCDF4修复这个问题?我不知道后台发生了什么。
英文:
I have a netcdf file with a time variable time
. When I print this variable I get this:
<class 'netCDF4._netCDF4.Variable'>
float32 time(time)
long_name: time
units: hours since 1900-01-01 00:00:00.0
calendar: gregorian
unlimited dimensions: time
current shape = (31,)
filling on, default _FillValue of 9.969209968386869e+36 used
I would like to see the dates stored in my time
variable. If I type and run print(time[:])
I will not really be able to see the dates of my time
variable. Instead I will see this:
[1004406. 1004430. 1004454. 1004478. 1004502. 1004526. 1004550. 1004574.
1004598. 1004622. 1004646. 1004670. 1004694. 1004718. 1004742. 1004766.
1004790. 1004814. 1004838. 1004862. 1004886. 1004910. 1004934. 1004958.
1004982. 1005006. 1005030. 1005054. 1005078. 1005102. 1005126.]
Now, This is what I have done to see the time in a date format:
dtime = nc.num2date(time[:],time.units)
However, when I print dtime
this is my result:
[cftime.DatetimeGregorian(2014, 8, 1, 6, 0, 0, 0, has_year_zero=False)
cftime.DatetimeGregorian(2014, 8, 2, 6, 0, 0, 0, has_year_zero=False)
cftime.DatetimeGregorian(2014, 8, 3, 6, 0, 0, 0, has_year_zero=False)
cftime.DatetimeGregorian(2014, 8, 4, 6, 0, 0, 0, has_year_zero=False)
cftime.DatetimeGregorian(2014, 8, 5, 6, 0, 0, 0, has_year_zero=False)
cftime.DatetimeGregorian(2014, 8, 6, 6, 0, 0, 0, has_year_zero=False)
cftime.DatetimeGregorian(2014, 8, 7, 6, 0, 0, 0, has_year_zero=False)
cftime.DatetimeGregorian(2014, 8, 8, 6, 0, 0, 0, has_year_zero=False)
cftime.DatetimeGregorian(2014, 8, 9, 6, 0, 0, 0, has_year_zero=False)...
Why is it showing 2014 instead of 1990 and how can I fix this using ndtcdf4?
I do not know what is happening on the background.
答案1
得分: 1
时间值是hours since 1900-01-01 00:00:00.0
,这意味着,如果你拿第一个值1004406,它应该对应于:
import datetime
datetime.datetime(1900, 1, 1) + datetime.timedelta(seconds=1004406 * 3600)
也就是datetime.datetime(2014, 8, 1, 6, 0)
,与您获取的结果完全相同。为什么是1990而不是2014呢?
我建议使用以下选项使用num2date
:
only_use_cftime_datetimes=False
和 only_use_python_datetimes=True
。
后者会将 dtime
转换为 datetime
对象,这些对象可以轻松与 matplotlib
一起使用。不知何故,cftime
不太容易支持...
因此,在您的脚本中:
dtime = nc.num2date(time[:], time.units, only_use_cftime_datetimes=False, only_use_python_datetimes=True)
英文:
Well, the time values are hours since 1900-01-01 00:00:00.0
meaning that for instance if you take the first value 1004406, it should correspond to:
import datetime
datetime.datetime(1900,1,1,)+datetime.timedelta(seconds=1004406*3600)
i.e. datetime.datetime(2014,8,1,6,0)
exactly as you get it. Why should it be 1990 instead of 2014?
I suggest using num2date
with following options:
only_use_cftime_datetimes=False and only_use_python_datetimes=True.
Latter would give dtime
as datetime
objects, which can be easily used with matplotlib
. Somehow the cftime
are not so easily supported...
So, in your script:
dtime = nc.num2date(time[:],time.units,only_use_cftime_datetimes=False,only_use_python_datetimes=True)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论