[AstroPy] AstroPy Digest, Vol 113, Issue 3

Roberts, Michael michael.roberts.15 at ucl.ac.uk
Thu Feb 11 06:58:03 EST 2016


Hi Daniel,

You're quite right, frustratingly I have included some errors in my script. Here is the offending section again:

    for i in range(1,len(hdulist_lss)):
        if hdulist_lss[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)

Essentially, my syntax is wrong at ^ in: hdulist_lss[i].data^=0 , and I will be honest: I don't know what the syntax should be. Essentially I want to say if a pixel in the image is 0, then data[mask_zero] = 0.0 (I know this seems odd setting all zeroes to equal zero but I required an else statement which then would say for all other values, 10, 34, 12120, 2344, whatever would then be equal to 1.)

Hopefully that makes sense, or if you have a better idea I would be more than welcome to hear it, of course.

Michael 

________________________________________
From: AstroPy <astropy-bounces at scipy.org> on behalf of astropy-request at scipy.org <astropy-request at scipy.org>
Sent: Thursday, February 11, 2016 11:40 AM
To: astropy at scipy.org
Subject: AstroPy Digest, Vol 113, Issue 3

Send AstroPy mailing list submissions to
        astropy at scipy.org

To subscribe or unsubscribe via the World Wide Web, visit
        https://mail.scipy.org/mailman/listinfo/astropy
or, via email, send a message with subject or body 'help' to
        astropy-request at scipy.org

You can reach the person managing the list at
        astropy-owner at scipy.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of AstroPy digest..."


Today's Topics:

   1. slicing (Stefano Covino)
   2. Re: Creating a mask file for .fits file (Daniel Evans)
   3. Re: slicing (Aldcroft, Thomas)


----------------------------------------------------------------------

Message: 1
Date: Thu, 11 Feb 2016 11:42:42 +0100
From: Stefano Covino <stefano.covino at brera.inaf.it>
To: Astronomical Python mailing list <astropy at scipy.org>
Subject: [AstroPy] slicing
Message-ID: <6A5CBBAC-2684-4CA9-94E3-9A2C92E8ADB7 at brera.inaf.it>
Content-Type: text/plain; charset=utf-8

Dear friends,

let?s assume we have a data array read from a FITS file:

data = getdata(?file.fits?)

I need to do some computations on a subset of it, for instance:

datf = dat[bb-size:bb+size,aa-size:aa+size]

where aa and bb are x,y positions and size a fixed number (e.g. 10).



So, the question. Is it possible that aa and bb are not simple integers but lists or arrays of integers?


Thanks,
        Stefano

------------------------------

Message: 2
Date: Thu, 11 Feb 2016 10:43:34 +0000
From: Daniel Evans <d.f.evans at keele.ac.uk>
To: Astronomical Python mailing list <astropy at scipy.org>
Subject: Re: [AstroPy] Creating a mask file for .fits file
Message-ID:
        <CANnaQaZzPwhEq3ua6+A0pPkqaJe1KnnKTYuKRMh7h_GjzD88PQ at mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

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: <https://mail.scipy.org/pipermail/astropy/attachments/20160211/775db52c/attachment-0001.html>

------------------------------

Message: 3
Date: Thu, 11 Feb 2016 06:39:45 -0500
From: "Aldcroft, Thomas" <aldcroft at head.cfa.harvard.edu>
To: Astronomical Python mailing list <astropy at scipy.org>
Subject: Re: [AstroPy] slicing
Message-ID:
        <CAMtEP6wAvALpCMZmVwqiXbDpgoKT31UhpLA1+BQKNfri00NHcw at mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Thu, Feb 11, 2016 at 5:42 AM, Stefano Covino <
stefano.covino at brera.inaf.it> wrote:

> Dear friends,
>
> let?s assume we have a data array read from a FITS file:
>
> data = getdata(?file.fits?)
>
> I need to do some computations on a subset of it, for instance:
>
> datf = dat[bb-size:bb+size,aa-size:aa+size]
>
> where aa and bb are x,y positions and size a fixed number (e.g. 10).
>
>
>
> So, the question. Is it possible that aa and bb are not simple integers
> but lists or arrays of integers?
>

Hi Stefano,

You can get a good idea of what is possible with array indexing by starting
with the numpy documentation:

  http://docs.scipy.org/doc/numpy-1.10.1/user/basics.indexing.html

- Tom


>
>
> Thanks,
>         Stefano
> _______________________________________________
> AstroPy mailing list
> AstroPy at scipy.org
> https://mail.scipy.org/mailman/listinfo/astropy
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.scipy.org/pipermail/astropy/attachments/20160211/dcc8ec8d/attachment.html>

------------------------------

Subject: Digest Footer

_______________________________________________
AstroPy mailing list
AstroPy at scipy.org
https://mail.scipy.org/mailman/listinfo/astropy


------------------------------

End of AstroPy Digest, Vol 113, Issue 3
***************************************


More information about the AstroPy mailing list