Python cftime.DatetimeNoLeap() Examples
The following are 14
code examples of cftime.DatetimeNoLeap().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
cftime
, or try the search function
.
Example #1
Source File: testelementplot.py From holoviews with BSD 3-Clause "New" or "Revised" License | 7 votes |
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)
Example #2
Source File: test_data_loader.py From aospy with Apache License 2.0 | 6 votes |
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)
Example #3
Source File: test_data_loader.py From aospy with Apache License 2.0 | 6 votes |
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)
Example #4
Source File: test_data_loader.py From aospy with Apache License 2.0 | 6 votes |
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
Example #5
Source File: util.py From forest with BSD 3-Clause "New" or "Revised" License | 6 votes |
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)))
Example #6
Source File: test_data_loader.py From aospy with Apache License 2.0 | 5 votes |
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)
Example #7
Source File: test_data_loader.py From aospy with Apache License 2.0 | 5 votes |
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)
Example #8
Source File: test_data_loader.py From aospy with Apache License 2.0 | 5 votes |
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)
Example #9
Source File: test_data_loader.py From aospy with Apache License 2.0 | 5 votes |
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)
Example #10
Source File: test_data_loader.py From aospy with Apache License 2.0 | 5 votes |
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')
Example #11
Source File: test_run.py From aospy with Apache License 2.0 | 5 votes |
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)
Example #12
Source File: test_utils.py From climpred with MIT License | 5 votes |
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
Example #13
Source File: test_NetCDFTimeConverter.py From nc-time-axis with BSD 3-Clause "New" or "Revised" License | 5 votes |
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.)
Example #14
Source File: test_core.py From esmlab with Apache License 2.0 | 4 votes |
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')