Metadata-Version: 2.4
Name: wndsn-tycho
Version: 0.1.0
Summary: Python client for the Tycho calculators at tycho.wndsn.com (sun, star, sight, moon, range, shadow, ...)
License-Expression: MIT
Project-URL: Calculators, https://tycho.wndsn.com
Project-URL: Source, https://github.com/gqt0/wndsn-tycho
Project-URL: Documentation, https://github.com/gqt0/wndsn-tycho/blob/main/docs/api.md
Project-URL: Changelog, https://github.com/gqt0/wndsn-tycho/blob/main/CHANGELOG.md
Project-URL: Issues, https://github.com/gqt0/wndsn-tycho/issues
Keywords: astronomy,navigation,sun,moon,sight reduction,rangefinding,quadrant,tycho,wndsn
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Topic :: Scientific/Engineering :: Astronomy
Classifier: Typing :: Typed
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# wndsn-tycho

A Python client for the [Tycho calculators](https://tycho.wndsn.com): the Sun, stars, sight
reduction, the Moon, map scale, range and height from angular size, dip of the horizon,
elevation, the shadow square, and Duv.

- One typed method per calculator, with arguments checked before anything is sent and
  results as dataclasses with named fields.
- Standard library only, Python 3.10+.
- Polite by default: at most one request per second, a User-Agent naming the package, retries
  on HTTP 429/5xx.
- An optional on-disk cache, so tests and repeated runs need no network.

## Install

```bash
pip install wndsn-tycho
```

## Examples

```python
from wndsn_tycho import Tycho

tycho = Tycho()

# Sun at 52.5° N on the June solstice
sun = tycho.sun(52.5, longitude=13.3, tz=1, date="2026-06-21")
sun.diurnal.noon_altitude          # 60.93...
sun.longitudinal.sunrise_time      # "03:44"  (clock times are text, here UTC+1)
sun.longitudinal.astronomical_dawn # None: no astronomical night in June at 52.5°

# Without a date, give the declination; notes say what needs a date
tycho.sun(52.5, declination=20).notes

# Range: any two of angle, size and distance give the third (d = s / tan α)
tycho.range(alpha=2, size=1.8).distance            # 51.545256
tycho.range(alpha=35, size=1.8, angular_unit="mil").distance

# Height from a known distance, h = d · tan α
tycho.height(100, 10).height                       # 17.632698

# Shadow square: umbra recta 12, umbra versa 8
s = tycho.shadow(umbra_recta=12, umbra_versa=8)
s.tan, s.arctan, s.complement                      # 0.666667, 33.690068, 56.309932

# Sight reduction
tycho.sight(52.5, 20, lha=40).hc                   # 45.198923...

# The Moon
moon = tycho.moon(date="2026-11-15", lat=52.5, lon=13.3)
moon.phase_of_moon.phase, moon.moon_of_the_day.moonrise
```

Module-level shortcuts use one shared default client:

```python
import wndsn_tycho

wndsn_tycho.range(alpha=2, size=1.8).distance
wndsn_tycho.configure(cache=True)   # change the default client
```

### Calculators

| Method | Computes |
|---|---|
| `sun(latitude, *, date, declination, longitude, tz, dst, sun_altitude)` | noon altitude, sunrise/sunset azimuths and times, twilight, equation of time, unequal hours |
| `star(latitude, *, declination, star, altitude)` | rising/setting azimuths, culminations, azimuth and hour angle at an altitude |
| `sight(latitude, declination, *, lha, gha, longitude, ho)` | computed altitude Hc, azimuth Zn, intercept |
| `moon(*, date, time, clock, utc, lat, lon, age, illum, wax)` | phase, position, moonrise/transit/moonset, unequal hours |
| `scale(scale, *, map_distance, map_unit, ground_distance, ground_unit)` | map ⇄ ground distance in every unit |
| `range(*, alpha, size, distance, multiplier, angular_unit, unit, solve)` | distance, size or angle; the multiplier 1/tan α; MIL/NATO-MIL/MOA |
| `height(known_distance, alpha, *, angular_unit)` | height from distance and angle |
| `dip(h1, *, h2, h1_unit, h2_unit)` | distance to the horizon, and at which a light is seen |
| `elevation(*, gamma, beta, size, a, b, c, unit)` | height and distance from two angles and a baseline |
| `shadow(*, umbra_recta, umbra_versa, angle, tan, cot)` | tan, cot, angle and its complement |
| `duv(*, x, y, u, v)` | CCT and Duv of a chromaticity |

Each returns a result dataclass (`SunResult`, `RangeResult`, ...) from `wndsn_tycho.results`.
Every result also has `input` (what Tycho was given), `notes` (Tycho's notes, a tuple),
`digits`, `raw` (the whole answer) and `as_dict()`.

Bad arguments raise `ValueError` or `TypeError` without a request: an unknown keyword, a latitude
of 95°, a size over Tycho's limit of 65536, three of range's three values. Input that only Tycho
can judge raises `TychoInputError` (with `.notes`). `TychoHTTPError` means Tycho could not be
reached or answered with an error.

### Raw queries

`get` sends any parameters unchecked and returns Tycho's answer as it is: parsed JSON, or the
text for XML and CSV.

```python
tycho.get("sun", latitude=52.5, date="2026-06-21")      # dict with input, output, digits
tycho.get("range", {"alpha": 2, "size": 1.8}, fmt="csv")
```

### Cache

Off by default. Turn it on for repeated runs, or to run tests offline:

```python
Tycho(cache=True)                       # the user cache directory (see user_cache_dir())
Tycho(cache="tests/tycho-cache")        # a directory of your choice, e.g. committed with tests
Tycho(cache="tests/tycho-cache", mode="offline")   # cache only: a miss raises CacheMiss
Tycho(cache="tests/tycho-cache", mode="refresh")   # fetch everything again
```

The default mode is `fill`: read the cache, fetch only what is missing. Each query is one JSON
file, `<cache>/<calculator>/<key>.json`, holding the query, its URL, when it was fetched, and the
answer. The key is a hash of the canonical query, so the order of parameters and `1` vs `1.0`
don't matter. Refresh a committed cache after a Tycho release and read the diff.

### Options

`Tycho(cache=None, mode="fill", base_url="https://tycho.wndsn.com", digits=6, min_interval=1.0,
timeout=20.0, retries=2, user_agent=...)`

- `digits`: precision Tycho rounds to, 0–9 (Tycho's own default is 2). `moon` takes at most 4.
- `min_interval`: seconds between live requests. Against tycho.wndsn.com it can't be below 1;
  all clients in a process share one throttle. A local copy of the site (`base_url`) may go faster.

### Command line

```bash
wndsn-tycho sun latitude=52.5 date=2026-06-21
```

```bash
wndsn-tycho range alpha=2 size=1.8 --format csv
```

```bash
python -m wndsn_tycho height known_distance=100 alpha=10 --cache
```

## The API

[docs/api.md](https://github.com/gqt0/wndsn-tycho/blob/main/docs/api.md) describes the HTTP API: parameters, limits, output fields, and the
quirks the client handles (`height`'s flat answer, `range`'s size limit, `sun` without a date,
`moon`'s digits).

## Tests

```bash
python -m unittest discover -s tests -t .
```

The unit tests run against a fake Tycho that serves real recorded answers, with no network. The
live test sends one query per calculator to tycho.wndsn.com (about 15 s):

```bash
TYCHO_LIVE=1 python -m unittest tests.test_live -v
```

## Versioning

[Semantic versioning](https://semver.org); the version is in `src/wndsn_tycho/_version.py`,
changes in [CHANGELOG.md](https://github.com/gqt0/wndsn-tycho/blob/main/CHANGELOG.md), and releases are tagged `vX.Y.Z`.

## Licence

MIT, see [LICENSE](https://github.com/gqt0/wndsn-tycho/blob/main/LICENSE).
