<div dir="ltr"><div><div><div><div><div><div>I would've thought the definitions for the masks would be:<br><br></div>mask_nan = np.isnan(hdulist_ex[i].data)<br></div>mask_zero = hdulist_ex[i].data == 0.0<br></div>mask_one = np.logical_not(np.logical_and(mask_nan, mask_zero))<br><br></div><div>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.<br></div><div><br></div>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.<br><br></div>Regards,<br></div>Daniel<br></div><div class="gmail_extra"><br><div class="gmail_quote">On 11 February 2016 at 10:26, Roberts, Michael <span dir="ltr"><<a href="mailto:michael.roberts.15@ucl.ac.uk" target="_blank">michael.roberts.15@ucl.ac.uk</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">




<div dir="ltr">
<div style="font-size:12pt;color:#000000;background-color:#ffffff;font-family:Calibri,Arial,Helvetica,sans-serif">
<p>Dear astropy community,</p>
<p><br>
</p>
<p>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).</p>
<p><br>
</p>
<p>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.</p>
<p><br>
</p>
<p>My attempted script is:</p>
<p><br>
</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)">
import<span style="color:#000000"> os</span></p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo">
<span style="color:#bb2ca2">from</span> <a href="http://astropy.io" target="_blank">astropy.io</a>
<span style="color:#bb2ca2">import</span> fits</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo">
<span style="color:#bb2ca2">import</span> numpy
<span style="color:#bb2ca2">as</span> np</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">
<br>
</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(132,129,135)">
#This is the main function.</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo">
<span style="color:#bb2ca2">def</span> main():</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">
<br>
</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(132,129,135)">
<span style="color:#000000">    </span>
#For all files in the current directory:</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo">
    <span style="color:#bb2ca2">for</span> filename
<span style="color:#bb2ca2">in</span> sorted(os.listdir(os.getcwd())):</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">
        </p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(132,129,135)">
<span style="color:#000000">       
</span>#If the file is not an exposure map, skip this file and continue with the next file.</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo">
        <span style="color:#bb2ca2">
if</span> <span style="color:#bb2ca2">
not</span> filename.endswith(<span style="color:#b06fcf">"lss_new.img"</span>):
<span style="color:#bb2ca2">continue</span></p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">
        </p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(132,129,135)">
<span style="color:#000000">       
</span>#Replace all NaNs and 0s by 0s and all other values with 1 in the large scale sensitivity maps.</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo">
        replace_mask(filename)</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">
<br>
</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(132,129,135)">
#Function to replace all masked pixels by 0s.</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(132,129,135)">
#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.</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo">
<span style="color:#bb2ca2">def</span> replace_pix(frame,mask,new_hdulist):</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo">
    data = frame.data</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo">
    header = frame.header</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo">
    data[mask_nan]  = <span style="color:#272ad8">
0.0</span></p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo">
    data[mask_zero] = <span style="color:#272ad8">
0.0</span></p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo">
    data[mask_one]  = <span style="color:#272ad8">
1.0</span></p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo">
    new_hdu = fits.ImageHDU(data,header)</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo">
    new_hdulist.append(new_hdu)</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">
<br>
</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(132,129,135)">
#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.</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo">
<span style="color:#bb2ca2">def</span> replace_mask(filename):</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(132,129,135)">
<span style="color:#000000">    </span>
#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.</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(176,111,207)">
<span style="color:#000000">    </span>
<span style="color:#bb2ca2">print</span><span style="color:#000000">
</span>"All NaNs and Os will be replaced by 0s in "<span style="color:#000000"> + filename +
</span>" and all other values will be set to zero."</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(132,129,135)">
<span style="color:#000000">    </span>
#Open the exposure map, the corresponding sky and large scale sensitivity map and copy the primary headers (extension 0 of hdulist) to new hdulists.</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo">
    hdulist_lss = fits.open(filename)</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo">
    new_hdu_header_lss = fits.PrimaryHDU(header=hdulist_lss[<span style="color:#272ad8">0</span>].header)</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo">
    new_hdulist_lss = fits.HDUList([new_hdu_header_lss])</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">
    </p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(132,129,135)">
<span style="color:#000000">    </span>
#For all frames in the image: Create the mask and run the function replace_pix.</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo">
    <span style="color:#bb2ca2">if</span> np.isnan(hdulist_ex[i].data):</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo">
        mask_nan</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo">
        replace_pix(hdulist_lss[i],mask_nan,new_hdulist_lss)</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo">
    <span style="color:#bb2ca2">elif</span> (hdulist_ex[i].data)=<span style="color:#272ad8">0</span>:</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo">
        mask_zero</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo">
        replace_pix(hdulist_lss[i],mask_zero,new_hdulist_lss)</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo">
    <span style="color:#bb2ca2">else</span>:</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo">
        mask_one</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo">
        replace_pix(hdulist_lss[i],mask_one,new_hdulist_lss)</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">
<br>
</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(132,129,135)">
<span style="color:#000000">    </span>
#Write the new hdulists to new images.</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo">
    new_hdulist_lss.writeto(filename.replace(<span style="color:#b06fcf">"lss_new.img"</span>,<span style="color:#b06fcf">"lss_mask.img"</span>))</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">
    </p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(132,129,135)">
#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.</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(176,111,207)">
<span style="color:#000000">    </span>
<span style="color:#bb2ca2">print</span><span style="color:#000000">
</span>"All NaNs and 0s are replaced by 0s in "<span style="color:#000000"> + filename +
</span>" and all other corresponding pixels in the large scale sensitivity map are replaced by 1s."</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">
<br>
</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo">
<span style="color:#bb2ca2">if</span> __name__ ==
<span style="color:#272ad8">'__main__'</span>:</p>
<p style="margin-right:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo">
    main()</p>
<p><br>
</p>
<p>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.</p>
<p><br>
</p>
<p>Many thanks,</p>
<p><br>
</p>
<p>Michael Roberts </p>
</div>
</div>

<br>_______________________________________________<br>
AstroPy mailing list<br>
<a href="mailto:AstroPy@scipy.org">AstroPy@scipy.org</a><br>
<a href="https://mail.scipy.org/mailman/listinfo/astropy" rel="noreferrer" target="_blank">https://mail.scipy.org/mailman/listinfo/astropy</a><br>
<br></blockquote></div><br></div>