Esempio n. 1
0
def test_spice_earth():
    # Replace the ICRS->AltAz transform in astropy with one using SPICE.
    # Confirm that star positions are the same as with the original transform
    # to within the error due to relativistic aberration (~ 21 arcsec)

    stars = ltests.get_catalog()

    lat, lon = 30, 25

    loc = EarthLocation.from_geodetic(lon, lat)

    altaz = stars.transform_to(AltAz(location=loc))

    trans_path, steps = frame_transform_graph.find_shortest_path(ICRS, AltAz)
    assert steps == 2

    # Make the Earth topo frame in spice.
    framename, idnum, frame_dict, latlon = lunarsky.spice_utils.topo_frame_def(
        lat, lon, moon=False)

    # One more kernel is needed for the ITRF93 frame.
    kname = 'pck/earth_latest_high_prec.bpc'
    _naif_kernel_url = 'https://naif.jpl.nasa.gov/pub/naif/generic_kernels'
    kurl = [_naif_kernel_url + '/' + kname]
    kernpath = download_files_in_parallel(kurl,
                                          cache=True,
                                          show_progress=False,
                                          pkgname='lunarsky')
    spice.furnsh(kernpath)

    frame_strs = ["{}={}".format(k, v) for (k, v) in frame_dict.items()]
    spice.lmpool(frame_strs)

    @frame_transform_graph.transform(FunctionTransformWithFiniteDifference,
                                     ICRS, AltAz)
    def icrs_to_mcmf(icrs_coo, mcmf_frame):

        mat = spice.pxform('J2000', framename, 0)
        newrepr = icrs_coo.cartesian.transform(mat)

        return mcmf_frame.realize_frame(newrepr)

    trans_path2, steps2 = frame_transform_graph.find_shortest_path(ICRS, AltAz)
    assert steps2 == 1

    altaz2 = stars.transform_to(AltAz(location=loc))

    # Having done the transform, remove the spice transform from the graph
    frame_transform_graph.remove_transform(ICRS, AltAz, None)
    trans_path2, steps2 = frame_transform_graph.find_shortest_path(ICRS, AltAz)
    assert steps2 == 2

    assert ltests.positions_close(altaz, altaz2, Angle(25, 'arcsec'))
def test_unit_representation_subclass():

    class Longitude180(Longitude):
        def __new__(cls, angle, unit=None, wrap_angle=180*u.deg, **kwargs):
            self = super(Longitude180, cls).__new__(cls, angle, unit=unit,
                                                    wrap_angle=wrap_angle, **kwargs)
            return self


    class UnitSphericalWrap180Representation(UnitSphericalRepresentation):
        attr_classes = OrderedDict([('lon', Longitude180),
                                    ('lat', Latitude)])
        recommended_units = {'lon': u.deg, 'lat': u.deg}


    class SphericalWrap180Representation(SphericalRepresentation):
        attr_classes = OrderedDict([('lon', Longitude180),
                                    ('lat', Latitude),
                                    ('distance', u.Quantity)])
        recommended_units = {'lon': u.deg, 'lat': u.deg}

        _unit_representation = UnitSphericalWrap180Representation


    class myframe(ICRS):
        default_representation = SphericalWrap180Representation
        frame_specific_representation_info = {
            'spherical': [RepresentationMapping('lon', 'ra'),
                          RepresentationMapping('lat', 'dec')]
        }
        frame_specific_representation_info['unitspherical'] = \
        frame_specific_representation_info['unitsphericalwrap180'] = \
        frame_specific_representation_info['sphericalwrap180'] = \
            frame_specific_representation_info['spherical']


    @frame_transform_graph.transform(FunctionTransform,
                                     myframe, astropy.coordinates.ICRS)
    def myframe_to_icrs(myframe_coo, icrs):
        return icrs.realize_frame(myframe_coo._data)

    f = myframe(10*u.deg, 10*u.deg)
    assert isinstance(f._data, UnitSphericalWrap180Representation)
    assert isinstance(f.ra, Longitude180)

    g = f.transform_to(astropy.coordinates.ICRS)
    assert isinstance(g, astropy.coordinates.ICRS)
    assert isinstance(g._data, UnitSphericalWrap180Representation)

    frame_transform_graph.remove_transform(myframe,
                                           astropy.coordinates.ICRS,
                                           None)
def test_unit_representation_subclass():
    class Longitude180(Longitude):
        def __new__(cls, angle, unit=None, wrap_angle=180 * u.deg, **kwargs):
            self = super(Longitude180, cls).__new__(cls,
                                                    angle,
                                                    unit=unit,
                                                    wrap_angle=wrap_angle,
                                                    **kwargs)
            return self

    class UnitSphericalWrap180Representation(UnitSphericalRepresentation):
        attr_classes = OrderedDict([('lon', Longitude180), ('lat', Latitude)])
        recommended_units = {'lon': u.deg, 'lat': u.deg}

    class SphericalWrap180Representation(SphericalRepresentation):
        attr_classes = OrderedDict([('lon', Longitude180), ('lat', Latitude),
                                    ('distance', u.Quantity)])
        recommended_units = {'lon': u.deg, 'lat': u.deg}

        _unit_representation = UnitSphericalWrap180Representation

    class myframe(ICRS):
        default_representation = SphericalWrap180Representation
        frame_specific_representation_info = {
            'spherical': [
                RepresentationMapping('lon', 'ra'),
                RepresentationMapping('lat', 'dec')
            ]
        }
        frame_specific_representation_info['unitspherical'] = \
        frame_specific_representation_info['unitsphericalwrap180'] = \
        frame_specific_representation_info['sphericalwrap180'] = \
            frame_specific_representation_info['spherical']

    @frame_transform_graph.transform(FunctionTransform, myframe,
                                     astropy.coordinates.ICRS)
    def myframe_to_icrs(myframe_coo, icrs):
        return icrs.realize_frame(myframe_coo._data)

    f = myframe(10 * u.deg, 10 * u.deg)
    assert isinstance(f._data, UnitSphericalWrap180Representation)
    assert isinstance(f.ra, Longitude180)

    g = f.transform_to(astropy.coordinates.ICRS)
    assert isinstance(g, astropy.coordinates.ICRS)
    assert isinstance(g._data, UnitSphericalWrap180Representation)

    frame_transform_graph.remove_transform(myframe, astropy.coordinates.ICRS,
                                           None)