Skip to content

Commit

Permalink
Factor out ra/dec calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
timj committed Oct 31, 2018
1 parent ac38a99 commit d817aa8
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 41 deletions.
23 changes: 6 additions & 17 deletions python/astro_metadata_translator/translators/decam.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@

import re

from astropy.coordinates import EarthLocation, SkyCoord, AltAz, Angle
from astropy.coordinates import EarthLocation, AltAz, Angle
import astropy.units as u

from ..translator import cache_translation
from .fits import FitsTranslator
from .helpers import altitude_from_zenith_distance, is_non_science
from .helpers import altitude_from_zenith_distance, is_non_science, \
tracking_from_degree_headers


class DecamTranslator(FitsTranslator):
Expand Down Expand Up @@ -169,21 +170,9 @@ def to_observation_type(self):

@cache_translation
def to_tracking_radec(self):
used = []
if "RADESYS" in self._header:
frame = self._header["RADESYS"].strip().lower()
used.append("RADESYS")
if frame == "gappt":
self._used_these_cards(*used)
# Moving target
return None
else:
frame = "icrs"
radec = SkyCoord(self._header["TELRA"], self._header["TELDEC"],
frame=frame, unit=(u.hourangle, u.deg),
obstime=self.to_datetime_begin(), location=self.to_location())
self._used_these_cards("TELRA", "TELDEC", *used)
return radec
radecsys = ("RADESYS",)
radecpairs = (("TELRA", "TELDEC"),)
return tracking_from_degree_headers(self, radecsys, radecpairs, unit=(u.hourangle, u.deg))

@cache_translation
def to_altaz_begin(self):
Expand Down
54 changes: 53 additions & 1 deletion python/astro_metadata_translator/translators/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@

__all__ = ("to_location_via_telescope_name",
"is_non_science",
"tracking_from_degree_headers",
"altitude_from_zenith_distance")

from astropy.coordinates import EarthLocation
from astropy.coordinates import EarthLocation, SkyCoord
import astropy.units as u


Expand Down Expand Up @@ -78,3 +79,54 @@ def altitude_from_zenith_distance(zd):
Altitude.
"""
return 90.*u.deg - zd


def tracking_from_degree_headers(self, radecsys, radecpairs, unit=u.deg):
"""Calculate the tracking coordinates from lists of headers.
Parameters
----------
radecsys : `list` or `tuple`
Header keywords to try corresponding to the tracking system. If none
match ICRS will be assumed.
pairs : `tuple` of `tuple` of pairs of `str`
Pairs of keywords containing the RA/Dec in units of ``unit``.
unit : `astropy.unit.BaseUnit` or `tuple`
Unit definition suitable for the `~astropy.coordinate.SkyCoord`
constructor.
Returns
-------
radec = `astropy.coordinates.SkyCoord`
The RA/Dec coordinates. None if this is a moving target or a
non-science observation without any RA/Dec definition.
Raises
------
KeyError
No RA/Dec keywords were found and this observation is a science
observation.
"""
used = []
for k in radecsys:
if k in self._header:
frame = self._header[k].strip().lower()
used.append(k)
if frame == "gappt":
self._used_these_cards(*used)
# Moving target
return None
break
else:
frame = "icrs"
for ra_key, dec_key in radecpairs:
print(f"RA: {ra_key}: {self._header[ra_key]} Dec: {dec_key}: {self._header[dec_key]}")
if ra_key in self._header and dec_key in self._header:
radec = SkyCoord(self._header[ra_key], self._header[dec_key],
frame=frame, unit=unit, obstime=self.to_datetime_begin(),
location=self.to_location())
self._used_these_cards(ra_key, dec_key, *used)
return radec
if self.to_observation_type() == "science":
raise KeyError("Unable to determine tracking RA/Dec of science observation")
return None
41 changes: 18 additions & 23 deletions python/astro_metadata_translator/translators/megaprime.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@

__all__ = ("MegaPrimeTranslator", )

from astropy.coordinates import EarthLocation, SkyCoord, AltAz, Angle
from astropy.coordinates import EarthLocation, AltAz, Angle
import astropy.units as u

from .fits import FitsTranslator
from .helpers import tracking_from_degree_headers

filters = {'u.MP9301': 'u',
'u.MP9302': 'u2',
Expand Down Expand Up @@ -131,28 +132,22 @@ def to_observation_type(self):
return obstype

def to_tracking_radec(self):
used = []
for k in ("RADECSYS", "OBJRADEC", "RADESYS"):
if k in self._header:
frame = self._header[k].strip().lower()
used.append(k)
if frame == "gappt":
self._used_these_cards(*used)
# Moving target
return None
break
else:
frame = "icrs"
for ra_key, dec_key in (("RA_DEG", "DEC_DEG"), ("BORE-RA", "BORE-DEC")):
if ra_key in self._header and dec_key in self._header:
radec = SkyCoord(self._header[ra_key], self._header[dec_key],
frame=frame, unit=u.deg, obstime=self.to_datetime_begin(),
location=self.to_location())
self._used_these_cards(ra_key, dec_key, *used)
return radec
if self.to_observation_type() == "science":
raise KeyError("Unable to determine tracking RA/Dec of science observation")
return None
"""Calculate the tracking RA/Dec for this observation.
Currently will be `None` for geocentric apparent coordinates.
Additionally, can be `None` for non-science observations.
The method supports multiple versions of header defining tracking
coordinates.
Returns
-------
coords : `astropy.coordinates.SkyCoord`
The tracking coordinates.
"""
radecsys = ("RADECSYS", "OBJRADEC", "RADESYS")
radecpairs = (("RA_DEG", "DEC_DEG"), ("BORE-RA", "BORE-DEC"))
return tracking_from_degree_headers(self, radecsys, radecpairs)

def to_altaz_begin(self):
"""Calculate the azimuth and elevation for the start of the
Expand Down

0 comments on commit d817aa8

Please sign in to comment.