Skip to content

Commit d2da8a7

Browse files
committed
Default/custom precision adaptions and docs updates
1 parent 766f850 commit d2da8a7

4 files changed

Lines changed: 98 additions & 47 deletions

File tree

‎astropy/io/ascii/mrt.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,10 @@ def write(self, lines):
472472
description=descrip)
473473
# Set default number of digits after decimal point for the
474474
# second values, and deg-min to (signed) 2-digit zero-padded integer.
475-
if name in ['RAs', 'DEs']:
476-
coord_col.format = '015.12f'
475+
if name == 'RAs':
476+
coord_col.format = '013.10f'
477+
elif name == 'DEs':
478+
coord_col.format = '012.9f'
477479
elif name == 'RAh':
478480
coord_col.format = '2d'
479481
elif name == 'DEd':
@@ -508,7 +510,7 @@ def write(self, lines):
508510
# representations to string valued columns. Those could either be types not
509511
# supported yet (e.g. 'helioprojective'), or already present and converted.
510512
# If there were any extra ``SkyCoord`` columns of one kind after the first one,
511-
# then they have been skipped the decomposition into their component columns.
513+
# then their decomposition into their component columns has been skipped.
512514
# This is done in order to not create duplicate component columns.
513515
# Explicit renaming of the extra coordinate component columns by appending some
514516
# suffix to their name, so as to distinguish them, is not yet implemented.

‎astropy/io/ascii/tests/test_cds.py‎

Lines changed: 70 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from astropy.coordinates import SkyCoord
1717
from astropy.time import Time
1818
from astropy.utils.data import get_pkg_data_filename
19+
from astropy.utils.exceptions import AstropyWarning
1920
from .common import assert_almost_equal
2021

2122

@@ -197,16 +198,16 @@ def test_write_byte_by_byte_for_masked_column():
197198
'41-42 I2 --- sameI [20] Description of sameI ',
198199
'44-45 I2 h RAh Right Ascension (hour) ',
199200
'47-48 I2 min RAm Right Ascension (minute) ',
200-
'50-64 F15.12 s RAs Right Ascension (second) ',
201-
' 66 A1 --- DE- Sign of Declination ',
202-
'67-68 I2 deg DEd Declination (degree) ',
203-
'70-71 I2 arcmin DEm Declination (arcmin) ',
204-
'73-87 F15.12 arcsec DEs Declination (arcsec) ',
201+
'50-62 F13.10 s RAs Right Ascension (second) ',
202+
' 64 A1 --- DE- Sign of Declination ',
203+
'65-66 I2 deg DEd Declination (degree) ',
204+
'68-69 I2 arcmin DEm Declination (arcmin) ',
205+
'71-82 F12.9 arcsec DEs Declination (arcsec) ',
205206
'--------------------------------------------------------------------------------',
206207
'Notes:',
207208
'--------------------------------------------------------------------------------',
208-
'HD81809 1e-07 22.25608 2e+00 67 5.0 20 22 02 15.450000000007 -61 39 34.599996000001',
209-
'HD103095 -3e+06 27.25000 -9e+34 -30 5.0 20 12 48 15.224407200005 +17 46 26.496624000004'],
209+
'HD81809 1e-07 22.25608 2e+00 67 5.0 20 22 02 15.4500000000 -61 39 34.599996000',
210+
'HD103095 -3e+06 27.25000 -9e+34 -30 5.0 20 12 48 15.2244072000 +17 46 26.496624000'],
210211

211212
positive_de=[
212213
'================================================================================',
@@ -223,16 +224,16 @@ def test_write_byte_by_byte_for_masked_column():
223224
'41-42 I2 --- sameI [20] Description of sameI ',
224225
'44-45 I2 h RAh Right Ascension (hour) ',
225226
'47-48 I2 min RAm Right Ascension (minute) ',
226-
'50-64 F15.12 s RAs Right Ascension (second) ',
227-
' 66 A1 --- DE- Sign of Declination ',
228-
'67-68 I2 deg DEd Declination (degree) ',
229-
'70-71 I2 arcmin DEm Declination (arcmin) ',
230-
'73-87 F15.12 arcsec DEs Declination (arcsec) ',
227+
'50-62 F13.10 s RAs Right Ascension (second) ',
228+
' 64 A1 --- DE- Sign of Declination ',
229+
'65-66 I2 deg DEd Declination (degree) ',
230+
'68-69 I2 arcmin DEm Declination (arcmin) ',
231+
'71-82 F12.9 arcsec DEs Declination (arcsec) ',
231232
'--------------------------------------------------------------------------------',
232233
'Notes:',
233234
'--------------------------------------------------------------------------------',
234-
'HD81809 1e-07 22.25608 2e+00 67 5.0 20 12 48 15.224407200005 +17 46 26.496624000004',
235-
'HD103095 -3e+06 27.25000 -9e+34 -30 5.0 20 12 48 15.224407200005 +17 46 26.496624000004'],
235+
'HD81809 1e-07 22.25608 2e+00 67 5.0 20 12 48 15.2244072000 +17 46 26.496624000',
236+
'HD103095 -3e+06 27.25000 -9e+34 -30 5.0 20 12 48 15.2244072000 +17 46 26.496624000'],
236237

237238
galactic=[
238239
'================================================================================',
@@ -339,11 +340,11 @@ def test_write_byte_by_byte_bytes_col_format():
339340
' 51 I1 --- singleByteCol [2] Description of singleByteCol ',
340341
'53-54 I2 h RAh Right Ascension (hour) ',
341342
'56-57 I2 min RAm Right Ascension (minute) ',
342-
'59-73 F15.12 s RAs Right Ascension (second) ',
343-
' 75 A1 --- DE- Sign of Declination ',
344-
'76-77 I2 deg DEd Declination (degree) ',
345-
'79-80 I2 arcmin DEm Declination (arcmin) ',
346-
'82-96 F15.12 arcsec DEs Declination (arcsec) ',
343+
'59-71 F13.10 s RAs Right Ascension (second) ',
344+
' 73 A1 --- DE- Sign of Declination ',
345+
'74-75 I2 deg DEd Declination (degree) ',
346+
'77-78 I2 arcmin DEm Declination (arcmin) ',
347+
'80-91 F12.9 arcsec DEs Declination (arcsec) ',
347348
'--------------------------------------------------------------------------------']
348349
t = ascii.read(test_dat)
349350
t.add_column([5.0, 5.0], name='sameF')
@@ -452,17 +453,17 @@ def test_write_extra_skycoord_cols():
452453
1- 7 A7 --- name Description of name
453454
9-10 I2 h RAh Right Ascension (hour)
454455
12-13 I2 min RAm Right Ascension (minute)
455-
15-29 F15.12 s RAs Right Ascension (second)
456-
31 A1 --- DE- Sign of Declination
457-
32-33 I2 deg DEd Declination (degree)
458-
35-36 I2 arcmin DEm Declination (arcmin)
459-
38-52 F15.12 arcsec DEs Declination (arcsec)
460-
54-67 A14 --- coord2 Description of coord2
456+
15-27 F13.10 s RAs Right Ascension (second)
457+
29 A1 --- DE- Sign of Declination
458+
30-31 I2 deg DEd Declination (degree)
459+
33-34 I2 arcmin DEm Declination (arcmin)
460+
36-47 F12.9 arcsec DEs Declination (arcsec)
461+
49-62 A14 --- coord2 Description of coord2
461462
--------------------------------------------------------------------------------
462463
Notes:
463464
--------------------------------------------------------------------------------
464-
HD4760 0 49 39.900000000000 +06 24 07.999200000000 12.4163 6.407
465-
HD81809 22 02 15.450000000007 -61 39 34.599996000001 330.564 -61.66
465+
HD4760 0 49 39.9000000000 +06 24 07.999200000 12.4163 6.407
466+
HD81809 22 02 15.4500000000 -61 39 34.599996000 330.564 -61.66
466467
''' # noqa: W291
467468
t = Table()
468469
t['name'] = ['HD4760', 'HD81809']
@@ -483,3 +484,45 @@ def test_write_extra_skycoord_cols():
483484
assert a[:18] == b[:18]
484485
assert a[30:42] == b[30:42]
485486
assert_almost_equal(np.fromstring(a[2:], sep=' '), np.fromstring(b[2:], sep=' '))
487+
488+
489+
def test_write_skycoord_with_format():
490+
"""
491+
Tests output with custom setting for ``SkyCoord`` (second) columns.
492+
"""
493+
exp_output = '''\
494+
================================================================================
495+
Byte-by-byte Description of file: table.dat
496+
--------------------------------------------------------------------------------
497+
Bytes Format Units Label Explanations
498+
--------------------------------------------------------------------------------
499+
1- 7 A7 --- name Description of name
500+
9-10 I2 h RAh Right Ascension (hour)
501+
12-13 I2 min RAm Right Ascension (minute)
502+
15-19 F5.2 s RAs Right Ascension (second)
503+
21 A1 --- DE- Sign of Declination
504+
22-23 I2 deg DEd Declination (degree)
505+
25-26 I2 arcmin DEm Declination (arcmin)
506+
28-31 F4.1 arcsec DEs Declination (arcsec)
507+
--------------------------------------------------------------------------------
508+
Notes:
509+
--------------------------------------------------------------------------------
510+
HD4760 0 49 39.90 +06 24 08.0
511+
HD81809 22 02 15.45 -61 39 34.6
512+
''' # noqa: W291
513+
t = Table()
514+
t['name'] = ['HD4760', 'HD81809']
515+
t['coord'] = SkyCoord([12.41625, 330.564375], [6.402222, -61.65961111], unit=u.deg)
516+
517+
out = StringIO()
518+
# This will raise a warning because `formats` is checked before the writer creating the
519+
# final list of columns is called.
520+
with pytest.warns(AstropyWarning, match=r"The key.s. {'[RD][AE]s', '[RD][AE]s'} specified in "
521+
r"the formats argument do not match a column name."):
522+
t.write(out, format='ascii.mrt', formats={'RAs': '05.2f', 'DEs': '04.1f'})
523+
524+
lines = out.getvalue().splitlines()
525+
i_bbb = lines.index('=' * 80)
526+
lines = lines[i_bbb:] # Select Byte-By-Byte section and following lines.
527+
# Check the written table.
528+
assert lines == exp_output.splitlines()

‎astropy/io/ascii/ui.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ def write(table, output=None, format=None, Writer=None, fast_writer=True, *,
825825

826826
if diff_format_with_names:
827827
warnings.warn(
828-
'The keys {} specified in the formats argument does not match a column name.'
828+
'The key(s) {} specified in the formats argument do not match a column name.'
829829
.format(diff_format_with_names), AstropyWarning)
830830

831831
if table.has_mixin_columns:

‎docs/io/ascii/write.rst‎

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -308,12 +308,18 @@ into their latitude(``ELAT``/``GLAT``) and longitude components (``ELON``/``GLAT
308308
The original table remains accessible as such, while the file is written from a modified
309309
copy of the table. The new coordinate component columns are appended to the end of the table.
310310

311-
It should be noted that the precision of the latitude and longitude and ``sec``, ``arcsec``
312-
columns is set at a default number of 12 digits after the decimal. Since these component
313-
columns are obtained by dividing up ``SkyCoord`` columns, this precision is internally set,
314-
and cannot be changed. For all other columns though, the format can be set by passing the
315-
``formats`` keyword to the ``write`` function or by setting the ``format`` attribute of
316-
individual columns.
311+
It should be noted that the default precision of the latitude, longitude and seconds (of arc)
312+
columns is set at a default number of 12, 10 and 9 digits after the decimal for ``deg``, ``sec``
313+
and ``arcsec`` values, repsectively. This default is set to match a machine precision of 1e-15
314+
relative to the original ``SkyCoord`` those columns were extracted from.
315+
As all other columns, the format can be expliclty set by passing the ``formats`` keyword to the
316+
``write`` function or by setting the ``format`` attribute of individual columns (the latter
317+
will only work for columns that are not decomposed).
318+
To customize the number of significant digits, presicions should therefore be specified in the
319+
``formats`` dictionary for the *output* column names, such as
320+
``formats={'RAs': '07.4f', 'DEs': '06.3f'}`` or ``formats={'GLAT': '+10.6f', 'GLON': '9.6f'}``
321+
for milliarcsecond accuracy. Note that the forms with leading zeros for the seconds and
322+
including the sign for latitudes are recommended for better consistency and readability.
317323

318324
The following code illustrates the above.
319325

@@ -344,16 +350,16 @@ After execution, the contents of ``coords_cols.dat`` will be::
344350
51-53 I3 s Cadence [100] Description of Cadence
345351
55-56 I2 h RAh Right Ascension (hour)
346352
58-59 I2 min RAm Right Ascension (minute)
347-
61-75 F15.12 s RAs Right Ascension (second)
348-
77 A1 --- DE- Sign of Declination
349-
78-79 I2 deg DEd Declination (degree)
350-
81-82 I2 arcmin DEm Declination (arcmin)
351-
84-98 F15.12 arcsec DEs Declination (arcsec)
353+
61-73 F13.10 s RAs Right Ascension (second)
354+
75 A1 --- DE- Sign of Declination
355+
76-77 I2 deg DEd Declination (degree)
356+
79-80 I2 arcmin DEm Declination (arcmin)
357+
82-93 F12.9 arcsec DEs Declination (arcsec)
352358
--------------------------------------------------------------------------------
353359
Notes:
354360
--------------------------------------------------------------------------------
355-
ASASSN-15lh 2.87819e-09 0.0250 1e-10 2019.0 100 22 02 15.450000000007 -61 39 34.599996000001
356-
ASASSN-14li 2.55935e-08 0.0188 2.044 4e+03 2019.0 100 12 48 15.224407200005 +17 46 26.496624000004
361+
ASASSN-15lh 2.87819e-09 0.0250 1e-10 2019.0 100 22 02 15.4500000000 -61 39 34.599996000
362+
ASASSN-14li 2.55935e-08 0.0188 2.044 4e+03 2019.0 100 12 48 15.2244072000 +17 46 26.496624000
357363

358364
And the file ``ecliptic_cols.dat`` will look like::
359365

@@ -431,6 +437,6 @@ The following example shows a similar situation, using the option to send the ou
431437
in ``Tables``. For tables with more than one coordinate column of a given kind
432438
(e.g. equatorial, galactic or ecliptic), only the first found coordinate column
433439
will be decomposed into its component columns, and the rest of the coordinate
434-
columns will be converted to string columns. Thus users should take care that the
435-
additional coordinate columns are dealt with (e.g. by converting them into unique
436-
``float``-valued columns) before using ``SkyCoord`` methods.
440+
columns of the same type will be converted to string columns. Thus users should take
441+
care that the additional coordinate columns are dealt with (e.g. by converting them
442+
to unique ``float``-valued columns) before using ``SkyCoord`` methods.

0 commit comments

Comments
 (0)