From 276abb7e6c5213fe4f04f5778b18d65e8054eb05 Mon Sep 17 00:00:00 2001
From: Aaron LI <aly@aaronly.me>
Date: Mon, 23 Jul 2018 16:05:51 +0800
Subject: astro/fitscube.py: Add 'div' (divide) sub-command

---
 astro/fits/fitscube.py | 43 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

(limited to 'astro')

diff --git a/astro/fits/fitscube.py b/astro/fits/fitscube.py
index fbdaaf8..720e169 100755
--- a/astro/fits/fitscube.py
+++ b/astro/fits/fitscube.py
@@ -384,6 +384,33 @@ def cmd_sub(args):
     print("Saved FITS cube: %s" % args.outfile)
 
 
+def cmd_div(args):
+    """
+    Sub-command: "div", divide one FITS cube by another one
+    """
+    if not args.clobber and os.path.exists(args.outfile):
+        raise FileExistsError("output file already exists: %s" % args.outfile)
+
+    cube = FITSCube(args.infile)
+    print("Data cube unit: %s" % cube.unit)
+    print("Image/slice size: %dx%d" % (cube.width, cube.height))
+    print("Number of slices: %d" % cube.nslice)
+
+    cube2 = FITSCube(args.infile2)
+    assert (cube.unit, cube.zunit) == (cube2.unit, cube2.zunit)
+    print("Dividing cube %s ..." % args.infile2)
+    with np.errstate(divide='warn'):
+        cube.data = cube.data / cube2.data
+
+    if args.fill_value:
+        print("Filling invalid data with: %s" % args.fill_value)
+        cube.data[~np.isfinite(cube.data)] = float(args.fill_value)
+
+    print("Saving FITS cube ...")
+    cube.write(args.outfile, clobber=args.clobber)
+    print("Saved FITS cube: %s" % args.outfile)
+
+
 def cmd_calibrate(args):
     """
     Sub-command: "calibrate", calibrate the z-axis slice/channel responses
@@ -613,6 +640,22 @@ def main():
                             help="another input FITS cube as the subtrahend")
     parser_sub.set_defaults(func=cmd_sub)
 
+    # sub-command: "divide"
+    parser_div = subparsers.add_parser(
+        "div", aliases=["divide"],
+        help="divide one FITS cube by another one")
+    parser_div.add_argument("-C", "--clobber", action="store_true",
+                            help="overwrite existing output file")
+    parser_div.add_argument("-F", "--fill-value", dest="fill_value",
+                            help="value to fill the invalid elements")
+    parser_div.add_argument("-o", "--outfile", required=True,
+                            help="output FITS cube filename")
+    parser_div.add_argument("-i", "--infile", required=True,
+                            help="input FITS cube as the dividend")
+    parser_div.add_argument("-I", "--infile2", required=True,
+                            help="another input FITS cube as the divisor")
+    parser_div.set_defaults(func=cmd_div)
+
     # sub-command: "calibrate"
     parser_cal = subparsers.add_parser(
         "cal", aliases=["calibrate"],
-- 
cgit v1.2.2