本文整理汇总了Python中cftime.DatetimeNoLeap方法的典型用法代码示例。如果您正苦于以下问题:Python cftime.DatetimeNoLeap方法的具体用法?Python cftime.DatetimeNoLeap怎么用?Python cftime.DatetimeNoLeap使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cftime
的用法示例。
在下文中一共展示了cftime.DatetimeNoLeap方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: to_datetime
# 需要导入模块: import cftime [as 别名]
# 或者: from cftime import DatetimeNoLeap [as 别名]
def to_datetime(d):
if isinstance(d, dt.datetime):
return d
if isinstance(d, cftime.DatetimeNoLeap):
return dt.datetime(d.year, d.month, d.day, d.hour, d.minute, d.second)
elif isinstance(d, cftime.DatetimeGregorian):
return dt.datetime(d.year, d.month, d.day, d.hour, d.minute, d.second)
elif isinstance(d, str):
errors = []
for fmt in (
"%Y-%m-%d %H:%M:%S",
"%Y-%m-%dT%H:%M:%S",
"%Y-%m-%dT%H:%M:%SZ"):
try:
return dt.datetime.strptime(d, fmt)
except ValueError as e:
errors.append(e)
continue
raise Exception(errors)
elif isinstance(d, np.datetime64):
return d.astype(dt.datetime)
else:
raise Exception("Unknown value: {} type: {}".format(d, type(d)))
示例2: test_load_variable_non_0001_refdate
# 需要导入模块: import cftime [as 别名]
# 或者: from cftime import DatetimeNoLeap [as 别名]
def test_load_variable_non_0001_refdate(load_variable_data_loader, year):
def preprocess(ds, **kwargs):
# This function converts our testing data (encoded with a units
# attribute with a reference data of 0001-01-01) to one
# with a reference data of 0004-01-01 (to do so we also need
# to offset the raw time values by three years).
three_yrs = 1095.
ds['time'] = ds['time'] - three_yrs
ds['time'].attrs['units'] = 'days since 0004-01-01 00:00:00'
ds['time'].attrs['calendar'] = 'noleap'
ds['time_bounds'] = ds['time_bounds'] - three_yrs
ds['time_bounds'].attrs['units'] = 'days since 0004-01-01 00:00:00'
ds['time_bounds'].attrs['calendar'] = 'noleap'
return ds
load_variable_data_loader.preprocess_func = preprocess
result = load_variable_data_loader.load_variable(
condensation_rain, DatetimeNoLeap(year, 1, 1),
DatetimeNoLeap(year, 12, 31),
intvl_in='monthly')
filepath = os.path.join(os.path.split(ROOT_PATH)[0], 'netcdf',
'000{}0101.precip_monthly.nc'.format(year))
expected = xr.open_dataset(filepath)['condensation_rain']
np.testing.assert_allclose(result.values, expected.values)
示例3: test_load_variable_preprocess
# 需要导入模块: import cftime [as 别名]
# 或者: from cftime import DatetimeNoLeap [as 别名]
def test_load_variable_preprocess(load_variable_data_loader):
def preprocess(ds, **kwargs):
if kwargs['start_date'] == DatetimeNoLeap(5, 1, 1):
ds['condensation_rain'] = 10. * ds['condensation_rain']
return ds
load_variable_data_loader.preprocess_func = preprocess
result = load_variable_data_loader.load_variable(
condensation_rain, DatetimeNoLeap(5, 1, 1),
DatetimeNoLeap(5, 12, 31),
intvl_in='monthly')
filepath = os.path.join(os.path.split(ROOT_PATH)[0], 'netcdf',
'00050101.precip_monthly.nc')
expected = 10. * xr.open_dataset(filepath)['condensation_rain']
np.testing.assert_allclose(result.values, expected.values)
result = load_variable_data_loader.load_variable(
condensation_rain, DatetimeNoLeap(4, 1, 1),
DatetimeNoLeap(4, 12, 31),
intvl_in='monthly')
filepath = os.path.join(os.path.split(ROOT_PATH)[0], 'netcdf',
'00040101.precip_monthly.nc')
expected = xr.open_dataset(filepath)['condensation_rain']
np.testing.assert_allclose(result.values, expected.values)
示例4: test_load_variable_mask_and_scale
# 需要导入模块: import cftime [as 别名]
# 或者: from cftime import DatetimeNoLeap [as 别名]
def test_load_variable_mask_and_scale(load_variable_data_loader):
def convert_all_to_missing_val(ds, **kwargs):
ds['condensation_rain'] = 0. * ds['condensation_rain'] + 1.0e20
ds['condensation_rain'].attrs['_FillValue'] = 1.0e20
return ds
load_variable_data_loader.preprocess_func = convert_all_to_missing_val
data = load_variable_data_loader.load_variable(
condensation_rain, DatetimeNoLeap(5, 1, 1),
DatetimeNoLeap(5, 12, 31),
intvl_in='monthly')
num_non_missing = np.isfinite(data).sum().item()
expected_num_non_missing = 0
assert num_non_missing == expected_num_non_missing
示例5: test_cftime_transform_noleap_warn
# 需要导入模块: import cftime [as 别名]
# 或者: from cftime import DatetimeNoLeap [as 别名]
def test_cftime_transform_noleap_warn(self):
try:
import cftime
except:
raise SkipTest('Test requires cftime library')
gregorian_dates = [cftime.DatetimeNoLeap(2000, 2, 28),
cftime.DatetimeNoLeap(2000, 3, 1),
cftime.DatetimeNoLeap(2000, 3, 2)]
curve = Curve((gregorian_dates, [1, 2, 3]))
plot = bokeh_renderer.get_plot(curve)
xs = plot.handles['cds'].data['x']
self.assertEqual(xs.astype('int64'),
np.array([951696000000, 951868800000, 951955200000]))
substr = (
"Converting cftime.datetime from a non-standard calendar "
"(noleap) to a standard calendar for plotting. This may "
"lead to subtle errors in formatting dates, for accurate "
"tick formatting switch to the matplotlib backend.")
self.log_handler.assertEndsWith('WARNING', substr)
示例6: test_recursively_compute_variable_native
# 需要导入模块: import cftime [as 别名]
# 或者: from cftime import DatetimeNoLeap [as 别名]
def test_recursively_compute_variable_native(load_variable_data_loader):
result = load_variable_data_loader.recursively_compute_variable(
condensation_rain, DatetimeNoLeap(5, 1, 1),
DatetimeNoLeap(5, 12, 31),
intvl_in='monthly')
filepath = os.path.join(os.path.split(ROOT_PATH)[0], 'netcdf',
'00050101.precip_monthly.nc')
expected = xr.open_dataset(filepath)['condensation_rain']
np.testing.assert_array_equal(result.values, expected.values)
示例7: test_recursively_compute_variable_one_level
# 需要导入模块: import cftime [as 别名]
# 或者: from cftime import DatetimeNoLeap [as 别名]
def test_recursively_compute_variable_one_level(load_variable_data_loader):
one_level = Var(
name='one_level', variables=(condensation_rain, condensation_rain),
func=lambda x, y: x + y)
result = load_variable_data_loader.recursively_compute_variable(
one_level, DatetimeNoLeap(5, 1, 1), DatetimeNoLeap(5, 12, 31),
intvl_in='monthly')
filepath = os.path.join(os.path.split(ROOT_PATH)[0], 'netcdf',
'00050101.precip_monthly.nc')
expected = 2. * xr.open_dataset(filepath)['condensation_rain']
np.testing.assert_array_equal(result.values, expected.values)
示例8: test_recursively_compute_grid_attr
# 需要导入模块: import cftime [as 别名]
# 或者: from cftime import DatetimeNoLeap [as 别名]
def test_recursively_compute_grid_attr(load_variable_data_loader):
result = load_variable_data_loader.recursively_compute_variable(
bk, DatetimeNoLeap(5, 1, 1),
DatetimeNoLeap(5, 12, 31), model=example_model,
intvl_in='monthly')
filepath = os.path.join(os.path.split(ROOT_PATH)[0], 'netcdf',
'00060101.sphum_monthly.nc')
expected = xr.open_dataset(filepath)['bk']
np.testing.assert_array_equal(result.values, expected.values)
示例9: test_recursively_compute_grid_attr_multi_level
# 需要导入模块: import cftime [as 别名]
# 或者: from cftime import DatetimeNoLeap [as 别名]
def test_recursively_compute_grid_attr_multi_level(load_variable_data_loader):
one_level = Var(
name='one_level', variables=(bk, ),
func=lambda x: 2 * x)
multi_level = Var(
name='multi_level', variables=(one_level, bk),
func=lambda x, y: x + y)
result = load_variable_data_loader.recursively_compute_variable(
multi_level, DatetimeNoLeap(5, 1, 1),
DatetimeNoLeap(5, 12, 31), model=example_model,
intvl_in='monthly')
filepath = os.path.join(os.path.split(ROOT_PATH)[0], 'netcdf',
'00060101.sphum_monthly.nc')
expected = 3 * xr.open_dataset(filepath)['bk']
np.testing.assert_array_equal(result.values, expected.values)
示例10: test_recursively_compute_grid_attr_error
# 需要导入模块: import cftime [as 别名]
# 或者: from cftime import DatetimeNoLeap [as 别名]
def test_recursively_compute_grid_attr_error(load_variable_data_loader):
# Should fail because zsurf is not provided to the example_model object
zsurf = Var(name=ZSURF_STR, def_time=False, def_vert=False,
def_lon=True, def_lat=True)
with pytest.raises(AttributeError):
load_variable_data_loader.recursively_compute_variable(
zsurf, DatetimeNoLeap(5, 1, 1),
DatetimeNoLeap(5, 12, 31), model=example_model,
intvl_in='monthly')
示例11: test_init_default_dates
# 需要导入模块: import cftime [as 别名]
# 或者: from cftime import DatetimeNoLeap [as 别名]
def test_init_default_dates(self):
gdl = GFDLDataLoader(data_start_date=cftime.DatetimeNoLeap(1, 1, 1),
data_end_date=cftime.DatetimeNoLeap(1, 12, 31))
run_ = Run(data_loader=gdl)
self.assertEqual(run_.default_start_date,
cftime.DatetimeNoLeap(1, 1, 1))
self.assertEqual(run_.default_end_date,
cftime.DatetimeNoLeap(1, 12, 31))
ddl = DictDataLoader({'monthly': '/a/'})
run_ = Run(data_loader=ddl)
self.assertEqual(run_.default_start_date, None)
self.assertEqual(run_.default_end_date, None)
示例12: test_shift_cftime_singular
# 需要导入模块: import cftime [as 别名]
# 或者: from cftime import DatetimeNoLeap [as 别名]
def test_shift_cftime_singular():
"""Tests that a singular ``cftime`` is shifted the appropriate amount."""
cftime_initial = cftime.DatetimeNoLeap(1990, 1, 1)
cftime_expected = cftime.DatetimeNoLeap(1990, 3, 1)
# Shift forward two months at month start.
cftime_from_func = shift_cftime_singular(cftime_initial, 2, 'MS')
assert cftime_expected == cftime_from_func
示例13: test_cftime_raw_date
# 需要导入模块: import cftime [as 别名]
# 或者: from cftime import DatetimeNoLeap [as 别名]
def test_cftime_raw_date(self):
val = cftime.DatetimeNoLeap(2014, 8, 12)
result = NetCDFTimeConverter().convert(val, None, None)
np.testing.assert_array_equal(result, 5333.)
示例14: test_esmlab_accessor
# 需要导入模块: import cftime [as 别名]
# 或者: from cftime import DatetimeNoLeap [as 别名]
def test_esmlab_accessor():
ds = xr.Dataset(
{
'temp': xr.DataArray(
[1, 2],
dims=['time'],
coords={'time': pd.date_range(start='2000', periods=2, freq='1D')},
)
}
)
attrs = {'calendar': 'noleap', 'units': 'days since 2000-01-01 00:00:00'}
ds.time.attrs = attrs
esm = ds.esmlab.set_time(time_coord_name='time')
xr.testing._assert_internal_invariants(esm._ds_time_computed)
# Time and Time bound Attributes
expected = dict(esm.time_attrs)
attrs['bounds'] = None
assert expected == attrs
assert esm.time_bound_attrs == {}
assert esm.variables == ['temp']
assert esm.static_variables == []
# Time bound diff
expected = xr.ones_like(ds.time, dtype='float64')
xr.testing.assert_equal(expected, esm.time_bound_diff)
# Compute time var
with pytest.raises(ValueError):
esm.compute_time_var(midpoint=True, year_offset=2100)
# Decode arbitrary time value
with pytest.raises(ValueError):
esm.decode_arbitrary_time(ds.time.data[0], units=attrs['units'], calendar=attrs['calendar'])
res = esm.decode_arbitrary_time(
np.array([30]), units=attrs['units'], calendar=attrs['calendar']
)
assert res[0] == cftime.DatetimeNoLeap(2000, 1, 31, 0, 0, 0, 0, 0, 31)
data = xr.DataArray(
[1, 2],
dims=['time'],
coords={'time': pd.date_range(start='2000', freq='1D', periods=2)},
attrs={'calendar': 'standard', 'units': 'days since 2001-01-01 00:00:00'},
name='rand',
).to_dataset()
data['time'] = xr.cftime_range(start='2000', freq='1D', periods=2)
with pytest.raises(ValueError):
data.esmlab.set_time().get_time_decoded()
with pytest.raises(ValueError):
data.esmlab.set_time().get_time_undecoded()
data = xr.DataArray(
[[1, 2], [7, 8]], dims=['x', 'y'], coords={'x': [1, 2], 'y': [2, 3]}, name='rand'
).to_dataset()
with pytest.raises(ValueError):
data.esmlab.set_time('time-bound-coord')