Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

corrections command #8

Merged
merged 4 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/lvmguider/actor/commands/corrections.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# @Author: José Sánchez-Gallego ([email protected])
# @Date: 2023-09-20
# @Filename: corrections.py
# @License: BSD 3-clause (http://www.opensource.org/licenses/BSD-3-Clause)

from __future__ import annotations

from typing import TYPE_CHECKING

import click

from lvmguider.actor import lvmguider_parser


if TYPE_CHECKING:
from lvmguider.actor import GuiderCommand


__all__ = ["corrections"]


@lvmguider_parser.command()
@click.argument("MODE", type=click.Choice(["enable", "disable"]))
async def corrections(command: GuiderCommand, mode: str):
"""Enables/disables corrections during guiding."""

if not command.actor.guider:
return command.fail("Guider is not running.")

if mode.lower() == "enable":
command.actor.guider.apply_corrections = True
elif mode.lower() == "disable":
command.actor.guider.apply_corrections = False
else:
return command.fail(f"Invalid mode {mode!r}.")

return command.finish()
8 changes: 6 additions & 2 deletions src/lvmguider/actor/commands/guide.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ async def guide(
actor = command.actor
MAX_EXPTIME: float = 18

guider = Guider(command, (ra, dec, pa), pixel=reference_pixel)
guider = Guider(
command,
(ra, dec, pa),
pixel=reference_pixel,
apply_corrections=apply_corrections,
)
command.actor.guider = guider

if actor.status & GuiderStatus.NON_IDLE:
Expand All @@ -98,7 +103,6 @@ async def guide(
guider.guide_one(
exposure_time,
guide_tolerance=guide_tolerance,
apply_correction=apply_corrections,
)
)
await actor.guide_task
Expand Down
1 change: 1 addition & 0 deletions src/lvmguider/etc/lvmguider.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ max_rot_correction: 3
guide_tolerance: 1
pa_tolerance: 0.012
revert_to_acquistion_threshold: 2
apply_guider_corrections: true

xz_full_frame: [2500.0, 1000.0]
xz_ag_frame: [800.0, 550.0]
Expand Down
17 changes: 12 additions & 5 deletions src/lvmguider/guider.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ class Guider:
pixel
The ``(x,y)`` pixel of the full frame to use to determine the pointing.
Default to the central pixel.
apply_corrections
Whether to apply the measured corrections. If `False`, outputs the
measurements but does not command the telescope.

"""

Expand All @@ -80,6 +83,7 @@ def __init__(
command: GuiderCommand,
field_centre: tuple[float, float, float],
pixel: tuple[float, float] | None = None,
apply_corrections: bool = True,
):
self.command = command
self.telescope = command.actor.telescope
Expand All @@ -88,6 +92,7 @@ def __init__(
self.config = command.actor.config
self.guide_tolerance = self.config["guide_tolerance"]
self.pa_tolerance = self.config["pa_tolerance"]
self.apply_corrections: bool = apply_corrections

self.field_centre = field_centre
self.pixel = pixel or self.config["xz_full_frame"]
Expand Down Expand Up @@ -160,7 +165,6 @@ async def guide_one(
exposure_time: float = 5.0,
force_astrometry_net: bool = False,
guide_tolerance: float | None = None,
apply_correction: bool = True,
):
"""Performs one guide iteration.

Expand Down Expand Up @@ -194,9 +198,6 @@ async def guide_one(
The separation between field RA/Dec and measured pointing at which
to consider than acquisition has been completed and guiding begins.
If `None`, defaults to the configuration file value.
apply_corrections
Whether to apply the measured corrections. If `False`, outputs the
measurements but does not command the telescope.

"""

Expand Down Expand Up @@ -298,10 +299,16 @@ async def guide_one(

corr_rot = self.pid_rot(offset_pa) or 0.0

apply_guider_corrections = self.config["apply_guider_corrections"]
if self.is_guiding() and not apply_guider_corrections:
apply_correction_this = False
else:
apply_correction_this = self.apply_corrections

applied_motax = numpy.array([0.0, 0.0])
applied_rot: float = 0.0
try:
if apply_correction:
if apply_correction_this:
self.command.actor.status &= ~GuiderStatus.PROCESSING
self.command.actor.status |= GuiderStatus.CORRECTING

Expand Down
77 changes: 22 additions & 55 deletions src/lvmguider/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,21 +337,14 @@ def update_fits(
hdu.header[key] = value


def get_gaia_sources(
wcs: WCS,
db_connection_params: dict[str, Any] = {},
include_lvm_mags: bool = True,
):
def get_gaia_sources(wcs: WCS, db_connection_params: dict[str, Any] = {}):
"""Returns a data frame with Gaia source information from a WCS.

Parameters
----------
wcs
A WCS associated with the image. Used to determine the centre of the
image and perform a radial query in the database.
include_lvm_mags
If `True`, match to ``lvm_magnitude`` and return LVM AG passband
magnitudes and fluxes.
db_connection_params
A dictionary of DB connection parameters to pass to `.get_db_connection`.

Expand All @@ -378,59 +371,33 @@ def get_gaia_sources(
GDR3 = peewee.Table(gdr3_table, schema=gdr3_sch).bind(conn)
LMAG = peewee.Table(lmag_table, schema=lmag_sch).bind(conn)

if include_lvm_mags:
cte = (
LMAG.select(
LMAG.c.source_id,
LMAG.c.lmag_ab,
LMAG.c.lflux,
)
.where(
peewee.fn.q3c_radial_query(
LMAG.c.ra,
LMAG.c.dec,
float(ra),
float(dec),
CAM_FOV,
)
)
.cte("cte", materialized=True)
query = (
GDR3.select(
GDR3.c.source_id,
GDR3.c.ra,
GDR3.c.dec,
GDR3.c.pmra,
GDR3.c.pmdec,
GDR3.c.phot_g_mean_mag,
LMAG.c.lmag_ab,
LMAG.c.lflux,
)

query = (
cte.select(
cte.star,
GDR3.c.ra,
GDR3.c.dec,
GDR3.c.pmra,
GDR3.c.pmdec,
GDR3.c.phot_g_mean_mag,
)
.join(GDR3, on=(cte.c.source_id == GDR3.c.source_id))
.with_cte(cte)
.dicts()
.join(
LMAG,
on=(LMAG.c.source_id == GDR3.c.source_id),
join_type=peewee.JOIN.LEFT_OUTER,
)

else:
query = (
GDR3.select(
.where(
peewee.fn.q3c_radial_query(
GDR3.c.ra,
GDR3.c.dec,
GDR3.c.pmra,
GDR3.c.pmdec,
GDR3.c.phot_g_mean_mag,
float(ra),
float(dec),
CAM_FOV,
)
.where(
peewee.fn.q3c_radial_query(
GDR3.c.ra,
GDR3.c.dec,
float(ra),
float(dec),
CAM_FOV,
)
)
.dicts()
)
.dicts()
)

with conn.atomic():
conn.execute_sql("SET LOCAL work_mem='2GB'")
Expand Down