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
17import xarray as _xarray
18
19from ..shortcuts import resolve_storage_client
20from ..types import MSC_PROTOCOL
21from .zarr import LazyZarrStore
22
23
[docs] 24def open_zarr(*args: Any, **kwargs: Any) -> _xarray.Dataset: 25 """ 26 Adapt ``xarray.open_zarr`` to use :py:class:`multistorageclient.contrib.zarr.LazyZarrStore` 27 when path matches the ``msc`` protocol. 28 29 If the path starts with the MSC protocol, it uses :py:class:`multistorageclient.contrib.zarr.LazyZarrStore` 30 with a resolved storage client and prefix, passing ``msc_max_workers`` if provided. Otherwise, it 31 directly calls ``xarray.open_zarr``. 32 """ 33 args_list = list(args) 34 path = args_list[0] if args_list else kwargs.get("store") 35 msc_max_workers = kwargs.pop("msc_max_workers", None) 36 if isinstance(path, str) and path.startswith(MSC_PROTOCOL): 37 storage_client, prefix = resolve_storage_client(path) 38 zarr_store = LazyZarrStore(storage_client, prefix=prefix, msc_max_workers=msc_max_workers) 39 if path == args_list[0]: 40 args_list[0] = zarr_store 41 else: 42 kwargs["store"] = zarr_store 43 return _xarray.open_zarr(*args_list, **kwargs) 44 return _xarray.open_zarr(*args, **kwargs)