Created
January 20, 2016 09:20
-
-
Save yuasatakayuki/cbe0b1157980faa91a40 to your computer and use it in GitHub Desktop.
a short example of FITS Table HDU read access using RubyFits
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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