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, software11# 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 and14# limitations under the License.1516fromtypingimportAny1718importxarrayas_xarray1920from..shortcutsimportresolve_storage_client21from..typesimportMSC_PROTOCOL22from.zarrimportLazyZarrStore2324
[docs]25defopen_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.2930 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, it32 directly calls ``xarray.open_zarr``.33 """34args_list=list(args)35path=args_list[0]ifargs_listelsekwargs.get("store")36msc_max_workers=kwargs.pop("msc_max_workers",None)37ifisinstance(path,str)andpath.startswith(MSC_PROTOCOL):38storage_client,prefix=resolve_storage_client(path)39zarr_store=LazyZarrStore(storage_client,prefix=prefix,msc_max_workers=msc_max_workers)40ifpath==args_list[0]:41args_list[0]=zarr_store42else:43kwargs["store"]=zarr_store44return_xarray.open_zarr(*args_list,**kwargs)45return_xarray.open_zarr(*args,**kwargs)