1616from astropy .coordinates import SkyCoord
1717from astropy .time import Time
1818from astropy .utils .data import get_pkg_data_filename
19+ from astropy .utils .exceptions import AstropyWarning
1920from .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)
45445512-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--------------------------------------------------------------------------------
462463Notes:
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 ()
0 commit comments