Skip to content

Instantly share code, notes, and snippets.

@yuasatakayuki
Created January 20, 2016 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuasatakayuki/cbe0b1157980faa91a40 to your computer and use it in GitHub Desktop.
Save yuasatakayuki/cbe0b1157980faa91a40 to your computer and use it in GitHub Desktop.
a short example of FITS Table HDU read access using RubyFits
#!/usr/bin/ruby
# This script dumps data contained in a FITS Table HDU into a CSV file using RubyFits.
# Usage:
# ruby conver_fits_to_csv.rb FITS_FILE
# Output:
# - Header: FITS_FILE_n_header_HDU_NAME
# - Data : FITS_FILE_n_data_HDU_NAME
# (n is HDU index)
require "RubyFits"
inputFileName=ARGV[0]
fits=Fits::FitsFile.new(inputFileName)
puts "There are #{fits.hdus.length} HDUs (including Primary HDU)."
#process all HDU except for Primary HDU
for i in 1...(fits.hdus.length)
hdu=fits.hdu(i)
hduName=hdu.getHDUName()
puts "Processing HDU[#{i}] = #{hduName}"
#dump header
outputFileNameHeader=inputFileName+"_#{i}_header_#{hduName}"
puts " Output file (header) = #{outputFileNameHeader}"
outputFileHeader = open(outputFileNameHeader,"w")
hdu.getHeaders().each(){|entry|
outputFileHeader.puts "#{"%-10s"%entry.keyword} = #{"%-30s"%entry.svalue} / #{entry.comment}"
}
#dump table data
outputFileNameData=inputFileName+"_#{i}_data_"+hduName
puts " Output file (data) = #{outputFileNameData}"
outputFileData = open(outputFileNameData,"w")
hdu.each_row(){|row|
outputFileData.puts row.join(",")
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment