NextAstro logo NextAstroDocumentation Open Hub

Python examples

Python example support code

The worked examples import these helpers from nextastro_obs_client.examples._common. They are already in the OBS checkout; use the module commands shown in each example.

Shared helpers

"""Shared command-line plumbing; no connection happens at import time."""
import argparse
import csv
from contextlib import contextmanager
from nextastro_obs_client import Observatory


def parser(description):
    p = argparse.ArgumentParser(description=description)
    p.add_argument("--port", type=int, default=8221)
    p.add_argument("--userid", default=None)
    p.add_argument("--camera", default=None, help="Camera ID from obs.capabilities()")
    return p


def target_arguments(p):
    p.add_argument("--ra", type=float, required=True, help="Right ascension in hours")
    p.add_argument("--dec", type=float, required=True, help="Declination in degrees")
    p.add_argument("--target", required=True)
    p.add_argument("--filter", required=True)


def point(obs, args):
    obs.mount.slew(ra_hours=args.ra, dec_deg=args.dec)


def execute(p, run, argv=None):
    args = p.parse_args(argv)
    obs = Observatory(port=args.port)
    camera = obs.camera(args.camera) if args.camera else obs.camera
    with obs.session(name=p.description, userid=args.userid):
        return run(obs, camera, args)


def write_rows(path, rows):
    if rows:
        with open(path, "w", newline="", encoding="utf-8") as handle:
            writer = csv.DictWriter(handle, fieldnames=list(rows[0]))
            writer.writeheader()
            writer.writerows(rows)
        print(path)


@contextmanager
def return_after(action):
    """A failed return move must not hide the original failure or interruption."""
    try:
        yield
    except BaseException as original:
        try:
            action()
        except Exception as cleanup:
            detail = f'Return move failed: {cleanup}'
            if hasattr(original, 'add_note'):
                original.add_note(detail)
            else:
                print(detail)
        raise
    else:
        action()

Example target CSV

The target-list example reads nextastro_obs_client/examples/targets.csv. Replace its illustrative rows with your own targets.

target_name,ra_hours,dec_deg,filter,seconds,count
Example field,12.5,-30,V,5,2