Skip to content

Commit 813a39d

Browse files
committed
Merge pull request #4929 from StuartLittlefair/hcrs_transforms
added transforms between HCRS and ICRS frames
1 parent 7048693 commit 813a39d

2 files changed

Lines changed: 85 additions & 2 deletions

File tree

‎astropy/coordinates/builtin_frames/icrs_cirs_transforms.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,28 @@ def gcrs_to_hcrs(gcrs_coo, hcrs_frame):
264264
return hcrs_frame.realize_frame(newrep)
265265

266266

267+
@frame_transform_graph.transform(FunctionTransform, HCRS, ICRS)
268+
def hcrs_to_icrs(hcrs_coo, icrs_frame):
269+
jd1, jd2 = get_jd12(hcrs_coo.obstime, 'tdb')
270+
earth_pv_helio, earth_pv_bary = erfa.epv00(jd1, jd2)
271+
bary_sun_pos = earth_pv_bary[..., 0, :] - earth_pv_helio[..., 0, :]
272+
sun_vector = u.Quantity(bary_sun_pos, u.au)
273+
new_vector = np.rollaxis(hcrs_coo.cartesian.xyz, -1, 0) + sun_vector
274+
newrep = CartesianRepresentation(np.rollaxis(new_vector, 0, new_vector.ndim))
275+
return icrs_frame.realize_frame(newrep)
276+
277+
278+
@frame_transform_graph.transform(FunctionTransform, ICRS, HCRS)
279+
def icrs_to_hcrs(icrs_coo, hcrs_frame):
280+
jd1, jd2 = get_jd12(hcrs_frame.obstime, 'tdb')
281+
earth_pv_helio, earth_pv_bary = erfa.epv00(jd1, jd2)
282+
bary_sun_pos = earth_pv_bary[..., 0, :] - earth_pv_helio[..., 0, :]
283+
sun_vector = u.Quantity(bary_sun_pos, u.au)
284+
new_vector = np.rollaxis(icrs_coo.cartesian.xyz, -1, 0) - sun_vector
285+
newrep = CartesianRepresentation(np.rollaxis(new_vector, 0, new_vector.ndim))
286+
return hcrs_frame.realize_frame(newrep)
287+
288+
267289
@frame_transform_graph.transform(FunctionTransform, HCRS, HCRS)
268290
def hcrs_to_hcrs(from_coo, to_frame):
269291
if np.all(from_coo.obstime == to_frame.obstime):

‎astropy/coordinates/tests/test_celestial_transformations.py‎

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from .. import SkyCoord
1414
from ...tests.helper import (pytest, quantity_allclose as allclose,
1515
assert_quantity_allclose as assert_allclose)
16-
from .. import EarthLocation
16+
from .. import EarthLocation, CartesianRepresentation
1717
from ...time import Time
1818

1919
# used below in the next parametrized test
@@ -176,6 +176,66 @@ def test_supergalactic():
176176
assert supergalactic.separation(icrs) < 0.005 * u.degree
177177

178178

179+
class TestHCRS():
180+
"""
181+
Check HCRS<->ICRS coordinate conversions.
182+
183+
Uses ICRS Solar positions predicted by get_body_barycentric; with `t1` and
184+
`tarr` as defined below, the ICRS Solar positions were predicted using, e.g.
185+
coord.ICRS(coord.get_body_barycentric(tarr, 'sun')).
186+
"""
187+
def setup(self):
188+
self.t1 = Time("2013-02-02T23:00")
189+
self.t2 = Time("2013-08-02T23:00")
190+
self.tarr = Time(["2013-02-02T23:00", "2013-08-02T23:00"])
191+
192+
self.sun_icrs_scalar = ICRS(ra=244.52984668*u.deg,
193+
dec=-22.36943723*u.deg,
194+
distance=406615.66347377*u.km)
195+
# array of positions corresponds to times in `tarr`
196+
self.sun_icrs_arr = ICRS(ra=[244.52989062, 271.40976248]*u.deg,
197+
dec=[-22.36943605, -25.07431079]*u.deg,
198+
distance=[406615.66347377, 375484.13558956]*u.km)
199+
200+
# corresponding HCRS positions
201+
self.sun_hcrs_t1 = HCRS(CartesianRepresentation([0.0, 0.0, 0.0] * u.km),
202+
obstime=self.t1)
203+
self.sun_hcrs_tarr1 = HCRS(CartesianRepresentation([0.0, 0.0, 0.0] * u.km),
204+
obstime=self.tarr)
205+
twod_rep = CartesianRepresentation([[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]] * u.km)
206+
self.sun_hcrs_tarr1 = HCRS(CartesianRepresentation([0.0, 0.0, 0.0] * u.km),
207+
obstime=self.tarr)
208+
self.sun_hcrs_tarr2 = HCRS(twod_rep,
209+
obstime=self.tarr)
210+
self.tolerance = 5*u.km
211+
212+
def test_from_hcrs(self):
213+
# test scalar transform
214+
transformed = self.sun_hcrs_t1.transform_to(ICRS())
215+
separation = transformed.separation_3d(self.sun_icrs_scalar)
216+
assert_allclose(separation, 0*u.km, atol=self.tolerance)
217+
218+
# test scalar position and non-scalar time
219+
transformed = self.sun_hcrs_tarr1.transform_to(ICRS())
220+
separation = transformed.separation_3d(self.sun_icrs_arr)
221+
assert_allclose(separation, 0*u.km, atol=self.tolerance)
222+
223+
# test non-scalar positions and times
224+
transformed = self.sun_hcrs_tarr2.transform_to(ICRS())
225+
separation = transformed.separation_3d(self.sun_icrs_arr)
226+
assert_allclose(separation, 0*u.km, atol=self.tolerance)
227+
228+
def test_from_icrs(self):
229+
# scalar positions
230+
transformed = self.sun_icrs_scalar.transform_to(HCRS(obstime=self.t1))
231+
separation = transformed.separation_3d(self.sun_hcrs_t1)
232+
assert_allclose(separation, 0*u.km, atol=self.tolerance)
233+
# nonscalar positions
234+
transformed = self.sun_icrs_arr.transform_to(HCRS(obstime=self.tarr))
235+
separation = transformed.separation_3d(self.sun_hcrs_tarr1)
236+
assert_allclose(separation, 0*u.km, atol=self.tolerance)
237+
238+
179239
class TestHelioBaryCentric():
180240
"""
181241
Check GCRS<->Heliocentric and Barycentric coordinate conversions.
@@ -188,7 +248,8 @@ def setup(self):
188248
self.wht_itrs = wht.get_itrs(obstime=self.obstime)
189249

190250
def test_heliocentric(self):
191-
helio = self.wht_itrs.transform_to(HCRS(obstime=self.obstime))
251+
gcrs = self.wht_itrs.transform_to(GCRS(obstime=self.obstime))
252+
helio = gcrs.transform_to(HCRS(obstime=self.obstime))
192253
# Check it doesn't change from previous times.
193254
previous = [-1.02597256e+11, 9.71725820e+10, 4.21268419e+10] * u.m
194255
assert_allclose(helio.cartesian.xyz, previous)

0 commit comments

Comments
 (0)