def test_Datetime_st2dt(self): for a in ( "1970-01-04", np.array("1970-01-04"), np.ma.array(["1970-01-04"]), ): b = cf.cfdatetime.st2rt( a, Units("days since 1970-01-01"), Units("days since 1970-01-01"), ) self.assertIsInstance(b, np.ndarray) self.assertEqual(b, 3)
def test_Datetime_rt2dt(self): self.assertEqual( cf.cfdatetime.rt2dt(1, Units('days since 2004-2-28')), numpy.array(cf.dt(2004, 2, 29, calendar='standard'), dtype='O')) self.assertTrue((cf.cfdatetime.rt2dt( [1, 3], Units('days since 2004-2-28')) == numpy.array([ datetime.datetime(2004, 2, 29), datetime.datetime(2004, 3, 2) ])).all()) a = numpy.array([ cf.dt(2004, 2, 29, calendar=None), cf.dt(2004, 3, 2, calendar='gregorian') ], dtype='O') b = cf.cfdatetime.rt2dt([1, 3], Units('days since 2004-2-28')) self.assertTrue((a == b).all())
def test_Datetime_dt2rt(self): units = Units('days since 2004-2-28') self.assertEqual( cf.cfdatetime.dt2rt(datetime.datetime(2004, 2, 29), None, units), numpy.array(1.0)) self.assertTrue((cf.cfdatetime.dt2rt( [datetime.datetime(2004, 2, 29), datetime.datetime(2004, 3, 2)], None, units) == numpy.array([1., 3.])).all()) units = Units('days since 2004-2-28', '360_day') self.assertTrue((cf.cfdatetime.dt2rt( [cf.dt(2004, 2, 29), cf.dt(2004, 3, 1)], None, units) == numpy.array([1., 3.])).all()) units = Units('seconds since 2004-2-28') self.assertEqual( cf.cfdatetime.dt2rt(datetime.datetime(2004, 2, 29), None, units), numpy.array(86400.0))
def test_Datetime_dt2rt(self): units = Units("days since 2004-2-28") self.assertEqual( cf.cfdatetime.dt2rt(datetime.datetime(2004, 2, 29), None, units), np.array(1.0), ) self.assertTrue((cf.cfdatetime.dt2rt( [ datetime.datetime(2004, 2, 29), datetime.datetime(2004, 3, 2), ], None, units, ) == np.array([1.0, 3.0])).all()) units = Units("days since 2004-2-28", "360_day") self.assertTrue((cf.cfdatetime.dt2rt( [cf.dt(2004, 2, 29), cf.dt(2004, 3, 1)], None, units) == np.array([1.0, 3.0])).all()) units = Units("seconds since 2004-2-28") self.assertEqual( cf.cfdatetime.dt2rt(datetime.datetime(2004, 2, 29), None, units), np.array(86400.0), )
def test_Datetime_rt2dt(self): for a in (1, np.array(1), np.ma.array(1)): self.assertEqual( cf.cfdatetime.rt2dt(a, Units("days since 2004-2-28")), np.array(cf.dt(2004, 2, 29, calendar="standard"), dtype="O"), ) for a in (np.ma.array(1, mask=True), np.ma.array([1], mask=True)): b = cf.cfdatetime.rt2dt(a, Units("days since 2004-2-28")) self.assertIsInstance(b, np.ndarray) self.assertEqual(b.mask, True) self.assertTrue( (cf.cfdatetime.rt2dt([1, 3], Units("days since 2004-2-28")) == np.array([ datetime.datetime(2004, 2, 29), datetime.datetime(2004, 3, 2), ])).all()) a = np.array( [ cf.dt(2004, 2, 29, calendar=None), cf.dt(2004, 3, 2, calendar="gregorian"), ], dtype="O", ) b = cf.cfdatetime.rt2dt([1, 3], Units("days since 2004-2-28")) self.assertTrue((a == b).all()) for a in ( np.ma.array(3), np.ma.array([3]), ): b = cf.cfdatetime.rt2dt(a, Units("days since 1970-01-01")) self.assertEqual(b, cf.dt(1970, 1, 4, calendar="gregorian")) for a in ( np.ma.array(3, mask=True), np.ma.array([3], mask=True), ): b = cf.cfdatetime.rt2dt(a, Units("days since 1970-01-01")) self.assertEqual(b.mask, True)