File: formatting.rst

package info (click to toggle)
astropy 5.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 41,972 kB
  • sloc: python: 219,331; ansic: 147,297; javascript: 13,556; lex: 8,496; sh: 3,319; xml: 1,622; makefile: 185
file content (47 lines) | stat: -rw-r--r-- 1,645 bytes parent folder | download | duplicates (3)
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
45
46
47
Formatting Coordinate Strings
*****************************

.. todo: @taldcroft should change this to start with a discussion of SkyCoord's capabilities

Getting a string representation of a coordinate is most powerfully
approached by treating the components (e.g., RA and Dec) separately.

Examples
--------

..
  EXAMPLE START
  Getting and Formatting String Representations of Coordinates

To get the string representation of a coordinate::

  >>> from astropy.coordinates import ICRS
  >>> from astropy import units as u
  >>> coo = ICRS(187.70592*u.degree, 12.39112*u.degree)
  >>> str(coo.ra) + ' ' + str(coo.dec)
  '187d42m21.312s 12d23m28.032s'

To get better control over the formatting, you can use the angles'
:meth:`~astropy.coordinates.Angle.to_string` method (see :doc:`angles` for
more). For example::

  >>> rahmsstr = coo.ra.to_string(u.hour)
  >>> str(rahmsstr)
  '12h30m49.4208s'
  >>> decdmsstr = coo.dec.to_string(u.degree, alwayssign=True)
  >>> str(decdmsstr)
  '+12d23m28.032s'
  >>> rahmsstr + ' ' + decdmsstr
  u'12h30m49.4208s +12d23m28.032s'

You can also use Python's `format` string method to create more complex
string expressions, such as IAU-style coordinates or even full sentences::

  >>> (f'SDSS J{coo.ra.to_string(unit=u.hourangle, sep="", precision=2, pad=True)}'
  ...  f'{coo.dec.to_string(sep="", precision=2, alwayssign=True, pad=True)}')
  'SDSS J123049.42+122328.03'
  >>> f'The galaxy M87, at an RA of {coo.ra.hour:.2f} hours and Dec of {coo.dec.deg:.1f} degrees, has an impressive jet.'
  'The galaxy M87, at an RA of 12.51 hours and Dec of 12.4 degrees, has an impressive jet.'

..
  EXAMPLE END