本文整理汇总了Python中cf_units.Unit.date2num方法的典型用法代码示例。如果您正苦于以下问题:Python Unit.date2num方法的具体用法?Python Unit.date2num怎么用?Python Unit.date2num使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cf_units.Unit
的用法示例。
在下文中一共展示了Unit.date2num方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _add_iris_coord
# 需要导入模块: from cf_units import Unit [as 别名]
# 或者: from cf_units.Unit import date2num [as 别名]
def _add_iris_coord(cube, name, points, dim, calendar=None):
"""
Add a Coord to a Cube from a Pandas index or columns array.
If no calendar is specified for a time series, Gregorian is assumed.
"""
units = Unit("unknown")
if calendar is None:
calendar = cf_units.CALENDAR_GREGORIAN
# Convert pandas datetime objects to python datetime obejcts.
if isinstance(points, DatetimeIndex):
points = np.array([i.to_datetime() for i in points])
# Convert datetime objects to Iris' current datetime representation.
if points.dtype == object:
dt_types = (datetime.datetime, netcdftime.datetime)
if all([isinstance(i, dt_types) for i in points]):
units = Unit("hours since epoch", calendar=calendar)
points = units.date2num(points)
points = np.array(points)
if (np.issubdtype(points.dtype, np.number) and
iris.util.monotonic(points, strict=True)):
coord = DimCoord(points, units=units)
coord.rename(name)
cube.add_dim_coord(coord, dim)
else:
coord = AuxCoord(points, units=units)
coord.rename(name)
cube.add_aux_coord(coord, dim)
示例2: _convert_datetime_to_coord_unit
# 需要导入模块: from cf_units import Unit [as 别名]
# 或者: from cf_units.Unit import date2num [as 别名]
def _convert_datetime_to_coord_unit(coord, dt):
"""Converts a datetime to be in the unit of a specified Coord.
"""
if isinstance(coord, iris.coords.Coord):
# The unit class is then cf_units.Unit.
iris_unit = coord.units
else:
iris_unit = Unit(coord.units)
return iris_unit.date2num(dt)
示例3: Test
# 需要导入模块: from cf_units import Unit [as 别名]
# 或者: from cf_units.Unit import date2num [as 别名]
class Test(tests.IrisTest):
def setUp(self):
self.section = {'year': 2007,
'month': 1,
'day': 15,
'hour': 0,
'minute': 3,
'second': 0}
self.unit = Unit('hours since epoch', calendar=CALENDAR_GREGORIAN)
dt = datetime(self.section['year'], self.section['month'],
self.section['day'], self.section['hour'],
self.section['minute'], self.section['second'])
self.point = self.unit.date2num(dt)
def _check(self, section, standard_name=None):
expected = DimCoord(self.point, standard_name=standard_name,
units=self.unit)
# The call being tested.
coord = reference_time_coord(section)
self.assertEqual(coord, expected)
def test_start_of_forecast_0(self):
section = deepcopy(self.section)
section['significanceOfReferenceTime'] = 0
self._check(section, 'forecast_reference_time')
def test_start_of_forecast_1(self):
section = deepcopy(self.section)
section['significanceOfReferenceTime'] = 1
self._check(section, 'forecast_reference_time')
def test_observation_time(self):
section = deepcopy(self.section)
section['significanceOfReferenceTime'] = 3
self._check(section, 'time')
def test_unknown_significance(self):
section = deepcopy(self.section)
section['significanceOfReferenceTime'] = 5
emsg = 'unsupported significance'
with self.assertRaisesRegexp(TranslationError, emsg):
self._check(section)
示例4: Test
# 需要导入模块: from cf_units import Unit [as 别名]
# 或者: from cf_units.Unit import date2num [as 别名]
class Test(tests.IrisTest):
def setUp(self):
self.section = {"year": 2007, "month": 1, "day": 15, "hour": 0, "minute": 3, "second": 0}
self.unit = Unit("hours since epoch", calendar=CALENDAR_GREGORIAN)
dt = datetime(
self.section["year"],
self.section["month"],
self.section["day"],
self.section["hour"],
self.section["minute"],
self.section["second"],
)
self.point = self.unit.date2num(dt)
def _check(self, section, standard_name=None):
expected = DimCoord(self.point, standard_name=standard_name, units=self.unit)
# The call being tested.
coord = reference_time_coord(section)
self.assertEqual(coord, expected)
def test_start_of_forecast(self):
section = deepcopy(self.section)
section["significanceOfReferenceTime"] = 1
self._check(section, "forecast_reference_time")
def test_observation_time(self):
section = deepcopy(self.section)
section["significanceOfReferenceTime"] = 3
self._check(section, "time")
def test_unknown_significance(self):
section = deepcopy(self.section)
section["significanceOfReferenceTime"] = 0
emsg = "unsupported significance"
with self.assertRaisesRegexp(TranslationError, emsg):
self._check(section)
示例5: NAME_to_cube
# 需要导入模块: from cf_units import Unit [as 别名]
# 或者: from cf_units.Unit import date2num [as 别名]
def NAME_to_cube(filenames, callback):
"""
Returns a generator of cubes given a list of filenames and a callback.
"""
for filename in filenames:
header, column_headings, data_arrays = load_NAME_III(filename)
for i, data_array in enumerate(data_arrays):
# turn the dictionary of column headers with a list of header
# information for each field into a dictionary of headers for just
# this field. Ignore the first 4 columns of grid position (data was
# located with the data array).
field_headings = dict((k, v[i + 4])
for k, v in column_headings.items())
# make an cube
cube = iris.cube.Cube(data_array)
# define the name and unit
name = ('%s %s' % (field_headings['species'],
field_headings['quantity']))
name = name.upper().replace(' ', '_')
cube.rename(name)
# Some units are badly encoded in the file, fix this by putting a
# space in between. (if gs is not found, then the string will be
# returned unchanged)
cube.units = field_headings['unit'].replace('gs', 'g s')
# define and add the singular coordinates of the field (flight
# level, time etc.)
cube.add_aux_coord(icoords.AuxCoord(field_headings['z_level'],
long_name='flight_level',
units='1'))
# define the time unit and use it to serialise the datetime for the
# time coordinate
time_unit = Unit('hours since epoch', calendar=CALENDAR_GREGORIAN)
time_coord = icoords.AuxCoord(
time_unit.date2num(field_headings['time']),
standard_name='time',
units=time_unit)
cube.add_aux_coord(time_coord)
# build a coordinate system which can be referenced by latitude and
# longitude coordinates
lat_lon_coord_system = icoord_systems.GeogCS(6371229)
# build regular latitude and longitude coordinates which have
# bounds
start = header['X grid origin'] + header['X grid resolution']
step = header['X grid resolution']
count = header['X grid size']
pts = start + np.arange(count, dtype=np.float32) * step
lon_coord = icoords.DimCoord(pts, standard_name='longitude',
units='degrees',
coord_system=lat_lon_coord_system)
lon_coord.guess_bounds()
start = header['Y grid origin'] + header['Y grid resolution']
step = header['Y grid resolution']
count = header['Y grid size']
pts = start + np.arange(count, dtype=np.float32) * step
lat_coord = icoords.DimCoord(pts, standard_name='latitude',
units='degrees',
coord_system=lat_lon_coord_system)
lat_coord.guess_bounds()
# add the latitude and longitude coordinates to the cube, with
# mappings to data dimensions
cube.add_dim_coord(lat_coord, 0)
cube.add_dim_coord(lon_coord, 1)
# implement standard iris callback capability. Although callbacks
# are not used in this example, the standard mechanism for a custom
# loader to implement a callback is shown:
cube = iris.io.run_callback(callback, cube,
[header, field_headings, data_array],
filename)
# yield the cube created (the loop will continue when the next()
# element is requested)
yield cube
示例6: __init__
# 需要导入模块: from cf_units import Unit [as 别名]
# 或者: from cf_units.Unit import date2num [as 别名]
class TestCategoriseCoordFunctionForTime:
def __init__(self):
self.u = Unit('days since 1600-01-01 00:00:00', calendar=CALENDAR_STANDARD)
self.points = np.arange(1, 5, 1)
self.coord = iris.coords.DimCoord(self.points, units=self.u)
self.start = datetime.datetime(2000, 1, 1)
self.end = datetime.datetime(2003, 4, 24)
self.start = self.u.date2num(self.start)
self.end = self.u.date2num(self.end)
def setup_func(self):
self.__init__()
@with_setup(setup_func)
def test_categorise_coord_function_time_year_only(self):
delta = date_delta_creator(1)
result_function = categorise_coord_function(self.start, self.end, delta, True)
expected = np.array([self.u.date2num(datetime.datetime(2000, 7, 1, 0, 0, 0)),
self.u.date2num(datetime.datetime(2001, 7, 1, 0, 0, 0)),
self.u.date2num(datetime.datetime(2002, 7, 1, 0, 0, 0))])
assert_equal(result_function(self.coord, self.u.date2num(datetime.datetime(2000, 1, 1, 0, 0, 0))), expected[0])
assert_equal(result_function(self.coord, self.u.date2num(datetime.datetime(2001, 3, 3, 0, 0, 0))), expected[1])
assert_equal(result_function(self.coord, self.u.date2num(datetime.datetime(2002, 5, 8, 0, 0, 0))), expected[2])
@with_setup(setup_func)
def test_categorise_coord_function_time_year_month(self):
delta = date_delta_creator(1, 1)
result_function = categorise_coord_function(self.start, self.end, delta, True)
expected = np.array([self.u.date2num(datetime.datetime(2000, 7, 15, 0, 0, 0)),
self.u.date2num(datetime.datetime(2001, 8, 15, 0, 0, 0)),
self.u.date2num(datetime.datetime(2002, 9, 15, 0, 0, 0))])
assert_equal(result_function(self.coord, self.u.date2num(datetime.datetime(2000, 1, 1, 0, 0, 0))), expected[0])
assert_equal(result_function(self.coord, self.u.date2num(datetime.datetime(2001, 3, 3, 0, 0, 0))), expected[1])
assert_equal(result_function(self.coord, self.u.date2num(datetime.datetime(2002, 5, 8, 0, 0, 0))), expected[2])
@with_setup(setup_func)
def test_categorise_coord_function_with_month_going_past_december(self):
start = datetime.datetime(2000, 11, 3)
start = self.u.date2num(start)
end = datetime.datetime(2004, 11, 3)
end = self.u.date2num(end)
delta = date_delta_creator(1, 2)
result_function = categorise_coord_function(start, end, delta, True)
expected = np.array([self.u.date2num(datetime.datetime(2001, 6, 3, 0, 0, 0)),
self.u.date2num(datetime.datetime(2002, 8, 3, 0, 0, 0)),
self.u.date2num(datetime.datetime(2003, 10, 3, 0, 0, 0))])
assert_equal(result_function(self.coord, self.u.date2num(datetime.datetime(1999, 1, 1, 0, 0, 0))), expected[0])
assert_equal(result_function(self.coord, self.u.date2num(datetime.datetime(2001, 3, 3, 0, 0, 0))), expected[0])
assert_equal(result_function(self.coord, self.u.date2num(datetime.datetime(2002, 3, 3, 0, 0, 0))), expected[1])
assert_equal(result_function(self.coord, self.u.date2num(datetime.datetime(2005, 5, 8, 0, 0, 0))), expected[2])
@with_setup(setup_func)
def test_categorise_coord_function_time_year_month_day_hour_minute_second(self):
delta = date_delta_creator(1, 3, 2, 4, 5, 6)
result_function = categorise_coord_function(self.start, self.end, delta, True)
expected = np.array([self.u.date2num(datetime.datetime(2000, 8, 16, 2, 2, 33)),
self.u.date2num(datetime.datetime(2001, 11, 18, 6, 7, 39)),
self.u.date2num(datetime.datetime(2003, 2, 20, 10, 12, 45))])
assert_equal(result_function(self.coord, self.u.date2num(datetime.datetime(2000, 1, 1, 0, 0, 0))), expected[0])
assert_equal(result_function(self.coord, self.u.date2num(datetime.datetime(2001, 7, 3, 0, 0, 0))), expected[1])
assert_equal(result_function(self.coord, self.u.date2num(datetime.datetime(2002, 9, 8, 0, 0, 0))), expected[2])