Skip to content

Commit 8ef17bf

Browse files
authored
Updating package files (#35)
Updating some package files and adding docstrings for `to_xarray` and `STACBackend.open_dataset`.
1 parent 342c197 commit 8ef17bf

File tree

5 files changed

+53
-29
lines changed

5 files changed

+53
-29
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ dist
55
.direnv
66
.mypy_cache
77
.pytest_cache
8-
.ruff_cache
8+
.ruff_cache
9+
*.egg-info/

environment.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: xpystac-broken
1+
name: xpystac
22
channels:
33
- conda-forge
44
- nodefaults

setup.cfg

Lines changed: 0 additions & 19 deletions
This file was deleted.

xpystac/core.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,26 @@ def to_xarray(
1313
stacking_library: Union[Literal["odc.stac", "stackstac"], None] = None,
1414
**kwargs,
1515
) -> xarray.Dataset:
16-
"""Given a pystac object return an xarray dataset
17-
18-
When stacking multiple items, an optional ``stacking_library`` argument
19-
is accepted. It defaults to ``odc.stac`` if available and otherwise ``stackstac``.
20-
Control the behavior by setting ``stacking_library``
16+
"""Given a PySTAC object return an xarray dataset.
17+
18+
The behavior of this method depends on the type of PySTAC object:
19+
20+
* Asset: if the asset points to a kerchunk file or a zarr file,
21+
reads the metadata in that file to construct the coordinates of the
22+
dataset. If the asset points to a COG, read that.
23+
* Item: stacks all the assets into a dataset with 1 more dimension than
24+
any given asset.
25+
* ItemCollection (output of pystac_client.search): stacks all the
26+
assets in all the items into a dataset with 2 more dimensions than
27+
any given asset.
28+
29+
Parameters
30+
----------
31+
obj : PySTAC object (Item, ItemCollection, Asset)
32+
The object from which to read data.
33+
stacking_library : "odc.stac", "stackstac", optional
34+
When stacking multiple items, this argument determines which library
35+
to use. Defaults to ``odc.stac`` if available and otherwise ``stackstac``.
2136
"""
2237
if _is_item_search(obj):
2338
item_collection = obj.item_collection()

xpystac/xarray_plugin.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Iterable, Union
1+
from typing import Any, Iterable, Literal, Union
22

33
import pystac
44
from xarray.backends import BackendEntrypoint
@@ -8,17 +8,44 @@
88

99

1010
class STACBackend(BackendEntrypoint):
11-
description = "Open pystac objects in Xarray"
11+
description = "Open STAC objects in Xarray"
1212
open_dataset_parameters = ("filename_or_obj", "drop_variables")
1313
url = "https://github.com/stac-utils/xpystac"
1414

1515
def open_dataset(
1616
self,
1717
filename_or_obj: Any,
1818
drop_variables: Union[str, Iterable[str], None] = None,
19+
stacking_library: Union[Literal["odc.stac", "stackstac"], None] = None,
1920
**kwargs,
2021
):
21-
return to_xarray(filename_or_obj, drop_variables=drop_variables, **kwargs)
22+
"""Given a PySTAC object return an xarray dataset
23+
24+
The behavior of this method depends on the type of PySTAC object:
25+
26+
* Asset: if the asset points to a kerchunk file or a zarr file,
27+
reads the metadata in that file to construct the coordinates of the
28+
dataset. If the asset points to a COG, read that.
29+
* Item: stacks all the assets into a dataset with 1 more dimension than
30+
any given asset.
31+
* ItemCollection (output of pystac_client.search): stacks all the
32+
assets in all the items into a dataset with 2 more dimensions than
33+
any given asset.
34+
35+
Parameters
36+
----------
37+
filename_or_obj : PySTAC object (Item, ItemCollection, Asset)
38+
The object from which to read data.
39+
stacking_library : "odc.stac", "stackstac", optional
40+
When stacking multiple items, this argument determines which library
41+
to use. Defaults to ``odc.stac`` if available and otherwise ``stackstac``.
42+
"""
43+
return to_xarray(
44+
filename_or_obj,
45+
drop_variables=drop_variables,
46+
stacking_library=stacking_library,
47+
**kwargs,
48+
)
2249

2350
def guess_can_open(self, filename_or_obj: Any):
2451
return isinstance(

0 commit comments

Comments
 (0)