Advertisement
canorve

cut NaNs from PanStarrs images and converts Variance image to Sigma Image

Jun 30th, 2024
689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.00 KB | Science | 0 0
  1. #! /usr/bin/env python3
  2.  
  3. import numpy as np
  4. import argparse
  5. import os
  6.  
  7. from astropy.io import fits
  8.  
  9. # program to be used for panstarrs data
  10. # Load your FITS file
  11. def main():
  12.  
  13.     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")
  14.     parser.add_argument("ImageFile", help="Fits image file")
  15.    
  16.     parser.add_argument("-var","--variance", type=str, help="the variance image file")
  17.  
  18.     args = parser.parse_args()
  19.     image_file = args.ImageFile
  20.     variance_file = args.variance
  21.  
  22.  
  23.     if not os.path.exists(image_file):
  24.         print ('{} image filename does not exist!'.format(image_file))
  25.         sys.exit(1)
  26.  
  27.     if variance_file:
  28.         if not os.path.exists(variance_file):
  29.             print ('{} image filename does not exist!'.format(variance_file))
  30.             sys.exit(1)
  31.  
  32.  
  33.  
  34.     hdul = fits.open(image_file)
  35.  
  36.     # Access the CompImageHDU (assuming it's the first extension)
  37.  
  38.     # Extract the data
  39.     data = hdul[0].data
  40.  
  41.     valid_rows = ~np.isnan(data).all(axis=1)
  42.     valid_cols = ~np.isnan(data).all(axis=0)
  43.  
  44.     cleaned_data = data[valid_rows][:, valid_cols]
  45.  
  46.     masknan = np.isnan(cleaned_data)
  47.     cleaned_data[masknan] = 30000
  48.  
  49.     hdul[0].data= cleaned_data
  50.     hdul.writeto(image_file.replace("fits", "new.fits"), overwrite=True)
  51.     hdul.close()
  52.  
  53.  
  54.  
  55.     if variance_file:
  56.  
  57.         hdus = fits.open(variance_file)
  58.         var_data = hdus[0].data
  59.  
  60.         var_data[np.isnan(var_data)] = 1e7
  61.         sig_data = np.sqrt(var_data)
  62.  
  63.         cleaned_sigma = sig_data[valid_rows][:, valid_cols]
  64.        
  65.  
  66.         hdu_new = fits.PrimaryHDU(cleaned_sigma, hdus[0].header)
  67.         hdu_new.header["IMTYPE"] = "sigma"
  68.         hdu_new.header["BUNIT"] = "It is unkknown"
  69.         hdu_new.writeto(variance_file.replace("fits", "sigma.fits"), overwrite=True)
  70.  
  71.         hdus.close()
  72.  
  73.  
  74.  
  75.     print("Done")
  76.  
  77.  
  78.  
  79.  
  80. if __name__ == '__main__':
  81.     main()
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement