How fix the error "ValueError: Julian Day must be positive" in netCDF4.num2date?

How fix the error "ValueError: Julian Day must be positive" in netCDF4.num2date?

部分代码如下:

import netCDF4
import pandas as pd
import matplotlib.pyplot as plt

file='/Users/dedeco/Downloads/_grib2netcdf-atls12-95e2cf679cd58ee9b4db4dd119a05a8d-OzkfHp.nc'
nc = netCDF4.Dataset(file)
nc.variables.keys()

lat = nc.variables['latitude'][:]
lon = nc.variables['longitude'][:]
time_var = nc.variables['time']
dtime = netCDF4.num2date(time_var[:],time_var.units)

可以在link下载文件:https://stream.ecmwf.int/data/atls12/data/data01/scratch/84/bc/_grib2netcdf-atls12-95e2cf679cd58ee9b4db4dd119a05a8d-OzkfHp.nc

所以,我得到了这个错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-47-3647c36af24c> in <module>()
      2 lon = nc.variables['longitude'][:]
      3 time_var = nc.variables['time']
----> 4 dtime = netCDF4.num2date(time_var[:],time_var.units)

cftime/_cftime.pyx in cftime._cftime.num2date()

cftime/_cftime.pyx in cftime._cftime.utime.num2date()

cftime/_cftime.pyx in cftime._cftime.DateFromJulianDay()

ValueError: Julian Day must be positive

我该如何解决?有什么想法吗?

我修复了设置参数的问题(默认是标准的):calendar:描述时间计算中使用的日历。

所以替换这个:

dtime = netCDF4.num2date(time_var[:],time_var.units)

by(在这种情况下,一年有 365 天):

dtime = netCDF4.num2date(time_var[:],time_var.units,'365_day')

Here is the documentation如下:

def date2num(

...)

date2num(dates,units,calendar='standard')

Return numeric time values given datetime objects. The units of the numeric time values are described by the units argument and the calendar keyword. The datetime objects must be in UTC with no time-zone offset. If there is a time-zone offset in units, it will be applied to the returned numeric values.

dates: A datetime object or a sequence of datetime objects. The datetime objects should not include a time-zone offset.

units: a string of the form since describing the time units. can be days, hours, minutes, seconds, milliseconds or microseconds. is the time origin.

calendar: describes the calendar used in the time calculations. All the values currently defined in the CF metadata convention Valid calendars 'standard', 'gregorian', 'proleptic_gregorian' 'noleap', '365_day', '360_day', 'julian', 'all_leap', '366_day'. Default is 'standard', which is a mixed Julian/Gregorian calendar.

returns a numeric time value, or an array of numeric time values with approximately millisecond accuracy.

可以找到关于转换的补充理解 here