File: inplace.rst

package info (click to toggle)
python-astropy 1.3-8~bpo8%2B2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 44,292 kB
  • sloc: ansic: 160,360; python: 137,322; sh: 11,493; lex: 7,638; yacc: 4,956; xml: 1,796; makefile: 474; cpp: 364
file content (44 lines) | stat: -rw-r--r-- 1,936 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
In Place Modification of Coordinates
====================================

Coordinates are generally considered to be immutable. If you want to create
another coordinate frame with different data you should use
`~astropy.coordinates.BaseCoordinateFrame.realize_frame`, this is the safest way
to change the data in a frame as it creates a new frame with that data.
Creating a new frame can be relatively slow, however, particularly for scalar
coordinates. Hence some situations may require that data by changed in-place in
an already existing frame object. This modification can be done by
modifiying the values of the representation data as follows::

    >>> import astropy.units as u
    >>> from astropy.coordinates import SkyCoord
    >>> c = SkyCoord([1,2],[3,4], unit='deg')
    >>> c.data.lon[()] = [10, 20] * u.deg
    >>> c
    <SkyCoord (ICRS): (ra, dec) in deg
        [( 10.,  3.), ( 20.,  4.)]>


This changes the longitude values of the frame. Unfortunately, doing just this
introduces problems: `~astropy.coordinates.SkyCoord` and
`~astropy.coordinates.BaseCoordinateFrame` cache various kinds of information to
speed up some repeated operations. So we need to tell the cache that it should
be cleared so that it can be re-calculated from the new data. This can be
achieved by doing::

    >>> c.cache.clear()

It should be noted that the only way to modify the data in a frame is by using
the ``.data`` attribute directly and not the aliases for components on the frame
*i.e.* the following will not work:::

    >>> c.ra[()] = 20 * u.deg

This is because a different representation object is used when acessing the
aliased component names. If you wish to inspect the mapping between frame
attributes i.e. ``.ra`` and representation attributes i.e. ``.lon`` you can look
at the following dictionary.::

    >>> c.representation_component_names
    OrderedDict([('ra', 'lon'), ('dec', 'lat'), ('distance', 'distance')])