fits_parser
A minimal Ruby gem to parse astronomy FITS files (.fit / .fits).
Citation
If you use fits_parser in research or publications, cite:
Bass, T. (2026). fits_parser (v0.1.0) [Software]. Zenodo. https://doi.org/10.5281/zenodo.19363404
Features
- Parse FITS HDUs and headers.
- Convert common FITS header value types (string, bool, int, float).
- Read basic BINTABLE metadata and rows for common
TFORMcodes (A,B,I,J,K,E,D).
Install
gem build fits_parser.gemspec
gem install ./fits_parser-0.1.0.gemUsage
require "fits_parser"
parser = FitsParser.new("/path/to/file.fit")
hdus = parser.parse_hdus
table_hdu = hdus.find { |h| h[:header]["XTENSION"] == "BINTABLE" }
if table_hdu
table = parser.read_bintable(table_hdu)
p table[:columns]
p table[:rows].first
end
parser.closeUsing block form:
FitsParser.open("/path/to/file.fits") do |parser|
p parser.parse_hdus
endStream BINTABLE rows (memory-safe)
Use this when tables are large and you do not want to load all rows at once.
require "fits_parser"
FitsParser.open("/path/to/file.fits") do |parser|
hdus = parser.parse_hdus
table_hdu = hdus.find { |h| h[:header]["XTENSION"] == "BINTABLE" }
raise "No BINTABLE found" unless table_hdu
columns = parser.bintable_columns(table_hdu)
p columns
parser.each_bintable_row(table_hdu).with_index do |row, idx|
puts "row=#{idx} #{row.inspect}"
break if idx >= 4 # sample only first 5 rows
end
endRead one specific row by index
require "fits_parser"
target_index = 123
FitsParser.open("/path/to/file.fits") do |parser|
hdu = parser.parse_hdus.find { |h| h[:header]["XTENSION"] == "BINTABLE" }
row = nil
parser.each_bintable_row(hdu).with_index do |r, i|
if i == target_index
row = r
break
end
end
p row
endAPI Summary
-
FitsParser.new(path)opens a FITS file. -
FitsParser.open(path) { |parser| ... }block form with auto-close. -
parse_hdusreturns HDU metadata (headers, data positions, sizes). -
read_bintable(hdu)returns{ columns:, rows: }(loads all rows). -
bintable_columns(hdu)returns column metadata only. -
each_bintable_row(hdu)yields rows one by one (streaming). -
closecloses the file handle.
CLI
bin/fits-parser /path/to/file.fitPrints a JSON summary of HDUs.
Testing
Run tests:
rake testAlternative direct test command:
ruby -Ilib:test test/fits_parser_test.rbQuick syntax checks:
ruby -c lib/fits_parser.rb
ruby -c lib/fits_parser/parser.rbNotes
- FITS is broad; this gem intentionally implements a practical subset.
- Variable-length arrays and many advanced FITS conventions are not yet supported.