Skip to content

Commit

Permalink
import top-level version of xarray classes (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomNicholas authored Oct 22, 2024
1 parent dacdd67 commit fffdc2d
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 28 deletions.
3 changes: 1 addition & 2 deletions virtualizarr/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
Optional,
)

from xarray import Dataset
from xarray.core.indexes import Index
from xarray import Dataset, Index

from virtualizarr.manifests import ManifestArray
from virtualizarr.readers import (
Expand Down
25 changes: 15 additions & 10 deletions virtualizarr/readers/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@
cast,
)

import xarray as xr
from xarray import Dataset
from xarray import (
Coordinates,
Dataset,
Index,
IndexVariable,
Variable,
open_dataset,
)
from xarray.backends import AbstractDataStore, BackendArray
from xarray.core.indexes import Index, PandasIndex
from xarray.core.variable import IndexVariable, Variable
from xarray.core.indexes import PandasIndex

from virtualizarr.manifests import ManifestArray
from virtualizarr.utils import _FsspecFSFromFilepath
Expand Down Expand Up @@ -62,7 +67,7 @@ def open_loadable_vars_and_indexes(
# fpath can be `Any` thanks to fsspec.filesystem(...).open() returning Any.
# We'll (hopefully safely) cast it to what xarray is expecting, but this might let errors through.

ds = xr.open_dataset(
ds = open_dataset(
cast(XArrayOpenT, fpath),
drop_variables=drop_variables,
group=group,
Expand Down Expand Up @@ -113,7 +118,7 @@ def construct_virtual_dataset(

data_vars, coords = separate_coords(vars, indexes, coord_names)

vds = xr.Dataset(
vds = Dataset(
data_vars,
coords=coords,
# indexes={}, # TODO should be added in a later version of xarray
Expand All @@ -126,10 +131,10 @@ def construct_virtual_dataset(


def separate_coords(
vars: Mapping[str, xr.Variable],
vars: Mapping[str, Variable],
indexes: MutableMapping[str, Index],
coord_names: Iterable[str] | None = None,
) -> tuple[dict[str, xr.Variable], xr.Coordinates]:
) -> tuple[dict[str, Variable], Coordinates]:
"""
Try to generate a set of coordinates that won't cause xarray to automatically build a pandas.Index for the 1D coordinates.
Expand All @@ -144,7 +149,7 @@ def separate_coords(
# split data and coordinate variables (promote dimension coordinates)
data_vars = {}
coord_vars: dict[
str, tuple[Hashable, Any, dict[Any, Any], dict[Any, Any]] | xr.Variable
str, tuple[Hashable, Any, dict[Any, Any], dict[Any, Any]] | Variable
] = {}
for name, var in vars.items():
if name in coord_names or var.dims == (name,):
Expand All @@ -164,7 +169,7 @@ def separate_coords(
else:
data_vars[name] = var

coords = xr.Coordinates(coord_vars, indexes=indexes)
coords = Coordinates(coord_vars, indexes=indexes)

return data_vars, coords

Expand Down
4 changes: 1 addition & 3 deletions virtualizarr/readers/dmrpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
from xml.etree import ElementTree as ET

import numpy as np
from xarray import Coordinates, Dataset
from xarray.core.indexes import Index
from xarray.core.variable import Variable
from xarray import Coordinates, Dataset, Index, Variable

from virtualizarr.manifests import ChunkManifest, ManifestArray
from virtualizarr.readers.common import VirtualBackend
Expand Down
3 changes: 1 addition & 2 deletions virtualizarr/readers/fits.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import Iterable, Mapping, Optional

from xarray import Dataset
from xarray.core.indexes import Index
from xarray import Dataset, Index

from virtualizarr.readers.common import (
VirtualBackend,
Expand Down
3 changes: 1 addition & 2 deletions virtualizarr/readers/hdf5.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import Iterable, Mapping, Optional

from xarray import Dataset
from xarray.core.indexes import Index
from xarray import Dataset, Index

from virtualizarr.readers.common import (
VirtualBackend,
Expand Down
3 changes: 1 addition & 2 deletions virtualizarr/readers/kerchunk.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from typing import Iterable, Mapping, Optional

import ujson
from xarray import Dataset
from xarray.core.indexes import Index
from xarray import Dataset, Index

from virtualizarr.readers.common import VirtualBackend
from virtualizarr.translators.kerchunk import dataset_from_kerchunk_refs
Expand Down
3 changes: 1 addition & 2 deletions virtualizarr/readers/netcdf3.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import Iterable, Mapping, Optional

from xarray import Dataset
from xarray.core.indexes import Index
from xarray import Dataset, Index

from virtualizarr.readers.common import (
VirtualBackend,
Expand Down
3 changes: 1 addition & 2 deletions virtualizarr/readers/tiff.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import warnings
from typing import Iterable, Mapping, Optional

from xarray import Dataset
from xarray.core.indexes import Index
from xarray import Dataset, Index

from virtualizarr.readers.common import (
VirtualBackend,
Expand Down
4 changes: 1 addition & 3 deletions virtualizarr/readers/zarr_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

import numcodecs
import numpy as np
from xarray import Dataset
from xarray.core.indexes import Index
from xarray.core.variable import Variable
from xarray import Dataset, Index, Variable

from virtualizarr.manifests import ChunkManifest, ManifestArray
from virtualizarr.readers.common import VirtualBackend, separate_coords
Expand Down

0 comments on commit fffdc2d

Please sign in to comment.