Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] add titiler.xarray module #1007

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/titiler/xarray/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
## titiler.xarray

Adds support for Xarray Dataset (NetCDF/Zarr) in Titiler.

## Installation

```bash
$ python -m pip install -U pip

# From Pypi
$ python -m pip install titiler.xarray

# Or from sources
$ git clone https://github.com/developmentseed/titiler.git
$ cd titiler && python -m pip install -e src/titiler/core -e src/titiler/xarray
```

## How To

```python
from fastapi import FastAPI
from titiler.xarray.factory import TilerFactory

# Create a FastAPI application
app = FastAPI(
description="A lightweight Cloud Optimized GeoTIFF tile server",
)

# Create a set of MosaicJSON endpoints
endpoint = TilerFactory()

# Register the Mosaic endpoints to the application
app.include_router(endpoint.router)
```

## Package structure

```
titiler/
└── xarray/
├── tests/ - Tests suite
└── titiler/xarray/ - `xarray` namespace package
├── dependencies.py - titiler-xarray dependencies
├── io.py - titiler-xarray Readers
└── factory.py - endpoints factory
```
71 changes: 71 additions & 0 deletions src/titiler/xarray/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
[project]
name = "titiler.xarray"
description = "Xarray plugin for TiTiler."
readme = "README.md"
requires-python = ">=3.8"
authors = [
{name = "Vincent Sarago", email = "[email protected]"},
{name = "Aimee Barciauskas", email = "[email protected]"},
]
license = {text = "MIT"}
keywords = [
"TiTiler",
"Xarray",
"Zarr",
"NetCDF",
"HDF",
]
classifiers = [
"Intended Audience :: Information Technology",
"Intended Audience :: Science/Research",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Topic :: Scientific/Engineering :: GIS",
]
dynamic = ["version"]
dependencies = [
"titiler.core==0.19.0.dev",
"rio-tiler>=7.1,<8.0", # Not Released yet
"cftime",
"h5netcdf",
"xarray",
"rioxarray",
"zarr",
"fsspec",
"s3fs",
"aiohttp",
"pandas",
"httpx",
]

[project.optional-dependencies]
test = [
"pytest",
"pytest-cov",
"pytest-asyncio",
"httpx",
]

[project.urls]
Homepage = "https://developmentseed.org/titiler/"
Documentation = "https://developmentseed.org/titiler/"
Issues = "https://github.com/developmentseed/titiler/issues"
Source = "https://github.com/developmentseed/titiler"
Changelog = "https://developmentseed.org/titiler/release-notes/"

[build-system]
requires = ["pdm-pep517"]
build-backend = "pdm.pep517.api"

[tool.pdm.version]
source = "file"
path = "titiler/xarray/__init__.py"

[tool.pdm.build]
includes = ["titiler/xarray"]
excludes = ["tests/", "**/.mypy_cache", "**/.DS_Store"]
1 change: 1 addition & 0 deletions src/titiler/xarray/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""titiler.xarray test configuration."""
89 changes: 89 additions & 0 deletions src/titiler/xarray/tests/test_io_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
"""test titiler.xarray.io utility functions."""

from datetime import datetime

import numpy
import pytest
import xarray

from titiler.xarray.io import get_variable


def test_get_variable():
"""test io.get_variable."""
arr = numpy.arange(0, 33 * 35 * 2).reshape(2, 33, 35)
data = xarray.DataArray(
arr,
dims=("time", "y", "x"),
coords={
"x": numpy.arange(-170, 180, 10),
"y": numpy.arange(-80, 85, 5),
"time": [datetime(2022, 1, 1), datetime(2023, 1, 1)],
},
)
data.attrs.update({"valid_min": arr.min(), "valid_max": arr.max()})
assert not data.rio.crs
assert data.dims == ("time", "y", "x")

ds = data.to_dataset(name="dataset")
da = get_variable(ds, "dataset")
assert da.rio.crs
assert da.dims == ("y", "x")
# Default to the first Time value
assert da["time"] == numpy.datetime64("2022-01-01")

da = get_variable(ds, "dataset", datetime="2023-01-01T01:00:00.000Z")
assert da.rio.crs
assert da.dims == ("y", "x")
assert da["time"] == numpy.datetime64("2023-01-01")

# Select the Nearest Time
da = get_variable(ds, "dataset", datetime="2024-01-01T01:00:00.000Z")
assert da.rio.crs
assert da.dims == ("y", "x")
assert da["time"] == numpy.datetime64("2023-01-01")

data = data.rename({"y": "Lat", "x": "Lon"})
assert data.dims == ("time", "Lat", "Lon")
ds = data.to_dataset(name="dataset")
da = get_variable(ds, "dataset")
assert da.rio.crs
assert da.dims == ("y", "x")

# 4D dataset
arr = numpy.arange(0, 33 * 35 * 2).reshape(2, 1, 33, 35)
data = xarray.DataArray(
arr,
dims=("time", "z", "y", "x"),
coords={
"x": numpy.arange(-170, 180, 10),
"y": numpy.arange(-80, 85, 5),
"z": [0],
"time": [datetime(2022, 1, 1), datetime(2023, 1, 1)],
},
)
ds = data.to_dataset(name="dataset")
da = get_variable(ds, "dataset")
assert da.rio.crs
assert da.dims == ("z", "y", "x")

# 5D dataset
arr = numpy.arange(0, 33 * 35 * 2).reshape(2, 1, 1, 33, 35)
data = xarray.DataArray(
arr,
dims=("time", "universe", "z", "y", "x"),
coords={
"x": numpy.arange(-170, 180, 10),
"y": numpy.arange(-80, 85, 5),
"z": [0],
"universe": ["somewhere"],
"time": [datetime(2022, 1, 1), datetime(2023, 1, 1)],
},
)
ds = data.to_dataset(name="dataset")
with pytest.raises(AssertionError):
get_variable(ds, "dataset")

da = get_variable(ds, "dataset", drop_dim="universe=somewhere")
assert da.rio.crs
assert da.dims == ("z", "y", "x")
3 changes: 3 additions & 0 deletions src/titiler/xarray/titiler/xarray/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""titiler.xarray"""

__version__ = "0.19.0.dev"
3 changes: 3 additions & 0 deletions src/titiler/xarray/titiler/xarray/dependencies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""titiler.xarray dependencies."""

# placeholder
3 changes: 3 additions & 0 deletions src/titiler/xarray/titiler/xarray/factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""TiTiler.xarray factory."""

# placeholder
Loading
Loading