Results 1 to 9 of 9

Thread: astropy.fits help: know any discussion sites? do you have experience?

  1. #1
    Join Date
    Feb 2010
    Posts
    628

    astropy.fits help: know any discussion sites? do you have experience?

    astropy is a very useful package for use in Python; in particular, its .fits is great for handling FITS files (extensions, headers, data ...) robustly and easily.

    However, it also has its peculiarities, especially from my perspective as a self-taught newbie. For example, try as I might searching the official documentation and various online tutorials, I have been unable to find a simple, robust way to copy cards from one header to another (not card values, nor card comments; those I can do); nor how to copy a header and use it as the header of a new FITS file. Data manipulation, on the other hand, has caused me very few wasted hours.

    I have not been able to find an online forum where I could ask questions about astropy, other than StackExchange.

    Do you know of any such forum?

    Are you, perhaps, comfortable enough with astropy.fits that I could pick your brain?

  2. #2
    Join Date
    Mar 2010
    Location
    United Kingdom
    Posts
    6,965
    Did you get an answer to this in the end?

    I've not used astropy much (so I didn't reply before in case someone more familiar with it was going to) but it looks like you should be able to do the latter of these things using the astropy.io.fits writeto routine. I just tried a quick test with:

    data = numpy.zeros((1024,1024),dtype='float32')
    hdr = astropy.io.fits.getheader("foo.fits")
    astropy.io.fits.writeto("bar.fits", data, header=hdr)

    This wrote the empty array 'data' to bar.fits using the foo.fits header. I ran it on the NASA example file FOCx38i0101t_c0f.fits and everything seemed happy, I got a new FITS and a quick check implied the header was fine. Not quite identical - for some reason there were small differences in the comments the FITS reader adds to the keywords (but not in the values).

    As for the cards - doesn't astropy handle this via astropy.io.fits.getval and setval? i.e.

    # get the keyword
    val = astropy.io.fits.getval("foo.fits", "DATE-OBS")
    # update DATE-OBS or add it if it is not in bar.fits
    astropy.io.fits.setval("bar.fits", "DATE-OBS", value=val)

    If this is noddy stuff and I have misunderstood what you are asking I apologise in advance!

  3. #3
    Join Date
    Feb 2003
    Location
    Depew, NY
    Posts
    11,467
    This may be dumb, but have you tried asking on Twitter? @astropy? I don't understand the appeal of Twitter, but it seems a lot of people use it.
    Solfe

  4. #4
    Join Date
    Feb 2010
    Posts
    628
    Quote Originally Posted by Shaula View Post
    Did you get an answer to this in the end?
    Thanks Shaula.

    I got a pointer, from a similar post in a different forum.

    I've not used astropy much (so I didn't reply before in case someone more familiar with it was going to) but it looks like you should be able to do the latter of these things using the astropy.io.fits writeto routine. I just tried a quick test with:

    data = numpy.zeros((1024,1024),dtype='float32')
    hdr = astropy.io.fits.getheader("foo.fits")
    astropy.io.fits.writeto("bar.fits", data, header=hdr)

    This wrote the empty array 'data' to bar.fits using the foo.fits header. I ran it on the NASA example file FOCx38i0101t_c0f.fits and everything seemed happy, I got a new FITS and a quick check implied the header was fine. Not quite identical - for some reason there were small differences in the comments the FITS reader adds to the keywords (but not in the values).

    As for the cards - doesn't astropy handle this via astropy.io.fits.getval and setval? i.e.

    # get the keyword
    val = astropy.io.fits.getval("foo.fits", "DATE-OBS")
    # update DATE-OBS or add it if it is not in bar.fits
    astropy.io.fits.setval("bar.fits", "DATE-OBS", value=val)

    If this is noddy stuff and I have misunderstood what you are asking I apologise in advance!
    Yes, setval (etc) works fine, as does writeto.

    Here's one thing I do not know how to do: suppose I have two FITS, say f1.fits and f2.fits. I want to copy the entire header of f1 to f2. Naively, like this (yeah, extension, etc missing):

    f1 = fits.open("f1.fits")
    f1h = f1.header
    f2 = fits.open("f2.fits", mode='update')
    f2h = f1h
    f2.flush()

    Of course, no one in their right mind would do this blindly! So how to do so for the subset of keywords which should be OK, without going through them all, one by one?

    Also, I can create a card image (I think), and add it in a particular slot (or to the end) ... but I don't know how to get a card image from an existing header (f1 in the example above).

    And yeah, I'm almost missing something that's incredibly simple (due to my weird understanding of how all this works) ...

  5. #5
    Join Date
    Feb 2010
    Posts
    628
    Quote Originally Posted by Solfe View Post
    This may be dumb, but have you tried asking on Twitter? @astropy? I don't understand the appeal of Twitter, but it seems a lot of people use it.
    I do not have a Twitter account, and never will (ditto Facebook). I understand that such a thing could be valuable, but for me it's already too much like 1984 ...

  6. #6
    Join Date
    Feb 2003
    Location
    Depew, NY
    Posts
    11,467
    Quote Originally Posted by Jean Tate View Post
    I do not have a Twitter account, and never will (ditto Facebook). I understand that such a thing could be valuable, but for me it's already too much like 1984 ...
    Agreed. Facebook is sort of marginal. Twitter looks an awful lot like "Thunderdome!" to me.
    Solfe

  7. #7
    Join Date
    Mar 2010
    Location
    United Kingdom
    Posts
    6,965
    Quote Originally Posted by Jean Tate View Post
    Here's one thing I do not know how to do: suppose I have two FITS, say f1.fits and f2.fits. I want to copy the entire header of f1 to f2. Naively, like this (yeah, extension, etc missing):

    f1 = fits.open("f1.fits")
    f1h = f1.header
    f2 = fits.open("f2.fits", mode='update')
    f2h = f1h
    f2.flush()

    Of course, no one in their right mind would do this blindly! So how to do so for the subset of keywords which should be OK, without going through them all, one by one?

    Also, I can create a card image (I think), and add it in a particular slot (or to the end) ... but I don't know how to get a card image from an existing header (f1 in the example above).

    And yeah, I'm almost missing something that's incredibly simple (due to my weird understanding of how all this works) ...
    I'll be honest - I'd pull the data out of FITS2 and write a new file as I did in the example. I'm very nervous of irrevocably altering files! There is an update_header() method on the HDU class which will update some keywords to match the file if you have left a mismatch but it only handles a few cases.

    Do you have a fixed list of keywords you want to copy across? If you you could just use the writeto() method to create a file with the barest minimum header, then use the getval() and setval() methods to copy just the keywords you want to the new file.

  8. #8
    Join Date
    Sep 2006
    Posts
    1,787
    If the two images have the same size, then you can try this really ugly trick:

    1) make a copy of f1. Call it f1a

    2) multiply all pixel values in f1a by zero

    3) add f2 to f1a

    Voila! The file f1a should now have the FITS header of f1, but the data of f2.

  9. #9
    Join Date
    Feb 2010
    Posts
    628
    Thanks!

    Shaula: that's pretty much what I ended up doing.

    StupendousMan: Cool! I will definitely try that next time I have data arrays of the same shape! Unfortunately, more common for me is different shapes; for example, I rotate an array, and the result is a bigger array, but apart from the size (and orientation!), all the keywords will have the same values (yeah, "size" keywords in astronomical FITS are more than just NAXIS1 etc).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •