• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python conventions.decode_cf_datetime函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中xray.conventions.decode_cf_datetime函数的典型用法代码示例。如果您正苦于以下问题:Python decode_cf_datetime函数的具体用法?Python decode_cf_datetime怎么用?Python decode_cf_datetime使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了decode_cf_datetime函数的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: test_decode_cf_datetime_non_standard_units

 def test_decode_cf_datetime_non_standard_units(self):
     expected = pd.date_range(periods=100, start='1970-01-01', freq='h')
     # netCDFs from madis.noaa.gov use this format for their time units
     # they cannot be parsed by netcdftime, but pd.Timestamp works
     units = 'hours since 1-1-1970'
     actual = conventions.decode_cf_datetime(np.arange(100), units)
     self.assertArrayEqual(actual, expected)
开发者ID:roxyboy,项目名称:xray,代码行数:7,代码来源:test_conventions.py


示例2: test_cf_datetime

 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'),
             (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'),
             ]:
         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))
开发者ID:benjwadams,项目名称:xray,代码行数:58,代码来源:test_conventions.py


示例3: test_decode_non_standard_calendar_single_element

 def test_decode_non_standard_calendar_single_element(self):
     units = 'days since 0001-01-01'
     for calendar in ['noleap', '365_day', '360_day', 'julian', 'all_leap',
                      '366_day']:
         for num_time in [735368, [735368], [[735368]]]:
             with warnings.catch_warnings():
                 warnings.filterwarnings('ignore', 'Unable to decode time axis')
                 actual = conventions.decode_cf_datetime(num_time, units,
                                                         calendar=calendar)
             self.assertEqual(actual.dtype, np.dtype('M8[ns]'))
开发者ID:ToddSmall,项目名称:xray,代码行数:10,代码来源:test_conventions.py


示例4: test_decode_cf_datetime_non_iso_strings

 def test_decode_cf_datetime_non_iso_strings(self):
     # datetime strings that are _almost_ ISO compliant but not quite,
     # but which netCDF4.num2date can still parse correctly
     expected = pd.date_range(periods=100, start='2000-01-01', freq='h')
     cases = [(np.arange(100), 'hours since 2000-01-01 0'),
              (np.arange(100), 'hours since 2000-1-1 0'),
              (np.arange(100), 'hours since 2000-01-01 0:00')]
     for num_dates, units in cases:
         actual = conventions.decode_cf_datetime(num_dates, units)
         self.assertArrayEqual(actual, expected)
开发者ID:roxyboy,项目名称:xray,代码行数:10,代码来源:test_conventions.py


示例5: test_cf_datetime_nan

 def test_cf_datetime_nan(self):
     for num_dates, units, expected_list in [
             ([np.nan], 'days since 2000-01-01', ['NaT']),
             ([np.nan, 0], 'days since 2000-01-01',
              ['NaT', '2000-01-01T00:00:00Z']),
             ([np.nan, 0, 1], 'days since 2000-01-01',
              ['NaT', '2000-01-01T00:00:00Z', '2000-01-02T00:00:00Z']),
             ]:
         with warnings.catch_warnings():
             warnings.filterwarnings('ignore', 'All-NaN')
             actual = conventions.decode_cf_datetime(num_dates, units)
         expected = np.array(expected_list, dtype='datetime64[ns]')
         self.assertArrayEqual(expected, actual)
开发者ID:ToddSmall,项目名称:xray,代码行数:13,代码来源:test_conventions.py


示例6: test_decode_non_standard_calendar_single_element_fallback

    def test_decode_non_standard_calendar_single_element_fallback(self):
        import netCDF4 as nc4

        units = 'days since 0001-01-01'
        dt = nc4.netcdftime.datetime(2001, 2, 29)
        for calendar in ['360_day', 'all_leap', '366_day']:
            num_time = nc4.date2num(dt, units, calendar)
            with self.assertWarns('Unable to decode time axis'):
                actual = conventions.decode_cf_datetime(num_time, units,
                                                        calendar=calendar)
            expected = np.asarray(nc4.num2date(num_time, units, calendar))
            print(num_time, calendar, actual, expected)
            self.assertEqual(actual.dtype, np.dtype('O'))
            self.assertEqual(expected, actual)
开发者ID:roxyboy,项目名称:xray,代码行数:14,代码来源:test_conventions.py


示例7: test_decode_non_standard_calendar

    def test_decode_non_standard_calendar(self):
        import netCDF4 as nc4

        for calendar in ['noleap', '365_day', '360_day', 'julian', 'all_leap',
                         '366_day']:
            units = 'days since 0001-01-01'
            times = pd.date_range('2001-04-01-00', end='2001-04-30-23',
                                  freq='H')
            noleap_time = nc4.date2num(times.to_pydatetime(), units,
                                       calendar=calendar)
            expected = times.values
            with warnings.catch_warnings():
                warnings.filterwarnings('ignore', 'Unable to decode time axis')
                actual = conventions.decode_cf_datetime(noleap_time, units,
                                                        calendar=calendar)
            self.assertEqual(actual.dtype, np.dtype('M8[ns]'))
            self.assertArrayEqual(actual, expected)
开发者ID:ToddSmall,项目名称:xray,代码行数:17,代码来源:test_conventions.py


示例8: test_cf_datetime

 def test_cf_datetime(self):
     import netCDF4 as nc4
     for num_dates, units in [
             (np.arange(100), 'days since 2000-01-01'),
             (np.arange(100).reshape(10, 10), 'days since 2000-01-01'),
             (12300 + np.arange(50), 'hours 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'),
             (0, 'days since 1000-01-01'),
             ([0], 'days since 1000-01-01'),
             ([[0]], 'days since 1000-01-01'),
             (np.arange(20), 'days since 1000-01-01'),
             (np.arange(0, 100000, 10000), 'days since 1900-01-01')
             ]:
         for calendar in ['standard', 'gregorian', 'proleptic_gregorian']:
             expected = 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, np.dtype('M8[ns]'))
                 # 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)
             self.assertArrayEqual(num_dates, np.around(encoded))
             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))
开发者ID:kirknorth,项目名称:xray,代码行数:46,代码来源:test_conventions.py


示例9: test_decode_non_standard_calendar_single_element_fallback

    def test_decode_non_standard_calendar_single_element_fallback(self):
        import netCDF4 as nc4

        units = 'days since 0001-01-01'
        dt = nc4.netcdftime.datetime(2001, 2, 29)
        for calendar in ['360_day', 'all_leap', '366_day']:
            num_time = nc4.date2num(dt, units, calendar)
            with warnings.catch_warnings(record=True) as w:
                warnings.simplefilter('always')
                actual = conventions.decode_cf_datetime(num_time, units,
                                                        calendar=calendar)
                self.assertEqual(len(w), 1)
                self.assertIn('Unable to decode time axis',
                              str(w[0].message))
            expected = np.asarray(nc4.num2date(num_time, units, calendar))
            print(num_time, calendar, actual, expected)
            self.assertEqual(actual.dtype, np.dtype('O'))
            self.assertEqual(expected, actual)
开发者ID:ToddSmall,项目名称:xray,代码行数:18,代码来源:test_conventions.py


示例10: test_decode_non_standard_calendar_fallback

    def test_decode_non_standard_calendar_fallback(self):
        import netCDF4 as nc4
        for year in [2010, 2011, 2012, 2013, 2014]: # insure leap year doesn't matter
            for calendar in ['360_day', '366_day', 'all_leap']:
                calendar = '360_day'
                units = 'days since {0}-01-01'.format(year)
                num_times = np.arange(100)
                expected = nc4.num2date(num_times, units, calendar)

                with warnings.catch_warnings(record=True) as w:
                    warnings.simplefilter('always')
                    actual = conventions.decode_cf_datetime(num_times, units,
                                                            calendar=calendar)
                    self.assertEqual(len(w), 1)
                    self.assertIn('Unable to decode time axis',
                                  str(w[0].message))

                self.assertEqual(actual.dtype, np.dtype('O'))
                self.assertArrayEqual(actual, expected)
开发者ID:ToddSmall,项目名称:xray,代码行数:19,代码来源:test_conventions.py


示例11: test_decode_non_standard_calendar

    def test_decode_non_standard_calendar(self):
        import netCDF4 as nc4

        for calendar in ['noleap', '365_day', '360_day', 'julian', 'all_leap',
                         '366_day']:
            units = 'days since 0001-01-01'
            times = pd.date_range('2001-04-01-00', end='2001-04-30-23',
                                  freq='H')
            noleap_time = nc4.date2num(times.to_pydatetime(), units,
                                       calendar=calendar)
            expected = times.values
            with warnings.catch_warnings():
                warnings.filterwarnings('ignore', 'Unable to decode time axis')
                actual = conventions.decode_cf_datetime(noleap_time, units,
                                                        calendar=calendar)
            self.assertEqual(actual.dtype, np.dtype('M8[ns]'))
            abs_diff = abs(actual - expected)
            # once we no longer support versions of netCDF4 older than 1.1.5,
            # we could do this check with near microsecond accuracy:
            # https://github.com/Unidata/netcdf4-python/issues/355
            self.assertTrue((abs_diff <= np.timedelta64(1, 's')).all())
开发者ID:roxyboy,项目名称:xray,代码行数:21,代码来源:test_conventions.py


示例12: test_decode_non_standard_calendar_multidim_time

    def test_decode_non_standard_calendar_multidim_time(self):
        import netCDF4 as nc4

        calendar = 'noleap'
        units = 'days since 0001-01-01'
        times1 = pd.date_range('2001-04-01', end='2001-04-05', freq='D')
        times2 = pd.date_range('2001-05-01', end='2001-05-05', freq='D')
        noleap_time1 = nc4.date2num(times1.to_pydatetime(), units,
                                    calendar=calendar)
        noleap_time2 = nc4.date2num(times2.to_pydatetime(), units,
                                    calendar=calendar)
        mdim_time = np.empty((len(noleap_time1), 2), )
        mdim_time[:, 0] = noleap_time1
        mdim_time[:, 1] = noleap_time2

        expected1 = times1.values
        expected2 = times2.values
        with warnings.catch_warnings():
            warnings.filterwarnings('ignore', 'Unable to decode time axis')
            actual = conventions.decode_cf_datetime(mdim_time, units,
                                                    calendar=calendar)
        self.assertEqual(actual.dtype, np.dtype('M8[ns]'))
        self.assertArrayEqual(actual[:, 0], expected1)
        self.assertArrayEqual(actual[:, 1], expected2)
开发者ID:ToddSmall,项目名称:xray,代码行数:24,代码来源:test_conventions.py



注:本文中的xray.conventions.decode_cf_datetime函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python pycompat.iteritems函数代码示例发布时间:2022-05-26
下一篇:
Python xray.Variable类代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap