本文整理匯總了Python中xarray.open_zarr方法的典型用法代碼示例。如果您正苦於以下問題:Python xarray.open_zarr方法的具體用法?Python xarray.open_zarr怎麽用?Python xarray.open_zarr使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類xarray
的用法示例。
在下文中一共展示了xarray.open_zarr方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_rectify_without_vars
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import open_zarr [as 別名]
def test_rectify_without_vars(self):
"""Test that rectify selects all variables when --var is not given."""
# For now, specify the image geometry explicitly with --size, --point,
# and --res to avoid triggering an "invalid y_min" ValueError when
# ImageGeom tries to determine it automatically. Once Issue #303 has
# been fixed, these options can be omitted.
result = self.invoke_cli(['rectify',
'--size', '508,253',
'--point', '-179.5,-89.5',
'--res', '0.7071067811865475',
TEST_ZARR_DIR])
self.assertEqual(0, result.exit_code)
self.assertEqual('Opening dataset from \'test.zarr\'...\n'
'Rectifying...\n'
'Writing rectified dataset to \'out.zarr\'...\n'
'Done.\n',
result.stdout)
self.assertTrue(os.path.isdir('out.zarr'))
ds = xr.open_zarr('out.zarr')
assert_cube(ds)
self.assertIn('precipitation', ds)
self.assertIn('temperature', ds)
self.assertIn('soil_moisture', ds)
示例2: test_dry_run
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import open_zarr [as 別名]
def test_dry_run(self):
result = self.invoke_cli(['prune', self.TEST_CUBE, "--dry-run"])
self.assertEqual(0, result.exit_code)
self.assertEqual("Opening cube from 'test.zarr'...\n"
"Identifying empty blocks...\n"
"Deleting 24 empty block file(s) for variable 'precipitation'...\n"
"Deleting 24 empty block file(s) for variable 'temperature'...\n"
"Done, 48 block file(s) deleted.\n",
result.stdout)
expected_file_names = sorted(['.zarray',
'.zattrs',
'0.0.0', '0.0.1', '0.0.2', '0.0.3', '0.1.0', '0.1.1', '0.1.2', '0.1.3',
'1.0.0', '1.0.1', '1.0.2', '1.0.3', '1.1.0', '1.1.1', '1.1.2', '1.1.3',
'2.0.0', '2.0.1', '2.0.2', '2.0.3', '2.1.0', '2.1.1', '2.1.2', '2.1.3'])
self.assertEqual(expected_file_names, sorted(os.listdir('test.zarr/precipitation')))
self.assertEqual(expected_file_names, sorted(os.listdir('test.zarr/temperature')))
ds = xr.open_zarr('test.zarr')
assert_cube(ds)
self.assertIn('precipitation', ds)
self.assertEqual((3, 180, 360), ds.precipitation.shape)
self.assertEqual(('time', 'lat', 'lon'), ds.precipitation.dims)
self.assertIn('temperature', ds)
self.assertEqual((3, 180, 360), ds.temperature.shape)
self.assertEqual(('time', 'lat', 'lon'), ds.temperature.dims)
示例3: test_no_dry_run
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import open_zarr [as 別名]
def test_no_dry_run(self):
result = self.invoke_cli(['prune', self.TEST_CUBE])
self.assertEqual(0, result.exit_code)
self.assertEqual("Opening cube from 'test.zarr'...\n"
"Identifying empty blocks...\n"
"Deleting 24 empty block file(s) for variable 'precipitation'...\n"
"Deleting 24 empty block file(s) for variable 'temperature'...\n"
"Done, 48 block file(s) deleted.\n",
result.stdout)
expected_file_names = sorted(['.zarray', '.zattrs'])
self.assertEqual(expected_file_names, sorted(os.listdir('test.zarr/precipitation')))
self.assertEqual(expected_file_names, sorted(os.listdir('test.zarr/temperature')))
ds = xr.open_zarr('test.zarr')
assert_cube(ds)
self.assertIn('precipitation', ds)
self.assertEqual((3, 180, 360), ds.precipitation.shape)
self.assertEqual(('time', 'lat', 'lon'), ds.precipitation.dims)
self.assertIn('temperature', ds)
self.assertEqual((3, 180, 360), ds.temperature.shape)
self.assertEqual(('time', 'lat', 'lon'), ds.temperature.dims)
示例4: _write_test_cube_pyramid
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import open_zarr [as 別名]
def _write_test_cube_pyramid(cls):
# Create bucket 'xcube-test', so it exists before we write a test pyramid
s3_conn = boto3.client('s3')
s3_conn.create_bucket(Bucket='xcube-test', ACL='public-read')
# Create a test cube pyramid with just one variable "conc_chl"
zarr_path = os.path.join(os.path.dirname(__file__), '../../examples/serve/demo/cube-1-250-250.zarr')
base_dataset = xr.open_zarr(zarr_path)
base_dataset = xr.Dataset(dict(conc_chl=base_dataset.conc_chl))
ml_dataset = BaseMultiLevelDataset(base_dataset)
# Write test cube pyramid
write_levels(ml_dataset,
'https://s3.amazonaws.com/xcube-test/cube-1-250-250.levels',
client_kwargs=dict(provider_access_key_id='test_fake_id',
provider_secret_access_key='test_fake_secret'))
示例5: test_insert_time_slice
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import open_zarr [as 別名]
def test_insert_time_slice(self):
self.write_cube('2019-01-02', 10)
insert_time_slice(self.CUBE_PATH, 5, self.make_slice('2019-01-06T02:00'))
insert_time_slice(self.CUBE_PATH, 10, self.make_slice('2019-01-10T02:00'))
insert_time_slice(self.CUBE_PATH, 0, self.make_slice('2019-01-01T02:00'))
cube = xr.open_zarr(self.CUBE_PATH)
expected = np.array(['2019-01-01T14:00', '2019-01-02T12:00',
'2019-01-03T12:00', '2019-01-04T12:00',
'2019-01-05T12:00', '2019-01-06T12:00',
'2019-01-06T14:00', '2019-01-07T12:00',
'2019-01-08T12:00', '2019-01-09T12:00',
'2019-01-10T12:00', '2019-01-10T14:00',
'2019-01-11T12:00'], dtype=cube.time.dtype)
self.assertEqual(13, cube.time.size)
self.assertEqual(None, cube.time.chunks)
np.testing.assert_equal(cube.time.values, expected)
示例6: test_update_corrupt_cube
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import open_zarr [as 別名]
def test_update_corrupt_cube(self):
self.write_cube('2019-01-01', 3)
cube = xr.open_zarr(self.CUBE_PATH)
t, y, x = cube.precipitation.shape
new_shape = y, t, x
t, y, x = cube.precipitation.dims
new_dims = y, t, x
cube['precipitation'] = xr.DataArray(cube.precipitation.values.reshape(new_shape),
dims=new_dims,
coords=cube.precipitation.coords)
cube.to_zarr(self.CUBE_PATH_2)
with self.assertRaises(ValueError) as cm:
insert_time_slice(self.CUBE_PATH_2, 2, self.make_slice('2019-01-02T06:30'))
self.assertEqual("dimension 'time' of variable 'precipitation' must be first dimension",
f"{cm.exception}")
示例7: test_process_inputs_replace_multiple_zarr
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import open_zarr [as 別名]
def test_process_inputs_replace_multiple_zarr(self):
status, output = gen_cube_wrapper(
[get_inputdata_path('20170101-IFR-L4_GHRSST-SSTfnd-ODYSSEA-NWE_002-v2.0-fv1.0.nc'),
get_inputdata_path('20170102-IFR-L4_GHRSST-SSTfnd-ODYSSEA-NWE_002-v2.0-fv1.0.nc'),
get_inputdata_path('20170103-IFR-L4_GHRSST-SSTfnd-ODYSSEA-NWE_002-v2.0-fv1.0.nc'),
get_inputdata_path('20170102-IFR-L4_GHRSST-SSTfnd-ODYSSEA-NWE_002-v2.0-fv1.0.nc')], 'l2c.zarr',
no_sort_mode=True)
self.assertEqual(True, status)
self.assertTrue('\nstep 9 of 9: creating input slice in l2c.zarr...\n' in output)
self.assertTrue('\nstep 9 of 9: appending input slice to l2c.zarr...\n' in output)
self.assertTrue('\nstep 9 of 9: replacing input slice at index 1 in l2c.zarr...\n' in output)
self.assert_cube_ok(xr.open_zarr('l2c.zarr'),
expected_time_dim=3,
expected_extra_attrs=dict(date_modified=None,
time_coverage_start='2016-12-31T12:00:00.000000000',
time_coverage_end='2017-01-03T12:00:00.000000000'))
self.assertTrue(os.path.exists(os.path.join('l2c.zarr', '.zmetadata')))
示例8: gen_index_var
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import open_zarr [as 別名]
def gen_index_var(dims, shape, chunks):
# noinspection PyUnusedLocal
def get_chunk(cube_store: ChunkStore, name: str, index: Tuple[int, ...]) -> bytes:
data = np.zeros(cube_store.chunks, dtype=np.uint64)
data_view = data.ravel()
if data_view.base is not data:
raise ValueError('view expected')
if data_view.size < cube_store.ndim * 2:
raise ValueError('size too small')
for i in range(cube_store.ndim):
j1 = cube_store.chunks[i] * index[i]
j2 = j1 + cube_store.chunks[i]
data_view[2 * i] = j1
data_view[2 * i + 1] = j2
return data.tobytes()
store = ChunkStore(dims, shape, chunks)
store.add_lazy_array('__index_var__', '<u8', get_chunk=get_chunk)
ds = xr.open_zarr(store)
return ds.__index_var__
示例9: _get_dataset_lazily
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import open_zarr [as 別名]
def _get_dataset_lazily(self, index: int, parameters: Dict[str, Any]) -> xr.Dataset:
"""
Read the dataset for the level at given *index*.
:param index: the level index
:param parameters: keyword arguments passed to xr.open_zarr()
:return: the dataset for the level at *index*.
"""
ext, level_path = self._level_paths[index]
if ext == ".link":
with open(level_path, "r") as fp:
level_path = fp.read()
# if file_path is a relative path, resolve it against the levels directory
if not os.path.isabs(level_path):
base_dir = os.path.dirname(self._dir_path)
level_path = os.path.join(base_dir, level_path)
with measure_time(tag=f"opened local dataset {level_path} for level {index}"):
return assert_cube(xr.open_zarr(level_path, **parameters), name=level_path)
示例10: open_ml_dataset_from_local_fs
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import open_zarr [as 別名]
def open_ml_dataset_from_local_fs(path: str,
data_format: str = None,
ds_id: str = None,
exception_type: type = ValueError,
**kwargs) -> MultiLevelDataset:
data_format = data_format or guess_ml_dataset_format(path)
if data_format == FORMAT_NAME_NETCDF4:
with measure_time(tag=f"opened local NetCDF dataset {path}"):
ds = assert_cube(xr.open_dataset(path, **kwargs))
return BaseMultiLevelDataset(ds, ds_id=ds_id)
elif data_format == FORMAT_NAME_ZARR:
with measure_time(tag=f"opened local zarr dataset {path}"):
ds = assert_cube(xr.open_zarr(path, **kwargs))
return BaseMultiLevelDataset(ds, ds_id=ds_id)
elif data_format == FORMAT_NAME_LEVELS:
with measure_time(tag=f"opened local levels dataset {path}"):
return FileStorageMultiLevelDataset(path, ds_id=ds_id, zarr_kwargs=kwargs)
raise exception_type(f'Unrecognized multi-level dataset format {data_format!r} for path {path!r}')
示例11: __init__
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import open_zarr [as 別名]
def __init__(self, url, headers, **kwargs):
"""
Initialise local xarray, whose dask arrays contain tasks that pull data
The matadata contains a key "internal", which is a result of running
``serialize_zarr_ds`` on the xarray on the server. It is a dict
containing the metadata parts of the original dataset (i.e., the
keys with names like ".z*"). This can be opened by xarray as-is, and
will make a local xarray object. In ``._get_schema()``, the numpy
parts (coordinates) are fetched and the dask-array parts (cariables)
have their dask graphs redefined to tasks that fetch data from the
server.
"""
import xarray as xr
super(RemoteXarray, self).__init__(url, headers, **kwargs)
self._schema = None
self._ds = xr.open_zarr(self.metadata['internal'])
示例12: open_minian
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import open_zarr [as 別名]
def open_minian(dpath, fname='minian', backend='netcdf', chunks=None, post_process=None):
if backend is 'netcdf':
fname = fname + '.nc'
if chunks is 'auto':
chunks = dict([(d, 'auto') for d in ds.dims])
mpath = pjoin(dpath, fname)
with xr.open_dataset(mpath) as ds:
dims = ds.dims
chunks = dict([(d, 'auto') for d in dims])
ds = xr.open_dataset(os.path.join(dpath, fname), chunks=chunks)
if post_process:
ds = post_process(ds, mpath)
return ds
elif backend is 'zarr':
mpath = pjoin(dpath, fname)
dslist = [xr.open_zarr(pjoin(mpath, d)) for d in listdir(mpath) if isdir(pjoin(mpath, d))]
ds = xr.merge(dslist)
if chunks is 'auto':
chunks = dict([(d, 'auto') for d in ds.dims])
if post_process:
ds = post_process(ds, mpath)
return ds.chunk(chunks)
else:
raise NotImplementedError("backend {} not supported".format(backend))
示例13: open_as_xr_dataset
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import open_zarr [as 別名]
def open_as_xr_dataset(self) -> xr.Dataset:
if self.in_memory:
chunks = None
else:
chunks = "auto"
ds = xr.open_zarr(
self.zgroup.store,
group=self.zgroup.path,
chunks=chunks,
consolidated=self.consolidated,
# disable mask (not nice with zarr default fill_value=0)
mask_and_scale=False,
)
if self.in_memory:
# lazy loading may be confusing for the default, in-memory option
ds.load()
else:
# load scalar data vars (there might be many of them: model params)
for da in ds.data_vars.values():
if not da.dims:
da.load()
return ds
示例14: test_all_defaults
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import open_zarr [as 別名]
def test_all_defaults(self):
result = self.invoke_cli(['resample', TEST_ZARR_DIR])
self.assertEqual(0, result.exit_code)
self.assertEqual("Opening cube from 'test.zarr'...\n"
"Resampling...\n"
"Writing resampled cube to 'out.zarr'...\n"
"Done.\n",
result.stdout)
self.assertTrue(os.path.isdir('out.zarr'))
ds = xr.open_zarr('out.zarr')
assert_cube(ds)
self.assertIn('precipitation_mean', ds)
self.assertIn('temperature_mean', ds)
self.assertIn('soil_moisture_mean', ds)
示例15: test_with_output
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import open_zarr [as 別名]
def test_with_output(self):
result = self.invoke_cli(['resample', TEST_ZARR_DIR, '--output', 'resampled.zarr'])
self.assertEqual(0, result.exit_code)
self.assertTrue(os.path.isdir('resampled.zarr'))
ds = xr.open_zarr('resampled.zarr')
assert_cube(ds)
self.assertIn('precipitation_mean', ds)
self.assertIn('temperature_mean', ds)
self.assertIn('soil_moisture_mean', ds)