Class: DTA_Reader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, out_filename = nil) ⇒ DTA_Reader

Returns a new instance of DTA_Reader.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/DTA_reader.rb', line 11

def initialize(filename, out_filename = nil)
  if File.exist?(filename) then
    @input_file = filename.to_s
    @input_ary = Array.new
    @output_ary = Array.new
    @fileinfo = Hash.new
    File.foreach(@input_file) { |line| @input_ary << line.chomp  }
    parse
    export(out_filename) if out_filename
  else
    raise DTA_ReaderErr, "File #{filename} do not exists!".red 
  end
end

Instance Attribute Details

#fileinfoObject (readonly)

Returns the value of attribute fileinfo.



9
10
11
# File 'lib/DTA_reader.rb', line 9

def fileinfo
  @fileinfo
end

#output_aryObject (readonly)

Returns the value of attribute output_ary.



9
10
11
# File 'lib/DTA_reader.rb', line 9

def output_ary
  @output_ary
end

Instance Method Details

#export(filename) ⇒ Object

Export data to TSV file



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/DTA_reader.rb', line 26

def export(filename)
  out = File.open(filename, 'w+')
  
  # Write file eader information
  out.puts '# ' + "#{@input_file} :: #{@fileinfo}"
  outstr = ""
  @output_ary.each { |e| 
    outstr += "#{e[:name]}_#{e[:unit]}\t"
  }
  out.puts outstr.rstrip

  (0..@output_ary[0][:values].size - 1).each { |i|
    outstr = ""
    @output_ary.each { |e|  
      outstr += "#{'%.8e' % e[:values][i]}\t"
    }
    out.puts outstr.rstrip
  }

  out.close
end