コード例 #1
0
ファイル: test_conventions.py プロジェクト: jhamman/xarray
 def test_cf_datetime(self):
     import netCDF4 as nc4
     for num_dates, units in [
             (np.arange(10), 'days since 2000-01-01'),
             (np.arange(10).reshape(2, 5), 'days since 2000-01-01'),
             (12300 + np.arange(5), 'hours since 1680-01-01 00:00:00'),
             # here we add a couple minor formatting errors to test
             # the robustness of the parsing algorithm.
             (12300 + np.arange(5), 'hour since 1680-01-01  00:00:00'),
             (12300 + np.arange(5), u'Hour  since 1680-01-01 00:00:00'),
             (12300 + np.arange(5), ' Hour  since  1680-01-01 00:00:00 '),
             (10, 'days since 2000-01-01'),
             ([10], 'daYs  since 2000-01-01'),
             ([[10]], 'days since 2000-01-01'),
             ([10, 10], 'days since 2000-01-01'),
             (np.array(10), 'days since 2000-01-01'),
             (0, 'days since 1000-01-01'),
             ([0], 'days since 1000-01-01'),
             ([[0]], 'days since 1000-01-01'),
             (np.arange(2), 'days since 1000-01-01'),
             (np.arange(0, 100000, 20000), 'days since 1900-01-01'),
             (17093352.0, 'hours since 1-1-1 00:00:0.0'),
             ([0.5, 1.5], 'hours since 1900-01-01T00:00:00'),
             (0, 'milliseconds since 2000-01-01T00:00:00'),
             (0, 'microseconds since 2000-01-01T00:00:00'),
             ]:
         for calendar in ['standard', 'gregorian', 'proleptic_gregorian']:
             expected = _ensure_naive_tz(nc4.num2date(num_dates, units, calendar))
             print(num_dates, units, calendar)
             with warnings.catch_warnings():
                 warnings.filterwarnings('ignore',
                                         'Unable to decode time axis')
                 actual = conventions.decode_cf_datetime(num_dates, units,
                                                         calendar)
             if (isinstance(actual, np.ndarray) and
                     np.issubdtype(actual.dtype, np.datetime64)):
                 # self.assertEqual(actual.dtype.kind, 'M')
                 # For some reason, numpy 1.8 does not compare ns precision
                 # datetime64 arrays as equal to arrays of datetime objects,
                 # but it works for us precision. Thus, convert to us
                 # precision for the actual array equal comparison...
                 actual_cmp = actual.astype('M8[us]')
             else:
                 actual_cmp = actual
             self.assertArrayEqual(expected, actual_cmp)
             encoded, _, _ = conventions.encode_cf_datetime(actual, units,
                                                            calendar)
             if '1-1-1' not in units:
                 # pandas parses this date very strangely, so the original
                 # units/encoding cannot be preserved in this case:
                 # (Pdb) pd.to_datetime('1-1-1 00:00:0.0')
                 # Timestamp('2001-01-01 00:00:00')
                 self.assertArrayEqual(num_dates, np.around(encoded, 1))
                 if (hasattr(num_dates, 'ndim') and num_dates.ndim == 1 and
                         '1000' not in units):
                     # verify that wrapping with a pandas.Index works
                     # note that it *does not* currently work to even put
                     # non-datetime64 compatible dates into a pandas.Index :(
                     encoded, _, _ = conventions.encode_cf_datetime(
                         pd.Index(actual), units, calendar)
                     self.assertArrayEqual(num_dates, np.around(encoded, 1))
コード例 #2
0
 def test_cf_datetime(self):
     import netCDF4 as nc4
     for num_dates, units in [
         (np.arange(10), 'days since 2000-01-01'),
         (np.arange(10).reshape(2, 5), 'days since 2000-01-01'),
         (12300 + np.arange(5), 'hours since 1680-01-01 00:00:00'),
             # here we add a couple minor formatting errors to test
             # the robustness of the parsing algorithm.
         (12300 + np.arange(5), 'hour since 1680-01-01  00:00:00'),
         (12300 + np.arange(5), u'Hour  since 1680-01-01 00:00:00'),
         (12300 + np.arange(5), ' Hour  since  1680-01-01 00:00:00 '),
         (10, 'days since 2000-01-01'),
         ([10], 'daYs  since 2000-01-01'),
         ([[10]], 'days since 2000-01-01'),
         ([10, 10], 'days since 2000-01-01'),
         (np.array(10), 'days since 2000-01-01'),
         (0, 'days since 1000-01-01'),
         ([0], 'days since 1000-01-01'),
         ([[0]], 'days since 1000-01-01'),
         (np.arange(2), 'days since 1000-01-01'),
         (np.arange(0, 100000, 20000), 'days since 1900-01-01'),
         (17093352.0, 'hours since 1-1-1 00:00:0.0'),
         ([0.5, 1.5], 'hours since 1900-01-01T00:00:00'),
         (0, 'milliseconds since 2000-01-01T00:00:00'),
         (0, 'microseconds since 2000-01-01T00:00:00'),
     ]:
         for calendar in ['standard', 'gregorian', 'proleptic_gregorian']:
             expected = _ensure_naive_tz(
                 nc4.num2date(num_dates, units, calendar))
             print(num_dates, units, calendar)
             with warnings.catch_warnings():
                 warnings.filterwarnings('ignore',
                                         'Unable to decode time axis')
                 actual = conventions.decode_cf_datetime(
                     num_dates, units, calendar)
             if (isinstance(actual, np.ndarray)
                     and np.issubdtype(actual.dtype, np.datetime64)):
                 # self.assertEqual(actual.dtype.kind, 'M')
                 # For some reason, numpy 1.8 does not compare ns precision
                 # datetime64 arrays as equal to arrays of datetime objects,
                 # but it works for us precision. Thus, convert to us
                 # precision for the actual array equal comparison...
                 actual_cmp = actual.astype('M8[us]')
             else:
                 actual_cmp = actual
             self.assertArrayEqual(expected, actual_cmp)
             encoded, _, _ = conventions.encode_cf_datetime(
                 actual, units, calendar)
             if '1-1-1' not in units:
                 # pandas parses this date very strangely, so the original
                 # units/encoding cannot be preserved in this case:
                 # (Pdb) pd.to_datetime('1-1-1 00:00:0.0')
                 # Timestamp('2001-01-01 00:00:00')
                 self.assertArrayEqual(num_dates, np.around(encoded, 1))
                 if (hasattr(num_dates, 'ndim') and num_dates.ndim == 1
                         and '1000' not in units):
                     # verify that wrapping with a pandas.Index works
                     # note that it *does not* currently work to even put
                     # non-datetime64 compatible dates into a pandas.Index :(
                     encoded, _, _ = conventions.encode_cf_datetime(
                         pd.Index(actual), units, calendar)
                     self.assertArrayEqual(num_dates, np.around(encoded, 1))