11import logging
22from typing import Tuple , Any
33
4- from astropy .coordinates import SkyCoord
4+ from astropy .coordinates import Angle
5+ from astropy .table import Table , Row
56from astropy .wcs import WCS
67
78from pyobs .images import Image
@@ -16,12 +17,11 @@ class BrightestStarOffsets(Offsets):
1617
1718 __module__ = "pyobs.images.processors.offsets"
1819
19- def __init__ (self , center : Tuple [str , str ] = ("CRPIX1" , "CRPIX2" ), ** kwargs : Any ):
20+ def __init__ (self , center_header_cards : Tuple [str , str ] = ("CRPIX1" , "CRPIX2" ), ** kwargs : Any ):
2021 """Initializes a new auto guiding system."""
2122 Offsets .__init__ (self , ** kwargs )
2223
23- # init
24- self ._center = center
24+ self ._center_header_cards = center_header_cards
2525
2626 async def __call__ (self , image : Image ) -> Image :
2727 """Processes an image and sets x/y pixel offset to reference in offset attribute.
@@ -36,31 +36,33 @@ async def __call__(self, image: Image) -> Image:
3636 ValueError: If offset could not be found.
3737 """
3838
39- # get catalog and sort by flux
40- cat = image .safe_catalog
41- if cat is None or len (cat ) < 1 :
39+ catalog = image .safe_catalog
40+ if catalog is None or len (catalog ) < 1 :
4241 log .warning ("No catalog found in image." )
4342 return image
44- cat .sort ("flux" , reverse = True )
4543
46- # get first X/Y coordinates
47- x , y = cat [ "x" ] [0 ], cat [ "y" ][ 0 ]
44+ star_pos = self . _get_brightest_star_position ( catalog )
45+ center = image . header [ self . _center_header_cards [0 ]], image . header [ self . _center_header_cards [ 1 ] ]
4846
49- # get center
50- center_x , center_y = image . header [ self ._center [ 0 ]], image . header [ self . _center [ 1 ]]
47+ offset = ( star_pos [ 0 ] - center [ 0 ], star_pos [ 1 ] - center [ 1 ])
48+ on_sky_distance = self ._calc_on_sky_distance ( image , center , star_pos )
5149
52- # calculate offset
53- dx , dy = x - center_x , y - center_y
50+ image .set_meta (PixelOffsets (* offset ))
51+ image .set_meta (OnSkyDistance (on_sky_distance ))
52+ return image
53+
54+ @staticmethod
55+ def _get_brightest_star_position (catalog : Table ) -> Tuple [float , float ]:
56+ brightest_star : Row = max (catalog , key = lambda row : row ["flux" ])
57+ return brightest_star ["x" ], brightest_star ["y" ]
5458
55- # get distance on sky
59+ @staticmethod
60+ def _calc_on_sky_distance (image : Image , center : Tuple [float , float ], star_pos : Tuple [float , float ]) -> Angle :
5661 wcs = WCS (image .header )
57- coords1 = wcs .pixel_to_world (center_x , center_y )
58- coords2 = wcs .pixel_to_world (center_x + dx , center_y + dy )
62+ center_coordinates = wcs .pixel_to_world (* center )
63+ star_coordinates = wcs .pixel_to_world (* star_pos )
5964
60- # set it and return image
61- image .set_meta (PixelOffsets (dx , dy ))
62- image .set_meta (OnSkyDistance (coords1 .separation (coords2 )))
63- return image
65+ return center_coordinates .separation (star_coordinates )
6466
6567
6668__all__ = ["BrightestStarOffsets" ]
0 commit comments