Guest User

Untitled

a guest
Jul 15th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. class Convertor
  2. def initialize(input_filename, force_overwrite)
  3. @input_filename = input_filename
  4. @force_overwrite = force_overwrite
  5. @input_data = []
  6. end
  7.  
  8. def convert!
  9. puts "Processing file #{@input_filename}"
  10. read_in_data
  11. split_comments!
  12. parse_header_attributes
  13. convert_from_string!
  14. strip_invalid!
  15. convert_fluxes_to_magnitudes!
  16. center_mag_on_zero!
  17. save!
  18. puts "Finished processing file #{@input_filename}"
  19. end
  20.  
  21. private
  22.  
  23. def read_in_data
  24. File.new(@input_filename, "r").each { |line| @input_data << line }
  25. #file closes automatically because it's opened in this method
  26. raise NoDataError if @input_data.empty?
  27. end
  28.  
  29. def split_comments!
  30. @comments, @input_data = @input_data.partition { |line| line =~ /^#/ }
  31. end
  32.  
  33. def convert_from_string!
  34. @input_data.map! { |line| line.split(" ").map(&:to_f)[0..1] }
  35. end
  36.  
  37. def strip_invalid!
  38. @input_data.delete_if { |record| record[1] == 0.0 }
  39. end
  40.  
  41. def convert_fluxes_to_magnitudes!
  42. @input_data.each { |record| record[1] = -2.5 * Math.log10(record[1]) }
  43. end
  44.  
  45. def average_mag
  46. @input_data.map { |record| record[1] }.inject(:+).to_f / @input_data.size
  47. end
  48.  
  49. def center_mag_on_zero!
  50. average = average_mag
  51. @input_data.each { |record| record[1] -= average }
  52. end
  53.  
  54. def output_filename
  55. # Determine the output filename from header
  56. "kic#{@attributes[:kic_number]}_#{@attributes[:season]}_#{@input_filename.split("_")[1]}.txt"
  57. end
  58.  
  59. def parse_header_attributes
  60. @attributes = @comments.select { |line| line.include? ":" }.map { |line| line.gsub("# ", "").split ":" }.to_hash
  61. end
  62.  
  63. def save!
  64. raise FileExistsError if File.exist?(output_filename) && !@force_overwrite
  65. output_file = File.new output_filename, "a+"
  66. output_file.truncate(0) if @force_overwrite
  67. @input_data.each { |record| output_file << "#{record.join("\t")}\n" }
  68. end
  69. end
  70.  
  71. class Array
  72. def to_hash
  73. self.inject({}) { |hash_version, element| hash_version[element[0].downcase.gsub(" ", "_").to_sym] = element[1].gsub(" ", "").strip; hash_version }
  74. end
  75. end
  76.  
  77. class FileExistsError < StandardError; end
  78. class NoDataError < StandardError; end
  79.  
  80. Kernel.abort "Please pass the input filename as an argument! Use -f to force overwrite." if ARGV.size < 1 #aborts the execution if no filename is provided as an argument with the program call
  81.  
  82. force_overwrite = false
  83.  
  84. if ARGV.first == "-f"
  85. force_overwrite = true
  86. ARGV.delete_at 0
  87. end
  88.  
  89. ARGV.each do |filename|
  90. begin
  91. Convertor.new(filename, force_overwrite).convert!
  92. rescue FileExistsError
  93. puts "Your output file (#{filename}) already exists, please remove it first (or something)."
  94. end
  95. end
Add Comment
Please, Sign In to add comment