Commit f685dc7d authored by Leo Pound Singer's avatar Leo Pound Singer

Work around https://github.com/astropy/astropy/pull/5720

Original: 02a0ac97baf7379f1818f86ca2233456660151ed
parent 088db024
...@@ -75,6 +75,13 @@ from astropy.table import Table ...@@ -75,6 +75,13 @@ from astropy.table import Table
from ..bayestar import moc from ..bayestar import moc
# FIXME: Remove this after all Astropy monkeypatches are obsolete.
import astropy
import distutils.version
astropy_version = distutils.version.StrictVersion(astropy.__version__)
def gps_to_iso8601(gps_time): def gps_to_iso8601(gps_time):
""" """
Convert a floating-point GPS time in seconds to an ISO 8601 date string. Convert a floating-point GPS time in seconds to an ISO 8601 date string.
...@@ -269,6 +276,7 @@ def write_sky_map(filename, m, **kwargs): ...@@ -269,6 +276,7 @@ def write_sky_map(filename, m, **kwargs):
TFIELDS = 1 / number of table fields TFIELDS = 1 / number of table fields
TTYPE1 = 'PROB ' TTYPE1 = 'PROB '
TFORM1 = 'D ' TFORM1 = 'D '
TUNIT1 = 'pix-1 '
PIXTYPE = 'HEALPIX ' / HEALPIX pixelisation PIXTYPE = 'HEALPIX ' / HEALPIX pixelisation
ORDERING= 'NESTED ' / Pixel ordering scheme: RING, NESTED, or NUNIQ ORDERING= 'NESTED ' / Pixel ordering scheme: RING, NESTED, or NUNIQ
COORDSYS= 'C ' / Ecliptic, Galactic or Celestial (equatorial) COORDSYS= 'C ' / Ecliptic, Galactic or Celestial (equatorial)
...@@ -278,7 +286,6 @@ def write_sky_map(filename, m, **kwargs): ...@@ -278,7 +286,6 @@ def write_sky_map(filename, m, **kwargs):
VCSSTAT = '...: ...' / Software version control status VCSSTAT = '...: ...' / Software version control status
VCSID = '...' / Software git commit hash VCSID = '...' / Software git commit hash
DATE-BLD= '...' / Software build date DATE-BLD= '...' / Software build date
TUNIT1 = 'pix-1 '
>>> uniq = moc.nest2uniq(np.uint8(order), np.arange(npix, dtype=np.uint64)) >>> uniq = moc.nest2uniq(np.uint8(order), np.arange(npix, dtype=np.uint64))
>>> probdensity = prob / hp.nside2pixarea(nside) >>> probdensity = prob / hp.nside2pixarea(nside)
...@@ -301,6 +308,7 @@ def write_sky_map(filename, m, **kwargs): ...@@ -301,6 +308,7 @@ def write_sky_map(filename, m, **kwargs):
TZERO1 = 9223372036854775808 TZERO1 = 9223372036854775808
TTYPE2 = 'PROBDENSITY' TTYPE2 = 'PROBDENSITY'
TFORM2 = 'D ' TFORM2 = 'D '
TUNIT2 = 'sr-1 '
PIXTYPE = 'HEALPIX ' / HEALPIX pixelisation PIXTYPE = 'HEALPIX ' / HEALPIX pixelisation
ORDERING= 'NUNIQ ' / Pixel ordering scheme: RING, NESTED, or NUNIQ ORDERING= 'NUNIQ ' / Pixel ordering scheme: RING, NESTED, or NUNIQ
COORDSYS= 'C ' / Ecliptic, Galactic or Celestial (equatorial) COORDSYS= 'C ' / Ecliptic, Galactic or Celestial (equatorial)
...@@ -309,7 +317,6 @@ def write_sky_map(filename, m, **kwargs): ...@@ -309,7 +317,6 @@ def write_sky_map(filename, m, **kwargs):
VCSSTAT = '...: ...' / Software version control status VCSSTAT = '...: ...' / Software version control status
VCSID = '...' / Software git commit hash VCSID = '...' / Software git commit hash
DATE-BLD= '...' / Software build date DATE-BLD= '...' / Software build date
TUNIT2 = 'sr-1 '
""" """
if isinstance(m, Table) or (isinstance(m, np.ndarray) and m.dtype.names): if isinstance(m, Table) or (isinstance(m, np.ndarray) and m.dtype.names):
...@@ -359,28 +366,35 @@ def write_sky_map(filename, m, **kwargs): ...@@ -359,28 +366,35 @@ def write_sky_map(filename, m, **kwargs):
if not col.unit: if not col.unit:
col.unit = default_unit col.unit = default_unit
try: if astropy_version >= '1.3.1':
# FIXME: astropy.io.fits.table_to_hdu was added in astropy 1.2. hdu = fits.table_to_hdu(m)
hdu.header.extend(extra_header)
hdulist = fits.HDUList([fits.PrimaryHDU(), hdu])
hdulist.writeto(filename, clobber=True)
else:
# FIXME: This code path works around a number of issues with older
# versions of Astropy. Remove it once we drop support for
# astropy < 1.3.1.
#
# astropy.io.fits.table_to_hdu was added in astropy 1.2.
# We must currently support astropy >= 1.1.1 on the LIGO Data Grid's # We must currently support astropy >= 1.1.1 on the LIGO Data Grid's
# Scientific Linux 7 computing clusters. # Scientific Linux 7 computing clusters.
fits.table_to_hdu #
except AttributeError: # With some old versions of astropy that we still have to
# FIXME: With some old versions of astropy that we still have to
# support, the astropy.table.Table.write method did not support the # support, the astropy.table.Table.write method did not support the
# clobber argument. So we have to manually delete the file first so # clobber argument. So we have to manually delete the file first so
# that astropy.io.fits does not complain that the file exists. # that astropy.io.fits does not complain that the file exists.
#
# Also this works around https://github.com/astropy/astropy/pull/5720,
# which was fixed in astropy 1.3.1.
from ..bayestar.command import rm_f from ..bayestar.command import rm_f
rm_f(filename) rm_f(filename)
m.write(filename, format='fits') m.write(filename, format='fits')
hdulist = fits.open(filename) hdulist = fits.open(filename)
_, hdu = hdulist _, hdu = hdulist
hdu.header.extend(extra_header) hdu.header.extend(extra_header)
hdulist.writeto(filename, clobber=True) hdulist.writeto(filename, clobber=True)
else:
hdu = fits.table_to_hdu(m)
hdu.header.extend(extra_header)
hdulist = fits.HDUList([fits.PrimaryHDU(), hdu])
hdulist.writeto(filename, clobber=True)
def read_sky_map(filename, nest=False, distances=False, moc=False): def read_sky_map(filename, nest=False, distances=False, moc=False):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment