Class: Csv

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.epochObject (readonly)

Returns the value of attribute epoch.



31
32
33
# File 'lib/mdarray/csv.rb', line 31

def epoch
  @epoch
end

Class Method Details

.read_numeric(filename, headers = false) ⇒ Object





38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/mdarray/csv.rb', line 38

def self.read_numeric(filename, headers = false)

  buffer = Array.new
  heading = Array.new
  lines = 0
  columns = nil

  CSV.foreach(filename,  
              {return_headers: false, 
                # headers: true,
                converters: [:numeric, :date]} ) do |row|

    if (headers)
      headers = false
      heading << row
      next
    end

    columns ||= row.size
    lines += 1

    row.each do |data|

      if (row.size != columns)
        raise "Data does not have the same number of columns for all lines"
      end

      # if it is a Date, then convert it to seconds since epoch
      if (data.is_a? Date)
        buffer << data.to_time.to_i
      end

      if (data.is_a? Numeric)
        buffer << data
      end
    end

  end

  [lines, columns, buffer, heading]

end