Source code for multistorageclient.contrib.xarray
1# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2# SPDX-License-Identifier: Apache-2.0
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16from typing import Any
17
18import xarray as _xarray
19
20from ..shortcuts import resolve_storage_client
21from ..types import MSC_PROTOCOL
22from .zarr import LazyZarrStore
23
24
[docs]
25def open_zarr(*args: Any, **kwargs: Any) -> _xarray.Dataset:
26 """
27 Adapt ``xarray.open_zarr`` to use :py:class:`multistorageclient.contrib.zarr.LazyZarrStore`
28 when path matches the ``msc`` protocol.
29
30 If the path starts with the MSC protocol, it uses :py:class:`multistorageclient.contrib.zarr.LazyZarrStore`
31 with a resolved storage client and prefix, passing ``msc_max_workers`` if provided. Otherwise, it
32 directly calls ``xarray.open_zarr``.
33 """
34 args_list = list(args)
35 path = args_list[0] if args_list else kwargs.get("store")
36 msc_max_workers = kwargs.pop("msc_max_workers", None)
37 if isinstance(path, str) and path.startswith(MSC_PROTOCOL):
38 storage_client, prefix = resolve_storage_client(path)
39 zarr_store = LazyZarrStore(storage_client, prefix=prefix, msc_max_workers=msc_max_workers)
40 if path == args_list[0]:
41 args_list[0] = zarr_store
42 else:
43 kwargs["store"] = zarr_store
44 return _xarray.open_zarr(*args_list, **kwargs)
45 return _xarray.open_zarr(*args, **kwargs)