Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python3
- import numpy as np
- import argparse
- import os
- from astropy.io import fits
- # program to be used for panstarrs data
- # Load your FITS file
- def main():
- parser = argparse.ArgumentParser(description="resizes image to cutout nan regions and removes nan pixels. It optionally converts the variance image to sigma and resizes it")
- parser.add_argument("ImageFile", help="Fits image file")
- parser.add_argument("-var","--variance", type=str, help="the variance image file")
- args = parser.parse_args()
- image_file = args.ImageFile
- variance_file = args.variance
- if not os.path.exists(image_file):
- print ('{} image filename does not exist!'.format(image_file))
- sys.exit(1)
- if variance_file:
- if not os.path.exists(variance_file):
- print ('{} image filename does not exist!'.format(variance_file))
- sys.exit(1)
- hdul = fits.open(image_file)
- # Access the CompImageHDU (assuming it's the first extension)
- # Extract the data
- data = hdul[0].data
- valid_rows = ~np.isnan(data).all(axis=1)
- valid_cols = ~np.isnan(data).all(axis=0)
- cleaned_data = data[valid_rows][:, valid_cols]
- masknan = np.isnan(cleaned_data)
- cleaned_data[masknan] = 30000
- hdul[0].data= cleaned_data
- hdul.writeto(image_file.replace("fits", "new.fits"), overwrite=True)
- hdul.close()
- if variance_file:
- hdus = fits.open(variance_file)
- var_data = hdus[0].data
- var_data[np.isnan(var_data)] = 1e7
- sig_data = np.sqrt(var_data)
- cleaned_sigma = sig_data[valid_rows][:, valid_cols]
- hdu_new = fits.PrimaryHDU(cleaned_sigma, hdus[0].header)
- hdu_new.header["IMTYPE"] = "sigma"
- hdu_new.header["BUNIT"] = "It is unkknown"
- hdu_new.writeto(variance_file.replace("fits", "sigma.fits"), overwrite=True)
- hdus.close()
- print("Done")
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement