rmsynthesis.fits

A collection of small utilities to interface with pyfits.

rmsynthesis.fits.get_data_offset_length(fits_name)

Returns the offset from the beginning of the file as well as the length of the data block, in bytes, for the first Header Data Unit (HDU). The length includes padding to the next 2880 byte boundary.

Parameters

fits_name : string
File name from which to read the header.

Returns

A tuple of integers (offset, data_length).

Raises

IOError:
If fits_name cannot be opened.

Example

>>> get_data_offset_length('testdata/Q_Fthinsource.fits')
(2880, 4000320)
rmsynthesis.fits.get_header(fits_name)

Read only the header in FITS file fits_name. Internally simply calls pyfits.getheader().

Parameters

fits_name : string
File name from which to read the header.

Returns

A pyfits.Header instance.

Raises

OSError
If fits_name cannot be opened.

Example

>>> print(get_header('testdata/Q_Fthinsource.fits'))
SIMPLE  =                    T / Written by IDL:  Tue Sep 28 22:42:54 2010      
BITPIX  =                  -32 / Number of bits per data pixel                  
NAXIS   =                    3 / Number of data axes                            
NAXIS1  =                  100 /                                                
NAXIS2  =                  100 /                                                
NAXIS3  =                  100 /                                                
DATE    = '2010-09-28'         / Creation UTC (CCCC-MM-DD) date of FITS header  
COMMENT FITS (Flexible Image Transport System) format is defined in 'Astronomy  
COMMENT and Astrophysics', volume 376, page 359; bibcode 2001A&A...376..359H    
CTYPE1  = 'X       '           /                                                
CRVAL1  =                    0 /                                                
CDELT1  =          1.30000E+06 /                                                
CRPIX1  =                    0 /                                                
CUNIT1  = 'Hz      '           /                                                
CTYPE2  = 'Y       '           /                                                
CRVAL2  =                    0 /                                                
CRVAL3  =          1.10000E+08 /                                                
POL     = 'Q       '           /                                                
>>> print(get_header('testdata/non-existent.fits'))
Traceback (most recent call last):
...
IOError: [Errno 2] No such file or directory: 'testdata/non-existent.fits'
rmsynthesis.fits.get_header_data(fits_name)

Returns the first HDU of fits_name as a (header, data) tuple.

Parameters

fits_name : string
File name from which to read the header.

Returns

A (pyfits.Header, numpy.ndarray) tuple.

Raises

IOError
If fits_name cannot be opened.

Example

>>> hdr, data = get_header_data('testdata/Q_Fthinsource.fits')
>>> print(hdr)
SIMPLE  =                    T / Written by IDL:  Tue Sep 28 22:42:54 2010      
BITPIX  =                  -32 / Number of bits per data pixel                  
NAXIS   =                    3 / Number of data axes                            
NAXIS1  =                  100 /                                                
NAXIS2  =                  100 /                                                
NAXIS3  =                  100 /                                                
DATE    = '2010-09-28'         / Creation UTC (CCCC-MM-DD) date of FITS header  
COMMENT FITS (Flexible Image Transport System) format is defined in 'Astronomy  
COMMENT and Astrophysics', volume 376, page 359; bibcode 2001A&A...376..359H    
CTYPE1  = 'X       '           /                                                
CRVAL1  =                    0 /                                                
CDELT1  =          1.30000E+06 /                                                
CRPIX1  =                    0 /                                                
CUNIT1  = 'Hz      '           /                                                
CTYPE2  = 'Y       '           /                                                
CRVAL2  =                    0 /                                                
CRVAL3  =          1.10000E+08 /                                                
POL     = 'Q       '           /                                                
>>> data.shape
(100, 100, 100)

FITS single floats are big endian 32 bit numbers. >>> str(data.dtype) ‘>f4’

Operating on them in any way, converts the output to ‘float32’: >>> str((data*1).dtype) ‘float32’

Of course, opening a non-existent file results in an OSError... >>> get_header_data(‘testdata/non-existent.fits’) Traceback (most recent call last): ... IOError: [Errno 2] No such file or directory: ‘testdata/non-existent.fits’

rmsynthesis.fits.image_frames(fits_name)

An iterator over a FITS image (hyper) cube:

Parameters

fits_name : string
Fits file over which to iterate.

Returns

A 2D numpy.array for each image in the (hyper)cube contained in the file. It has shape (NAXIS2, NAXIS1).

Example

>>> i = 0
>>> for frame in image_frames('testdata/Q_Fthinsource.fits'):
...     if i % 10 == 0:
...         print('%r: %3.2f' % (frame.shape, frame[50,50]))
...     i += 1
(100, 100): -9.29
(100, 100): 23.13
(100, 100): 81.73
(100, 100): -87.40
(100, 100): 40.75
(100, 100): 68.86
(100, 100): -72.43
(100, 100): -87.07
(100, 100): -0.70
(100, 100): 76.28
rmsynthesis.fits.streaming_output_hdu(fits_name, fits_header, force_overwrite)

Creates an HDU stream to which data can be written incrementally. This is very useful for writing large FITS image cubes.

Parameters

fits_name : string
Output file name..
fits_header : pyfits.Header
The header for the output FITS file.
force_overwrite : Bool
If True, overwrite existing file with name fits_name. If False, an OSError is raised if the file already exists.

Returns

A pyfits.core.StreamingHDU instance.

Raises

IOError
If output file exists and force_overwrite is False

Example

>>> fits_name = 'testdata/partial_output.fits'
>>> if os.path.exists(fits_name): os.remove(fits_name)
>>> 
>>> hdr, data = get_header_data('testdata/Q_Fthinsource.fits')
>>> shdu  = streaming_output_hdu(fits_name, hdr, force_overwrite = False)
>>> for image in data:
...     reached_end = shdu.write(image)
>>> shdu.close()
>>> 
>>> os.path.exists(fits_name)
True
>>> os.stat(fits_name).st_size
4003200
>>> hdr2, data2 = get_header_data(fits_name)
>>> print(hdr2)
SIMPLE  =                    T / Written by IDL:  Tue Sep 28 22:42:54 2010      
BITPIX  =                  -32 / Number of bits per data pixel                  
NAXIS   =                    3 / Number of data axes                            
NAXIS1  =                  100 /                                                
NAXIS2  =                  100 /                                                
NAXIS3  =                  100 /                                                
DATE    = '2010-09-28'         / Creation UTC (CCCC-MM-DD) date of FITS header  
COMMENT FITS (Flexible Image Transport System) format is defined in 'Astronomy  
COMMENT and Astrophysics', volume 376, page 359; bibcode 2001A&A...376..359H    
CTYPE1  = 'X       '           /                                                
CRVAL1  =                    0 /                                                
CDELT1  =          1.30000E+06 /                                                
CRPIX1  =                    0 /                                                
CUNIT1  = 'Hz      '           /                                                
CTYPE2  = 'Y       '           /                                                
CRVAL2  =                    0 /                                                
CRVAL3  =          1.10000E+08 /                                                
POL     = 'Q       '           /                                                
>>> (data2 == data).all()
True

If we try this again:

>>> shdu  = streaming_output_hdu(fits_name, hdr, force_overwrite = False)
Traceback (most recent call last):
...
IOError: testdata/partial_output.fits already exists. Will not overwrite unless forced.

Let’s try that again:

>>> import time
>>> current_time = time.time()
>>> shdu  = streaming_output_hdu(fits_name, hdr, force_overwrite = True)
Overwriting existing file 'testdata/partial_output.fits'.
>>> for image in data:
...     reached_end = shdu.write(image)
>>> shdu.close()
>>> 
>>> os.path.exists(fits_name)
True
>>> os.stat(fits_name).st_size
4003200
>>> os.stat(fits_name).st_mtime > current_time
True
>>> os.remove(fits_name)
rmsynthesis.fits.write_cube(fits_name, fits_header, data, force_overwrite=False)

Write an image cube to a FITS file containing only one HDU.

Parameters

fits_name : string
Output file name.
fits_header : pyfits.Header
The header describing data.
data : numpy.array
The (hyper)cube to write to the FITS file.

force_overwrite : bool

If True, the output file will be overwritten. Otherwise, an IOError is raised if the output file already exists.

Returns

None

Raises

IOError
If the output file already exists and force_overwrite == False.

Example

>>> fits_name = 'testdata/write_cube_output.fits'
>>> if os.path.exists(fits_name): os.remove(fits_name)
>>> 
>>> hdr, data = get_header_data('testdata/Q_Fthinsource.fits')
>>> write_cube(fits_name, hdr, data)
>>> hdr2, data2 = get_header_data(fits_name)
>>> print(hdr2)
SIMPLE  =                    T / Written by IDL:  Tue Sep 28 22:42:54 2010      
BITPIX  =                  -32 / Number of bits per data pixel                  
NAXIS   =                    3 / Number of data axes                            
NAXIS1  =                  100                                                  
NAXIS2  =                  100                                                  
NAXIS3  =                  100                                                  
DATE    = '2010-09-28'         / Creation UTC (CCCC-MM-DD) date of FITS header  
COMMENT FITS (Flexible Image Transport System) format is defined in 'Astronomy  
COMMENT and Astrophysics', volume 376, page 359; bibcode 2001A&A...376..359H    
CTYPE1  = 'X       '           /                                                
CRVAL1  =                    0 /                                                
CDELT1  =          1.30000E+06 /                                                
CRPIX1  =                    0 /                                                
CUNIT1  = 'Hz      '           /                                                
CTYPE2  = 'Y       '           /                                                
CRVAL2  =                    0 /                                                
CRVAL3  =          1.10000E+08 /                                                
POL     = 'Q       '           /                                                
>>> (data2 == data).all()
True
>>> write_cube(fits_name, hdr, data)
Traceback (most recent call last):
...
IOError: File 'testdata/write_cube_output.fits' already exists.
>>> import time
>>> current_time = time.time()
>>> write_cube(fits_name, hdr, data, force_overwrite = True)    
Overwriting existing file 'testdata/write_cube_output.fits'.
>>> os.path.exists(fits_name)
True
>>> os.stat(fits_name).st_size
4003200
>>> os.stat(fits_name).st_ctime > current_time
True
>>> os.remove(fits_name)

Previous topic

Welcome to RM-Synthesis’s documentation!

This Page