aboutsummaryrefslogtreecommitdiffstats
path: root/astro/fits
diff options
context:
space:
mode:
authorAaron LI <aly@aaronly.me>2017-09-10 23:51:03 +0800
committerAaron LI <aly@aaronly.me>2017-09-10 23:51:03 +0800
commit3d3eeb8e0154a50bfcb709e29f1c7723634aac45 (patch)
tree4507c6edc015f3fe0f8e0e7f49dee189ca407a1b /astro/fits
parent278ad947bca22994b580f863a1588cf93ee245ac (diff)
downloadatoolbox-3d3eeb8e0154a50bfcb709e29f1c7723634aac45.tar.bz2
Add two scripts: fitsimgadd.py & fitsimgsub.py
Diffstat (limited to 'astro/fits')
-rwxr-xr-xastro/fits/fitsimgadd.py57
-rwxr-xr-xastro/fits/fitsimgsub.py55
2 files changed, 112 insertions, 0 deletions
diff --git a/astro/fits/fitsimgadd.py b/astro/fits/fitsimgadd.py
new file mode 100755
index 0000000..52afb95
--- /dev/null
+++ b/astro/fits/fitsimgadd.py
@@ -0,0 +1,57 @@
+#!/usr/bin/env python3
+#
+# Copyright (c) Weitian LI <weitian@aaronly.me>
+# MIT license
+#
+
+"""
+Add multiple FITS images of same shape.
+"""
+
+import os
+import sys
+import argparse
+
+from astropy.io import fits
+
+
+def main():
+ parser = argparse.ArgumentParser(
+ description="Add two or more FITS images of same shape")
+ parser.add_argument("-C", "--clobber", action="store_true",
+ help="overwrite existing files")
+ parser.add_argument("-i", "--infile", nargs="+",
+ help=">=2 FITS images to be added")
+ parser.add_argument("-o", "--outfile", required=True,
+ help="filename of added FITS image")
+ args = parser.parse_args()
+
+ nimg = len(args.infile)
+ if nimg < 2:
+ raise RuntimeError("more than 2 input FITS images required")
+
+ if os.path.exists(args.outfile):
+ if args.clobber:
+ os.remove(args.outfile)
+ print("WARNING: removed existing output file: %s" % args.outfile)
+ else:
+ raise OSError("output file already exists: %s" % args.outfile)
+
+ with fits.open(args.infile[0]) as f:
+ image = f[0].data
+ header = f[0].header
+ print("Opened 1st image: %s" % args.infile[0])
+ print("Image shape: %s" % str(list(reversed(image.shape))))
+
+ for fn in args.infile[1:]:
+ print("Adding FITS image: %s ..." % fn)
+ image += fits.open(fn)[0].data
+
+ header.add_history(" ".join(sys.argv))
+ hdu = fits.PrimaryHDU(data=image, header=header)
+ hdu.writeto(args.outfile)
+ print("Wrote added FITS image to: %s" % args.outfile)
+
+
+if __name__ == "__main__":
+ main()
diff --git a/astro/fits/fitsimgsub.py b/astro/fits/fitsimgsub.py
new file mode 100755
index 0000000..cae0190
--- /dev/null
+++ b/astro/fits/fitsimgsub.py
@@ -0,0 +1,55 @@
+#!/usr/bin/env python3
+#
+# Copyright (c) Weitian LI <weitian@aaronly.me>
+# MIT license
+#
+
+"""
+Subtract a FITS image by one or more FITS images of same shape.
+"""
+
+import os
+import sys
+import argparse
+
+from astropy.io import fits
+
+
+def main():
+ parser = argparse.ArgumentParser(
+ description="Subtract a FITS image by >=1 images of same shape")
+ parser.add_argument("-C", "--clobber", action="store_true",
+ help="overwrite existing files")
+ parser.add_argument("-1", "--infile1", required=True,
+ help="the FITS image from which to be subtracted")
+ parser.add_argument("-2", "--infile2", nargs="+",
+ help="one or more FITS images to be subtracted by")
+ parser.add_argument("-o", "--outfile", required=True,
+ help="filename of subtracted FITS image")
+ args = parser.parse_args()
+
+ if os.path.exists(args.outfile):
+ if args.clobber:
+ os.remove(args.outfile)
+ print("WARNING: removed existing output file: %s" % args.outfile)
+ else:
+ raise OSError("output file already exists: %s" % args.outfile)
+
+ with fits.open(args.infile1) as f:
+ image = f[0].data
+ header = f[0].header
+ print("Opened FITS image: %s" % args.infile1)
+ print("Image shape: %s" % str(list(reversed(image.shape))))
+
+ for fn in args.infile2:
+ print("Subtracting FITS image: %s ..." % fn)
+ image -= fits.open(fn)[0].data
+
+ header.add_history(" ".join(sys.argv))
+ hdu = fits.PrimaryHDU(data=image, header=header)
+ hdu.writeto(args.outfile)
+ print("Wrote subtracted FITS image to: %s" % args.outfile)
+
+
+if __name__ == "__main__":
+ main()