aboutsummaryrefslogtreecommitdiffstats
path: root/fg21sim/utils/fits.py
diff options
context:
space:
mode:
authorAaron LI <aaronly.me@outlook.com>2016-10-10 00:29:31 +0800
committerAaron LI <aaronly.me@outlook.com>2016-10-10 00:29:31 +0800
commitc1c5b3d5e899f8e385c90aae4ff881ecad3c7422 (patch)
treef8e1a627bb437ba04ef78558507f532f31181763 /fg21sim/utils/fits.py
parentcbbd8eec608ac6d1b037e33ecbe0b76c4bfa6ccc (diff)
downloadfg21sim-c1c5b3d5e899f8e385c90aae4ff881ecad3c7422.tar.bz2
utils: Preseve the dtype when read/write FITS files
* utils/fits.py: hack the dtype to ignore the byteorder (FITS data use big endianness, e.g., dtype(">f4")) * utils/healpix.py: explicit convert the dtype and log the dtype * bin/healpix2hpx, bin/hpx2healpix: remove the --float argument * other minor fixes/updates
Diffstat (limited to 'fg21sim/utils/fits.py')
-rw-r--r--fg21sim/utils/fits.py19
1 files changed, 11 insertions, 8 deletions
diff --git a/fg21sim/utils/fits.py b/fg21sim/utils/fits.py
index 365ed7e..96b5f58 100644
--- a/fg21sim/utils/fits.py
+++ b/fg21sim/utils/fits.py
@@ -53,8 +53,10 @@ def read_fits_healpix(filename):
if isinstance(filename, fits.BinTableHDU):
hdu = filename
else:
- hdu = fits.open(filename)[0]
- dtype = hdu.data.dtype
+ # Read the first extended table
+ hdu = fits.open(filename)[1]
+ # Hack to ignore the dtype byteorder, use native endianness
+ dtype = np.dtype(hdu.data.field(0).dtype.type)
header = hdu.header
data = hp.read_map(hdu, nest=False, verbose=False)
return (data.astype(dtype), header)
@@ -94,9 +96,12 @@ def write_fits_healpix(filename, hpmap, header=None, clobber=False):
- This function (currently) only implement the very basic feature of
the `healpy.write_map()`.
"""
- hpmap = np.array(hpmap)
+ hpmap = np.asarray(hpmap)
if hpmap.ndim != 1:
raise ValueError("Invalid HEALPix data: only support 1D array")
+ # Hack to ignore the dtype byteorder, use native endianness
+ dtype = np.dtype(hpmap.dtype.type)
+ hpmap = hpmap.astype(dtype)
#
npix = hpmap.size
nside = int((npix / 12) ** 0.5)
@@ -120,13 +125,11 @@ def write_fits_healpix(filename, hpmap, header=None, clobber=False):
#
hdr["EXTNAME"] = ("HEALPIX", "Name of the binary table extension")
hdr["CREATOR"] = (__name__, "File creator")
- hdr["DATE"] = (
- datetime.now(timezone.utc).astimezone().isoformat(),
- "File creation date"
- )
+ hdr["DATE"] = (datetime.now(timezone.utc).astimezone().isoformat(),
+ "File creation date")
# merge user-provided header
if header is not None:
- hdr.update(fits.Header(header))
+ hdr.extend(fits.Header(header))
#
hdu = fits.BinTableHDU.from_columns([
fits.Column(name="I", array=hpmap, format=colfmt)