简体   繁体   中英

astropy.fits: Manipulating image data from a fits Table? (e.g., 3072R x 2C)

I'm currently having a little issue with a fits file. The data is in table format, a format I haven't previously used. I'm a python user, and rely heavily on astropy.fits to manipulate fits images. A quick output of the info gives:

No.    Name         Type      Cards   Dimensions   Format
0    PRIMARY     PrimaryHDU      60   ()              
1                BinTableHDU     29   3072R x 2C   [1024E, 1024E]

The header for the BinTableHDU is as follows:

XTENSION= 'BINTABLE'           /Written by IDL:  Mon Jun 22 23:28:21 2015       
BITPIX  =                    8 /                                                
NAXIS   =                    2 /Binary table                                    
NAXIS1  =                 8192 /Number of bytes per row                         
NAXIS2  =                 3072 /Number of rows                                  
PCOUNT  =                    0 /Random parameter count                          
GCOUNT  =                    1 /Group count                                     
TFIELDS =                    2 /Number of columns                               
TFORM1  = '1024E   '           /Real*4 (floating point)                         
TFORM2  = '1024E   '           /Real*4 (floating point)                         
TTYPE1  = 'COUNT_RATE'         /                                                
TUNIT1  = '1e-6cts/s/arcmin^2' /                                                
TTYPE2  = 'UNCERTAINTY'        /                                                
TUNIT2  = '1e-6cts/s/arcmin^2' /
HISTORY g000m90r1b120pm.fits created on 10/08/97. PI channel range:  8: 19      
PIXTYPE = 'HEALPIX '           / HEALPIX pixelisation                           
ORDERING= 'NESTED  '           / Pixel ordering scheme, either RING or NESTED   
NSIDE   =                  512 / Healpix resolution parameter                   
NPIX    =              3145728 / Total number of pixels                         
OBJECT  = 'FULLSKY '           / Sky coverage, either FULLSKY or PARTIAL        
FIRSTPIX=                    0 / First pixel # (0 based)                        
LASTPIX =              3145727 / Last pixel # (zero based)                      
INDXSCHM= 'IMPLICIT'           / indexing : IMPLICIT or EXPLICIT                
GRAIN   =                    0 / GRAIN = 0: No index,                           
COMMENT         GRAIN =1: 1 pixel index for each pixel,                         
COMMENT         GRAIN >1: 1 pixel index for Grain consecutive pixels            
BAD_DATA=         -1.63750E+30 / Sentinel value given to bad pixels             
COORDSYS= 'G       '           / Pixelization coordinate system                 
COMMENT         G = Galactic, E = ecliptic, C = celestial = equatorial          
END

I'd like to access the fits image which is stored within the TTYPE labeled 'COUNT-RATE', and then have this in a format with which I can then add to other count-rate arrays with the same dimensions.

I started with my usual prodcedure for opening a fits file:

hdulist_RASS_SXRB_R1 = fits.open('/Users/.../RASS_SXRB_R1.fits')
hdulist_RASS_SXRB_R1.info()
image_XRAY_SKYVIEW_R1 = hdulist_RASS_SXRB_R1[1].data
image_XRAY_SKYVIEW_R1 = numpy.array(image_XRAY_SKYVIEW_R1)
image_XRAY_SKYVIEW_header_R1 = hdulist_RASS_SXRB_R1[1].header

But this is coming back with IndexError: too many indices for array . I've had a look at accessing table data in the astropy documentation here ( Accessing data stored as a table in a multi-extension FITS (MEF) file )

If anyone has a tried and tested method for accessing such images from a fits table I'd be very grateful! Many thanks.

I can't be sure without seeing the full traceback but I think the exception you're getting is from this:

image_XRAY_SKYVIEW_R1 = numpy.array(image_XRAY_SKYVIEW_R1)

There's no reason to manually wrap numpy.array() around the array. It's already a Numpy array. But in this case it's a structured array (see http://docs.scipy.org/doc/numpy/user/basics.rec.html ).

@Andromedae93's answer is right one. But also for general documentation on this see: http://docs.astropy.org/en/stable/io/fits/index.html#working-with-table-data

However, the way you're working (which is fine for images) of manually calling fits.open , accessing the .data attribute of the HDU, etc. is fairly low level, and Numpy structured arrays are good at representing tables, but not great for manipulating them.

You're better off generally using Astropy's higher-level Table interface. A FITS table can be read directly into an Astropy Table object with Table.read() : http://docs.astropy.org/en/stable/io/unified.html#fits

The only reason the same thing doesn't exist for FITS images is there's no a generic "Image" class yet.

I used astropy.io.fits during my internship in Astrophysics and this is my process to open file .fits and make some operations :

# Opening the .fits file which is named SMASH.fits
field = fits.open(SMASH.fits)         

# Data fits reading  
tbdata = field[1].data 

Now, with this kind of method, tbdata is a numpy.array and you can make lots of things.

For example, if you have data like :

ID, Name, Object
1, HD 1527, Star
2, HD 7836, Star
3, NGC 6739, Galaxy

If you want to print data along one condition :

Data_name = tbdata['Name']

You will get :

HD 1527
HD 7836
NGC 6739

I don't know what do you want exactly with your data, but I can help you ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM