[AstroPy] AstroPy Digest, Vol 112, Issue 5

Roberts, Michael michael.roberts.15 at ucl.ac.uk
Sun Jan 17 10:47:23 EST 2016


Hi Evert,

Update: I have implemented your suggestions and it is now working correctly.

Many (many!) thanks,

Michael

________________________________________
From: Roberts, Michael
Sent: Sunday, January 17, 2016 12:44 PM
To: astropy at scipy.org
Cc: evert.rol at gmail.com
Subject: Re: AstroPy Digest, Vol 112, Issue 5

Hi Evert,

I've attempted to try and implement your suggestions, but I'm getting a few syntax errors.

Could you run through the exact changes you would make for me. I'm a little lost with this...

Kindest regards,

Michael
________________________________________
From: AstroPy <astropy-bounces at scipy.org> on behalf of astropy-request at scipy.org <astropy-request at scipy.org>
Sent: Sunday, January 17, 2016 12:00 PM
To: astropy at scipy.org
Subject: AstroPy Digest, Vol 112, Issue 5

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. Re: IOError: [Errno 2] No such file or directory when     using
      satrapy fits moduel in IPython environment (Evert Rol)
   2. Re: AstroPy Digest, Vol 112, Issue 4 (Roberts, Michael)


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

Message: 1
Date: Sun, 17 Jan 2016 22:03:48 +1100
From: Evert Rol <evert.rol at gmail.com>
To: Astronomical Python mailing list <astropy at scipy.org>
Subject: Re: [AstroPy] IOError: [Errno 2] No such file or directory
        when    using satrapy fits moduel in IPython environment
Message-ID: <D2C882FE-1860-4971-8B61-E9BF76E1D6E3 at gmail.com>
Content-Type: text/plain; charset=iso-8859-1

Michael, if I understand your problem correctly, then you shouldn't attempt to open the ...sk_corrected file (for reading). That's what you're doing now, hence Python can't find the file.

Instead, create a HDUList (or simply a PrimaryHDU, store the corrected data in that HDUList (as you do in your loop), and then use the .writeto() method as you do at the bottom, with the sk_corrected filename.
In fact, you seem to already do this, in the two lines below the faulty line.

There is no need to try and create a new FITS file first: HDUList.writeto can do that all in one go.

It's a bit unclear to me why you want an ...sk_corrected file, and then at the bottom you write three other (...new.img) files, which seem to be the actually NaN corrected files.
Perhaps you don't really need the ...sk_corrected file?

Judging from the loop, perhaps you're trying to make copies of the original file, so that they are guaranteed to have the same amount of HDUs?
Instead, you can iterate over the original HDUList and append the corrected HDUs.
That would give something like:

with fits.open(filename) as hdulist:
  hdulist_sk = fits.HDUList([fits.PrimaryHDU(hdulist[0].header)])
  for hdu in hdulist[1:]:
    mask = np.isnan(hdu.data)
    tmphdu = fits.ImageHDU()
    replace_pix(hdu, mask, tmphdu)
    hdulist_sk.append(tmphdu.copy())
hdulist_sk.writeto(filename.replace("ex", "sk_corrected")

I'm taking a few shortcuts above, using the with statement and iterating directly over the hdulist.
Note if you let the function replace_pix() create its own, new, corrected, HDU, and let it return it, that line and the two lines around it can become:
   hdulist_sk.append(replace_pix(hdu, mask))
Now, you'll need the .copy() method.

But do check if the above code is what you're trying to achieve.

Cheers,

  Evert


>
> Dear Astropy community,
>
> I'm having a little problem with a script that I am using. The parts of the script which is giving me the problems are as follows:
>
> #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_nan(filename):
>
>
> #Print that all NaNs 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 will be replaced by 0s in " + filename + " and the corresponding pixels in the sky and large scale sensitivity map will also be replaced by 0s."
>
>
> #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_ex
> = fits.open(filename)
>
>     new_hdu_header_ex
> = fits.PrimaryHDU(header=hdulist_ex[0].header)
>
>     new_hdulist_ex
> = fits.HDUList([new_hdu_header_ex])
>
>     hdulist_sk
> = fits.open(filename.replace("ex","sk_corrected"))
>
>     new_hdu_header_sk
> = fits.PrimaryHDU(header=hdulist_sk[0].header)
>
>     new_hdulist_sk
> = fits.HDUList([new_hdu_header_sk])
>
>     hdulist_lss
> = fits.open(filename.replace("ex","lss_m"))
>
>     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.
>
>
> for i in range(1,len(hdulist_ex)):
>
>         mask
> = np.isnan(hdulist_ex[i].data)
>
>         replace_pix
> (hdulist_ex[i],mask,new_hdulist_ex)
>
>         replace_pix
> (hdulist_sk[i],mask,new_hdulist_sk)
>
>         replace_pix
> (hdulist_lss[i],mask,new_hdulist_lss)
>
>
>
> #Write the new hdulists to new images.
>
>     new_hdulist_ex
> .writeto(filename.replace(".img","_new.img"))
>
>     new_hdulist_sk
> .writeto(filename.replace("ex.img","sk_new.img"))
>
>     new_hdulist_lss
> .writeto(filename.replace("ex.img","lss_new.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 are replaced by 0s in " + filename + " and the corresponding pixels in the sky and large scale sensitivity map are also replaced by 0s."
>
> When running:
>
> replace_nan("/Users/.../sw00031048001uw1_ex.img")
> (where I have dotted out my path for convenience.) it is failing on (traceback) is hdulist_sk = fits.open(filename.replace("ex","sk_corrected"))
>
> The error is simply
>
> IOError: [Errno 2] No such file or directory: '/Users/.../sw00031048001uw1_sk_corrected.img'
> But this is the file I am attempting to create by replacing '/Users/.../sw00031048001uw1_ex.img'
>
> I'm within the iPython development environment (if that helps, or if that is relevant). I'm guessing at this stage that maybe I don't have permissions to be messing around with files from the iPython console? Or I need some extra arguments for this to work...
>
> Any suggestions would be warmly welcomed.
>
> Many thanks,
>
> Michael Roberts
>
>
>
>
> _______________________________________________
> AstroPy mailing list
> AstroPy at scipy.org
> https://mail.scipy.org/mailman/listinfo/astropy



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

Message: 2
Date: Sun, 17 Jan 2016 11:16:35 +0000
From: "Roberts, Michael" <michael.roberts.15 at ucl.ac.uk>
To: "astropy at scipy.org" <astropy at scipy.org>
Subject: Re: [AstroPy] AstroPy Digest, Vol 112, Issue 4
Message-ID:
        <HE1PR01MB1225B35B637FF929CDE9662C90CF0 at HE1PR01MB1225.eurprd01.prod.exchangelabs.com>

Content-Type: text/plain; charset="iso-8859-1"

Hi Domink,

Not sure I follow you on that. Could you give an example of what you think I may need to do?

I was hoping that this would work as it was given to me as a working script....surely it must be permissions for the files I am trying to modify?

Michael

________________________________________
From: AstroPy <astropy-bounces at scipy.org> on behalf of astropy-request at scipy.org <astropy-request at scipy.org>
Sent: Sunday, January 17, 2016 11:03 AM
To: astropy at scipy.org
Subject: AstroPy Digest, Vol 112, Issue 4

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. Re: IOError: [Errno 2] No such file or directory when using
      satrapy fits moduel in IPython environment (Dominik Klaes)


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

Message: 1
Date: Sun, 17 Jan 2016 12:03:01 +0100
From: Dominik Klaes <dklaes at astro.uni-bonn.de>
To: Astronomical Python mailing list <astropy at scipy.org>
Subject: Re: [AstroPy] IOError: [Errno 2] No such file or directory
        when using satrapy fits moduel in IPython environment
Message-ID:
        <CAAc-CLQ7v+7NXBRY7eQgRh-0CYuRTT+otsoBH6K7rG4wq45+Ww at mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Hi Michael,

I think what you are trying is to open this file before you create it with
fits.open(). If you create a FITS file from scratch, which is I think what
you want to do, then you don't need this command, just as you do in the end
hdu.writeto().

Cheers,

Dominik

2016-01-17 11:43 GMT+01:00 Roberts, Michael <michael.roberts.15 at ucl.ac.uk>:

> Dear Astropy community,
>
>
> I'm having a little problem with a script that I am using. The parts of
> the script which is giving me the problems are as follows:
>
>
> #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_nan(filename):
>     #Print that all NaNs 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 will be replaced by 0s in " + filename + " and the corresponding pixels in the sky and large scale sensitivity map will also be replaced by 0s."
>     #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_ex = fits.open(filename)
>     new_hdu_header_ex = fits.PrimaryHDU(header=hdulist_ex[0].header)
>     new_hdulist_ex = fits.HDUList([new_hdu_header_ex])
>     hdulist_sk = fits.open(filename.replace("ex","sk_corrected"))
>     new_hdu_header_sk = fits.PrimaryHDU(header=hdulist_sk[0].header)
>     new_hdulist_sk = fits.HDUList([new_hdu_header_sk])
>     hdulist_lss = fits.open(filename.replace("ex","lss_m"))
>     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.
>     for i in range(1,len(hdulist_ex)):
>         mask = np.isnan(hdulist_ex[i].data)
>         replace_pix(hdulist_ex[i],mask,new_hdulist_ex)
>         replace_pix(hdulist_sk[i],mask,new_hdulist_sk)
>         replace_pix(hdulist_lss[i],mask,new_hdulist_lss)
>
>     #Write the new hdulists to new images.
>     new_hdulist_ex.writeto(filename.replace(".img","_new.img"))
>     new_hdulist_sk.writeto(filename.replace("ex.img","sk_new.img"))
>     new_hdulist_lss.writeto(filename.replace("ex.img","lss_new.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 are replaced by 0s in " + filename + " and the corresponding pixels in the sky and large scale sensitivity map are also replaced by 0s."
>
>
> When running:
>
>
> replace_nan("/Users/.../sw00031048001uw1_ex.img")
>
> (where I have dotted out my path for convenience.) it is failing on
> (traceback) is hdulist_sk = fits.open(filename.replace("ex","sk_corrected"
> ))
>
>
> The error is simply
>
>
> IOError: [Errno 2] No such file or directory: '/Users/.../sw00031048001uw1_sk_corrected.img'
>
> But this is the file I am attempting to create by replacing
> '/Users/.../sw00031048001uw1_ex.img'
>
>
> I'm within the iPython development environment (if that helps, or if that
> is relevant). I'm guessing at this stage that maybe I don't have
> permissions to be messing around with files from the iPython console? Or I
> need some extra arguments for this to work...
>
>
> Any suggestions would be warmly welcomed.
>
>
> Many thanks,
>
>
> Michael Roberts
>
>
>
>
>
>
> _______________________________________________
> AstroPy mailing list
> AstroPy at scipy.org
> https://mail.scipy.org/mailman/listinfo/astropy
>
>


--
Dominik Klaes
Argelander-Institut f?r Astronomie
Room 2.027a
Auf dem H?gel 71
53121 Bonn

Telefon: 0228/73-5773
E-Mail: dklaes at astro.uni-bonn.de <dklaes at astro.uni-bonn.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.scipy.org/pipermail/astropy/attachments/20160117/99a706c7/attachment.html>

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

Subject: Digest Footer

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


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

End of AstroPy Digest, Vol 112, Issue 4
***************************************


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

Subject: Digest Footer

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


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

End of AstroPy Digest, Vol 112, Issue 5
***************************************



More information about the AstroPy mailing list