aboutsummaryrefslogtreecommitdiffstats
path: root/python
diff options
context:
space:
mode:
authorAaron LI <aly@aaronly.me>2017-09-07 16:00:31 +0800
committerAaron LI <aly@aaronly.me>2017-09-07 16:00:31 +0800
commit6ee82c3a41c4291fddb2f170f8bce7ce066f8617 (patch)
tree445846091f837abbed9c4beb900017ef226ca106 /python
parent6f55d5d90797aa975fe4d4732dfd54c8507402bd (diff)
downloadatoolbox-6ee82c3a41c4291fddb2f170f8bce7ce066f8617.tar.bz2
calc_radial_psd.py: support FITS image with NAXIS>2
e.g., radio images are often has 4 axes: X, Y, FREQ, STOKES
Diffstat (limited to 'python')
-rwxr-xr-xpython/calc_radial_psd.py54
1 files changed, 45 insertions, 9 deletions
diff --git a/python/calc_radial_psd.py b/python/calc_radial_psd.py
index 6bb1708..dcc772a 100755
--- a/python/calc_radial_psd.py
+++ b/python/calc_radial_psd.py
@@ -272,12 +272,51 @@ class AstroImage:
self.load_expmap(expmap, verbose=verbose)
self.load_bkgmap(bkgmap, verbose=verbose)
+ @staticmethod
+ def open_image(infile):
+ """
+ Open the slice image and return its header and 2D image data.
+
+ NOTE
+ ----
+ The input slice image may have following dimensions:
+ * NAXIS=2: [Y, X]
+ * NAXIS=3: [FREQ=1, Y, X]
+ * NAXIS=4: [STOKES=1, FREQ=1, Y, X]
+
+ NOTE
+ ----
+ Only open slice image that has only ONE frequency and ONE Stokes
+ parameter.
+
+ Returns
+ -------
+ header : `~astropy.io.fits.Header`
+ image : 2D `~numpy.ndarray`
+ The 2D [Y, X] image part of the slice image.
+ """
+ with fits.open(infile) as f:
+ header = f[0].header
+ data = f[0].data
+ if data.ndim == 2:
+ # NAXIS=2: [Y, X]
+ image = data
+ elif data.ndim == 3 and data.shape[0] == 1:
+ # NAXIS=3: [FREQ=1, Y, X]
+ image = data[0, :, :]
+ elif data.ndim == 4 and data.shape[0] == 1 and data.shape[1] == 1:
+ # NAXIS=4: [STOKES=1, FREQ=1, Y, X]
+ image = data[0, 0, :, :]
+ else:
+ raise ValueError("Slice '{0}' has invalid dimensions: {1}".format(
+ infile, data.shape))
+ return (header, image)
+
def load_image(self, image, verbose=False):
if verbose:
print("Loading image ... ", end="", flush=True)
- with fits.open(image) as imgfits:
- self.image = imgfits[0].data.astype(np.float)
- self.exposure = imgfits[0].header.get("EXPOSURE")
+ self.header, self.image = self.open_image(image)
+ self.exposure = self.header.get("EXPOSURE")
if verbose:
print("DONE", flush=True)
@@ -285,8 +324,7 @@ class AstroImage:
if expmap:
if verbose:
print("Loading exposure map ... ", end="", flush=True)
- with fits.open(expmap) as imgfits:
- self.expmap = imgfits[0].data.astype(np.float)
+ __, self.expmap = self.open_image(expmap)
if verbose:
print("DONE", flush=True)
@@ -294,9 +332,8 @@ class AstroImage:
if bkgmap:
if verbose:
print("Loading background map ... ", end="", flush=True)
- with fits.open(bkgmap) as imgfits:
- self.bkgmap = imgfits[0].data.astype(np.float)
- self.exposure_bkg = imgfits[0].header.get("EXPOSURE")
+ header, self.bkgmap = self.open_image(bkgmap)
+ self.exposure_bkg = header.get("EXPOSURE")
if verbose:
print("DONE", flush=True)
@@ -447,4 +484,3 @@ def main():
if __name__ == "__main__":
main()
-