Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

howto: write out a cutout subset of an image with a new header #10

Closed
richardgmcmahon opened this issue Jun 29, 2013 · 2 comments
Closed

Comments

@richardgmcmahon
Copy link

Hi Erin,

I want to use use fitsio to create a 'FITs object' in memory of a cutout.

With pyfits I do this:

fh = pyfits.open(filename)
head = fh[ext].header.copy()
...
various header manipulations to work out the pixel ranges
and update the new header
...
cutout = fh[ext].data[ymin:ymax,xmin:xmax]
newfh = pyfits.PrimaryHDU(data=cutout,header=head)

with fitsio I do

fh=fitsio.FITS(filename)
head = fh[ext].read_header()
....
work out the pixel range I want and update the header
....
cutout=fh[ext][ymin:ymax,xmin:xmax]

how do I make a FITs object from cutout, newhead that I can then pass out of my
function?

Also how do I write this to a new fits file?

This would be a useful example of how to create and write out a image cutout.

Thanks, r.

@esheldon
Copy link
Owner

On Sat, Jun 29, 2013 at 5:36 PM, richardgmcmahon
notifications@github.comwrote:

Hi Erin,

I want to use use fitsio to create a 'FITs object' in memory of a cutout.

With pyfits I do this:

fh = pyfits.open(filename)
head = fh[ext].header.copy()
...
various header manipulations to work out the pixel ranges
and update the new header
...
cutout = fh[ext].data[ymin:ymax,xmin:xmax]
newfh = pyfits.PrimaryHDU(data=cutout,header=head)

with fitsio I do

fh=fitsio.FITS(filename)
head = fh[ext].read_header()
....
work out the pixel range I want and update the header
....
cutout=fh[ext][ymin:ymax,xmin:xmax]

how do I make a FITs object from cutout, newhead that I can then pass out
of my
function?

You get a numpy array from fh[ext][ymin:ymax,xmin:xmax]. The easiest
thing to do is just write that along with the header

fitsio.write(filename, cutout, header=head)

If the file already exists, that will add a new extension. You can add
clobber=True to over-write the file.

If you want to create a new FITS object directly, you can just do the
following

fits=FITS(filename, 'rw', clobber=clobber, **other_keywords)
fits.write(cutout, **other_keywords)

Note the FITS object represents file structure, not data in memory. I
intentionally separated that; you create your data structures separately
and then write them into the file. The documentation for this
functionality is available on the front page, but more readily using ? in
ipython or help().

Also how do I write this to a new fits file?

This would be a useful example of how to create and write out a image
cutout.

good idea!

-e

Thanks, r.


Reply to this email directly or view it on GitHubhttps://github.com//issues/10
.

Erin Scott Sheldon
Brookhaven National Laboratory erin dot sheldon at gmail dot com

@esheldon
Copy link
Owner

I'm going to close this since I haven't heard any complaints.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants