FITS Image Library for Swift.
This library provides a simple interface to read FITS files in Swift, based on the FITS 4.0 standard.
SwiftFITS is currently read-only: it parses existing FITS files into their header/data structure. Write and serialization support is planned but not yet implemented.
SwiftFITS targets the base FITS 4.0 standard. The following properties are intentional, not latent surprises:
- Keyword names may contain only
A–Z,0–9,_and-, and must be left-justified in the 8-byte keyword field (a leading space is rejected). - Long-keyword conventions are out of scope:
HIERARCHand similar ESO conventions are treated as ordinary 8-byte keywords, not expanded. OnlyCONTINUE/HISTORY/COMMENTmulti-record merging is supported. - Mandatory keywords (
SIMPLE/XTENSION,BITPIX,NAXIS,NAXISn, andPCOUNT/GCOUNTfor extensions) must appear in their exact standard order and index — this is enforced even in lenient mode. - Section layout is geometry-driven: each header's declared geometry
(
|BITPIX|/8 × GCOUNT × (PCOUNT + ∏ NAXISn), with random groups handled) determines exactly how many data blocks follow, rather than guessing from byte content. Trailing all-blank padding is preserved. ENDis excluded from a section'spropertiesbut is retained in the raw bytes, so a parsed file round-trips byte-for-byte throughFITSFile.data.- Strict vs. lenient:
.strictrejects technically-noncompliant input, while.lenient(the default) tolerates common real-world deviations (unknown value types, trailing characters after a string's closing quote, non-printable header text, data-length mismatches, a missing space after the=value indicator, lowercasee/dfloat exponents, NUL-padded headers, a truncated trailing partial block, and non-blank records after theENDmarker). - Not thread-safe:
FITSFile,FITSSectionandFITSBlockcarry mutable state and cache structural facts lazily on read, so they are notSendableand must not be shared across threads without external synchronization.
SwiftFITS is written in pure Swift and depends only on Foundation — no third-party dependencies. It is developed, built and tested on macOS (see the CI badge above). Being Foundation-only, the library is expected to be portable to Linux; Linux CI and SwiftPM-on-Linux verification are not yet in place and are tracked as future work.
SwiftFITS ships a Package.swift and can be consumed as a Swift package. Add it to your
dependencies:
.package( url: "https://github.com/macmade/SwiftFITS.git", branch: "main" )The Xcode project (SwiftFITS.xcodeproj) is also provided for development.
This project uses submodules.
To clone it, use the following command:
git clone --recursive https://github.com/macmade/SwiftFITS.gitimport Foundation
import SwiftFITS
do
{
let file = try FITSFile( url: URL( fileURLWithPath: "/path/to/file.fits" ), options: .lenient )
if let header = file.header
{
print( header.properties )
}
}
catch // SwiftFITS.FITSError
{
print( error )
}Project is released under the terms of the MIT License.
Owner: Jean-David Gadina - XS-Labs
Web: www.xs-labs.com
Blog: www.noxeos.com
Twitter: @macmade
GitHub: github.com/macmade
LinkedIn: ch.linkedin.com/in/macmade/
StackOverflow: stackoverflow.com/users/182676/macmade