本文整理匯總了Python中xarray.decode_cf方法的典型用法代碼示例。如果您正苦於以下問題:Python xarray.decode_cf方法的具體用法?Python xarray.decode_cf怎麽用?Python xarray.decode_cf使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類xarray
的用法示例。
在下文中一共展示了xarray.decode_cf方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_maybe_apply_time_shift_inst
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import decode_cf [as 別名]
def test_maybe_apply_time_shift_inst(gfdl_data_loader, ds_inst, var_name,
generate_file_set_args):
ds_inst = xr.decode_cf(ds_inst)
generate_file_set_args['dtype_in_time'] = 'inst'
generate_file_set_args['intvl_in'] = '3hr'
da = ds_inst[var_name]
result = gfdl_data_loader._maybe_apply_time_shift(
da.copy(), **generate_file_set_args)[TIME_STR]
expected = da[TIME_STR] + np.timedelta64(-3, 'h')
expected[TIME_STR] = expected
assert result.identical(expected)
generate_file_set_args['intvl_in'] = 'daily'
da = ds_inst[var_name]
result = gfdl_data_loader._maybe_apply_time_shift(
da.copy(), **generate_file_set_args)[TIME_STR]
expected = da[TIME_STR]
expected[TIME_STR] = expected
assert result.identical(expected)
示例2: create_range
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import decode_cf [as 別名]
def create_range(i, decode=False):
where = create_dset_where(i)
ngates = where["nbins"]
range_start = where["rstart"] * 1000.0
bin_range = where["rscale"]
cent_first = range_start + bin_range / 2.0
range_data = np.arange(
cent_first, range_start + bin_range * ngates, bin_range, dtype="float32"
)
range_attrs = io.xarray.range_attrs
range_attrs["meters_to_center_of_first_gate"] = cent_first[0]
range_attrs["meters_between_gates"] = bin_range[0]
da = xr.DataArray(range_data, dims=["range"], attrs=range_attrs)
if decode:
da = xr.decode_cf(xr.Dataset({"arr": da})).arr
return da
示例3: _make_coords_faces
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import decode_cf [as 別名]
def _make_coords_faces(self, all_iters):
time = self.delta_t * all_iters
time_attrs = {'units': self.time_units,
'calendar': self.calendar}
coords = {'face': ('face', np.arange(self.nface)),
'i': ('i', np.arange(self.nx)),
'i_g': ('i_g', np.arange(self.nx)),
'j': ('j', np.arange(self.nx)),
'j_g': ('j_g', np.arange(self.nx)),
'k': ('k', np.arange(self.nz)),
'k_u': ('k_u', np.arange(self.nz)),
'k_l': ('k_l', np.arange(self.nz)),
'k_p1': ('k_p1', np.arange(self.nz + 1)),
'niter': ('time', all_iters),
'time': ('time', time, time_attrs)
}
return xr.decode_cf(xr.Dataset(coords=coords))
示例4: test_create_ensemble
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import decode_cf [as 別名]
def test_create_ensemble(self):
ens = ensembles.create_ensemble(self.nc_files_simple)
assert len(ens.realization) == len(self.nc_files_simple)
assert len(ens.time) == 151
# create again using xr.Dataset objects
ds_all = []
for n in self.nc_files_simple:
ds = xr.open_dataset(n, decode_times=False)
ds["time"] = xr.decode_cf(ds).time
ds_all.append(ds)
ens1 = ensembles.create_ensemble(ds_all)
coords = list(ens1.coords)
coords.extend(list(ens1.data_vars))
for c in coords:
np.testing.assert_array_equal(ens[c], ens1[c])
for i in np.arange(0, len(ens1.realization)):
np.testing.assert_array_equal(
ens1.isel(realization=i).tg_mean.values, ds_all[i].tg_mean.values
)
示例5: prep_time_data
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import decode_cf [as 別名]
def prep_time_data(ds):
"""Prepare time coordinate information in Dataset for use in aospy.
1. If the Dataset contains a time bounds coordinate, add attributes
representing the true beginning and end dates of the time interval used
to construct the Dataset
2. If the Dataset contains a time bounds coordinate, overwrite the time
coordinate values with the averages of the time bounds at each timestep
3. Decode the times into np.datetime64 objects for time indexing
Parameters
----------
ds : Dataset
Pre-processed Dataset with time coordinate renamed to
internal_names.TIME_STR
Returns
-------
Dataset
The processed Dataset
"""
ds = ensure_time_as_index(ds)
if TIME_BOUNDS_STR in ds:
ds = ensure_time_avg_has_cf_metadata(ds)
ds[TIME_STR] = average_time_bounds(ds)
else:
logging.warning("dt array not found. Assuming equally spaced "
"values in time, even though this may not be "
"the case")
ds = add_uniform_time_weights(ds)
return xr.decode_cf(ds, decode_times=True, decode_coords=False,
mask_and_scale=True)
示例6: test_assert_has_data_for_time
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import decode_cf [as 別名]
def test_assert_has_data_for_time():
time_bounds = np.array([[0, 31], [31, 59], [59, 90]])
nv = np.array([0, 1])
time = np.array([15, 46, 74])
data = np.zeros((3))
var_name = 'a'
ds = xr.DataArray(data,
coords=[time],
dims=[TIME_STR],
name=var_name).to_dataset()
ds[TIME_BOUNDS_STR] = xr.DataArray(time_bounds,
coords=[time, nv],
dims=[TIME_STR, BOUNDS_STR],
name=TIME_BOUNDS_STR)
units_str = 'days since 2000-01-01 00:00:00'
ds[TIME_STR].attrs['units'] = units_str
ds = ensure_time_avg_has_cf_metadata(ds)
ds = set_grid_attrs_as_coords(ds)
ds = xr.decode_cf(ds)
da = ds[var_name]
start_date = np.datetime64('2000-01-01')
end_date = np.datetime64('2000-03-31')
_assert_has_data_for_time(da, start_date, end_date)
start_date_bad = np.datetime64('1999-12-31')
end_date_bad = np.datetime64('2000-04-01')
with pytest.raises(AssertionError):
_assert_has_data_for_time(da, start_date_bad, end_date)
with pytest.raises(AssertionError):
_assert_has_data_for_time(da, start_date, end_date_bad)
with pytest.raises(AssertionError):
_assert_has_data_for_time(da, start_date_bad, end_date_bad)
示例7: test_assert_has_data_for_time_cftime_datetimes
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import decode_cf [as 別名]
def test_assert_has_data_for_time_cftime_datetimes(calendar, date_type):
time_bounds = np.array([[0, 2], [2, 4], [4, 6]])
nv = np.array([0, 1])
time = np.array([1, 3, 5])
data = np.zeros((3))
var_name = 'a'
ds = xr.DataArray(data,
coords=[time],
dims=[TIME_STR],
name=var_name).to_dataset()
ds[TIME_BOUNDS_STR] = xr.DataArray(time_bounds,
coords=[time, nv],
dims=[TIME_STR, BOUNDS_STR],
name=TIME_BOUNDS_STR)
units_str = 'days since 0002-01-02 00:00:00'
ds[TIME_STR].attrs['units'] = units_str
ds[TIME_STR].attrs['calendar'] = calendar
ds = ensure_time_avg_has_cf_metadata(ds)
ds = set_grid_attrs_as_coords(ds)
ds = xr.decode_cf(ds)
da = ds[var_name]
start_date = date_type(2, 1, 2)
end_date = date_type(2, 1, 8)
_assert_has_data_for_time(da, start_date, end_date)
start_date_bad = date_type(2, 1, 1)
end_date_bad = date_type(2, 1, 9)
with pytest.raises(AssertionError):
_assert_has_data_for_time(da, start_date_bad, end_date)
with pytest.raises(AssertionError):
_assert_has_data_for_time(da, start_date, end_date_bad)
with pytest.raises(AssertionError):
_assert_has_data_for_time(da, start_date_bad, end_date_bad)
示例8: test_assert_has_data_for_time_str_input
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import decode_cf [as 別名]
def test_assert_has_data_for_time_str_input():
time_bounds = np.array([[0, 31], [31, 59], [59, 90]])
nv = np.array([0, 1])
time = np.array([15, 46, 74])
data = np.zeros((3))
var_name = 'a'
ds = xr.DataArray(data,
coords=[time],
dims=[TIME_STR],
name=var_name).to_dataset()
ds[TIME_BOUNDS_STR] = xr.DataArray(time_bounds,
coords=[time, nv],
dims=[TIME_STR, BOUNDS_STR],
name=TIME_BOUNDS_STR)
units_str = 'days since 2000-01-01 00:00:00'
ds[TIME_STR].attrs['units'] = units_str
ds = ensure_time_avg_has_cf_metadata(ds)
ds = set_grid_attrs_as_coords(ds)
ds = xr.decode_cf(ds)
da = ds[var_name]
start_date = '2000-01-01'
end_date = '2000-03-31'
_assert_has_data_for_time(da, start_date, end_date)
start_date_bad = '1999-12-31'
end_date_bad = '2000-04-01'
# With strings these checks are disabled
_assert_has_data_for_time(da, start_date_bad, end_date)
_assert_has_data_for_time(da, start_date, end_date_bad)
_assert_has_data_for_time(da, start_date_bad, end_date_bad)
示例9: test_sel_time
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import decode_cf [as 別名]
def test_sel_time():
time_bounds = np.array([[0, 31], [31, 59], [59, 90]])
nv = np.array([0, 1])
time = np.array([15, 46, 74])
data = np.zeros((3))
var_name = 'a'
ds = xr.DataArray(data,
coords=[time],
dims=[TIME_STR],
name=var_name).to_dataset()
ds[TIME_BOUNDS_STR] = xr.DataArray(time_bounds,
coords=[time, nv],
dims=[TIME_STR, BOUNDS_STR],
name=TIME_BOUNDS_STR)
units_str = 'days since 2000-01-01 00:00:00'
ds[TIME_STR].attrs['units'] = units_str
ds = ensure_time_avg_has_cf_metadata(ds)
ds = set_grid_attrs_as_coords(ds)
ds = xr.decode_cf(ds)
da = ds[var_name]
start_date = np.datetime64('2000-02-01')
end_date = np.datetime64('2000-03-31')
result = sel_time(da, start_date, end_date)
assert result[SUBSET_START_DATE_STR].values == start_date
assert result[SUBSET_END_DATE_STR].values == end_date
示例10: test_maybe_apply_time_shift_ts
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import decode_cf [as 別名]
def test_maybe_apply_time_shift_ts(gfdl_data_loader, ds_with_time_bounds,
var_name, generate_file_set_args):
ds = xr.decode_cf(ds_with_time_bounds)
da = ds[var_name]
result = gfdl_data_loader._maybe_apply_time_shift(
da.copy(), **generate_file_set_args)[TIME_STR]
assert result.identical(da[TIME_STR])
示例11: read
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import decode_cf [as 別名]
def read(self, filename, **kwargs):
"""Read and parse a NetCDF file and load it to a xarray.Dataset
Args:
filename: Path and name of the file as string or FileInfo object.
**kwargs: Additional key word arguments that are allowed for the
:class:`~typhon.files.handlers.common.NetCDF4` class.
Returns:
A xarray.Dataset object.
"""
# Make sure that the standard fields are always gonna be imported:
fields = kwargs.pop("fields", None)
if fields is not None:
fields = {"time", "lat", "lon"} | set(fields)
# xarray has problems with decoding the time variable correctly. Hence,
# we disable it here:
decode_cf = kwargs.pop("decode_cf", True)
data = super().read(filename, fields=fields, decode_cf=False, **kwargs)
# Then we fix the problem (we need integer64 instead of integer 32):
attrs = data["time"].attrs.copy()
data["time"] = data["time"].astype(int)
data["time"].attrs = attrs
# Do decoding now (just if the user wanted it!)
if decode_cf:
return xr.decode_cf(data)
return data
示例12: create_ray_time
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import decode_cf [as 別名]
def create_ray_time(i, decode=False, nrays=360):
time_data = (create_startazT(i, nrays=nrays) + create_stopazT(i, nrays=nrays)) / 2.0
da = xr.DataArray(time_data, dims=["azimuth"], attrs=io.xarray.time_attrs)
if decode:
da = xr.decode_cf(xr.Dataset({"arr": da})).arr
return da
示例13: create_azimuth
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import decode_cf [as 別名]
def create_azimuth(decode=False, nrays=360):
startaz = create_startazA(nrays=nrays)
stopaz = create_stopazA(nrays=nrays)
zero_index = np.where(stopaz < startaz)
stopaz[zero_index[0]] += 360
azimuth_data = (startaz + stopaz) / 2.0
da = xr.DataArray(azimuth_data, dims=["azimuth"], attrs=io.xarray.az_attrs)
if decode:
da = xr.decode_cf(xr.Dataset({"arr": da})).arr
return da
示例14: create_elevation
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import decode_cf [as 別名]
def create_elevation(i, decode=False, nrays=360):
startel = create_startelA(i, nrays=nrays)
stopel = create_stopelA(i, nrays=nrays)
elevation_data = (startel + stopel) / 2.0
da = xr.DataArray(elevation_data, dims=["azimuth"], attrs=io.xarray.el_attrs)
if decode:
da = xr.decode_cf(xr.Dataset({"arr": da})).arr
return da
示例15: _decode_cf
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import decode_cf [as 別名]
def _decode_cf(self, obj):
if isinstance(obj, xr.DataArray):
out = xr.decode_cf(xr.Dataset({"arr": obj}), self._kwargs).arr
else:
out = xr.decode_cf(obj, self._kwargs)
return out