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.1516fromtypingimportAny17importxarrayas_xarray1819from..shortcutsimportresolve_storage_client20from..typesimportMSC_PROTOCOL21from.zarrimportLazyZarrStore2223
[docs]24defopen_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.2829 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, it31 directly calls ``xarray.open_zarr``.32 """33args_list=list(args)34path=args_list[0]ifargs_listelsekwargs.get("store")35msc_max_workers=kwargs.pop("msc_max_workers",None)36ifisinstance(path,str)andpath.startswith(MSC_PROTOCOL):37storage_client,prefix=resolve_storage_client(path)38zarr_store=LazyZarrStore(storage_client,prefix=prefix,msc_max_workers=msc_max_workers)39ifpath==args_list[0]:40args_list[0]=zarr_store41else:42kwargs["store"]=zarr_store43return_xarray.open_zarr(*args_list,**kwargs)44return_xarray.open_zarr(*args,**kwargs)