API Reference
File operations
FITSIO.FITS
— Type.FITS(filename::String, mode::String="r")
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 thei
-th HDU.f[name]
orf[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
Base.length
— Method.length(f::FITS)
Number of HDUs in the file.
Base.close
— Method.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.
Header operations
FITSIO.read_key
— Function.read_key(hdu, key::String) -> (value, comment)
reads the HDU header record specified by keyword and returns 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). An error is thrown if key
is not found.
read_key(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).
FITSIO.write_key
— Function.write_key(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.
FITSIO.read_header
— Function.read_header(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
.
read_header(hdu, String) -> String
Read the entire header from the given HDU as a single string.
FITSIO.FITSHeader
— Type.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.
Base.length
— Method.length(hdr)
Number of records.
Base.haskey
— Method.haskey(hdr)
Header keyword exists.
Base.keys
— Method.keys(hdr)
Array of keywords (not a copy).
Base.values
— Method.values(hdr)
Array of values (not a copy).
FITSIO.get_comment
— Function.get_comment(hdr, key)
Get the comment based on keyword or index.
FITSIO.set_comment!
— Function.set_comment!(hdr, key, comment)
Set the comment baed on keyword or index.
Image operations
Base.write
— Method.write(f::FITS, data::Array; 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.
Base.read
— Method.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.
Base.ndims
— Method.ndims(hdu::ImageHDU)
Get number of image dimensions, without reading the image into memory.
Base.size
— Method.size(hdu::ImageHDU)
size(hdu::ImageHDU, i)
Get image dimensions (or i
th dimension), without reading the image into memory.
Base.length
— Method.length(hdu::ImageHDU)
Get total number of pixels in image (product of $size(hdu)$).
FITSIO.copy_section
— Function.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)
Table operations
FITSIO.colnames
— Function.colnames(hdu) -> Vector{String}
Return the names of columns in a table HDU.
Base.write
— Method.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
, Int64
, Float32
, Float64
, Complex{Float32}
, Complex{Float64}
, String
, Bool
.
Optional inputs:
hdutype
: Type of table extension to create. Can be eitherTableHDU
(binary table) orASCIITableHDU
(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 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).
Base.write
— Method.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.
Base.read
— Method.read(hdu, 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.
Miscellaneous
FITSIO.libcfitsio_version
— Function.libcfitsio_version() -> VersionNumber
Return the version of the underlying CFITSIO library
Example
julia> FITSIO.libcfitsio_version()
v"3.37.0"