Skip to content

Instantly share code, notes, and snippets.

@Sunmish
Created June 14, 2021 09:23
Show Gist options
  • Save Sunmish/cc3edfe911c379fc390e415e5dde21ad to your computer and use it in GitHub Desktop.
Save Sunmish/cc3edfe911c379fc390e415e5dde21ad to your computer and use it in GitHub Desktop.
Clip a FITS image using a different FITS image (e.g. of RMS noise or sensitivity).
#! /usr/bin/env python
from astropy.io import fits
import numpy as np
from argparse import ArgumentParser
def clip(image, clip_image, clip_level, outname,
fill_value=0):
"""
"""
f = fits.open(image)
g = fits.open(clip_image)
f[0].data[np.where(g[0].data < clip_level)] = 0
fits.writeto(outname, f[0].data, f[0].header, clobber=True)
def __main__():
"""
"""
ps = ArgumentParser()
ps.add_argument("image", help="image to clip")
ps.add_argument("clip_image", help="image to clip by")
ps.add_argument("-c", "--clip_level", required=True, type=float, help="clip level")
ps.add_argument("-o", "--outname", type=str, default="output.fits", help="output file name. This OVERWRITES existing image of the same name.")
ps.add_argument("-f", "--fill_value", type=float, help="clipped values replaced with this value")
ps.add_argument("-F", "--nan_fill_value", action="store_true", help="use NaN instead of fill_value")
args = ps.parse_args()
if args.nan_fill_value:
args.fill_value = np.nan
clip(image=args.image,
clip_image=clip_image,
clip_level=args.clip_level,
outname=args.outname,
fill_value=args.fill_value)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment