Class: Quadtone::CGATS::Section

Inherits:
Object
  • Object
show all
Defined in:
lib/quadtone/cgats.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSection

Returns a new instance of Section.



93
94
95
96
97
# File 'lib/quadtone/cgats.rb', line 93

def initialize
  @header = {}
  @data = []
  @data_fields = []
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



90
91
92
# File 'lib/quadtone/cgats.rb', line 90

def data
  @data
end

#data_fieldsObject

Returns the value of attribute data_fields.



91
92
93
# File 'lib/quadtone/cgats.rb', line 91

def data_fields
  @data_fields
end

#headerObject

Returns the value of attribute header.



89
90
91
# File 'lib/quadtone/cgats.rb', line 89

def header
  @header
end

Instance Method Details

#<<(set) ⇒ Object



99
100
101
# File 'lib/quadtone/cgats.rb', line 99

def <<(set)
  @data << set
end

#write(io) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/quadtone/cgats.rb', line 103

def write(io)
  # header
  @header.each { |k, v| io.puts k.to_s + (v ? " \"#{v}\"" : '') }
  # data format
  io.puts
  io.puts "NUMBER_OF_FIELDS #{@data_fields.length}"
  io.puts 'BEGIN_DATA_FORMAT'
  io.puts @data_fields.join(' ')
  io.puts 'END_DATA_FORMAT'
  # data
  io.puts
  io.puts "NUMBER_OF_SETS #{@data.length}"
  io.puts 'BEGIN_DATA'
  @data.each do |set|
    fields = @data_fields.map do |f|
      case (d = set[f])
      when Float
        '%.05f' % d
      when String
        '"' + d + '"'
      else
        d
      end
    end
    io.puts fields.join(' ')
  end
  io.puts 'END_DATA'
  nil
end