Main Content

fitswrite

Write image to FITS file

Description

example

fitswrite(imagedata,filename) writes the specified image data to the Flexible Image Transport System (FITS) file specified by filename. If the file exists, fitswrite overwrites it.

example

fitswrite(imagedata,filename,Name,Value) creates a file with additional options using one or more Name-Value pair arguments. For example, Compression,'rice' specifies the Rice compression algorithm.

Examples

collapse all

Create a FITS file containing the red channel of an RGB image. The red channel will be represented by grayscale in the final image.

Read a sample image and isolate its red channel.

X = imread('ngc6543a.jpg');
R = X(:,:,1);

Write the red channel data to a FITS file.

fitswrite(R,'myfile.fits');

Display the FITS metadata.

fitsdisp('myfile.fits');
HDU:  1 (Primary HDU)
	SIMPLE  =                    T / file does conform to FITS standard
	BITPIX  =                    8 / number of bits per data pixel
	NAXIS   =                    2 / number of data axes
	NAXIS1  =                  600 / length of data axis 1
	NAXIS2  =                  650 / length of data axis 2
	EXTEND  =                    T / FITS dataset may contain extensions
	COMMENT   FITS (Flexible Image Transport System) format is defined in 'Astronomy
	COMMENT   and Astrophysics', volume 376, page 359; bibcode: 2001A&A...376..359H

Create a compressed FITS file with three images constructed from the channels of an RGB image. First create the file with one channel, then append the file with the other two.

Read a sample image and isolate its red, green, and blue channels.

X = imread('ngc6543a.jpg');
R = X(:,:,1);
G = X(:,:,2);
B = X(:,:,3);

Write red channel data to a FITS file using Rice compression.

fitswrite(R,'myfile.fits','Compression','rice');

Append the green and blue channel data to the FITS file using Rice compression.

fitswrite(G,'myfile.fits','WriteMode','append','Compression','rice');
fitswrite(B,'myfile.fits','WriteMode','append','Compression','rice');

Display the FITS file.

fitsdisp('myfile.fits');
HDU:  1 (Primary HDU)
	SIMPLE  =                    T / file does conform to FITS standard
	BITPIX  =                   16 / number of bits per data pixel
	NAXIS   =                    0 / number of data axes
	EXTEND  =                    T / FITS dataset may contain extensions
	COMMENT   FITS (Flexible Image Transport System) format is defined in 'Astronomy
	COMMENT   and Astrophysics', volume 376, page 359; bibcode: 2001A&A...376..359H

HDU:  2 
	XTENSION= 'BINTABLE'           / binary table extension
	BITPIX  =                    8 / 8-bit bytes
	NAXIS   =                    2 / 2-dimensional binary table
	NAXIS1  =                    8 / width of table in bytes
	NAXIS2  =                  650 / number of rows in table
	PCOUNT  =               101902 / size of special data area
	GCOUNT  =                    1 / one data group (required keyword)
	TFIELDS =                    1 / number of fields in each row
	TTYPE1  = 'COMPRESSED_DATA'    / label for field   1
	TFORM1  = '1PB(470)'           / data format of field: variable length array

HDU:  3 
	XTENSION= 'BINTABLE'           / binary table extension
	BITPIX  =                    8 / 8-bit bytes
	NAXIS   =                    2 / 2-dimensional binary table
	NAXIS1  =                    8 / width of table in bytes
	NAXIS2  =                  650 / number of rows in table
	PCOUNT  =                95976 / size of special data area
	GCOUNT  =                    1 / one data group (required keyword)
	TFIELDS =                    1 / number of fields in each row
	TTYPE1  = 'COMPRESSED_DATA'    / label for field   1
	TFORM1  = '1PB(470)'           / data format of field: variable length array

HDU:  4 
	XTENSION= 'BINTABLE'           / binary table extension
	BITPIX  =                    8 / 8-bit bytes
	NAXIS   =                    2 / 2-dimensional binary table
	NAXIS1  =                    8 / width of table in bytes
	NAXIS2  =                  650 / number of rows in table
	PCOUNT  =                96255 / size of special data area
	GCOUNT  =                    1 / one data group (required keyword)
	TFIELDS =                    1 / number of fields in each row
	TTYPE1  = 'COMPRESSED_DATA'    / label for field   1
	TFORM1  = '1PB(470)'           / data format of field: variable length array

Input Arguments

collapse all

Input image data, specified as a one-dimensional or multi-dimensional array.

File name, specified as a character vector or string scalar.

Name-Value Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: fitswrite(R,'myfile.fits','WriteMode','append')

Writing mode, specified as a character vector or a string scalar. If the file specified already exists, the writing mode determines the behavior of fitswrite.

Writing Mode

Description

'overwrite'

Overwrite the existing file. This is the default behavior

'append'

Append the image data to the existing file.

If the file does not exist, then fitswrite creates a new file regardless of writing mode.

Compression algorithm to be used when writing a FITS image, specified as one of the following values.

Values

Description

'none'

No compression. This is the default behavior.

'gzip'

Compress image data as a GNU ZIP file.

'rice'

Compress image data using the Rice algorithm.

'hcompress'

Compress image data using the HCOMPRESS algorithm.

'plio'Compress image data using the PLIO algorithm.

Tips

  • For copyright information, see the cfitsiocopyright.txt file. View this file using the open function.

    open cfitsiocopyright.txt

Introduced in R2012a