本文整理汇总了Python中xarray.Dataset类的典型用法代码示例。如果您正苦于以下问题:Python Dataset类的具体用法?Python Dataset怎么用?Python Dataset使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Dataset类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_open_and_do_math
def test_open_and_do_math(self):
original = Dataset({'foo': ('x', np.random.randn(10))})
with create_tmp_file() as tmp:
original.to_netcdf(tmp)
with open_mfdataset(tmp) as ds:
actual = 1.0 * ds
self.assertDatasetAllClose(original, actual)
示例2: radec2azel
def radec2azel(scale: xarray.Dataset,
latlon: Tuple[float, float], time: datetime=None) -> xarray.Dataset:
if latlon is None or not isinstance(scale, xarray.Dataset):
return None
if time is None:
with fits.open(scale.filename, mode='readonly') as f:
try:
t = f[0].header['FRAME'] # TODO this only works from Solis?
except KeyError:
logging.error('no time given in file or manually, cannot compute az/el')
return None
time = parse(t)
logging.info('using FITS header for time')
elif isinstance(time, datetime):
pass
elif isinstance(time, (float, int)): # assume UT1_Unix
time = datetime.utcfromtimestamp(time)
else: # user override of frame time
time = parse(time)
print('image time:', time)
# %% knowing camera location, time, and sky coordinates observed, convert to az/el for each pixel
az, el = pymap3d.radec2azel(scale['ra'], scale['dec'], latlon[0], latlon[1], time)
# %% collect output
scale['az'] = (('y', 'x'), az)
scale['el'] = (('y', 'x'), el)
scale.attrs['lat'] = latlon[0]
scale.attrs['lon'] = latlon[1]
scale.attrs['time'] = time
return scale
示例3: test_concat_multiindex
def test_concat_multiindex(self):
x = pd.MultiIndex.from_product([[1, 2, 3], ['a', 'b']])
expected = Dataset({'x': x})
actual = concat([expected.isel(x=slice(2)),
expected.isel(x=slice(2, None))], 'x')
assert expected.equals(actual)
assert isinstance(actual.x.to_index(), pd.MultiIndex)
示例4: test_roundtrip_strings_with_fill_value
def test_roundtrip_strings_with_fill_value(self):
values = np.array(['ab', 'cdef', np.nan], dtype=object)
encoding = {'_FillValue': np.string_('X'), 'dtype': np.dtype('S1')}
original = Dataset({'x': ('t', values, {}, encoding)})
expected = original.copy(deep=True)
expected['x'][:2] = values[:2].astype('S')
with self.roundtrip(original) as actual:
self.assertDatasetIdentical(expected, actual)
original = Dataset({'x': ('t', values, {}, {'_FillValue': '\x00'})})
if not isinstance(self, Only32BitTypes):
# these stores can save unicode strings
expected = original.copy(deep=True)
if isinstance(self, BaseNetCDF4Test):
# netCDF4 can't keep track of an empty _FillValue for VLEN
# variables
expected['x'][-1] = ''
elif (isinstance(self, (NetCDF3ViaNetCDF4DataTest,
NetCDF4ClassicViaNetCDF4DataTest)) or
(has_netCDF4 and type(self) is GenericNetCDFDataTest)):
# netCDF4 can't keep track of an empty _FillValue for nc3, either:
# https://github.com/Unidata/netcdf4-python/issues/273
expected['x'][-1] = np.string_('')
with self.roundtrip(original) as actual:
self.assertDatasetIdentical(expected, actual)
示例5: _preprocess_dataset
def _preprocess_dataset(self, ds: Dataset):
# Convert specific data variables to coordinate variables
for var_name in EXTRA_COORDS_VAR_NAMES:
if var_name in ds.data_vars:
ds.set_coords(var_name, inplace=True)
# print(ds)
return ds
示例6: test_coordinates_encoding
def test_coordinates_encoding(self):
def equals_latlon(obj):
return obj == 'lat lon' or obj == 'lon lat'
original = Dataset({'temp': ('x', [0, 1]), 'precip': ('x', [0, -1])},
{'lat': ('x', [2, 3]), 'lon': ('x', [4, 5])})
with self.roundtrip(original) as actual:
self.assertDatasetIdentical(actual, original)
with create_tmp_file() as tmp_file:
original.to_netcdf(tmp_file)
with open_dataset(tmp_file, decode_coords=False) as ds:
self.assertTrue(equals_latlon(ds['temp'].attrs['coordinates']))
self.assertTrue(equals_latlon(ds['precip'].attrs['coordinates']))
self.assertNotIn('coordinates', ds.attrs)
self.assertNotIn('coordinates', ds['lat'].attrs)
self.assertNotIn('coordinates', ds['lon'].attrs)
modified = original.drop(['temp', 'precip'])
with self.roundtrip(modified) as actual:
self.assertDatasetIdentical(actual, modified)
with create_tmp_file() as tmp_file:
modified.to_netcdf(tmp_file)
with open_dataset(tmp_file, decode_coords=False) as ds:
self.assertTrue(equals_latlon(ds.attrs['coordinates']))
self.assertNotIn('coordinates', ds['lat'].attrs)
self.assertNotIn('coordinates', ds['lon'].attrs)
示例7: adjust_temporal_attrs_impl
def adjust_temporal_attrs_impl(ds: xr.Dataset) -> xr.Dataset:
"""
Adjust the global temporal attributes of the dataset by doing some
introspection of the dataset and adjusting the appropriate attributes
accordingly.
In case the determined attributes do not exist in the dataset, these will
be added.
For more information on suggested global attributes see
`Attribute Convention for Data Discovery
<http://wiki.esipfed.org/index.php/Attribute_Convention_for_Data_Discovery>`_
:param ds: Dataset to adjust
:return: Adjusted dataset
"""
temporal_attrs = _get_temporal_cf_attrs_from_var(ds)
if temporal_attrs:
ds = ds.copy()
# Align temporal attributes with the ones from the shallow Dataset copy
for key in temporal_attrs:
if temporal_attrs[key] is not None:
ds.attrs[key] = temporal_attrs[key]
else:
ds.attrs.pop(key, None)
return ds
示例8: test_roundtrip_object_dtype
def test_roundtrip_object_dtype(self):
floats = np.array([0.0, 0.0, 1.0, 2.0, 3.0], dtype=object)
floats_nans = np.array([np.nan, np.nan, 1.0, 2.0, 3.0], dtype=object)
letters = np.array(['ab', 'cdef', 'g'], dtype=object)
letters_nans = np.array(['ab', 'cdef', np.nan], dtype=object)
all_nans = np.array([np.nan, np.nan], dtype=object)
original = Dataset({'floats': ('a', floats),
'floats_nans': ('a', floats_nans),
'letters': ('b', letters),
'letters_nans': ('b', letters_nans),
'all_nans': ('c', all_nans),
'nan': ([], np.nan)})
expected = original.copy(deep=True)
if isinstance(self, Only32BitTypes):
# for netCDF3 tests, expect the results to come back as characters
expected['letters_nans'] = expected['letters_nans'].astype('S')
expected['letters'] = expected['letters'].astype('S')
with self.roundtrip(original) as actual:
try:
self.assertDatasetIdentical(expected, actual)
except AssertionError:
# Most stores use '' for nans in strings, but some don't
# first try the ideal case (where the store returns exactly)
# the original Dataset), then try a more realistic case.
# ScipyDataTest, NetCDF3ViaNetCDF4DataTest and NetCDF4DataTest
# all end up using this case.
expected['letters_nans'][-1] = ''
self.assertDatasetIdentical(expected, actual)
示例9: test_to_dask_dataframe
def test_to_dask_dataframe(self):
# Test conversion of Datasets to dask DataFrames
x = da.from_array(np.random.randn(10), chunks=4)
y = np.arange(10, dtype='uint8')
t = list('abcdefghij')
ds = Dataset(OrderedDict([('a', ('t', x)),
('b', ('t', y)),
('t', ('t', t))]))
expected_pd = pd.DataFrame({'a': x,
'b': y},
index=pd.Index(t, name='t'))
# test if 1-D index is correctly set up
expected = dd.from_pandas(expected_pd, chunksize=4)
actual = ds.to_dask_dataframe(set_index=True)
# test if we have dask dataframes
assert isinstance(actual, dd.DataFrame)
# use the .equals from pandas to check dataframes are equivalent
assert_frame_equal(expected.compute(), actual.compute())
# test if no index is given
expected = dd.from_pandas(expected_pd.reset_index(drop=False),
chunksize=4)
actual = ds.to_dask_dataframe(set_index=False)
assert isinstance(actual, dd.DataFrame)
assert_frame_equal(expected.compute(), actual.compute())
示例10: state_to_xarray
def state_to_xarray(state):
'''Convert a dictionary of climlab.Field objects to xarray.Dataset
Input: dictionary of climlab.Field objects
(e.g. process.state or process.diagnostics dictionary)
Output: xarray.Dataset object with all spatial axes,
including 'bounds' axes indicating cell boundaries in each spatial dimension.
Any items in the dictionary that are not instances of climlab.Field
are ignored.'''
from climlab.domain.field import Field
ds = Dataset()
for name, field in state.items():
if isinstance(field, Field):
ds[name] = Field_to_xarray(field)
dom = field.domain
for axname, ax in dom.axes.items():
bounds_name = axname + '_bounds'
ds.coords[bounds_name] = DataArray(ax.bounds, dims=[bounds_name],
coords={bounds_name:ax.bounds})
try:
ds[bounds_name].attrs['units'] = ax.units
except:
pass
else:
warnings.warn('{} excluded from Dataset because it is not a Field variable.'.format(name))
return ds
示例11: test_dask_layers_and_dependencies
def test_dask_layers_and_dependencies():
ds = Dataset({'foo': ('x', range(5)),
'bar': ('x', range(5))}).chunk()
x = dask.delayed(ds)
assert set(x.__dask_graph__().dependencies).issuperset(
ds.__dask_graph__().dependencies)
assert set(x.foo.__dask_graph__().dependencies).issuperset(
ds.__dask_graph__().dependencies)
示例12: test_save_mfdataset_roundtrip
def test_save_mfdataset_roundtrip(self):
original = Dataset({'foo': ('x', np.random.randn(10))})
datasets = [original.isel(x=slice(5)),
original.isel(x=slice(5, 10))]
with create_tmp_file() as tmp1:
with create_tmp_file() as tmp2:
save_mfdataset(datasets, [tmp1, tmp2])
with open_mfdataset([tmp1, tmp2]) as actual:
self.assertDatasetIdentical(actual, original)
示例13: adjust_spatial_attrs_impl
def adjust_spatial_attrs_impl(ds: xr.Dataset, allow_point: bool) -> xr.Dataset:
"""
Adjust the global spatial attributes of the dataset by doing some
introspection of the dataset and adjusting the appropriate attributes
accordingly.
In case the determined attributes do not exist in the dataset, these will
be added.
For more information on suggested global attributes see
`Attribute Convention for Data Discovery
<http://wiki.esipfed.org/index.php/Attribute_Convention_for_Data_Discovery>`_
:param ds: Dataset to adjust
:param allow_point: Whether to accept single point cells
:return: Adjusted dataset
"""
copied = False
for dim in ('lon', 'lat'):
geo_spatial_attrs = _get_geo_spatial_cf_attrs_from_var(ds, dim, allow_point=allow_point)
if geo_spatial_attrs:
# Copy any new attributes into the shallow Dataset copy
for key in geo_spatial_attrs:
if geo_spatial_attrs[key] is not None:
if not copied:
ds = ds.copy()
copied = True
ds.attrs[key] = geo_spatial_attrs[key]
lon_min = ds.attrs.get('geospatial_lon_min')
lat_min = ds.attrs.get('geospatial_lat_min')
lon_max = ds.attrs.get('geospatial_lon_max')
lat_max = ds.attrs.get('geospatial_lat_max')
if lon_min is not None and lat_min is not None and lon_max is not None and lat_max is not None:
if not copied:
ds = ds.copy()
ds.attrs['geospatial_bounds'] = 'POLYGON(({} {}, {} {}, {} {}, {} {}, {} {}))'. \
format(lon_min, lat_min, lon_min, lat_max, lon_max, lat_max, lon_max, lat_min, lon_min, lat_min)
# Determination of the following attributes from introspection in a general
# way is ambiguous, hence it is safer to drop them than to risk preserving
# out of date attributes.
drop = ['geospatial_bounds_crs', 'geospatial_bounds_vertical_crs',
'geospatial_vertical_min', 'geospatial_vertical_max',
'geospatial_vertical_positive', 'geospatial_vertical_units',
'geospatial_vertical_resolution']
for key in drop:
ds.attrs.pop(key, None)
return ds
示例14: test_concat_coords
def test_concat_coords(self):
data = Dataset({"foo": ("x", np.random.randn(10))})
expected = data.assign_coords(c=("x", [0] * 5 + [1] * 5))
objs = [data.isel(x=slice(5)).assign_coords(c=0), data.isel(x=slice(5, None)).assign_coords(c=1)]
for coords in ["different", "all", ["c"]]:
actual = concat(objs, dim="x", coords=coords)
self.assertDatasetIdentical(expected, actual)
for coords in ["minimal", []]:
with self.assertRaisesRegexp(ValueError, "not equal across"):
concat(objs, dim="x", coords=coords)
示例15: test_concat_encoding
def test_concat_encoding(self):
# Regression test for GH1297
ds = Dataset({'foo': (['x', 'y'], np.random.random((2, 3))),
'bar': (['x', 'y'], np.random.random((2, 3)))},
{'x': [0, 1]})
foo = ds['foo']
foo.encoding = {"complevel": 5}
ds.encoding = {"unlimited_dims": 'x'}
assert concat([foo, foo], dim="x").encoding == foo.encoding
assert concat([ds, ds], dim="x").encoding == ds.encoding