Metadata-Version: 2.5
Name: prescient-sdk
Version: 1.1.0rc1
Summary: A Python SDK for integrating with Prescient services
Project-URL: Homepage, https://sparkgeo.github.io/prescient-sdk/
Project-URL: Repository, https://github.com/sparkgeo/prescient-sdk
Project-URL: Issues, https://github.com/sparkgeo/prescient-sdk/issues
License: MIT License
        
        Copyright (c) 2024 sparkgeo.com
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Requires-Python: >=3.11
Requires-Dist: boto3>=1.35.2
Requires-Dist: pydantic-settings>=2.5.2
Requires-Dist: pydantic>=2.0
Requires-Dist: pyyaml>=6.0.3
Requires-Dist: requests>=2.32.3
Requires-Dist: rich>=13.0
Description-Content-Type: text/markdown

# prescient-sdk

A Python SDK for the Prescient platform. Use it to search your Organization's STAC catalog, read its data, and run ingestions.

Documentation: https://sparkgeo.github.io/prescient-sdk/

If you used a version before 1.0, read [Migrate to version 1.0](https://sparkgeo.github.io/prescient-sdk/migration.html). Version 1.0 removes the earlier sign-in settings and replaces the S3 upload.

## Install

The SDK needs Python 3.11 or later.

```
pip install prescient-sdk
```

Or from conda-forge:

```
conda install -c conda-forge prescient-sdk
```

## Configure

The SDK signs in with an API key for your Organization. Create the key in the Keycloak Account Console of your Organization, on the **API Keys** page. The key starts with `pk_`.

| Setting | Required | Value |
| --- | --- | --- |
| `prescient_endpoint_url` | Yes | Your Organization host, for example `https://<org>.<env>.prescient.earth` |
| `prescient_api_key` | Yes | Your API key (`pk_...`) |
| `prescient_auth_url` | No | The Keycloak base URL. If you do not set it, the SDK gets it from `prescient_endpoint_url` |

Set the values as environment variables, or put them in a `config.env` file in your working directory:

```
PRESCIENT_ENDPOINT_URL="https://<org>.<env>.prescient.earth"
PRESCIENT_API_KEY="pk_..."
```

Do not commit the file. For other ways to configure the client, see the [configuration example](https://sparkgeo.github.io/prescient-sdk/examples/configuration.html).

## Search the STAC catalog

This example needs `pystac-client`.

```python
from pystac_client import Client

from prescient_sdk import PrescientClient

client = PrescientClient()
catalog = Client.open(client.stac_url, request_modifier=client.request_modifier)
items = list(catalog.search(max_items=10).items())
```

Asset links point at the platform over HTTPS. To read an asset, send the header from `client.headers`. You do not need AWS credentials. The [STAC search example](https://sparkgeo.github.io/prescient-sdk/examples/stac_search.html) shows how to read an asset with rasterio.

## Run an ingestion

You must be an admin of your Organization. The input files must be in your Organization bucket or in an External bucket.

```python
from prescient_sdk import IngestClient, IngestResource, IngestSpec
from prescient_sdk.ingest_models import ErrorSeverity, Status

client = IngestClient()
spec = IngestSpec.from_file("spec.yaml")  # a version 2 spec: version: "2.0.0"

ing = IngestResource.create(client, spec)
ing.wait_until_ready()
problems = [e for e in ing.errors() if e.severity != ErrorSeverity.WARNING]
if ing.status is Status.READY and not problems:
    ing.start().wait_until_done()
```

See the [ingest example](https://sparkgeo.github.io/prescient-sdk/examples/ingest.html).

## Upload local files

You need the ingest permission in your Organization. `upload` sends the files in a local directory to an Upload location in your Organization bucket and returns the location. Use the location as a location `path` in an ingestion spec.

```python
from prescient_sdk import PrescientClient, upload

client = PrescientClient()
location = upload(client, "/data/rasters", "rasters/2026", pattern=r".*\.tif$")
```

A location is 1 to 4 `/`-separated parts of lower-case letters, digits, `.`, `_` and `-`. If you do not give one, the platform creates one. Files that already exist in the location are skipped, so you can run an interrupted upload again to send the rest. Pass `overwrite=True` to replace them.

To resume, run the upload again with the same location. If you did not give one and the upload fails, the created location is only in the SDK's INFO log, and a new call creates another location. So give a location for any upload you may need to resume.

If a spec points a location at a local directory, `spec.with_uploaded_sources(["<source file set>"], client=client)` uploads the files that the source file set selects and returns a new spec that points at the Upload location.

## Local development

The project uses [uv](https://docs.astral.sh/uv/) for package management. Install uv with [these instructions](https://docs.astral.sh/uv/getting-started/installation/).

1. Install the project with its development dependencies:

    ```
    uv sync
    ```

2. Run the tests:

    ```
    uv run pytest
    ```

3. Check and format the code with [Ruff](https://docs.astral.sh/ruff/):

    ```
    uv run ruff check
    uv run ruff format
    ```

### Add or remove dependencies

```
uv add <some-dependency>
uv add <some-dependency> --dev
uv remove <some-dependency>
```

For more, see the [uv documentation](https://docs.astral.sh/uv/concepts/dependencies/).

### Build the documentation

The documentation is a [Jupyter Book](https://jupyterbook.org/en/stable/intro.html) in the [docs](./docs) folder.

1. Build the HTML:

    ```
    uv run jupyter-book build docs/
    ```

2. Open `docs/_build/html/index.html` in a browser.

## Cut a release

1. Make a branch from `main`. Name it with the new version, `vX.Y.Z`, following semantic versioning.

1. Set the version in [pyproject.toml](./pyproject.toml) under `[project]`.

1. Update [uv.lock](./uv.lock):

    ```
    uv sync
    ```

1. Open a pull request against `main`. Get it reviewed and merged.

1. Create a GitHub release with a tag and title `vX.Y.Z`. Describe the main changes.

    The release starts the GitHub Actions that publish to PyPI and update the documentation on GitHub Pages.

1. Release to conda-forge. This step is manual. See the next section.

## Conda-forge

This package is on conda-forge. For the full documentation, see https://conda-forge.org/docs/maintainer/. To release a new version, you must be a maintainer of the conda-forge recipe. To become a maintainer, follow https://conda-forge.org/docs/maintainer/updating_pkgs/#updating-the-maintainer-list

The feedstock repository is https://github.com/conda-forge/prescient-sdk-feedstock

Make changes to the feedstock repository from a fork. Do not create a branch in the repository itself.

### Update the conda-forge release after a PyPI release

A conda-forge bot opens a pull request in the feedstock repository after a new PyPI release. The pull request can miss changes, for example new dependencies.

1. Review the `meta.yaml` file in the `recipe` folder of the bot's pull request.

1. Generate a `meta.yaml` file locally with [grayskull](https://github.com/conda/grayskull). Compare it with the file in the pull request, to check the dependencies.

1. To keep several versions on conda-forge, make a separate pull request from a different branch. Follow https://conda-forge.org/docs/maintainer/updating_pkgs/#maintaining-several-versions
