Skip to content

Commit 05aad75

Browse files
authored
Merge pull request #50 from jbusecke/patch-1
Remove year 0 ticks if generated
2 parents 531dd0d + de5cb17 commit 05aad75

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

nc_time_axis/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,15 @@ def tick_values(self, vmin, vmax):
190190
else:
191191
msg = 'Resolution {} not implemented yet.'.format(resolution)
192192
raise ValueError(msg)
193-
193+
# Some calenders do not allow a year 0.
194+
# Remove ticks to avoid raising an error.
195+
if self.calendar in [
196+
"proleptic_gregorian",
197+
"gregorian",
198+
"julian",
199+
"standard",
200+
]:
201+
ticks = [t for t in ticks if t.year != 0]
194202
return utime.date2num(ticks)
195203

196204

nc_time_axis/tests/unit/test_NetCDFTimeDateLocator.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,45 @@ def test_yearly(self):
115115
self.check(5, 0, 5*365), [31., 485., 942., 1399., 1856.])
116116

117117

118+
class Test_tick_values_yr0(unittest.TestCase):
119+
def setUp(self):
120+
self.date_unit = 'days since 0001-01-01 00:00'
121+
self.all_calendars = [
122+
'standard',
123+
'gregorian',
124+
'proleptic_gregorian',
125+
'noleap',
126+
'365_day',
127+
'360_day',
128+
'julian',
129+
'all_leap',
130+
'366_day',
131+
]
132+
self.yr0_remove_calendars = [
133+
'proleptic_gregorian',
134+
'gregorian',
135+
'julian',
136+
'standard',
137+
]
138+
139+
def check(self, max_n_ticks, num1, num2, calendar):
140+
locator = NetCDFTimeDateLocator(
141+
max_n_ticks=max_n_ticks, calendar=calendar,
142+
date_unit=self.date_unit
143+
)
144+
return locator.tick_values(num1, num2)
145+
146+
def test_yearly_yr0_remove(self):
147+
for calendar in self.all_calendars:
148+
# convert values to dates, check that none of them has year 0
149+
num2date = cftime.utime(self.date_unit, calendar).num2date
150+
ticks = self.check(5, 0, 100 * 365, calendar)
151+
year_ticks = [num2date(t).year for t in ticks]
152+
if calendar in self.yr0_remove_calendars:
153+
self.assertNotIn(0, year_ticks)
154+
else:
155+
self.assertIn(0, year_ticks)
156+
157+
118158
if __name__ == "__main__":
119159
unittest.main()

0 commit comments

Comments
 (0)