aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron LI <aaronly.me@outlook.com>2016-10-09 22:31:36 +0800
committerAaron LI <aaronly.me@outlook.com>2016-10-09 22:31:36 +0800
commit4ce3c0b5ce2bd4b2d2bb89eaae669a4e3eba6662 (patch)
tree038ea66cd76dddc16a31ac54d3513617accc5554
parente2d5281609750da823c229856359f8edcb77bd37 (diff)
downloadfg21sim-4ce3c0b5ce2bd4b2d2bb89eaae669a4e3eba6662.tar.bz2
utils: Add function "read_fits_healpix()"
This function wraps on the `healpy.read_map()`, but reset the data array to its original dtype in FITS file, as well as return the FITS header in `astropy.io.fits.Header` object.
-rw-r--r--fg21sim/utils/__init__.py2
-rw-r--r--fg21sim/utils/fits.py29
2 files changed, 30 insertions, 1 deletions
diff --git a/fg21sim/utils/__init__.py b/fg21sim/utils/__init__.py
index b6cf3fd..97243a7 100644
--- a/fg21sim/utils/__init__.py
+++ b/fg21sim/utils/__init__.py
@@ -1,6 +1,6 @@
# Copyright (c) 2016 Weitian LI <liweitianux@live.com>
# MIT license
-from .fits import write_fits_healpix
+from .fits import read_fits_healpix, write_fits_healpix
from .healpix import healpix2hpx, hpx2healpix
from .logging import setup_logging
diff --git a/fg21sim/utils/fits.py b/fg21sim/utils/fits.py
index e865528..be5dc68 100644
--- a/fg21sim/utils/fits.py
+++ b/fg21sim/utils/fits.py
@@ -9,6 +9,7 @@ from datetime import datetime, timezone
import numpy as np
from astropy.io import fits
+import healpy as hp
# Reference:
@@ -26,6 +27,34 @@ FITS_COLUMN_FORMATS = {
}
+def read_fits_healpix(filename):
+ """Read the HEALPix map from a FITS file to 1D array in *RING* ordering.
+
+ Parameters
+ ----------
+ filename : str
+ Filename of the HEALPix FITS file
+
+ Returns
+ -------
+ data : 1D `~numpy.ndarray`
+ HEALPix data in *RING* ordering
+ header : `~astropy.io.fits.Header`
+ Header of the input FITS file
+
+ NOTE
+ ----
+ This function wraps on `healpy.read_map()`, but set the data type of
+ data array to its original value as in FITS file, as well as return
+ FITS header as `~astropy.io.fits.Header` instance.
+ """
+ hdu = fits.open(filename)[0]
+ dtype = hdu.data.dtype
+ header = hdu.header
+ data = hp.read_map(hdu, nest=False, verbose=False)
+ return (data.astype(dtype), header)
+
+
def write_fits_healpix(filename, hpmap, header=None, clobber=False):
"""Write the HEALPix map to a FITS file with proper header as well
as the user-provided header.