API Reference

File operations

FITSIO.FITSType
FITS(filename::String[, mode::String = "r"]; extendedparser = true)

Open or create a FITS file. mode can be one of "r" (read-only), "r+" (read-write) or "w" (write). In "write" mode, any existing file of the same name is overwritten.

A FITS object is a collection of "Header-Data Units" (HDUs) and supports the following operations:

  • f[i]: Return the i-th HDU.

  • f[name] or f[name, ver]: Return the HDU containing the given the given EXTNAME (or HDUNAME) keyword (a String), and optionally the given EXTVER (or HDUVER) number (an Integer).

  • Iteration:

    for hdu in f
        ...
    end

The keyword argument extendedparser may be used to enable or disable the extended filename parser. If disabled, filename is treated exactly as the name of the file and is not tokenized into parameters.

source
Base.lengthFunction
length(f::FITS)

Number of HDUs in the file.

source
length(hdr::FITSHeader)

Number of records in header of HDU.

source
length(hdu::ImageHDU)

Get total number of pixels in image (product of size(hdu)).

source
Base.closeFunction
close(f::FITS)

Close the file.

Subsequent attempts to operate on f will result in an error. FITS objects are also automatically closed when they are garbage collected.

source
Base.deleteat!Function
deleteat!(f::FITS, i::Integer)

Delete the HDU at index i in the FITS file. If i == 1, this deletes the primary HDU and replaces it with a bare HDU with no data and a minimal header. If i > 1, this removes the HDU at index i and moves the following HDUs forward.

source

Header operations

FITSIO.read_keyFunction
read_key(hdu::HDU, key::String) -> (value, comment)

Read the HDU header record specified by keyword and return a tuple where value is the keyword parsed value (of type String, Bool, Int, Float64 or Nothing), comment is the keyword comment (as a string). Throw an error if key is not found.

read_key(hdu::HDU, key::Integer) -> (keyname, value, comment)

Same as above but FITS card is specified by its position and returns a 3 element tuple where keyname is the keyword name (a string).

source
FITSIO.write_keyFunction
write_key(hdu::HDU, key::String, value[, comment])

Write a keyword value the HDU's header. value can be a standard header type (String, Bool, Integer, AbstractFloat) or nothing, in which case the value part of the record will be empty. If the keyword already exists, the value will be overwritten. The comment will only be overwritten if given. If the keyword does not already exist, a new record will be appended at the end of the header.

source
FITSIO.read_headerFunction
read_header(filename::AbstractString, hduindex = 1) -> FITSHeader

Convenience function to read the entire header corresponding to the HDU at index hduindex contained in the FITS file named filename. Functionally read_header(filename, hduindex) is equivalent to

FITS(filename, "r") do f
    read_header(f[hduindex])
end
source
read_header(hdu::HDU) -> FITSHeader

Read the entire header from the given HDU and return a FITSHeader object. The value of each header record is parsed as Int, Float64, String, Bool or nothing according to the FITS standard.

If the value cannot be parsed according to the FITS standard, the value is stored as the raw unparsed String.

source
read_header(hdu::HDU, String) -> String

Read the entire header from the given HDU as a single string.

source
FITSIO.FITSHeaderType
FITSHeader(keys::Vector{String}, values::Vector, comments::Vector{String})

An in-memory representation of the header of an HDU. It stores the (key, value, comment) information for each 80-character "card" in a header.

Note that this structure is not linked to a FITS file in any way; it is just a convenient structure for storing the header contents after reading from a file. (This is similar to how an Array returned by read(f[1]) is not linked to the FITS file f.) Manipulating a FITSHeader will therefore have no immediate impact on any file, even if it was created by read_header(::HDU). You can, however, write a FITSHeader to a file using the write(::FITS, ...) methods that append a new HDU to a file.

source
Base.lengthMethod
length(hdr::FITSHeader)

Number of records in header of HDU.

source
Base.haskeyMethod
haskey(hdr::FITSHeader, key::String)

Returns true if key exists in header, otherwise false.

source
Base.keysMethod
keys(hdr::FITSHeader)

Array of keywords in header of HDU (not a copy).

source
Base.valuesMethod
values(hdr::FITSHeader)

Array of values in header of HDU (not a copy).

source
FITSIO.get_commentFunction
get_comment(hdr::FITSHeader, key_or_index::Union{String,Integer})

Get the comment based on keyword or index.

source
FITSIO.set_comment!Function
set_comment!(hdr::FITSHeader, key_or_index::Union{String,Integer}, comment::String)

Set the comment based on keyword or index.

source
FITSIO.default_headerFunction
default_header(data::AbstractArray)

Creates a default header for the given array with the SIMPLE, BITPIX, NAXIS, NAXIS*, and EXTEND entries.

source

Image operations

Base.readMethod
read(hdu::ImageHDU)
read(hdu::ImageHDU, range...)

Read the data array or a subset thereof from disk. The first form reads the entire data array. The second form reads a slice of the array given by the specified ranges or integers. Dimensions specified by integers will be dropped in the returned array, while those specified by ranges will be retained.

Note

Julia follows a column-major array indexing convention, so the indices provided must account for this. In particular this means that FITS files created externally following a row-major convention (eg. using astropy) will have the sequence of axes flipped when read in using FITSIO.

source
Base.read!Function
read!(hdu::ImageHDU, A::StridedArray)
read!(hdu::ImageHDU, A::StridedArray, range...)

Read the data or a subset thereof from disk, and save it in a pre-allocated output array A. The first form reads the entire data from disk. The second form reads a slice of the array given by the specified ranges or integers. The array A needs to have the same length as the number of elements to be read in. Additionally A needs to be stored contiguously in memory.

Note

Julia follows a column-major array indexing convention, so the indices provided must account for this. In particular this means that FITS files created externally following a row-major convention (eg. using astropy) will have the sequence of the axes flipped when read in using FITSIO.

source
FITSIO.fitsreadFunction
fitsread(filename::AbstractString[, hduindex = 1[, arrayindices...]]; extendedparser = true)

Convenience function to read in an image corresponding to the HDU at index hduindex contained in the FITS file named filename. If arrayindices are provided, only a slice of the image corresponding to the indices is read in.

Functionally fitsread(filename, hduindex, arrayindices...; extendedparser) is equivalent to

FITS(filename, "r"; extendedparser = extendedparser) do f
    read(f[hduindex], arrayindices...)
end

The keyword argument extendedparser may be used to enable or disable the extended filename parser. If disabled, filename is treated exactly as the name of the file and is not tokenized into parameters.

Note

Julia follows a column-major array indexing convention, so the indices provided must account for this. In particular this means that FITS files created externally following a row-major convention (eg. using astropy) will have the sequence of axes flipped when read in using FITSIO.

See also: read

source
Base.writeMethod
write(f::FITS, data::StridedArray{<:Real}; header=nothing, name=nothing, ver=nothing)

Add a new image HDU to FITS file f with contents data. The following array element types are supported: UInt8, Int8, UInt16, Int16, UInt32, Int32, Int64, Float32, Float64. If a FITSHeader object is passed as the header keyword argument, the header will also be added to the new HDU. The data to be written out must be stored contiguously in memory.

Unsupported element types

It might be possible to write out an array with an element type other than those mentioned above by reinterpreting it as one that is supported. For example, to write out a Complex array and read it back in, we may use

julia> a = rand(ComplexF64, 2)
2-element Array{Complex{Float64},1}:
 0.4943325325752195 + 0.2034650017475852im
 0.2495752009567498 + 0.819163869249041im

# We may write this out as Float64
julia> FITSIO.fitswrite("temp.fits", reinterpret(Float64, a))

# reinterpret it back as a complex one while reading it in
julia> reinterpret(ComplexF64, FITSIO.fitsread("temp.fits"))
2-element reinterpret(Complex{Float64}, ::Array{Float64,1}):
 0.4943325325752195 + 0.2034650017475852im
 0.2495752009567498 + 0.819163869249041im

While this often works in practice, such a workaround is not officially supported by FITSIO, and care must be taken to ensure the correctness of data.

source
Base.writeMethod
write(hdu::ImageHDU, data::StridedArray{<:Real})

Write data to an existing image HDU. The data to be written out must be stored contiguously in memory.

source
FITSIO.fitswriteFunction
fitswrite(filename::AbstractString, data; extendedparser = true, kwargs...)

Convenience function to write the image array data to a file named filename.

Functionally fitswrite(filename, data; extendedparser, kwargs...) is equivalent to

FITS(filename, "w"; extendedparser = extendedparser) do f
    write(f, data; kwargs...)
end

The keyword argument extendedparser may be used to enable or disable the extended filename parser. If disabled, filename is treated exactly as the name of the file and is not tokenized into parameters.

Warning

Existing files with the same name will be overwritten.

See also: write

source
Base.eltypeMethod
eltype(hdu::ImageHDU)

Return the element type of the image in hdu.

source
Base.ndimsMethod
ndims(hdu::ImageHDU)

Get number of image dimensions, without reading the image into memory.

source
Base.sizeMethod
size(hdu::ImageHDU)
size(hdu::ImageHDU, i)

Get image dimensions (or ith dimension), without reading the image into memory.

source
Base.lengthMethod
length(hdu::ImageHDU)

Get total number of pixels in image (product of size(hdu)).

source
FITSIO.copy_sectionFunction
copy_section(hdu, dest, r...)

Copy a rectangular section of an image and write it to a new FITS primary image or image extension in FITS object dest. The new image HDU is appended to the end of dest. All the keywords in the input image will be copied to the output image. The common WCS keywords will be updated if necessary to correspond to the coordinates of the section.

Examples

Copy the lower-left 200 x 200 pixel section of the image in hdu to an open file, f

copy_section(hdu, f, 1:200, 1:200)

Same as above but only copy odd columns in y:

copy_section(hdu, f, 1:200, 1:2:200)
source

Table operations

FITSIO.colnamesFunction
colnames(hdu) -> Vector{String}

Return the names of columns in a table HDU.

source
Base.writeMethod
write(f::FITS, data::Dict; hdutype=TableHDU, name=nothing, ver=nothing, header=nothing, units=nothing, varcols=nothing)

Create a new table extension and write data to it. If the FITS file is currently empty then a dummy primary array will be created before appending the table extension to it. data should be a dictionary with String keys (giving the column names) and Array values (giving data to write to each column). The following types are supported in binary tables: UInt8, Int8, UInt16, Int16, UInt32, Int32, UInt64, Int64, Float32, Float64, Complex{Float32}, Complex{Float64}, String, Bool.

Optional inputs:

  • hdutype: Type of table extension to create. Can be either TableHDU (binary table) or ASCIITableHDU (ASCII table).
  • name: Name of extension.
  • ver: Version of extension (Int).
  • header: FITSHeader instance to write to new extension.
  • units: Dictionary mapping column name to units (as a string).
  • varcols: An array giving the column names or column indicies to write as "variable-length columns".
Variable length columns

Variable length columns allow a column's row entries to contain arrays of different lengths. They can potentially save diskspace when the rows of a column vary greatly in length, as the column data is all written to a contiguous heap area at the end of the table. Only column data of type Vector{String} or types such as Vector{Vector{UInt8}} can be written as variable length columns. In the second case, ensure that the column data type is a leaf type. That is, the type cannot be Vector{Vector{T}}, which would be an array of arrays having potentially non-uniform element types (which would not be writable as a FITS table column).

source
Base.writeMethod
write(f::FITS, colnames, coldata; hdutype=TableHDU, name=nothing, ver=nothing, header=nothing, units=nothing, varcols=nothing)

Same as write(f::FITS, data::Dict; ...) but providing column names and column data as a separate arrays. This is useful for specifying the order of the columns. Column names must be Vector{String} and column data must be a vector of arrays.

source
Base.readMethod
read(hdu::TableHDU, colname; case_sensitive=true)

Read a column as an array from the given table HDU.

The column name may contain wild card characters (*, ?, or #). The * wild card character matches any sequence of characters (including zero characters) and the ? character matches any single character. The # wildcard will match any consecutive string of decimal digits (0-9). The string must match a unique column. The optional boolean keyword case_sensitive, true by default, specifies whether the column name is to be considered case sensitive.

Array order

Julia arrays are column-major (like Fortran), not row-major (like C and numpy), so elements of multi-dimensional columns will be the transpose of what you get with astropy.

source