|
9 | 9 | from ..core.variable import Variable
|
10 | 10 | from .common import AbstractWritableDataStore, BackendArray, _encode_variable_name
|
11 | 11 |
|
| 12 | +from .api import open_dataset |
| 13 | + |
12 | 14 | # need some special secret attributes to tell us the dimensions
|
13 | 15 | DIMENSION_KEY = "_ARRAY_DIMENSIONS"
|
14 | 16 |
|
@@ -569,126 +571,135 @@ def open_zarr(
|
569 | 571 | ----------
|
570 | 572 | http://zarr.readthedocs.io/
|
571 | 573 | """
|
572 |
| - if "auto_chunk" in kwargs: |
573 |
| - auto_chunk = kwargs.pop("auto_chunk") |
574 |
| - if auto_chunk: |
575 |
| - chunks = "auto" # maintain backwards compatibility |
576 |
| - else: |
577 |
| - chunks = None |
578 |
| - |
579 |
| - warnings.warn( |
580 |
| - "auto_chunk is deprecated. Use chunks='auto' instead.", |
581 |
| - FutureWarning, |
582 |
| - stacklevel=2, |
583 |
| - ) |
584 |
| - |
585 |
| - if kwargs: |
586 |
| - raise TypeError( |
587 |
| - "open_zarr() got unexpected keyword arguments " + ",".join(kwargs.keys()) |
588 |
| - ) |
589 |
| - |
590 |
| - if not isinstance(chunks, (int, dict)): |
591 |
| - if chunks != "auto" and chunks is not None: |
592 |
| - raise ValueError( |
593 |
| - "chunks must be an int, dict, 'auto', or None. " |
594 |
| - "Instead found %s. " % chunks |
595 |
| - ) |
596 | 574 |
|
597 |
| - if chunks == "auto": |
598 |
| - try: |
599 |
| - import dask.array # noqa |
600 |
| - except ImportError: |
601 |
| - chunks = None |
602 |
| - |
603 |
| - if not decode_cf: |
604 |
| - mask_and_scale = False |
605 |
| - decode_times = False |
606 |
| - concat_characters = False |
607 |
| - decode_coords = False |
608 |
| - |
609 |
| - def maybe_decode_store(store, lock=False): |
610 |
| - ds = conventions.decode_cf( |
611 |
| - store, |
612 |
| - mask_and_scale=mask_and_scale, |
613 |
| - decode_times=decode_times, |
614 |
| - concat_characters=concat_characters, |
615 |
| - decode_coords=decode_coords, |
616 |
| - drop_variables=drop_variables, |
| 575 | + warnings.warn( |
| 576 | + "open_zarr is deprecated. Use open_dataset(engine='zarr') instead.", |
| 577 | + DeprecationWarning, |
617 | 578 | )
|
618 | 579 |
|
619 |
| - # TODO: this is where we would apply caching |
620 |
| - |
621 |
| - return ds |
622 |
| - |
623 |
| - # Zarr supports a wide range of access modes, but for now xarray either |
624 |
| - # reads or writes from a store, never both. For open_zarr, we only read |
625 |
| - mode = "r" |
626 |
| - zarr_store = ZarrStore.open_group( |
627 |
| - store, |
628 |
| - mode=mode, |
629 |
| - synchronizer=synchronizer, |
630 |
| - group=group, |
631 |
| - consolidated=consolidated, |
632 |
| - ) |
633 |
| - ds = maybe_decode_store(zarr_store) |
634 |
| - |
635 |
| - # auto chunking needs to be here and not in ZarrStore because variable |
636 |
| - # chunks do not survive decode_cf |
637 |
| - # return trivial case |
638 |
| - if not chunks: |
639 |
| - return ds |
640 |
| - |
641 |
| - # adapted from Dataset.Chunk() |
642 |
| - if isinstance(chunks, int): |
643 |
| - chunks = dict.fromkeys(ds.dims, chunks) |
644 |
| - |
645 |
| - if isinstance(chunks, tuple) and len(chunks) == len(ds.dims): |
646 |
| - chunks = dict(zip(ds.dims, chunks)) |
647 |
| - |
648 |
| - def get_chunk(name, var, chunks): |
649 |
| - chunk_spec = dict(zip(var.dims, var.encoding.get("chunks"))) |
650 |
| - |
651 |
| - # Coordinate labels aren't chunked |
652 |
| - if var.ndim == 1 and var.dims[0] == name: |
653 |
| - return chunk_spec |
654 |
| - |
655 |
| - if chunks == "auto": |
656 |
| - return chunk_spec |
657 |
| - |
658 |
| - for dim in var.dims: |
659 |
| - if dim in chunks: |
660 |
| - spec = chunks[dim] |
661 |
| - if isinstance(spec, int): |
662 |
| - spec = (spec,) |
663 |
| - if isinstance(spec, (tuple, list)) and chunk_spec[dim]: |
664 |
| - if any(s % chunk_spec[dim] for s in spec): |
665 |
| - warnings.warn( |
666 |
| - "Specified Dask chunks %r would " |
667 |
| - "separate Zarr chunk shape %r for " |
668 |
| - "dimension %r. This significantly " |
669 |
| - "degrades performance. Consider " |
670 |
| - "rechunking after loading instead." |
671 |
| - % (chunks[dim], chunk_spec[dim], dim), |
672 |
| - stacklevel=2, |
673 |
| - ) |
674 |
| - chunk_spec[dim] = chunks[dim] |
675 |
| - return chunk_spec |
676 |
| - |
677 |
| - def maybe_chunk(name, var, chunks): |
678 |
| - from dask.base import tokenize |
679 |
| - |
680 |
| - chunk_spec = get_chunk(name, var, chunks) |
681 |
| - |
682 |
| - if (var.ndim > 0) and (chunk_spec is not None): |
683 |
| - # does this cause any data to be read? |
684 |
| - token2 = tokenize(name, var._data) |
685 |
| - name2 = "zarr-%s" % token2 |
686 |
| - var = var.chunk(chunk_spec, name=name2, lock=None) |
687 |
| - if overwrite_encoded_chunks and var.chunks is not None: |
688 |
| - var.encoding["chunks"] = tuple(x[0] for x in var.chunks) |
689 |
| - return var |
690 |
| - else: |
691 |
| - return var |
692 |
| - |
693 |
| - variables = {k: maybe_chunk(k, v, chunks) for k, v in ds.variables.items()} |
694 |
| - return ds._replace_vars_and_dims(variables) |
| 580 | + ds = open_dataset() |
| 581 | + |
| 582 | + # if "auto_chunk" in kwargs: |
| 583 | + # auto_chunk = kwargs.pop("auto_chunk") |
| 584 | + # if auto_chunk: |
| 585 | + # chunks = "auto" # maintain backwards compatibility |
| 586 | + # else: |
| 587 | + # chunks = None |
| 588 | + |
| 589 | + # warnings.warn( |
| 590 | + # "auto_chunk is deprecated. Use chunks='auto' instead.", |
| 591 | + # FutureWarning, |
| 592 | + # stacklevel=2, |
| 593 | + # ) |
| 594 | + |
| 595 | + # if kwargs: |
| 596 | + # raise TypeError( |
| 597 | + # "open_zarr() got unexpected keyword arguments " + ",".join(kwargs.keys()) |
| 598 | + # ) |
| 599 | + |
| 600 | + # if not isinstance(chunks, (int, dict)): |
| 601 | + # if chunks != "auto" and chunks is not None: |
| 602 | + # raise ValueError( |
| 603 | + # "chunks must be an int, dict, 'auto', or None. " |
| 604 | + # "Instead found %s. " % chunks |
| 605 | + # ) |
| 606 | + |
| 607 | + # if chunks == "auto": |
| 608 | + # try: |
| 609 | + # import dask.array # noqa |
| 610 | + # except ImportError: |
| 611 | + # chunks = None |
| 612 | + |
| 613 | + # if not decode_cf: |
| 614 | + # mask_and_scale = False |
| 615 | + # decode_times = False |
| 616 | + # concat_characters = False |
| 617 | + # decode_coords = False |
| 618 | + |
| 619 | + # def maybe_decode_store(store, lock=False): |
| 620 | + # ds = conventions.decode_cf( |
| 621 | + # store, |
| 622 | + # mask_and_scale=mask_and_scale, |
| 623 | + # decode_times=decode_times, |
| 624 | + # concat_characters=concat_characters, |
| 625 | + # decode_coords=decode_coords, |
| 626 | + # drop_variables=drop_variables, |
| 627 | + # ) |
| 628 | + |
| 629 | + # # TODO: this is where we would apply caching |
| 630 | + |
| 631 | + # return ds |
| 632 | + |
| 633 | + # # Zarr supports a wide range of access modes, but for now xarray either |
| 634 | + # # reads or writes from a store, never both. For open_zarr, we only read |
| 635 | + # mode = "r" |
| 636 | + # zarr_store = ZarrStore.open_group( |
| 637 | + # store, |
| 638 | + # mode=mode, |
| 639 | + # synchronizer=synchronizer, |
| 640 | + # group=group, |
| 641 | + # consolidated=consolidated, |
| 642 | + # ) |
| 643 | + # ds = maybe_decode_store(zarr_store) |
| 644 | + |
| 645 | + # # auto chunking needs to be here and not in ZarrStore because variable |
| 646 | + # # chunks do not survive decode_cf |
| 647 | + # # return trivial case |
| 648 | + # if not chunks: |
| 649 | + # return ds |
| 650 | + |
| 651 | + # # adapted from Dataset.Chunk() |
| 652 | + # if isinstance(chunks, int): |
| 653 | + # chunks = dict.fromkeys(ds.dims, chunks) |
| 654 | + |
| 655 | + # if isinstance(chunks, tuple) and len(chunks) == len(ds.dims): |
| 656 | + # chunks = dict(zip(ds.dims, chunks)) |
| 657 | + |
| 658 | + # def get_chunk(name, var, chunks): |
| 659 | + # chunk_spec = dict(zip(var.dims, var.encoding.get("chunks"))) |
| 660 | + |
| 661 | + # # Coordinate labels aren't chunked |
| 662 | + # if var.ndim == 1 and var.dims[0] == name: |
| 663 | + # return chunk_spec |
| 664 | + |
| 665 | + # if chunks == "auto": |
| 666 | + # return chunk_spec |
| 667 | + |
| 668 | + # for dim in var.dims: |
| 669 | + # if dim in chunks: |
| 670 | + # spec = chunks[dim] |
| 671 | + # if isinstance(spec, int): |
| 672 | + # spec = (spec,) |
| 673 | + # if isinstance(spec, (tuple, list)) and chunk_spec[dim]: |
| 674 | + # if any(s % chunk_spec[dim] for s in spec): |
| 675 | + # warnings.warn( |
| 676 | + # "Specified Dask chunks %r would " |
| 677 | + # "separate Zarr chunk shape %r for " |
| 678 | + # "dimension %r. This significantly " |
| 679 | + # "degrades performance. Consider " |
| 680 | + # "rechunking after loading instead." |
| 681 | + # % (chunks[dim], chunk_spec[dim], dim), |
| 682 | + # stacklevel=2, |
| 683 | + # ) |
| 684 | + # chunk_spec[dim] = chunks[dim] |
| 685 | + # return chunk_spec |
| 686 | + |
| 687 | + # def maybe_chunk(name, var, chunks): |
| 688 | + # from dask.base import tokenize |
| 689 | + |
| 690 | + # chunk_spec = get_chunk(name, var, chunks) |
| 691 | + |
| 692 | + # if (var.ndim > 0) and (chunk_spec is not None): |
| 693 | + # # does this cause any data to be read? |
| 694 | + # token2 = tokenize(name, var._data) |
| 695 | + # name2 = "zarr-%s" % token2 |
| 696 | + # var = var.chunk(chunk_spec, name=name2, lock=None) |
| 697 | + # if overwrite_encoded_chunks and var.chunks is not None: |
| 698 | + # var.encoding["chunks"] = tuple(x[0] for x in var.chunks) |
| 699 | + # return var |
| 700 | + # else: |
| 701 | + # return var |
| 702 | + |
| 703 | + # variables = {k: maybe_chunk(k, v, chunks) for k, v in ds.variables.items()} |
| 704 | + # return ds._replace_vars_and_dims(variables) |
| 705 | + return ds |
0 commit comments