[AstroPy] Creating a mask file for .fits file

Daniel Evans d.f.evans at keele.ac.uk
Thu Feb 11 05:43:34 EST 2016


I would've thought the definitions for the masks would be:

mask_nan = np.isnan(hdulist_ex[i].data)
mask_zero = hdulist_ex[i].data == 0.0
mask_one = np.logical_not(np.logical_and(mask_nan, mask_zero))

Which replace your "if" statements in replace_mask(), which aren't actually
assigning any indices to the masks as far as I can see. Are hdulist_ex and
hdulist_lss are meant to the same variable? I can't find where hdulist_ex
is defined.

You also need to change the definition of replace_pix(), as you pass it
"mask", but the function refers to "mask_nan", "mask_zero", and "mask_one",
which it doesn't know about - the simplest method would be to pass all
three masks.

Regards,
Daniel

On 11 February 2016 at 10:26, Roberts, Michael <michael.roberts.15 at ucl.ac.uk
> wrote:

> 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:
>
>
> import os
>
> from astropy.io import fits
>
> import numpy as np
>
>
> #This is the main function.
>
> def main():
>
>
>     #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.
>
>         replace_mask(filename)
>
>
> #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.
>
> def replace_pix(frame,mask,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)
>
>     new_hdulist.append(new_hdu)
>
>
> #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.
>
> def replace_mask(filename):
>
>     #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[0].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.
>
>     if np.isnan(hdulist_ex[i].data):
>
>         mask_nan
>
>         replace_pix(hdulist_lss[i],mask_nan,new_hdulist_lss)
>
>     elif (hdulist_ex[i].data)=0:
>
>         mask_zero
>
>         replace_pix(hdulist_lss[i],mask_zero,new_hdulist_lss)
>
>     else:
>
>         mask_one
>
>         replace_pix(hdulist_lss[i],mask_one,new_hdulist_lss)
>
>
>     #Write the new hdulists to new images.
>
>     new_hdulist_lss.writeto(filename.replace("lss_new.img","lss_mask.img"
> ))
>
>
>
> #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__':
>
>     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.
>
>
> Many thanks,
>
>
> Michael Roberts
>
> _______________________________________________
> AstroPy mailing list
> AstroPy at scipy.org
> https://mail.scipy.org/mailman/listinfo/astropy
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/astropy/attachments/20160211/775db52c/attachment.html>


More information about the AstroPy mailing list