def test_select_dummy(self): nutime = self.TestTime(datetime(1950, 1, 1), 366, 24, "hours since 1900-01-01", "standard") dates = [datetime(1950, 1, 2, 6), datetime(1950, 1, 3), datetime(1950, 1, 3, 18)] t = date2index(dates, nutime, select="before") assert_equal(t, [1, 2, 2]) t = date2index(dates, nutime, select="after") assert_equal(t, [2, 2, 3]) t = date2index(dates, nutime, select="nearest") assert_equal(t, [1, 2, 3])
def test_nonuniform(self): """Check that the fallback mechanism works. """ nutime = self.TestTime(datetime(1950, 1, 1), 366, 24, "hours since 1900-01-01", "standard") # Let's remove the second entry, so that the computed stride is not # representative and the bisection method is needed. nutime._data = nutime._data[numpy.r_[0, slice(2, 200)]] t = date2index(datetime(1950, 2, 1), nutime) assert_equal(t, 30) t = date2index([datetime(1950, 2, 1), datetime(1950, 2, 3)], nutime) assert_equal(t, [30, 32])
def test_nonuniform(self): """Check that the fallback mechanism works. """ nutime = self.TestTime(datetime(1950, 1, 1), 366, 24, 'hours since 1900-01-01', 'standard') # Let's remove the second entry, so that the computed stride is not # representative and the bisection method is needed. nutime._data = nutime._data[numpy.r_[0, slice(2, 200)]] t = date2index(datetime(1950, 2, 1), nutime) assert_equal(t, 30) t = date2index([datetime(1950, 2, 1), datetime(1950, 2, 3)], nutime) assert_equal(t, [30, 32])
def test_select_dummy(self): nutime = self.TestTime(datetime(1950, 1, 1), 366, 24, 'hours since 1900-01-01', 'standard') dates = [datetime(1950,1,2,6), datetime(1950,1,3), datetime(1950,1,3,18)] t = date2index(dates, nutime, select='before') assert_equal(t, [1, 2, 2]) t = date2index(dates, nutime, select='after') assert_equal(t, [2, 2, 3]) t = date2index(dates, nutime, select='nearest') assert_equal(t, [1,2,3])
def test_singletime(self): # issue 215 test (date2index with time variable length == 1) f = Dataset(self.file) time2 = f.variables['time2'] result_index = date2index(self.first_timestamp,time2, select="exact") assert_equal(result_index, 0) f.close()
def test_singletime(self): # issue 215 test (date2index with time variable length == 1) f = Dataset(self.file) time2 = f.variables['time2'] result_index = date2index(self.first_timestamp, time2, select="exact") assert_equal(result_index, 0) f.close()
def test_failure(self): nutime = self.TestTime(datetime(1950, 1, 1), 366, 24, "hours since 1900-01-01", "standard") try: t = date2index(datetime(1949, 2, 1), nutime) except ValueError: pass else: raise ValueError("This test should have failed.")
def test_select_nc(self): f = Dataset(self.file, 'r') nutime = f.variables['time'] dates = [datetime(1950,1,2,6), datetime(1950,1,3), datetime(1950,1,3,18)] t = date2index(dates, nutime, select='before') assert_equal(t, [1, 2, 2]) t = date2index(dates, nutime, select='after') assert_equal(t, [2, 2, 3]) t = date2index(dates, nutime, select='nearest') assert_equal(t, [1,2,3]) # Test dates outside the support with select t = date2index(datetime(1949,12,1), nutime, select='nearest') assert_equal(t, 0) t = date2index(datetime(1978,1,1), nutime, select='nearest') assert_equal(t, 365) # Test dates outsied the support with before self.assertRaises(ValueError, date2index, datetime(1949,12,1), nutime, select='before') t = date2index(datetime(1978,1,1), nutime, select='before') assert_equal(t, 365) # Test dates outsied the support with after t = date2index(datetime(1949,12,1), nutime, select='after') assert_equal(t, 0) self.assertRaises(ValueError, date2index, datetime(1978,1,1), nutime, select='after')
def test_failure(self): nutime = self.TestTime(datetime(1950, 1, 1), 366, 24, 'hours since 1900-01-01', 'standard') try: t = date2index(datetime(1949, 2, 1), nutime) except ValueError: pass else: raise ValueError('This test should have failed.')
def test_issue444(self): # make sure integer overflow not causing error in # calculation of nearest index when sum of adjacent # time values won't fit in 32 bits. query_time = datetime(2037, 1, 3, 21, 12) print(self.time_vars['time3']) index = date2index(query_time, self.time_vars['time3'], select='nearest') assert(index == 11)
def test_select_nc(self): f = Dataset(self.file, "r") nutime = f.variables["time"] dates = [datetime(1950, 1, 2, 6), datetime(1950, 1, 3), datetime(1950, 1, 3, 18)] t = date2index(dates, nutime, select="before") assert_equal(t, [1, 2, 2]) t = date2index(dates, nutime, select="after") assert_equal(t, [2, 2, 3]) t = date2index(dates, nutime, select="nearest") assert_equal(t, [1, 2, 3]) # Test dates outside the support with select t = date2index(datetime(1949, 12, 1), nutime, select="nearest") assert_equal(t, 0) t = date2index(datetime(1978, 1, 1), nutime, select="nearest") assert_equal(t, 365) # Test dates outside the support with before self.assertRaises(ValueError, date2index, datetime(1949, 12, 1), nutime, select="before") t = date2index(datetime(1978, 1, 1), nutime, select="before") assert_equal(t, 365) # Test dates outside the support with after t = date2index(datetime(1949, 12, 1), nutime, select="after") assert_equal(t, 0) self.assertRaises(ValueError, date2index, datetime(1978, 1, 1), nutime, select="after") # test microsecond and millisecond units unix_epoch = "milliseconds since 1970-01-01T00:00:00Z" from netCDF4 import date2num d = datetime(2038, 1, 19, 3, 14, 7) millisecs = int(date2num(d, unix_epoch, calendar="proleptic_gregorian")) assert_equal(millisecs, (2 ** 32 / 2 - 1) * 1000) unix_epoch = "microseconds since 1970-01-01T00:00:00Z" microsecs = int(date2num(d, unix_epoch)) assert_equal(microsecs, (2 ** 32 / 2 - 1) * 1000000) # test microsecond accuracy in date2num/num2date roundtrip # note: microsecond accuracy lost for time intervals greater # than about 270 years. from dateutil.tz import tzutc units = "microseconds since 1776-07-04 00:00:00-12:00" dates = [ datetime(1962, 10, 27, 6, 1, 30, 9001), datetime(1993, 11, 21, 12, 5, 25, 999), datetime(1995, 11, 25, 18, 7, 59, 999999), ] times2 = date2num(dates, units) dates2 = num2date(times2, units) for date, date2 in zip(dates, dates2): assert_equal(date.replace(tzinfo=tzutc()), date2) f.close()
def test_select_nc(self): f = Dataset(self.file, 'r') nutime = f.variables['time'] dates = [datetime(1950, 1, 2, 6), datetime( 1950, 1, 3), datetime(1950, 1, 3, 18)] t = date2index(dates, nutime, select='before') assert_equal(t, [1, 2, 2]) t = date2index(dates, nutime, select='after') assert_equal(t, [2, 2, 3]) t = date2index(dates, nutime, select='nearest') assert_equal(t, [1, 2, 3]) # Test dates outside the support with select t = date2index(datetime(1949, 12, 1), nutime, select='nearest') assert_equal(t, 0) t = date2index(datetime(1978, 1, 1), nutime, select='nearest') assert_equal(t, 365) # Test dates outside the support with before self.assertRaises( ValueError, date2index, datetime(1949, 12, 1), nutime, select='before') t = date2index(datetime(1978, 1, 1), nutime, select='before') assert_equal(t, 365) # Test dates outside the support with after t = date2index(datetime(1949, 12, 1), nutime, select='after') assert_equal(t, 0) self.assertRaises( ValueError, date2index, datetime(1978, 1, 1), nutime, select='after') # test microsecond and millisecond units unix_epoch = "milliseconds since 1970-01-01T00:00:00Z" from netCDF4 import date2num d = datetime(2038, 1, 19, 3, 14, 7) millisecs = int( date2num(d, unix_epoch, calendar='proleptic_gregorian')) assert_equal(millisecs, (2 ** 32 / 2 - 1) * 1000) unix_epoch = "microseconds since 1970-01-01T00:00:00Z" microsecs = int(date2num(d, unix_epoch)) assert_equal(microsecs, (2 ** 32 / 2 - 1) * 1000000) # test microsecond accuracy in date2num/num2date roundtrip # note: microsecond accuracy lost for time intervals greater # than about 270 years. from dateutil.tz import tzutc units = 'microseconds since 1776-07-04 00:00:00-12:00' dates =\ [datetime(1962, 10, 27, 6, 1, 30, 9001), datetime( 1993, 11, 21, 12, 5, 25, 999), datetime(1995, 11, 25, 18, 7, 59, 999999)] times2 = date2num(dates, units) dates2 = num2date(times2, units) for date, date2 in zip(dates, dates2): assert_equal(date.replace(tzinfo=tzutc()), date2) f.close()
def test_issue444(self): # make sure integer overflow not causing error in # calculation of nearest index when sum of adjacent # time values won't fit in 32 bits. ntimes = 20 f = Dataset(self.file, 'r') query_time = datetime(2037, 1, 3, 21, 12) index = date2index(query_time, f.variables['time3'], select='nearest') assert (index == 11) f.close()
def test_simple(self): t = date2index(datetime(1950,2,1), self.standardtime) assert_equal(t, 31)
def test_list(self): t = date2index( [datetime(1950, 2, 1), datetime(1950, 2, 3)], self.standardtime) assert_equal(t, [31, 33])
def test_list(self): t = date2index([datetime(1950,2,1), datetime(1950,2,3)], self.standardtime) assert_equal(t, [31, 33])
def test_simple(self): t = date2index(datetime(1950, 2, 1), self.standardtime) assert_equal(t, 31)
def test_singletime(self): # issue 215 test (date2index with time variable length == 1) time2 = self.time_vars['time2'] result_index = date2index(self.first_timestamp, time2, select="exact") assert_equal(result_index, 0)