Skip to content

Commit

Permalink
add via_cf_namespace option
Browse files Browse the repository at this point in the history
  • Loading branch information
cisaacstern committed Nov 10, 2021
1 parent 2d794ea commit 944dea3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ author-email = "taugspurger@microsoft.com"
classifiers = [ "License :: OSI Approved :: MIT License",]
requires = [
"xarray",
"cf_xarray",
"numpy",
"pystac>=1.0.0b3",
"pandas",
Expand Down
8 changes: 6 additions & 2 deletions test_xstac.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ def ds():
return fix_attrs(ds)


def test_xarray_to_stac(ds):
@pytest.mark.parametrize("via_cf_namespace", [False, True])
def test_xarray_to_stac(ds, via_cf_namespace):
template = {
"id": "id",
"type": "Collection",
Expand All @@ -132,6 +133,7 @@ def test_xarray_to_stac(ds):
temporal_dimension="time",
x_dimension="x",
y_dimension="y",
via_cf_namespace=via_cf_namespace,
)
assert result.id == "id"
assert isinstance(result, pystac.Collection)
Expand Down Expand Up @@ -457,7 +459,8 @@ def test_validation_with_none():
c.validate()


def test_xarray_to_stac_item(ds):
@pytest.mark.parametrize("via_cf_namespace", [False, True])
def test_xarray_to_stac_item(ds, via_cf_namespace):
template = {
"id": "id",
"type": "Feature",
Expand All @@ -473,6 +476,7 @@ def test_xarray_to_stac_item(ds):
temporal_dimension="time",
x_dimension="x",
y_dimension="y",
via_cf_namespace=via_cf_namespace,
)
assert result.id == "id"
assert isinstance(result, pystac.Item)
Expand Down
32 changes: 27 additions & 5 deletions xstac/_xstac.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import copy
import json
from pystac import stac_object
import cf_xarray # adds `.cf` namespace to xarray datasets
import xarray as xr
import numpy as np
import pystac
Expand Down Expand Up @@ -72,8 +73,10 @@ def maybe_infer_step(da, step):
return step


def build_temporal_dimension(ds, name, extent, values, step):
time = ds.coords[name]
def build_temporal_dimension(
ds, name, extent, values, step, via_cf_namespace: bool = False
):
time = ds.coords[name] if not via_cf_namespace else ds.cf[name]
extent = (
pd.to_datetime(
np.concatenate([time.min(keepdims=True), time.max(keepdims=True)])
Expand Down Expand Up @@ -103,8 +106,17 @@ def build_temporal_dimension(ds, name, extent, values, step):
)


def build_horizontal_dimension(ds, name, axis, extent, values, step, reference_system):
da = ds[name]
def build_horizontal_dimension(
ds,
name,
axis,
extent,
values,
step,
reference_system,
via_cf_namespace: bool = False,
):
da = ds[name] if not via_cf_namespace else ds.cf[name]
if extent is None:
extent = np.asarray(
np.concatenate([da.min(keepdims=True), da.max(keepdims=True)])
Expand Down Expand Up @@ -215,13 +227,19 @@ def build_datacube(
vertical_extent=None,
vertical_values=None,
vertical_step=None,
via_cf_namespace: bool = False,
**additional_dimensions,
):
dimensions = {}

if temporal_dimension is not None:
dimensions[temporal_dimension] = build_temporal_dimension(
ds, temporal_dimension, temporal_extent, temporal_values, temporal_step
ds,
temporal_dimension,
temporal_extent,
temporal_values,
temporal_step,
via_cf_namespace=via_cf_namespace,
)

if x_dimension:
Expand All @@ -233,6 +251,7 @@ def build_datacube(
x_values,
x_step,
reference_system=reference_system,
via_cf_namespace=via_cf_namespace,
)
if y_dimension:
dimensions[y_dimension] = build_horizontal_dimension(
Expand All @@ -243,6 +262,7 @@ def build_datacube(
y_values,
y_step,
reference_system=reference_system,
via_cf_namespace=via_cf_namespace,
)

if vertical_dimension:
Expand Down Expand Up @@ -288,6 +308,7 @@ def xarray_to_stac(
vertical_values=None,
vertical_step=None,
validate: bool = True,
via_cf_namespace: bool = False,
**additional_dimensions,
) -> pystac.Collection:
"""
Expand Down Expand Up @@ -328,6 +349,7 @@ def xarray_to_stac(
vertical_extent=vertical_extent,
vertical_values=vertical_values,
vertical_step=vertical_step,
via_cf_namespace=via_cf_namespace,
**additional_dimensions,
)

Expand Down

0 comments on commit 944dea3

Please sign in to comment.