Skip to content

Commit

Permalink
Move telescopes to routers
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed Nov 11, 2023
1 parent fadb550 commit 0f0b637
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 35 deletions.
37 changes: 2 additions & 35 deletions src/lvmapi/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,15 @@

from __future__ import annotations

from typing import Literal

from fastapi import FastAPI

from lvmapi.tools import CluClient

from lvmapi.routers import telescopes

TELESCOPES_TYPE = Literal["sci", "spec", "skye", "skyw"]

app = FastAPI()
app.include_router(telescopes.router)


@app.get("/")
def root():
return {}


@app.get("/telescopes/{telescope}/pointing")
async def get_pointing(
telescope: TELESCOPES_TYPE,
frame: Literal["radec", "altaz"] = "radec",
):
"""Gets the pointing of a telescope."""

try:
async with CluClient() as client:
status_cmd = await client.send_command(f"lvm.{telescope}.pwi", "status")

if frame == "radec":
ax0 = status_cmd.replies.get("ra_apparent_hours") * 15
ax1 = status_cmd.replies.get("dec_apparent_degs")
elif frame == "altaz":
ax0 = status_cmd.replies.get("altitude_degs")
ax1 = status_cmd.replies.get("azimuth_degs")
else:
raise ValueError(f"Invalid frame {frame}")

except Exception:
ax0 = -999.0
ax1 = -999.0

if frame == "radec":
return {"ra": ax0, "dec": ax1}
else:
return {"alt": ax0, "az": ax1}
Empty file added src/lvmapi/routers/__init__.py
Empty file.
50 changes: 50 additions & 0 deletions src/lvmapi/routers/telescopes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# @Author: José Sánchez-Gallego (gallegoj@uw.edu)
# @Date: 2023-11-11
# @Filename: telescopes.py
# @License: BSD 3-clause (http://www.opensource.org/licenses/BSD-3-Clause)

from __future__ import annotations

from typing import Literal

from fastapi import APIRouter, HTTPException

from lvmapi.tools import CluClient


router = APIRouter(prefix="/telescopes", tags=["telescopes"])


@router.get("/{telescope}/pointing")
async def get_pointing(
telescope: Literal["sci", "spec", "skye", "skyw"],
frame: Literal["radec", "altaz"] = "radec",
):
"""Gets the pointing of a telescope."""

try:
async with CluClient() as client:
status_cmd = await client.send_command(f"lvm.{telescope}.pwi", "status")

if frame == "radec":
ax0 = status_cmd.replies.get("ra_apparent_hours") * 15
ax1 = status_cmd.replies.get("dec_apparent_degs")
elif frame == "altaz":
ax0 = status_cmd.replies.get("altitude_degs")
ax1 = status_cmd.replies.get("azimuth_degs")
else:
raise ValueError(f"Invalid frame {frame}")

except Exception:
raise HTTPException(
status_code=500,
detail="Error retrieving telescope information.",
)

if frame == "radec":
return {"ra": ax0, "dec": ax1}
else:
return {"alt": ax0, "az": ax1}

0 comments on commit 0f0b637

Please sign in to comment.