Metadata-Version: 2.4
Name: merulabs
Version: 0.3.0
Summary: Meru — one API for Earth observation. Name a place, a date, and what you want.
Author: MERU Labs
License: Apache-2.0
Project-URL: Homepage, https://www.merulabs.space
Project-URL: Documentation, https://app.merulabs.space/api
Keywords: earth-observation,satellite,geospatial,ndvi,sar,stac,remote-sensing
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: GIS
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# Meru SDK

One API for Earth observation. Name a place, a date, and what you want.

```bash
pip install merulabs
export MERU_BASE_URL=https://app.merulabs.space   # the hosted API; see below
export MERU_API_KEY=meru_sk_...                   # create one in the dashboard's API panel
```

The package is `merulabs` on PyPI; the import and the command are both `meru`
(`import meru`, `meru --help`).

`MERU_BASE_URL` is not set for you. If you leave it unset the SDK talks to
`http://127.0.0.1:8088`, a local development server, and a call fails with a connection
error. Set it to `https://app.merulabs.space` for the hosted API, or pass `base_url=`.

The SDK is free and open source (Apache-2.0). The hosted API needs an API key from
MERU Labs: sign up at https://app.merulabs.space.

```python
import meru

r = meru.get("ndvi", region="Gujarat, India", date="2026-06")
print(r.stats)        # {'mean': 0.62, 'min': -0.11, 'max': 0.89, 'valid_pct': 0.94}
print(r.tiles_url)    # XYZ tile template — drop into any map

# any data type, any source — see what's available:
meru.catalog()       # [{'source': 'sentinel-2', 'products': [...]}, ...]
meru.get("backscatter", region="Sundarbans", source="sentinel-1-rtc")
meru.get("landcover", aoi=[72.5, 22.9, 72.7, 23.1])
```

`date` accepts `'YYYY-MM'`, `'YYYY-MM-DD'`, `'start/end'`, or nothing (the last 100 days).
Pass `region=` for a place name or `aoi=[west, south, east, north]` for a box. Both
`base_url=` and `api_key=` can be given per call instead of via the environment.

## What else a recipe can do

```python
r = meru.get("ndvi", region="Nashik, India", date="2026-05")

r.series()                       # value over time, one point per scene
r.series(max_points=30)          # the whole timeline, not the default 12-point sample

r.download("tif", path="ndvi.tif")     # GeoTIFF of the real values (float NDVI, °C, dB…)
r.download("png", i=3)                 # the colorized image, scene 3 of the timeline
r.download_all("png", path="all.zip")  # every scene, one file per date

r.map_tiles_url()                # tiles URL a browser map can load by itself
```

Each `series()` point carries its `index` into the timeline, so it lines up with
`download(i=...)`.

### Tiles in a map

`r.tiles_url` is authenticated by header, which a map library cannot send — Leaflet and
Mapbox GL fetch tiles as images. `r.map_tiles_url()` asks the API for a short-lived token
scoped to that one recipe and returns a URL that carries it:

```python
tiles = r.map_tiles_url(ttl_seconds=3600)
# L.tileLayer(tiles).addTo(map)
```

The token expires, works only on that recipe's tiles, and is not a credential anywhere
else — so it is safe in a page in a way the API key would not be.

## Datasets

Bring your own data into your organisation's Datasphere. These need a key minted inside an
organisation (the dashboard's API keys page); the zero-setup self-serve key belongs to none,
so `meru datasets` never mints one and tells you so.

```bash
meru datasets upload fields.zip --terms owned --personal-data no [--name N] [--note TEXT]
meru datasets list [--before ds_…] [--json]
meru datasets show ds_…  [--json]
meru datasets confirm ds_… --layer fields/f.shp --crs EPSG:32643
meru datasets object-set ds_… [--layer x.gpkg::fields]
meru datasets download ds_… [--file original|cog|vectors] [--layer L0] [--out PATH]
meru datasets delete ds_…
```

`--terms` (`owned`, `licensed` or `open`) and `--personal-data yes|no` are required on
upload; there is no default for either. `upload` sends the file straight to storage in
parts, then polls until intake reaches a final status and prints the report. A coordinate
system intake could not find is never guessed: the dataset waits in `needs_confirmation`
until `confirm` names one. `object-set` turns a polygon layer into an `obj_…` handle that
`meru table` accepts.

A layer has two names, and `show` prints both. Its **key** is the path inside the upload
(`fields/f.shp`, or `x.gpkg::fields` for one of several layers in a file); `confirm` and
`object-set` take it. Its **output** is `L0`, `L1`, …; `download --layer` takes that.

```python
d = meru.upload_dataset("fields.zip", terms="owned", personal_data=False)
d["status"]                                   # 'ready' | 'needs_confirmation' | 'rejected' | …
meru.confirm_dataset(d["id"], {"fields/f.shp": "EPSG:32643"})   # the layer key
obj = meru.dataset_object_set(d["id"], layer="fields/f.shp")     # then meru.table(…) with obj["id"]
path = meru.download_dataset(d["id"], file="original", path="fields.zip")
```

| Function | Returns |
|---|---|
| `upload_dataset(path, *, terms, personal_data, name=None, note=None, wait=True, poll=3.0, timeout=None)` | the dataset with its intake report (`wait=False`: the queued dataset) |
| `datasets(before=None)` | `{"datasets": […], "storage": {"used", "limit"}, "next"}` |
| `dataset(dataset_id)` | one dataset, with its report |
| `wait_for_dataset(dataset_id, poll=3.0, timeout=None)` | the dataset once its status is final |
| `confirm_dataset(dataset_id, crs)` | the dataset, re-queued; `crs` is `{"<layer key>": "EPSG:…"}` |
| `dataset_object_set(dataset_id, layer=None)` | the object-set manifest; `layer` is a layer key, needed when several can convert |
| `download_dataset(dataset_id, file="original", layer=None, path=None)` | `layer` is an output stem (`L0`). **The path it wrote**, not bytes: streamed to disk, and an existing file is refused |
| `delete_dataset(dataset_id)` | `None` |

Every function also takes `base_url=` and `api_key=`.

Starting an upload and finishing one each count against your organisation's compute rate
limit (20 a minute by default), so a batch runs at about ten files a minute. A batch script
should catch `rate_limited` on `upload_dataset` and wait `e.retry_after`. Finishing is the one
step that waits by itself: by then every part is stored, so it waits rather than throwing the
upload away.

## Errors

Everything raises, and every exception descends from `meru.MeruError`:

```python
try:
    r = meru.get("ndvi", region="Reykjavík", date="2026-01")
except meru.NoDataError as e:
    # valid request, nothing published for it
    print(e.reason)           # 'too_cloudy' | 'out_of_range' | 'no_coverage'
    print(e.suggested_time)   # a window that would resolve, when we can name one
    r = meru.get("ndvi", region="Reykjavík", date=e.suggested_time)
except meru.MeruAPIError as e:
    # the API refused the request; e.detail is its own explanation
    print(e.status, e.detail)
    if e.code == "rate_limited":
        time.sleep(e.retry_after)
    elif e.code in ("allowance_exhausted", "spend_cap_reached", "pilot_units_exhausted"):
        raise                 # 402: nothing ran, nothing was charged
except meru.MeruConnectionError:
    # the API could not be reached — check MERU_BASE_URL
    raise
```

`e.code` is set only where a client is expected to branch on it (`rate_limited`, and the
402s `allowance_exhausted`, `spend_cap_reached`, `pilot_units_exhausted`; `quota_exceeded` is
retired); `e.detail` is prose meant for a person and may be reworded.

`meru.billing()` returns your plan, units used, allowance, remaining, reset time and, on
pay-as-you-go, the estimated charge (`GET /v1/billing`). `meru usage` prints the same.

## Notes

Stdlib-only, no dependencies. Place names are geocoded through the API rather than
straight to OpenStreetMap, so a region means the same thing here as on the dashboard.
A cold resolve reads live from remote COGs and can take a minute — `MERU_TIMEOUT`
(seconds, default 180) or `timeout=` per call.

Without `MERU_BASE_URL` (or `base_url=`), the SDK uses `http://127.0.0.1:8088`, a local
development server. The CLI also reuses the base URL it stored the first time it minted a key.
