Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cftime 1.3.0 has value error that 1.2.0 doesn not #210

Closed
guidov opened this issue Dec 2, 2020 · 4 comments
Closed

cftime 1.3.0 has value error that 1.2.0 doesn not #210

guidov opened this issue Dec 2, 2020 · 4 comments

Comments

@guidov
Copy link

guidov commented Dec 2, 2020

When updating to cftime=1.3.0, xarray plot throws a value error now. (downgrading to 1.2.0 doesn not have the same problem)

datetime variable looks like
array(cftime.DatetimeNoLeap(2114, 12, 16, 0, 0, 0, 0), dtype=object)


ValueError Traceback (most recent call last)
~/anaconda3/lib/python3.8/site-packages/ipykernel/pylab/backend_inline.py in show(close, block)
41 display(
42 figure_manager.canvas.figure,
---> 43 metadata=_fetch_figure_metadata(figure_manager.canvas.figure)
44 )
45 finally:

~/anaconda3/lib/python3.8/site-packages/ipykernel/pylab/backend_inline.py in _fetch_figure_metadata(fig)
178 if _is_transparent(fig.get_facecolor()):
179 # the background is transparent
--> 180 ticksLight = _is_light([label.get_color()
181 for axes in fig.axes
182 for axis in (axes.xaxis, axes.yaxis)

~/anaconda3/lib/python3.8/site-packages/ipykernel/pylab/backend_inline.py in (.0)
181 for axes in fig.axes
182 for axis in (axes.xaxis, axes.yaxis)
--> 183 for label in axis.get_ticklabels()])
184 if ticksLight.size and (ticksLight == ticksLight[0]).all():
185 # there are one or more tick labels, all with the same lightness

~/anaconda3/lib/python3.8/site-packages/matplotlib/axis.py in get_ticklabels(self, minor, which)
1318 if minor:
1319 return self.get_minorticklabels()
-> 1320 return self.get_majorticklabels()
1321
1322 def get_majorticklines(self):

~/anaconda3/lib/python3.8/site-packages/matplotlib/axis.py in get_majorticklabels(self)
1274 def get_majorticklabels(self):
1275 'Return a list of Text instances for the major ticklabels.'
-> 1276 ticks = self.get_major_ticks()
1277 labels1 = [tick.label1 for tick in ticks if tick.label1.get_visible()]
1278 labels2 = [tick.label2 for tick in ticks if tick.label2.get_visible()]

~/anaconda3/lib/python3.8/site-packages/matplotlib/axis.py in get_major_ticks(self, numticks)
1429 'Get the tick instances; grow as necessary.'
1430 if numticks is None:
-> 1431 numticks = len(self.get_majorticklocs())
1432
1433 while len(self.majorTicks) < numticks:

~/anaconda3/lib/python3.8/site-packages/matplotlib/axis.py in get_majorticklocs(self)
1346 def get_majorticklocs(self):
1347 """Get the array of major tick locations in data coordinates."""
-> 1348 return self.major.locator()
1349
1350 def get_minorticklocs(self):

~/anaconda3/lib/python3.8/site-packages/nc_time_axis/init.py in call(self)
136 def call(self):
137 vmin, vmax = self.axis.get_view_interval()
--> 138 return self.tick_values(vmin, vmax)
139
140 def tick_values(self, vmin, vmax):

~/anaconda3/lib/python3.8/site-packages/nc_time_axis/init.py in tick_values(self, vmin, vmax)
154 # appropriate.
155 years = self._max_n_locator.tick_values(lower.year, upper.year)
--> 156 ticks = [cftime.datetime(int(year), 1, 1) for year in years]
157 elif resolution == 'MONTHLY':
158 # TODO START AT THE BEGINNING OF A DECADE/CENTURY/MILLENIUM as

~/anaconda3/lib/python3.8/site-packages/nc_time_axis/init.py in (.0)
154 # appropriate.
155 years = self._max_n_locator.tick_values(lower.year, upper.year)
--> 156 ticks = [cftime.datetime(int(year), 1, 1) for year in years]
157 elif resolution == 'MONTHLY':
158 # TODO START AT THE BEGINNING OF A DECADE/CENTURY/MILLENIUM as

cftime/_cftime.pyx in cftime._cftime.datetime.init()

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

ValueError: invalid year provided in cftime.datetime(0, 1, 1, 0, 0, 0, 0, calendar='gregorian')

@spencerkclark
Copy link
Collaborator

This looks to be related to #202, which caused the cftime.datetime constructor to create calendar-aware datetimes (with a Gregorian calendar by default). @jswhit started on a PR in nc-time-axis to fix plotting issues related to this, SciTools/nc-time-axis#51, but I think some additional changes will be required there (e.g. a calendar argument will need to be added here).

@jswhit
Copy link
Collaborator

jswhit commented Dec 6, 2020

You don't say what code actually triggers this error, but as @spencerkclark suggested it looks to be coming from matplotlib, or the nc-time-axis toolkit. That code appears to trying to create a calendar instance with year zero for a calendar in which there is no year zero. cftime-1.3.0 checks for this, but previous versions did not.

@guidov
Copy link
Author

guidov commented Dec 10, 2020

Hi , Sorry I didn't provide more info.

Here is a test notebook that throws the error , only for a particular time sequence (the third plot throws the error which is odd)

The netcdf file can be downloaded here:
https://www.dropbox.com/s/j3zw2d9hs5zxzbg/cftime_test_MOC.nc?dl=0

####################################
import numpy as np
import matplotlib.pyplot as plt
import xarray as xr
import cftime
import nc_time_axis
import datetime

cftime.version
cftime_file = xr.open_dataset("cftime_test_MOC.nc", engine='netcdf4')
cftime_file.time
cftime_file.MOC.isel(transport_reg=0, moc_comp=2,moc_z=20,lat_aux_grid=70).isel(time=slice(0,4)).plot()
cftime_file.MOC.isel(transport_reg=0, moc_comp=2,moc_z=20,lat_aux_grid=70).isel(time=slice(1,5)).plot()
cftime_file.MOC.isel(transport_reg=0, moc_comp=2,moc_z=20,lat_aux_grid=70).isel(time=slice(0,5)).plot()

Thanks

@jswhit
Copy link
Collaborator

jswhit commented Jan 10, 2021

this confirms that the problem is related to SciTools/nc-time-axis#51. nc-time-axis needs be updated to work with cftime >= 1.3.0

@jswhit jswhit closed this as completed Jan 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants