[AstroPy] Creating a mask file for .fits file
michael.roberts.15 at ucl.ac.uk
Thu Feb 11 05:26:25 EST 2016
Dear astropy community,
I'm looking to create a mask file for a Swift .img fits format file, which has one dimensionless primary header and anywhere between 1 and 9 image headers (with dimensions).
I am attempting to create a mask whereby if any value in the image is either a 0 or a NaN the new mask will have that corresponding pixel as 0, and for all other values the mask will have a value of 1.
My attempted script is:
from astropy.io import fits
import numpy as np
#This is the main function.
#For all files in the current directory:
for filename in sorted(os.listdir(os.getcwd())):
#If the file is not an exposure map, skip this file and continue with the next file.
if not filename.endswith("lss_new.img"): continue
#Replace all NaNs and 0s by 0s and all other values with 1 in the large scale sensitivity maps.
#Function to replace all masked pixels by 0s.
#Open the data and replace all masked pixels by 0s and all other values with 1. Save the header and the new data of the frame to a new hdu. Append this hdu to the new hdulist.
data = frame.data
header = frame.header
data[mask_nan] = 0.0
data[mask_zero] = 0.0
data[mask_one] = 1.0
new_hdu = fits.ImageHDU(data,header)
#Function to replace all NaNs in the exposure map by 0s and to replace the corresponding pixels in the sky and large scale sensitivity map by 0s.
#Print that all NaNs and Os will be replaced by 0s in the exposure map and that the corresponding pixels in the sky and large scale sensitivity map will also be replaced by 0s.
print "All NaNs and Os will be replaced by 0s in " + filename + " and all other values will be set to zero."
#Open the exposure map, the corresponding sky and large scale sensitivity map and copy the primary headers (extension 0 of hdulist) to new hdulists.
hdulist_lss = fits.open(filename)
new_hdu_header_lss = fits.PrimaryHDU(header=hdulist_lss.header)
new_hdulist_lss = fits.HDUList([new_hdu_header_lss])
#For all frames in the image: Create the mask and run the function replace_pix.
#Write the new hdulists to new images.
#Print that all NaNs are replaced by 0s in the exposure map and that the corresponding pixels in the sky and large scale sensitivity map are also replaced by 0s.
print "All NaNs and 0s are replaced by 0s in " + filename + " and all other corresponding pixels in the large scale sensitivity map are replaced by 1s."
if __name__ == '__main__':
Essentially, hopefully it is obvious, but it is failing around the if, elif, else statements (essentially I'm not defining the values correctly). If anyone would know the correct syntax for this I would be greatly appreciative.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the AstroPy